From 86359e537f44920e0b18ae57b8df8c2c29ce7105 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Wed, 16 Aug 2017 18:23:43 -0700 Subject: [PATCH] test: improve system test (#44) * Use a old-of-tree tmp directory to make sure there is no incidental interaction with parent node_modules * Minimize the test fixtures. * Add system test for `gts init` * Expand test for build. * Simplify test for clean. --- test/fixtures/kitchen/package-lock.json | 14 ----- test/fixtures/kitchen/package.json | 11 ---- test/fixtures/kitchen/tsconfig.json | 11 ---- test/test-kitchen.ts | 79 +++++++++++++++++++++++++ test/test.ts | 65 -------------------- 5 files changed, 79 insertions(+), 101 deletions(-) delete mode 100644 test/fixtures/kitchen/package-lock.json delete mode 100644 test/fixtures/kitchen/tsconfig.json create mode 100644 test/test-kitchen.ts delete mode 100644 test/test.ts diff --git a/test/fixtures/kitchen/package-lock.json b/test/fixtures/kitchen/package-lock.json deleted file mode 100644 index b6d0a134..00000000 --- a/test/fixtures/kitchen/package-lock.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "kitchen", - "version": "1.0.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "typescript": { - "version": "2.4.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.4.2.tgz", - "integrity": "sha1-+DlfhdRZJ2BnyYiqQYN6j4KHCEQ=", - "dev": true - } - } -} diff --git a/test/fixtures/kitchen/package.json b/test/fixtures/kitchen/package.json index 4cf41209..95c6f4c3 100644 --- a/test/fixtures/kitchen/package.json +++ b/test/fixtures/kitchen/package.json @@ -1,17 +1,6 @@ { "name": "kitchen", "version": "1.0.0", - "description": "", - "main": "index.js", - "scripts": { - "build": "tsc -p .", - "fix": "gts fix", - "clean": "gts clean", - "lint": "gts lint" - }, - "keywords": [], - "author": "", - "license": "ISC", "devDependencies": { "typescript": "^2.4.2", "google-ts-style": "file:../google-ts-style.tgz" diff --git a/test/fixtures/kitchen/tsconfig.json b/test/fixtures/kitchen/tsconfig.json deleted file mode 100644 index 6924cd38..00000000 --- a/test/fixtures/kitchen/tsconfig.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "extends": "./node_modules/google-ts-style/tsconfig-google.json", - "compilerOptions": { - "rootDir": ".", - "outDir": "build" - }, - "include": [ - "src/**/*.ts", - "test/**/*.ts" - ] -} diff --git a/test/test-kitchen.ts b/test/test-kitchen.ts new file mode 100644 index 00000000..5ee02b47 --- /dev/null +++ b/test/test-kitchen.ts @@ -0,0 +1,79 @@ +import test from 'ava'; +import * as chalk from 'chalk'; +import * as cp from 'child_process'; +import * as fs from 'fs'; +import * as ncp from 'ncp'; +import * as path from 'path'; +import * as pify from 'pify'; +import * as rimraf from 'rimraf'; +import * as tmp from 'tmp'; + +const pkg = require('../../package.json'); + +const rimrafp = pify(rimraf); +const mkdirp = pify(fs.mkdir); +const execp = pify(cp.exec); +const renamep = pify(fs.rename); +const ncpp = pify(ncp.ncp); + +const keep = !!process.env.GTS_KEEP_TEMPDIRS; +const stagingDir = tmp.dirSync({keep: keep, unsafeCleanup: true}); +const stagingPath = stagingDir.name; +const execOpts = { + cwd: `${stagingPath}/kitchen` +}; + +/** + * Create a staging directory with temp fixtures used + * to test on a fresh application. + */ +test.before(async () => { + try { + await execp('npm pack'); + const tarball = `${pkg.name}-${pkg.version}.tgz`; + await renamep(tarball, `${stagingPath}/google-ts-style.tgz`); + await ncpp('test/fixtures', `${stagingPath}/`); + await execp('npm install', execOpts); + } catch (e) { + console.error('Failed to prepare test staging sandbox.'); + console.error(e); + throw e; + } +}); + +test.serial('init', async t => { + await execp('./node_modules/.bin/google-ts-style init -y', execOpts); + fs.accessSync(`${stagingPath}/kitchen/tsconfig.json`); + t.pass(); +}); + +test.serial('build', async t => { + await execp('npm run compile', execOpts); + fs.accessSync(`${stagingPath}/kitchen/build/src/server.js`); + fs.accessSync(`${stagingPath}/kitchen/build/src/server.js.map`); + fs.accessSync(`${stagingPath}/kitchen/build/src/server.d.ts`); + t.pass(); +}); + +/** + * Verify the `gts clean` command actually removes the + * output dir + */ +test.serial('clean', async t => { + await execp('npm run clean', execOpts); + t.throws(() => { + fs.accessSync(`${stagingPath}/kitchen/build`); + }); +}); + + +/** + * CLEAN UP - remove the staging directory when done. + */ +test.after.always('cleanup staging', async () => { + if (!keep) { + stagingDir.removeCallback(); + } else { + console.log(`${chalk.blue(`${__filename} staging area: ${stagingPath}`)}`); + } +}); diff --git a/test/test.ts b/test/test.ts deleted file mode 100644 index db65814b..00000000 --- a/test/test.ts +++ /dev/null @@ -1,65 +0,0 @@ -import test from 'ava'; -import * as chalk from 'chalk'; -import * as cp from 'child_process'; -import * as fs from 'fs'; -import * as ncp from 'ncp'; -import * as path from 'path'; -import * as pify from 'pify'; -import * as rimraf from 'rimraf'; - -const rimrafp = pify(rimraf); -const mkdirp = pify(fs.mkdir); -const execp = pify(cp.exec); -const renamep = pify(fs.rename); -const ncpp = pify(ncp.ncp); - -const stagingPath = path.normalize('./staging'); - -/** - * Create a staging directory with temp fixtures used - * to test on a fresh application. - */ -test.before(async () => { - try { - await mkdirp(stagingPath); - const tarball: string = await execp('npm pack'); - await renamep(tarball.trim(), 'staging/google-ts-style.tgz'); - await ncpp('test/fixtures', 'staging/'); - const execOpts = {cwd: 'staging/kitchen'}; - await execp('npm install', execOpts); - await execp('npm run build', execOpts); - console.log( - `${chalk.blue.bold('**** Staging area prepared for tests ****')}`); - } catch (e) { - console.error('Failed to prepare test staging sandbox.'); - console.error(e); - throw e; - } -}); - -/** - * Verify the `gts clean` command actually removes the - * output dir - */ -test('clean', t => { - return new Promise((resolve) => { - cp.spawn('npm', ['run', 'clean'], { - stdio: 'inherit', - cwd: 'staging/kitchen' - }).on('close', () => { - const buildPath = 'staging/kitchen/build'; - fs.exists(buildPath, exists => { - t.false(exists); - resolve(); - }); - }); - }); -}); - - -/** - * CLEAN UP - remove the staging directory when done. - */ -test.after.always('cleanup staging', async () => { - await rimrafp(stagingPath); -});