From f8a8cb57ce819bb09eb4ebaaa44c80ae3d94f94c Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Fri, 8 Jun 2018 17:43:22 -0700 Subject: [PATCH] fix: do not fix format errors with --dry-run (#171) Also added more tests. --- src/format.ts | 5 ++ test/test-format.ts | 108 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/src/format.ts b/src/format.ts index ad74203c..5611651e 100644 --- a/src/format.ts +++ b/src/format.ts @@ -28,6 +28,11 @@ const baseArgs = ['-style=file']; */ export async function format( options: Options, files: string[] = [], fix = false): Promise { + 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 diff --git a/test/test-format.ts b/test/test-format.ts index 29e33799..58624624 100644 --- a/test/test-format.ts +++ b/test/test-format.ts @@ -15,6 +15,7 @@ */ import test from 'ava'; +import fs from 'fs'; import * as path from 'path'; import {Options} from '../src/cli'; @@ -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, '../..'), @@ -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}, @@ -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); + }); +});