Skip to content

Commit

Permalink
fix: do not fix format errors with --dry-run (#171)
Browse files Browse the repository at this point in the history
Also added more tests.
  • Loading branch information
ofrobots committed Jun 9, 2018
1 parent a34ca31 commit f8a8cb5
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
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);
});
});

0 comments on commit f8a8cb5

Please sign in to comment.