-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add fixture mechanism for unit testing (#38)
Makes it possible to run runs with fixtures without having out of band fixtures directories.
- Loading branch information
Showing
11 changed files
with
269 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* 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, Test} from 'ava'; | ||
import * as fs from 'fs'; | ||
import * as makeDir from 'make-dir'; | ||
import * as path from 'path'; | ||
import * as pify from 'pify'; | ||
import * as tmp from 'tmp'; | ||
|
||
const writeFilep = pify(fs.writeFile); | ||
|
||
export interface Fixtures { | ||
// If string, we create a file with that string contents. If fixture, we | ||
// create a subdirectory and recursively install the fixture. | ||
// TODO: support buffers to allow non-text files. | ||
[name: string]: string|Fixtures; | ||
} | ||
|
||
async function setupFixtures(dir: string, fixtures: Fixtures) { | ||
await makeDir(dir); | ||
const keys = Object.keys(fixtures); | ||
for (let key of keys) { | ||
const filePath = path.join(dir, key); | ||
if (typeof fixtures[key] === 'string') { | ||
const contents = fixtures[key] as string; | ||
await writeFilep(filePath, contents); | ||
} else { | ||
const fixture = fixtures[key] as Fixtures; | ||
await setupFixtures(filePath, fixture); | ||
} | ||
} | ||
} | ||
|
||
export async function withFixtures( | ||
fixtures: Fixtures, fn: (fixturesDir: string) => Promise<any>) { | ||
const keep = !!process.env.GTS_KEEP_TEMPDIRS; | ||
const dir = tmp.dirSync({keep: keep, unsafeCleanup: true}); | ||
|
||
await setupFixtures(dir.name, fixtures); | ||
|
||
const origDir = process.cwd(); | ||
process.chdir(dir.name); | ||
|
||
const result = await fn(dir.name); | ||
|
||
process.chdir(origDir); | ||
if (!keep) { | ||
dir.removeCallback(); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
// Problem: the following doesn't quite work with Ava. Ava expects direct calls | ||
// to `test`, and if it doesn't find them, it thinks that there do not exist | ||
// any tests in a file. | ||
// TODO: figure out a solution. Without this, the users of withFixtures have to | ||
// remember to use test.serial rather than test. | ||
export async function testWithFixtures( | ||
name: string, fixtures: Fixtures, testFn: Test) { | ||
return await withFixtures(fixtures, async () => { | ||
// The tests are run serially because we chdir to temp directory. | ||
return await test.serial(name, testFn); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/** | ||
* 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 {Options} from '../src/cli'; | ||
import * as lint from '../src/lint'; | ||
|
||
import {withFixtures} from './fixtures'; | ||
|
||
const OPTIONS: Options = { | ||
gtsRootDir: './', | ||
targetRootDir: './', | ||
dryRun: false, | ||
yes: false | ||
}; | ||
|
||
const GOOD_CODE = ` | ||
console.log('hello world'); | ||
`; | ||
|
||
const BAD_CODE = `console.log('hello world');`; // no newline. | ||
|
||
const TSLINT_CONFIG = require('../../tslint.json'); | ||
|
||
test.serial('createProgram should return an object', async t => { | ||
await withFixtures({'tsconfig.json': '{}'}, async () => { | ||
const program = lint.createProgram(OPTIONS); | ||
t.truthy(program); | ||
}); | ||
}); | ||
|
||
// TODO: make the error friendlier. This error implies that our module messed | ||
// up, or is not installed/integrated properly. | ||
test.serial('lint should throw error if tslint is missing', async t => { | ||
await withFixtures( | ||
{ | ||
'tsconfig.json': JSON.stringify({files: ['a.ts']}), | ||
'a.ts': GOOD_CODE, | ||
}, | ||
async () => { | ||
const err = t.throws(() => { | ||
lint.lint(OPTIONS); | ||
}); | ||
t.truthy(err.message.match('Could not find config.*tslint\.json')); | ||
}); | ||
}); | ||
|
||
test.serial('lint should return true on good code', async t => { | ||
await withFixtures( | ||
{ | ||
'tsconfig.json': JSON.stringify({files: ['a.ts']}), | ||
'tslint.json': JSON.stringify(TSLINT_CONFIG), | ||
'a.ts': GOOD_CODE, | ||
}, | ||
async () => { | ||
const okay = lint.lint(OPTIONS); | ||
t.is(okay, true); | ||
}); | ||
}); | ||
|
||
test.serial('lint should return false on bad code', async t => { | ||
await withFixtures( | ||
{ | ||
'tsconfig.json': JSON.stringify({files: ['a.ts']}), | ||
'tslint.json': JSON.stringify(TSLINT_CONFIG), | ||
'a.ts': BAD_CODE, | ||
}, | ||
async () => { | ||
const okay = lint.lint(OPTIONS, false, true /*silent*/); | ||
t.is(okay, false); | ||
}); | ||
}); | ||
|
||
test.serial('lint should lint files listed in tsconfig.files', async t => { | ||
await withFixtures( | ||
{ | ||
'tsconfig.json': JSON.stringify({files: ['a.ts']}), | ||
'tslint.json': JSON.stringify(TSLINT_CONFIG), | ||
'a.ts': GOOD_CODE, | ||
'b.ts': BAD_CODE | ||
}, | ||
async () => { | ||
const okay = lint.lint(OPTIONS); | ||
t.is(okay, true); | ||
}); | ||
}); | ||
|
||
test.serial( | ||
'lint should lint *.ts files when no files or inlcude has been specified', | ||
async t => { | ||
await withFixtures( | ||
{ | ||
'tsconfig.json': JSON.stringify({}), | ||
'tslint.json': JSON.stringify(TSLINT_CONFIG), | ||
'a.ts': GOOD_CODE, | ||
'b.ts': BAD_CODE | ||
}, | ||
async () => { | ||
const okay = lint.lint(OPTIONS, false, true /*silent*/); | ||
t.is(okay, false); | ||
}); | ||
}); | ||
|
||
test.serial('lint should not lint files listed in exclude', async t => { | ||
await withFixtures( | ||
{ | ||
'tsconfig.json': JSON.stringify({exclude: ['b.*']}), | ||
'tslint.json': JSON.stringify(TSLINT_CONFIG), | ||
'a.ts': GOOD_CODE, | ||
'b.ts': BAD_CODE | ||
}, | ||
async () => { | ||
const okay = lint.lint(OPTIONS); | ||
t.is(okay, true); | ||
}); | ||
}); | ||
|
||
test.serial('lint should lint globs listed in include', async t => { | ||
await withFixtures( | ||
{ | ||
'tsconfig.json': JSON.stringify({include: ['dirb/*']}), | ||
'tslint.json': JSON.stringify(TSLINT_CONFIG), | ||
dira: {'a.ts': GOOD_CODE}, | ||
dirb: {'b.ts': BAD_CODE} | ||
}, | ||
async () => { | ||
const okay = lint.lint(OPTIONS, false, true /*silent*/); | ||
t.is(okay, false); | ||
}); | ||
}); | ||
|
||
// TODO: test for when tsconfig.json is missing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.