9594352f6974817bd1790790e9cb04707f5e9580.svn-base
2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict'
const fs = require('fs')
const mkdirp = require('mkdirp')
const path = require('path')
const rimraf = require('rimraf')
const cwd = process.cwd()
class Snapshot {
constructor (test) {
this.indexes = new Map()
this.test = test
// name them .test.js so that nyc ignores them
this.file = path.resolve(
cwd,
'tap-snapshots',
this.test.fullname.trim().replace(/[^a-zA-Z0-9\._\-]+/g, '-')
) + '.test.js'
this.snapshot = null
}
// should only ever call _one_ of read/save
read (message) {
const index = +this.indexes.get(message) || 1
this.indexes.set(message, index + 1)
try {
this.snapshot = this.snapshot || require(this.file)
} catch (er) {
throw new Error(
'Snapshot file not found: ' + this.file + '\n' +
'Run with TAP_SNAPSHOT=1 in the environment\n' +
'to create snapshot files'
)
}
const entry = message + ' ' + index
const s = this.snapshot[entry]
if (s === undefined)
throw new Error(
'Snapshot entry not found: "' + entry + '"\n' +
'Run with TAP_SNAPSHOT=1 in the environment\n' +
'to create snapshots'
)
return s.replace(/^\n|\n$/g, '')
}
snap (data, message) {
const index = +this.indexes.get(message) || 1
this.indexes.set(message, index + 1)
this.snapshot = this.snapshot || {}
this.snapshot[message + ' ' + index] = data
}
save () {
if (!this.snapshot)
rimraf.sync(this.file)
else {
const escape = s => s
.replace(/\\/g, '\\\\')
.replace(/\`/g, '\\\`')
.replace(/\$\{/g, '\\${')
const data =
'/* IMPORTANT\n' +
' * This snapshot file is auto-generated, but designed for humans.\n' +
' * It should be checked into source control and tracked carefully.\n' +
' * Re-generate by setting TAP_SNAPSHOT=1 and running tests.\n' +
' * Make sure to inspect the output below. Do not ignore changes!\n' +
' */\n\'use strict\'\n' + (
Object.keys(this.snapshot).sort().map(s =>
`exports[\`${
escape(s)
}\`] = \`\n${
escape(this.snapshot[s])
}\n\`\n`).join('\n'))
mkdirp.sync(path.dirname(this.file))
const writeFile = require('write-file-atomic')
writeFile.sync(this.file, data, 'utf8')
}
}
}
module.exports = Snapshot