1 | 'use strict';
|
2 |
|
3 | var assert = require('assert');
|
4 | var disparity = require('../disparity');
|
5 | var cli = require('../disparity-cli');
|
6 | var fs = require('fs');
|
7 | var path = require('path');
|
8 |
|
9 | function readFile(name) {
|
10 | var filePath = path.join(__dirname, name);
|
11 | return fs.readFileSync(filePath).toString();
|
12 | }
|
13 |
|
14 | function compare(diff, expected, name) {
|
15 | if (diff !== expected) {
|
16 |
|
17 | process.stderr.write('disparity.' + name + '() failure!\n');
|
18 | process.stderr.write('=== expected result:\n');
|
19 | process.stderr.write(expected);
|
20 | process.stderr.write('=== actual result:\n');
|
21 | process.stderr.write(diff);
|
22 | throw new Error('assertion error');
|
23 | }
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | var diff, expected;
|
30 | var file1 = readFile('file1.js');
|
31 | var file2 = readFile('file2.js');
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | diff = disparity.chars(file1, file2);
|
37 | expected = readFile('chars.txt');
|
38 | compare(diff, expected, 'chars');
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | diff = disparity.unified(file1, file2);
|
44 | expected = readFile('unified.txt');
|
45 | compare(diff, expected, 'unified');
|
46 |
|
47 | diff = disparity.unified(file1, file2, 'test/file1.js', 'test/file2.js');
|
48 | expected = readFile('unified_2.txt');
|
49 | compare(diff, expected, 'unified_2');
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | diff = disparity.unifiedNoColor(file1, file2, 'test/file1.js', 'test/file2.js');
|
55 | expected = readFile('unified_no_color.txt');
|
56 | compare(diff, expected, 'unified_no_color');
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | var _oldColors = disparity.colors;
|
62 |
|
63 | disparity.colors = {
|
64 |
|
65 | charsRemoved: { open: '<bggreen>', close: '</bggreen>' },
|
66 | charsAdded: { open: '<bgred>', close: '</bgred>' },
|
67 |
|
68 |
|
69 | removed: { open: '<red>', close: '</red>' },
|
70 | added: { open: '<green>', close: '</green>' },
|
71 | header: { open: '<yellow>', close: '</yellow>' },
|
72 | section: { open: '<magenta>', close: '</magenta>' }
|
73 | };
|
74 |
|
75 | diff = disparity.chars(file1, file2);
|
76 | expected = readFile('chars.html');
|
77 | compare(diff, expected, 'chars.html');
|
78 |
|
79 | diff = disparity.unified(file1, file2, 'test/file1.js', 'test/file2.js');
|
80 | expected = readFile('unified.html');
|
81 | compare(diff, expected, 'unified.html');
|
82 |
|
83 | disparity.colors = _oldColors;
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | var args = cli.parse([]);
|
89 | assert.ok(args.help, 'help');
|
90 | assert.equal(args.errors.length, 0, 'error 1');
|
91 |
|
92 | args = cli.parse(['--help']);
|
93 | assert.ok(args.help, 'help 2');
|
94 |
|
95 | args = cli.parse(['-h']);
|
96 | assert.ok(args.help, 'help 3');
|
97 |
|
98 | args = cli.parse(['-v']);
|
99 | assert.ok(args.version, 'version');
|
100 | assert.equal(args.errors.length, 0, 'error 2');
|
101 |
|
102 | args = cli.parse(['-u']);
|
103 | assert.equal(args.errors[0], 'Error: missing or invalid <file_1> and/or <file_2> path.', 'error 3');
|
104 |
|
105 | args = cli.parse(['-u', 'foo.js', '--bar']);
|
106 | assert.ok(args.unified, '-u');
|
107 | assert.equal(args.filePath1, 'foo.js');
|
108 |
|
109 | assert.equal(args.filePath2, '--bar');
|
110 | assert.equal(args.errors[0], 'Error: missing or invalid <file_1> and/or <file_2> path.', 'error 4');
|
111 |
|
112 | args = cli.parse(['--unified', 'foo.js', 'bar.js']);
|
113 | assert.ok(args.unified, '--unified');
|
114 | assert.equal(args.filePath1, 'foo.js');
|
115 | assert.equal(args.filePath2, 'bar.js');
|
116 | assert.equal(args.errors.length, 0, 'error 5');
|
117 |
|
118 | args = cli.parse(['-c', 'foo.js', 'bar.js']);
|
119 | assert.ok(!args.unified, '!--unified');
|
120 | assert.ok(args.chars, '-c');
|
121 |
|
122 | args = cli.parse(['--chars', 'foo.js', 'bar.js']);
|
123 | assert.ok(!args.unified, '!--unified');
|
124 | assert.ok(args.chars, '--chars');
|
125 |
|
126 | args = cli.parse(['--no-color', 'foo.js', 'bar.js']);
|
127 | assert.ok(args.errors.length, '--no-color errors');
|
128 | assert.ok(args.noColor, '--no-color');
|
129 |
|
130 | args = cli.parse(['--unified', '--no-color', 'foo.js', 'bar.js']);
|
131 | assert.ok(!args.errors.length, '--unified --no-color errors');
|
132 | assert.ok(args.noColor, '--no-color');
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | function FakeStream(){
|
138 | this.data = '';
|
139 | this.write = function(data) {
|
140 | this.data += data;
|
141 | };
|
142 | }
|
143 |
|
144 | var code;
|
145 | var out = new FakeStream();
|
146 | var err = new FakeStream();
|
147 |
|
148 | function run(args) {
|
149 | code = null;
|
150 | out.data = '';
|
151 | err.data = '';
|
152 | return cli.run(args, out, err);
|
153 | }
|
154 |
|
155 | code = run({ help: true });
|
156 | assert.ok(!code, 'exit code');
|
157 | assert.ok(out.data.length > 100, 'output help');
|
158 |
|
159 | code = run({ version: true });
|
160 | assert.ok(!code, 'exit code');
|
161 | assert.equal(out.data, 'disparity v' + require('../package.json').version +'\n', 'version');
|
162 |
|
163 | code = run({ errors: ['Error: foo bar'] });
|
164 | assert.ok(code, 'exit code error');
|
165 | assert.equal(err.data, 'Error: foo bar\n\n');
|
166 | assert.ok(out.data.search('Options:'));
|
167 |
|
168 | code = run({
|
169 | chars: true,
|
170 | filePath1: 'test/file1.js',
|
171 | filePath2: 'test/file2.js'
|
172 | });
|
173 | expected = readFile('chars.txt');
|
174 | assert.ok(!code, 'exit code chars');
|
175 | assert.equal(out.data, expected);
|
176 | assert.equal(err.data, '');
|
177 |
|
178 | code = run({
|
179 | unified: true,
|
180 | filePath1: 'test/file1.js',
|
181 | filePath2: 'test/file2.js'
|
182 | });
|
183 | expected = readFile('unified_2.txt');
|
184 | assert.ok(!code, 'exit code chars');
|
185 | assert.equal(out.data, expected);
|
186 | assert.equal(err.data, '');
|
187 |
|
188 | code = run({
|
189 | unified: true,
|
190 | noColor: true,
|
191 | filePath1: 'test/file1.js',
|
192 | filePath2: 'test/file2.js'
|
193 | });
|
194 | expected = readFile('unified_no_color.txt');
|
195 | assert.ok(!code, 'exit code chars');
|
196 | assert.equal(out.data, expected);
|
197 | assert.equal(err.data, '');
|