From 08989fbca8ca0f2f7030c2ebb82a601b637b7eef Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Tue, 26 Jun 2018 21:09:53 -0700 Subject: [PATCH] chore: update package-lock.json --- package-lock.json | 6 ++-- test/test-init.ts | 80 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1d6b2de7..57a053e0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -277,9 +277,9 @@ } }, "@types/inquirer": { - "version": "0.0.41", - "resolved": "https://registry.npmjs.org/@types/inquirer/-/inquirer-0.0.41.tgz", - "integrity": "sha512-kIWkK3FECGKt9OrURxRvi59gwMNiWTePXWOvaJn+xhplbEvu91hIDMfLe5PUu+cEEMmD6EFU4VFJJKKp5kzCtw==", + "version": "0.0.42", + "resolved": "https://registry.npmjs.org/@types/inquirer/-/inquirer-0.0.42.tgz", + "integrity": "sha512-flMaNWU2g9NrtZ4bIV+7SEY2W7OdWNNhmJ0rE1lWVxGrkp3TfFGMcFCxRIBmGWigI8e6n+2HqLjizTTfgcpHLg==", "dev": true, "requires": { "@types/rx": "*", diff --git a/test/test-init.ts b/test/test-init.ts index e245ea94..1aca0119 100644 --- a/test/test-init.ts +++ b/test/test-init.ts @@ -19,7 +19,8 @@ import path from 'path'; import {Options} from '../src/cli'; import * as init from '../src/init'; -import {nop} from '../src/util'; +import {nop, readJsonp as readJson} from '../src/util'; + import {withFixtures} from './fixtures'; const OPTIONS: Options = { @@ -30,16 +31,27 @@ const OPTIONS: Options = { no: false, logger: {log: nop, error: nop, dir: nop} }; +const OPTIONS_YES = Object.assign({}, OPTIONS, {yes: true}); +const OPTIONS_NO = Object.assign({}, OPTIONS, {no: true}); +const OPTIONS_DRY_RUN = Object.assign({}, OPTIONS, {dryRun: true}); + +function hasExpectedScripts(packageJson: init.PackageJson): boolean { + return !!packageJson.scripts && [ + 'check', 'clean', 'compile', 'fix', 'prepare', 'pretest', 'posttest' + ].every(s => !!packageJson.scripts![s]); +} + +function hasExpectedDependencies(packageJson: init.PackageJson): boolean { + return !!packageJson.devDependencies && + ['gts', 'typescript'].every(d => !!packageJson.devDependencies![d]); +} test('addScripts should add a scripts section if none exists', async t => { const pkg: init.PackageJson = {}; const result = await init.addScripts(pkg, OPTIONS); t.is(result, true); // made edits. t.truthy(pkg.scripts); - ['check', 'clean', 'compile', 'fix', 'prepare', 'pretest', 'posttest'] - .forEach(s => { - t.truthy(pkg.scripts![s]); - }); + t.truthy(hasExpectedScripts(pkg)); }); test('addScripts should not edit existing scripts on no', async t => { @@ -53,8 +65,8 @@ test('addScripts should not edit existing scripts on no', async t => { posttest: `fake run check` }; const pkg: init.PackageJson = {scripts: Object.assign({}, SCRIPTS)}; - const optionsWithNo = Object.assign({}, OPTIONS, {no: true}); - const result = await init.addScripts(pkg, optionsWithNo); + + const result = await init.addScripts(pkg, OPTIONS_NO); t.is(result, false); // no edits. t.deepEqual(pkg.scripts, SCRIPTS); }); @@ -70,8 +82,7 @@ test('addScripts should edit existing scripts on yes', async t => { posttest: `fake run check` }; const pkg: init.PackageJson = {scripts: Object.assign({}, SCRIPTS)}; - const optionsWithYes = Object.assign({}, OPTIONS, {yes: true}); - const result = await init.addScripts(pkg, optionsWithYes); + const result = await init.addScripts(pkg, OPTIONS_YES); t.is(result, true); // made edits. t.notDeepEqual(pkg.scripts, SCRIPTS); }); @@ -86,8 +97,8 @@ test('addDependencies should add a deps section if none exists', async t => { test('addDependencies should not edit existing deps on no', async t => { const DEPS = {gts: 'something', typescript: 'or the other'}; const pkg: init.PackageJson = {devDependencies: Object.assign({}, DEPS)}; - const optionsWithNo = Object.assign({}, OPTIONS, {no: true}); - const result = await init.addDependencies(pkg, optionsWithNo); + const OPTIONS_NO = Object.assign({}, OPTIONS, {no: true}); + const result = await init.addDependencies(pkg, OPTIONS_NO); t.is(result, false); // no edits. t.deepEqual(pkg.devDependencies, DEPS); }); @@ -95,20 +106,45 @@ test('addDependencies should not edit existing deps on no', async t => { test('addDependencies should edit existing deps on yes', async t => { const DEPS = {gts: 'something', typescript: 'or the other'}; const pkg: init.PackageJson = {devDependencies: Object.assign({}, DEPS)}; - const optionsWithYes = Object.assign({}, OPTIONS, {yes: true}); - const result = await init.addDependencies(pkg, optionsWithYes); + + const result = await init.addDependencies(pkg, OPTIONS_YES); t.is(result, true); // made edits. t.notDeepEqual(pkg.devDependencies, DEPS); }); -// TODO: this test has not been completed yet. -// test.serial('init should read local package.json', t => { -// return withFixtures( -// {'package.json': JSON.stringify({some: 'property'})}, async () => { -// const optionsWithDryRun = Object.assign({}, OPTIONS, {dryRun: true}); -// const result = await init.init(optionsWithDryRun); -// t.truthy(result); -// }); -// }); +// TODO: test generateConfigFile + +// init +test.serial('init should read local package.json', t => { + const originalContents = {some: 'property'}; + return withFixtures( + {'package.json': JSON.stringify(originalContents)}, async () => { + // TODO: this test causes `npm install` to run in the fixture directory. + // This may make it sensistive to the network, npm resiliency. Find a + // way to mock npm. + const result = await init.init(OPTIONS_YES); + t.truthy(result); + const contents = await readJson('./package.json'); + + t.not(contents, originalContents, 'the file should have been modified'); + t.is( + contents.some, originalContents.some, + 'unrelated property should have preserved'); + }); +}); + +test.serial('init should handle missing package.json', t => { + return withFixtures({}, async () => { + // TODO: this test causes `npm install` to run in the fixture directory. + // This may make it sensistive to the network, npm resiliency. Find a way to + // mock npm. + const result = await init.init(OPTIONS_YES); + t.truthy(result); + const contents = await readJson('./package.json'); + t.truthy(hasExpectedScripts(contents)); + t.truthy(hasExpectedDependencies(contents)); + }); +}); + // TODO: need more tests.