Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not fix format errors with --dry-run #171

Merged
merged 2 commits into from Jun 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/format.ts
Expand Up @@ -28,6 +28,11 @@ const baseArgs = ['-style=file'];
*/
export async function format(
options: Options, files: string[] = [], fix = false): Promise<boolean> {
if (options.dryRun && fix) {
options.logger.log('format: skipping auto fix since --dry-run was passed');
fix = false;
}

const program = createProgram(options);
// Obtain a list of source files to format.
// We use program.getRootFileNames to get only the files that match the
Expand Down
108 changes: 107 additions & 1 deletion test/test-format.ts
Expand Up @@ -15,6 +15,7 @@
*/

import test from 'ava';
import fs from 'fs';
import * as path from 'path';

import {Options} from '../src/cli';
Expand All @@ -25,6 +26,7 @@ import {withFixtures} from './fixtures';

// clang-format won't pass this code because of trailing spaces.
const BAD_CODE = 'export const foo = 2; ';
const GOOD_CODE = 'export const foo = 2;';

const OPTIONS: Options = {
gtsRootDir: path.resolve(__dirname, '../..'),
Expand All @@ -35,6 +37,15 @@ const OPTIONS: Options = {
logger: {log: console.log, error: console.error, dir: nop}
};

test.serial('format should return false for well-formatted files', t => {
return withFixtures(
{'tsconfig.json': JSON.stringify({files: ['a.ts']}), 'a.ts': GOOD_CODE},
async () => {
const result = await format.format(OPTIONS, [], false);
t.true(result);
});
});

test.serial('format should return false for ill-formatted files', t => {
return withFixtures(
{'tsconfig.json': JSON.stringify({files: ['a.ts']}), 'a.ts': BAD_CODE},
Expand All @@ -57,4 +68,99 @@ test.serial('format should only look in root files', t => {
});
});

// TODO: Add more tests
test.serial('format should auto fix problems', t => {
return withFixtures(
{'tsconfig.json': JSON.stringify({files: ['a.ts']}), 'a.ts': BAD_CODE},
async (fixturesDir) => {
const result = await format.format(OPTIONS, [], true);
t.true(result);
const contents =
fs.readFileSync(path.join(fixturesDir, 'a.ts'), 'utf8');
t.deepEqual(contents, GOOD_CODE);
});
});

test.serial('format should format files listed in tsconfig.files', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({files: ['a.ts']}),
'a.ts': GOOD_CODE,
'b.ts': BAD_CODE
},
async () => {
const okay = await format.format(OPTIONS);
t.true(okay);
});
});

test.serial(
'format should format *.ts files when no files or inlcude has been specified',
async t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({}),
'a.ts': GOOD_CODE,
'b.ts': BAD_CODE
},
async () => {
const okay = await format.format(OPTIONS);
t.false(okay);
});
});

test.serial(
'format files listed in tsconfig.files when empty list is provided',
async t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({files: ['a.ts']}),
'a.ts': BAD_CODE,
'b.ts': BAD_CODE
},
async (fixturesDir) => {
const okay = await format.format(OPTIONS, [], true);
t.is(okay, true);
const contents =
fs.readFileSync(path.join(fixturesDir, 'a.ts'), 'utf8');
t.deepEqual(contents, GOOD_CODE);
});
});

test.serial('skip files listed in exclude', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({exclude: ['b.*']}),
'a.ts': GOOD_CODE,
'b.ts': BAD_CODE
},
async () => {
const okay = await format.format(OPTIONS);
t.is(okay, true);
});
});

test.serial('format globs listed in include', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({include: ['dirb/*']}),
dira: {'a.ts': GOOD_CODE},
dirb: {'b.ts': BAD_CODE}
},
async () => {
const okay = await format.format(OPTIONS);
t.is(okay, false);
});
});

test.serial('format should not auto fix on dry-run', t => {
return withFixtures(
{'tsconfig.json': JSON.stringify({files: ['a.ts']}), 'a.ts': BAD_CODE},
async (fixturesDir) => {
const optionsWithDryRun = Object.assign({}, OPTIONS, {dryRun: true});
const okay = await format.format(optionsWithDryRun, [], true);
t.is(okay, false);
const contents =
fs.readFileSync(path.join(fixturesDir, 'a.ts'), 'utf8');
t.deepEqual(contents, BAD_CODE);
});
});