Skip to content

Commit

Permalink
beef up tests and add nyc for coverage reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
tavisrudd committed Jun 7, 2019
1 parent 177694f commit 0ed1dc4
Show file tree
Hide file tree
Showing 13 changed files with 1,203 additions and 76 deletions.
1,105 changes: 1,095 additions & 10 deletions package-lock.json

Large diffs are not rendered by default.

36 changes: 34 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"postinstall": "patch-package",
"build": "tsc -p .",
"watch": "tsc --watch",
"dev-test-watch": "mocha-typescript-watch -p tsconfig.json lib/tests/_init.js lib/tests/**/*js",
"lint": "tslint -p . src/*ts src/**/*ts",
"pkg-binaries": "pkg --out-path dist -t node8-macos,node8-linux package.json",
"test": "mocha lib/tests/_init.js lib/tests/**/*js",
"test": "mocha lib/tests/**/*js",
"test-watch": "mocha --watch lib/tests/**/*js",
"ts-test-watch": "mocha --watch src/tests/**/*ts",
"coverage": "nyc --cache mocha src/tests/**/*ts",
"version": "auto-changelog -p && git add CHANGELOG.md"
},
"author": "Tavis Rudd <[email protected]>",
Expand Down Expand Up @@ -74,9 +76,39 @@
"intercept-stdout": "^0.1.2",
"mocha": "^6.1.4",
"mocha-typescript": "^1.1.17",
"nyc": "^14.0.0",
"patch-package": "^6.1.2",
"pkg": "^4.3.7",
"source-map-support": "^0.5.12",
"ts-mocha": "^6.0.0",
"ts-node": "^8.1.0",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
},
"mocha": {
"require": [
"ts-node/register",
"src/tests/_init.ts"
]
},
"nyc": {
"extension": [
".js",
".ts",
".tsx"
],
"exclude": [
"**/*.d.ts"
],
"include": [
"src/**/*.ts"
],
"reporter": [
"html"
],
"all": true,
"cache": true,
"sourceMap": true,
"instrument": true
}
}
5 changes: 4 additions & 1 deletion src/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as child_process from 'child_process';

import {logger} from '../logger';

export function diff(a: string, b: string, context = 3): void {
export function diff(a: string, b: string, context = 3): boolean {
const tmpdir = tmp.dirSync();
const aPath = pathmod.join(tmpdir.name, 'a');
const bPath = pathmod.join(tmpdir.name, 'b');
Expand All @@ -23,8 +23,11 @@ export function diff(a: string, b: string, context = 3): void {

if (res.status === 0) {
logger.info('Templates are the same');
return true;
} else if (res.status !== 1) {
throw new Error(`Error producing diff "${cmd}"`);
} else {
return false;
}
} catch (e) {
throw e;
Expand Down
13 changes: 10 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ global.Promise = bluebird;

import * as yargs from 'yargs';
import * as cli from 'cli-color';
import {Handler, description, fakeCommandSeparator, wrapCommandHandler, stackNameOpt, lintTemplateOpt} from './cli/utils';
import {Handler,
description,
fakeCommandSeparator,
wrapCommandHandler,
stackNameOpt,
lintTemplateOpt} from './cli/utils';
import {Commands} from './cli/command-types';

// TODO bring these two in line with the new lazy load scheme
Expand All @@ -30,14 +35,14 @@ import {buildApprovalCommands} from './cfn/approval/cli'
// faster. See the git history of this file to see the non-lazy form.
// Investigate this again if we can use babel/webpack to shrinkwrap

function lazyLoad(fnname: keyof Commands): Handler {
export function lazyLoad(fnname: keyof Commands): Handler {
return (args) => {
const {implementations} = require('./cli/command-implemntations');
return implementations[fnname](args);
}
}

function lazyGetter(target: any, key: keyof Commands) {
function lazyGetter(target: Commands, key: keyof Commands) {
Object.defineProperty(target, key, {value: lazyLoad(key)});
}

Expand Down Expand Up @@ -78,6 +83,7 @@ export function buildArgs(commands = new LazyCommands(), wrapMainHandler = wrapC
+ ' Cancelled (130) User responded \'No\' to iidy prompt or interrupt (CTRL-C) was received');

return yargs
.scriptName('iidy')
.env('IIDY')
.command(
'create-stack <argsfile>',
Expand Down Expand Up @@ -474,6 +480,7 @@ export function buildArgs(commands = new LazyCommands(), wrapMainHandler = wrapC

export async function main() {
// called for side-effect to force parsing / handling
// tslint:disable-next-line
buildArgs().argv;
}

Expand Down
8 changes: 4 additions & 4 deletions src/tests/test-aws-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require('./support'); // for side-effect
import * as process from 'process';
import * as fs from 'fs';
import * as path from 'path';
Expand Down Expand Up @@ -32,10 +31,12 @@ if (awsUserDir && fs.existsSync(awsUserDir)) {
});
aws.config.region = undefined;
};
const restoreOriginalEnvVarSettings = () => _.merge(process.env, originalEnvVarSettings);

beforeEach(unsetAWSRegionEnvVars);
after(restoreOriginalEnvVarSettings);
afterEach(() => {
_.merge(process.env, originalEnvVarSettings);
aws.config.region = originalDefaultRegion;
});

it("does not barf with no arguments", async () => {
// assumes a default region is set in ~/.aws
Expand All @@ -53,7 +54,6 @@ if (awsUserDir && fs.existsSync(awsUserDir)) {
expect(getCurrentAWSRegion()).to.equal(region);
}
}
restoreOriginalEnvVarSettings();
});

it("updates aws.config.region after each call", async () => {
Expand Down
1 change: 0 additions & 1 deletion src/tests/test-cfn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require('./support'); // for side-effect
import {expect} from 'chai';
import {loadStackArgs} from "../cfn/loadStackArgs";
import * as aws from 'aws-sdk'
Expand Down
25 changes: 25 additions & 0 deletions src/tests/test-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {expect} from 'chai';

import {buildArgs, lazyLoad} from "../main";
import {implementations} from "../cli/command-implemntations";
import {} from "../cfn/approval/cli";
import {} from "../cfn/approval/index";

describe('cli', () => {
it('--help displays help', async () => {
const parser = buildArgs().exitProcess(false);
const output = await new Promise((resolve) => {
parser.parse("--help", (_err: {}, _argv: {}, data: string) => {
resolve(data);
})
});
expect(output).to.contain("CloudFormation with Confidence");
});

it('lazy loaded implementations are loadable', () => {
expect(typeof lazyLoad('createStackMain')).to.equal('function');
expect(implementations.createStackMain).to.equal(
require('../cli/command-implemntations').implementations.createStackMain);
});

});
9 changes: 9 additions & 0 deletions src/tests/test-diff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {expect} from 'chai';
import {diff} from "../diff";

describe('diff', () => {
it('works and doesn\'t explode', () => {
expect(diff('a', 'a')).to.equal(true);
expect(diff('a', 'b')).to.equal(false);
});
});
1 change: 0 additions & 1 deletion src/tests/test-render.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require('./support'); // for side-effect
import {expect} from 'chai';

import * as yaml from '../yaml';
Expand Down
10 changes: 10 additions & 0 deletions src/tests/test-runCommandSet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {expect} from 'chai';
import {runCommandSet} from '../cfn/runCommandSet';

describe('runCommandSet', () => {
it('returns the commands run and does not barf', async () => {
const commands = ['echo 123', 'echo abc'];
const output = runCommandSet(commands, '.');
expect(output).to.deep.equal(commands);
})
});
1 change: 0 additions & 1 deletion src/tests/test-yaml-parsing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require('./support'); // for side-effect
import {expect} from 'chai';

import * as _ from 'lodash';
Expand Down
3 changes: 1 addition & 2 deletions src/tests/test-yaml-preprocessing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require('./support'); // for side-effect
import {expect} from 'chai';
import * as _ from 'lodash';
import * as jsyaml from 'js-yaml';
Expand Down Expand Up @@ -84,7 +83,6 @@ describe('Yaml pre-processing', () => {
});

});

//////////////////////////////////////////////////////////////////////
describe('$imports:', () => {

Expand Down Expand Up @@ -289,6 +287,7 @@ aref: !$ nested.aref`, mockLoader)).to.deep.equal({aref: 'mock'});
const testEnvOutsideCustomResource = mkTestEnv({});

it('!Sub ${} reference rewriting', async () => {
// tslint:disable no-invalid-template-strings
for (const {input, output} of [
{input: 'Foo', output: 'Foo'},
{input: 'Bar', output: 'Bar'},
Expand Down
62 changes: 11 additions & 51 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,16 @@
"exclude": ["node_modules", "*/**/flycheck_*"],
"compilerOptions": {
"pretty": true,
/* Basic Options */
"target": "ES2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["es5","es2015.promise","es2015"], /* Specify library files to be included in the compilation: */
//"allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
//"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./lib", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noStrictGenericChecks": true, /* for rxjs https://stackoverflow.com/questions/44810195/how-do-i-get-around-this-subject-incorrectly-extends-observable-error-in-types */
"target": "ES2017",
"module": "commonjs",
"lib": ["es5","es2015.promise","es2015"],
"declaration": true,
"sourceMap": true,
"outDir": "./lib",
"strict": true,
"noStrictGenericChecks": true,
"strictPropertyInitialization": false,
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
"typeRoots": ["@types", "node_modules/@types"], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */

/* Source Map Options */
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
"experimentalDecorators": true /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
}
"typeRoots": ["@types", "node_modules/@types"],
"experimentalDecorators": true
}
}

0 comments on commit 0ed1dc4

Please sign in to comment.