Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a little util for making and saving snapshots
First pass, possibly bad. Need to play test it a little bit.
- Loading branch information
Showing
5 changed files
with
150 additions
and
12 deletions.
There are no files selected for viewing
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,51 @@ | ||
'use strict' | ||
|
||
const writeFile = require('write-file-atomic') | ||
const fs = require('fs') | ||
const mkdirp = require('mkdirp') | ||
const path = require('path') | ||
|
||
class Snapshot { | ||
constructor (test) { | ||
this.indexes = new Map() | ||
this.test = test | ||
this.file = path.resolve( | ||
process.cwd(), | ||
'tap-snapshots', | ||
this.test.fullname.replace(/[^a-zA-Z0-9\._\-]+/g, '-') | ||
) | ||
this.snapshot = null | ||
} | ||
|
||
// should only ever call _one_ of match/save | ||
match (data, message) { | ||
// XXX test line-by-line instead of all at once | ||
const index = this.indexes.get(message) || 0 | ||
this.indexes.set(message, index + 1) | ||
try { | ||
this.snapshot = this.snapshot || require(this.file) | ||
} catch (er) { | ||
throw new Error('Snapshot file not found\n' + | ||
'Run with TAP_SNAPSHOT=1 in the environment\n' + | ||
'to create snapshot files') | ||
} | ||
return this.snapshot[message + '_' + index] === data | ||
} | ||
|
||
snap (data, message) { | ||
const index = this.indexes.get(message) || 0 | ||
this.indexes.set(message, index + 1) | ||
this.snapshot = this.snapshot || {} | ||
this.snapshot[message + '_' + index] = data | ||
} | ||
|
||
save () { | ||
const data = `'use strict'\n` + ( | ||
Object.keys(this.snapshot).map(s => | ||
`exports[\`${s}\`] = \`${this.snapshot[s]}\`\n`).join('\n')) | ||
mkdirp.sync(path.dirname(this.file)) | ||
writeFile.sync(this.file, data, 'utf8') | ||
} | ||
} | ||
|
||
module.exports = Snapshot |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
const t = require('../') | ||
const Snapshot = require('../lib/snapshot.js') | ||
const rimraf = require('rimraf') | ||
const mkdirp = require('mkdirp') | ||
const path = require('path') | ||
const dir = path.resolve(__dirname, 'snapshot') | ||
const fs = require('fs') | ||
|
||
t.test('cleanup first', t => { | ||
rimraf.sync(dir) | ||
mkdirp.sync(dir) | ||
process.chdir(dir) | ||
t.end() | ||
}) | ||
|
||
t.test('actual test', t => { | ||
t.comment('not using subtests, because snapshots are per-test') | ||
|
||
t.test('checking snapshot without creating throws', t => { | ||
const s = new Snapshot(t) | ||
t.throws(_ => s.match('asdf', 'asdf')) | ||
t.end() | ||
}) | ||
|
||
const s = new Snapshot(t) | ||
t.comment('create some snapshots') | ||
s.snap(fs.readFileSync(__filename, 'utf8'), 'this file') | ||
s.snap('this is fine', 'a statement of acceptance') | ||
s.save() | ||
|
||
t.comment('now check that the snapshots are valid') | ||
const ss = new Snapshot(t) | ||
t.ok(ss.match(fs.readFileSync(__filename, 'utf8'), 'this file')) | ||
t.ok(ss.match('this is fine', 'a statement of acceptance')) | ||
|
||
t.end() | ||
}) | ||
|
||
t.test('cleanup after', t => { | ||
rimraf.sync(dir) | ||
process.chdir(__dirname) | ||
t.end() | ||
}) |