1 |
|
2 | var cli = require(".")
|
3 | , child = require("child_process")
|
4 | , path = require("path")
|
5 | , relPathRe = /[^(]+(?=:\d+:\d+\))/gm
|
6 | , relPathFn = path.relative.bind(path, process.cwd())
|
7 | , seen = {}
|
8 |
|
9 |
|
10 | describe.assert.cmdSnapshot = function(cmd, file, opts) {
|
11 | var actual
|
12 | try {
|
13 | actual = child.execSync(cmd, opts).toString("utf8").replace(relPathRe, relPathFn)
|
14 | } catch(e) {
|
15 | return this(0, "Snapshot command failed: " + cmd + "\n---\n" + e.stdout.toString("utf8"))
|
16 | }
|
17 | return this.matchSnapshot(file, actual)
|
18 | }
|
19 |
|
20 | describe.assert.matchSnapshot = function(file, transform) {
|
21 | var expected
|
22 | , actual = typeof transform === "function" ? transform(cli.readFile(file)) : transform || cli.readFile(file)
|
23 | , snapFile = file + ".snap" + (seen[file] || "")
|
24 |
|
25 | seen[file] = (seen[file] || 0) + 1
|
26 |
|
27 | try {
|
28 | expected = cli.readFile(snapFile).replace(relPathRe, relPathFn)
|
29 | } catch(e) {}
|
30 | if (actual === expected) {
|
31 | this.ok(1)
|
32 | } else if (describe.conf.up) {
|
33 | console.error("# Update snapshot %s", file)
|
34 | cli.writeFile(snapFile, actual)
|
35 | this.ok(1)
|
36 | } else {
|
37 | try {
|
38 | child.execSync("git diff --no-index --color -- " + snapFile + " -", {
|
39 | input: actual,
|
40 | encoding: "utf8"
|
41 | })
|
42 |
|
43 | } catch(e) {
|
44 | return this(0, e.stdout ?
|
45 | "Snapshot " + file + "\n---\n" + e.stdout :
|
46 | "Snapshot diff failed, add --up to update snapshot\n---\n" + e.stderr
|
47 | )
|
48 | }
|
49 | }
|
50 |
|
51 | return this
|
52 | }
|
53 |
|