|
| 1 | +import test from 'ava'; |
| 2 | +import * as chalk from 'chalk'; |
| 3 | +import * as cp from 'child_process'; |
| 4 | +import * as fs from 'fs'; |
| 5 | +import * as ncp from 'ncp'; |
| 6 | +import * as path from 'path'; |
| 7 | +import * as pify from 'pify'; |
| 8 | +import * as rimraf from 'rimraf'; |
| 9 | +import * as tmp from 'tmp'; |
| 10 | + |
| 11 | +const pkg = require('../../package.json'); |
| 12 | + |
| 13 | +const rimrafp = pify(rimraf); |
| 14 | +const mkdirp = pify(fs.mkdir); |
| 15 | +const execp = pify(cp.exec); |
| 16 | +const renamep = pify(fs.rename); |
| 17 | +const ncpp = pify(ncp.ncp); |
| 18 | + |
| 19 | +const keep = !!process.env.GTS_KEEP_TEMPDIRS; |
| 20 | +const stagingDir = tmp.dirSync({keep: keep, unsafeCleanup: true}); |
| 21 | +const stagingPath = stagingDir.name; |
| 22 | +const execOpts = { |
| 23 | + cwd: `${stagingPath}/kitchen` |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * Create a staging directory with temp fixtures used |
| 28 | + * to test on a fresh application. |
| 29 | + */ |
| 30 | +test.before(async () => { |
| 31 | + try { |
| 32 | + await execp('npm pack'); |
| 33 | + const tarball = `${pkg.name}-${pkg.version}.tgz`; |
| 34 | + await renamep(tarball, `${stagingPath}/google-ts-style.tgz`); |
| 35 | + await ncpp('test/fixtures', `${stagingPath}/`); |
| 36 | + await execp('npm install', execOpts); |
| 37 | + } catch (e) { |
| 38 | + console.error('Failed to prepare test staging sandbox.'); |
| 39 | + console.error(e); |
| 40 | + throw e; |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +test.serial('init', async t => { |
| 45 | + await execp('./node_modules/.bin/google-ts-style init -y', execOpts); |
| 46 | + fs.accessSync(`${stagingPath}/kitchen/tsconfig.json`); |
| 47 | + t.pass(); |
| 48 | +}); |
| 49 | + |
| 50 | +test.serial('build', async t => { |
| 51 | + await execp('npm run compile', execOpts); |
| 52 | + fs.accessSync(`${stagingPath}/kitchen/build/src/server.js`); |
| 53 | + fs.accessSync(`${stagingPath}/kitchen/build/src/server.js.map`); |
| 54 | + fs.accessSync(`${stagingPath}/kitchen/build/src/server.d.ts`); |
| 55 | + t.pass(); |
| 56 | +}); |
| 57 | + |
| 58 | +/** |
| 59 | + * Verify the `gts clean` command actually removes the |
| 60 | + * output dir |
| 61 | + */ |
| 62 | +test.serial('clean', async t => { |
| 63 | + await execp('npm run clean', execOpts); |
| 64 | + t.throws(() => { |
| 65 | + fs.accessSync(`${stagingPath}/kitchen/build`); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | + |
| 70 | +/** |
| 71 | + * CLEAN UP - remove the staging directory when done. |
| 72 | + */ |
| 73 | +test.after.always('cleanup staging', async () => { |
| 74 | + if (!keep) { |
| 75 | + stagingDir.removeCallback(); |
| 76 | + } else { |
| 77 | + console.log(`${chalk.blue(`${__filename} staging area: ${stagingPath}`)}`); |
| 78 | + } |
| 79 | +}); |
0 commit comments