forked from songkick/replaceinfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
86 lines (75 loc) · 2.88 KB
/
test.js
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
85
86
var child_process = require('child_process');
var path = require('path');
var fs = require('fs');
var tap = require('tap');
var rimraf = require('rimraf');
function cleanDist() {
rimraf.sync(path.join(__dirname, 'test/dist/*.txt'));
rimraf.sync(path.join(__dirname, 'test/dist/*.json'));
}
function getContent(filePath) {
return fs.readFileSync(path.join(__dirname, filePath)).toString();
}
function getReport() {
return JSON.parse(fs.readFileSync(path.join(__dirname, 'test/dist/report.json')));
}
var expectations = {
one: getContent('test/expected/one.txt'),
two: getContent('test/expected/two.txt'),
three: getContent('test/expected/three.txt')
}
tap.test('CLI', function(test){
test.plan(3);
cleanDist();
child_process.execFile('./cli.js', [
"-s", "test/src/*.txt",
"-r", "test/replace-map.json",
"-d", "test/dist/{base}-test-output",
"-o", "test/dist/report.json"
], function(error, stdout, stderr){
test.equal(error, null, 'returned without an error code');
test.test('Generated files', function(test){
test.plan(3);
test.equal(getContent('test/dist/one.txt-test-output'), expectations.one, 'content matches expectations');
test.equal(getContent('test/dist/two.txt-test-output'), expectations.two, 'content matches expectations');
test.equal(getContent('test/dist/three.txt-test-output'), expectations.three, 'content matches expectations');
});
test.test('Report', function(test){
var report = getReport();
test.plan(3);
test.ok(report, 'a report is written');
test.test('options', function(test){
var options = report.options;
test.plan(5);
test.equal(options.source, 'test/src/*.txt', 'contains source');
test.equal(options.replaceMapPath, 'test/replace-map.json', 'contains replace map path');
test.equal(options.destPattern, 'test/dist/{base}-test-output', 'contains dest pattern');
test.equal(options.outputPath, 'test/dist/report.json', 'contains output dest path');
test.same(options.replaceMap, {
"hello": "goodbye",
"world": "earth"
}, 'contains the replace map');
});
test.test('result', function(test){
var result = report.result;
test.plan(4);
test.ok(Array.isArray(result), 'is an array');
test.same(result[0], {
src: 'test/src/one.txt',
dest: 'test/dist/one.txt-test-output',
changed: true,
}, 'contains correct data for one.txt');
test.same(result[1], {
src: 'test/src/three.txt',
dest: 'test/dist/three.txt-test-output',
changed: false,
}, 'contains correct data for three.txt');
test.same(result[2], {
src: 'test/src/two.txt',
dest: 'test/dist/two.txt-test-output',
changed: true,
}, 'contains correct data for two.txt');
});
});
});
});