From 388c498be2797af9e199327db0843e6f479b09c5 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Thu, 17 Aug 2017 10:42:56 -0700 Subject: [PATCH] test: add tests for clean (#46) --- test/test-clean.ts | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 test/test-clean.ts diff --git a/test/test-clean.ts b/test/test-clean.ts new file mode 100644 index 00000000..52540e6e --- /dev/null +++ b/test/test-clean.ts @@ -0,0 +1,90 @@ +/** + * Copyright 2017 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import test from 'ava'; +import * as fs from 'fs'; +import * as path from 'path'; +import {clean} from '../src/clean'; +import {nop} from '../src/util'; +import {Options} from '../src/cli'; +import {withFixtures} from './fixtures'; + +const OPTIONS: Options = { + gtsRootDir: './', + targetRootDir: './', + dryRun: false, + yes: false, + logger: { + log: nop, + error: nop, + dir: nop + } +}; + +test.failing.serial( + 'should gracefully error if tsconfig is missing', async t => { + await withFixtures({}, async () => { + await clean(OPTIONS); + t.pass(); + }); + }); + +test.serial( + 'should gracefully error if tsconfig does not have valid outDir', + async t => { + await withFixtures({'tsconfig.json': JSON.stringify({})}, async () => { + const deleted = await clean(OPTIONS); + t.is(deleted, false); + }); + }); + +test.serial('should avoid deleting .', async t => { + await withFixtures( + {'tsconfig.json': JSON.stringify({compilerOptions: {outDir: '.'}})}, + async () => { + const deleted = await clean(OPTIONS); + t.is(deleted, false); + }); +}); + +test.failing.serial('should ensure that outDir is local to targetRoot', async t => { + await withFixtures( + {'tsconfig.json': JSON.stringify({compilerOptions: {outDir: '../out'}})}, + async () => { + const deleted = await clean(OPTIONS); + t.is(deleted, false); + }); +}); + +test.serial('should remove outDir', async t => { + const OUT = 'outputDirectory'; + await withFixtures( + { + 'tsconfig.json': JSON.stringify({compilerOptions: {outDir: OUT}}), + [OUT]: {} + }, + async (dir) => { + const outputPath = path.join(dir, OUT); + // make sure the output directory exists. + fs.accessSync(outputPath); + const deleted = await clean(OPTIONS); + t.is(deleted, true); + // make sure the directory has been deleted. + t.throws(() => { + fs.accessSync(outputPath); + }); + }); +});