Skip to content

Commit b7d05fe

Browse files
authored
Merge pull request #9 from strongloop/better-testing
Improve test quality and coverage.
2 parents f3ae232 + 847e07f commit b7d05fe

File tree

6 files changed

+278
-0
lines changed

6 files changed

+278
-0
lines changed

test/common.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright IBM Corp. 2018. All Rights Reserved.
2+
// Node module: strong-log-transformer
3+
// This file is licensed under the Apache License 2.0.
4+
// License text available at https://opensource.org/licenses/Apache-2.0
5+
6+
var child_process = require('child_process');
7+
var sltCLIPath = require.resolve('../bin/sl-log-transformer');
8+
9+
exports.sltCLI = runCLIWithInput;
10+
11+
function runCLIWithInput(args, input, callback) {
12+
var execPath = process.execPath;
13+
var argv = [sltCLIPath].concat(args);
14+
var output = [];
15+
var slt = child_process.spawn(execPath, argv, {stdio: 'pipe'});
16+
slt.stdout.on('data', function(data) {
17+
output.push(data);
18+
});
19+
slt.stderr.on('data', function(data) {
20+
output.push(data);
21+
});
22+
slt.on('close', function(code, signal) {
23+
if (code || signal) {
24+
return callback(new Error('process exited with code: ' + code));
25+
}
26+
callback(null, Buffer.concat(output));
27+
});
28+
if (input && input.pipe) {
29+
input.pipe(slt.stdin);
30+
} else if (input) {
31+
slt.stdin.end(input);
32+
} else {
33+
slt.stdin.end();
34+
}
35+
}

test/fixtures/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text eol=lf

test/fixtures/basic.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
one
22
two
33
three
4+
45
four

test/test-ansi-color-tags.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var tap = require('tap');
2+
var Log = require('../');
3+
4+
tap.test('tag object with ansi escape codes', function(t) {
5+
var slt = Log({
6+
tag: {
7+
blue: '\u001b[1m\u001b[34mblue\u001b[39m\u001b[22m',
8+
green: '\u001b[32mgreen\u001b[39m',
9+
},
10+
});
11+
var input = [
12+
'good line',
13+
'good line',
14+
'good line',
15+
];
16+
var expected = input.map(function(line) {
17+
return 'blue:\u001b[1m\u001b[34mblue\u001b[39m\u001b[22m green:\u001b[32mgreen\u001b[39m ' + line + '\n';
18+
}).join('');
19+
var received = '';
20+
21+
slt.on('data', function(buf) {
22+
t.comment(buf);
23+
if (Buffer.isBuffer(buf)) {
24+
received += buf.toString('utf8');
25+
} else if (buf !== null) {
26+
received += buf;
27+
}
28+
});
29+
slt.on('end', function() {
30+
t.same(received, expected, 'output is input + trailing newline');
31+
t.end();
32+
});
33+
slt.write(input.join('\n'));
34+
slt.end();
35+
});
36+
37+
tap.test('tag string with ansi escape codes', function(t) {
38+
var slt = Log({
39+
tag: '\u001b[1m\u001b[34mblue\u001b[39m\u001b[22m',
40+
});
41+
var input = [
42+
'good line',
43+
'good line',
44+
'good line',
45+
];
46+
var expected = input.map(function(line) {
47+
return '\u001b[1m\u001b[34mblue\u001b[39m\u001b[22m ' + line + '\n';
48+
}).join('');
49+
var received = '';
50+
51+
slt.on('data', function(buf) {
52+
t.comment(buf);
53+
if (Buffer.isBuffer(buf)) {
54+
received += buf.toString('utf8');
55+
} else if (buf !== null) {
56+
received += buf;
57+
}
58+
});
59+
slt.on('end', function() {
60+
t.same(received, expected, 'output is input + trailing newline');
61+
t.end();
62+
});
63+
slt.write(input.join('\n'));
64+
slt.end();
65+
});

test/test-bad-utf8.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var tap = require('tap');
2+
var Log = require('../');
3+
4+
tap.test('truncated utf8', function(t) {
5+
var slt = Log();
6+
var input = [
7+
Buffer.from('good line\n'),
8+
Buffer.from('good line\n'),
9+
Buffer.from('good line\n'),
10+
Buffer.from('good line\n'),
11+
Buffer.from([
12+
// an incomplete utf8 sequence (3/4 bytes)
13+
0xf0, // byte 1 of 4 marker
14+
0xbf, // byte 2 of 4 marker
15+
0xbf, // byte 3 of 4 marker
16+
]),
17+
];
18+
var expected = Buffer.concat([
19+
Buffer.from('good line\n'),
20+
Buffer.from('good line\n'),
21+
Buffer.from('good line\n'),
22+
Buffer.from('good line\n'),
23+
Buffer.from([
24+
0xef, 0xbf, 0xbd, // single replacement character
25+
0x0a, // trailing newline adde by strong-log-transformer
26+
]),
27+
]);
28+
var received = '';
29+
30+
if (/^v(4|6)\./.test(process.version)) {
31+
expected = Buffer.concat([
32+
Buffer.from('good line\n'),
33+
Buffer.from('good line\n'),
34+
Buffer.from('good line\n'),
35+
Buffer.from('good line\n'),
36+
Buffer.from([
37+
// prior to node 8 each byte of an invalid utf8 sequence would be
38+
// replaced by a UTF replacement character. For more details, see
39+
// https://github.com/nodejs/node/commit/24ef1e6775
40+
0xef, 0xbf, 0xbd, // replacement character
41+
0xef, 0xbf, 0xbd, // replacement character
42+
0xef, 0xbf, 0xbd, // replacement character
43+
0x0a, // trailing newline adde by strong-log-transformer
44+
]),
45+
]);
46+
}
47+
slt.on('data', function(buf) {
48+
t.comment(buf);
49+
if (Buffer.isBuffer(buf)) {
50+
received += buf.toString('utf8');
51+
} else if (buf !== null) {
52+
received += buf;
53+
}
54+
});
55+
slt.on('end', function() {
56+
var expectedStr = expected.toString('utf8');
57+
t.same(received, expectedStr, 'output is input + trailing newline');
58+
t.end();
59+
});
60+
slt.write(Buffer.concat(input));
61+
slt.end();
62+
});

test/test-cli.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright IBM Corp. 2018. All Rights Reserved.
2+
// Node module: strong-log-transformer
3+
// This file is licensed under the Apache License 2.0.
4+
// License text available at https://opensource.org/licenses/Apache-2.0
5+
6+
var common = require('./common');
7+
var fs = require('fs');
8+
var tap = require('tap');
9+
10+
// These tests were ported from the original shell script based tests. The
11+
// intention was to fully exercise the module while also fully exercising the
12+
// CLI wrapper in order to achieve 100% coverage of both the API and CLI.
13+
14+
tap.test('version', function(t) {
15+
common.sltCLI(['--version'], null, function(_err, output) {
16+
output = output.toString('utf8');
17+
t.match(output, /^strong-log-transformer v[0-9]+\.[0-9]+\.[0-9]+/, '--version output looks right');
18+
t.end();
19+
});
20+
});
21+
22+
tap.test('help', function(t) {
23+
common.sltCLI(['--help'], null, function(_err, output) {
24+
output = output.toString('utf8');
25+
t.has(output, 'Usage', 'has Usage banner');
26+
t.has(output, 'timeStamp', 'has timeStamp option');
27+
t.has(output, 'mergeMultiline', 'has mergeMultiline option');
28+
t.has(output, 'tag TAG', 'has tag option');
29+
t.has(output, 'format FORMAT', 'has format option');
30+
t.end();
31+
});
32+
});
33+
34+
tap.test('text (default)', function(t) {
35+
var input = fs.readFileSync('test/fixtures/basic.in');
36+
var expected = fs.readFileSync('test/fixtures/basic.out');
37+
common.sltCLI([], input, function(err, output) {
38+
t.ifErr(err);
39+
t.same(output, expected);
40+
t.end();
41+
});
42+
});
43+
44+
tap.test('text: lineMerge', function(t) {
45+
var input = fs.readFileSync('test/fixtures/lineMerge.in');
46+
var expected = fs.readFileSync('test/fixtures/lineMerge.out');
47+
common.sltCLI(['--mergeMultiline'], input, function(err, output) {
48+
t.ifErr(err);
49+
t.same(output, expected);
50+
t.end();
51+
});
52+
});
53+
54+
tap.test('text: string tagged', function(t) {
55+
var input = fs.readFileSync('test/fixtures/basic.in');
56+
var expected = fs.readFileSync('test/fixtures/tagged.out');
57+
common.sltCLI(['--tag', 'SOMETHING_AWESOME'], input, function(err, output) {
58+
t.ifErr(err);
59+
t.same(output, expected);
60+
t.end();
61+
});
62+
});
63+
64+
tap.test('text: object tagged', function(t) {
65+
var input = fs.readFileSync('test/fixtures/basic.in');
66+
var expected = fs.readFileSync('test/fixtures/text-object-tagged.out');
67+
common.sltCLI(['--tag.one', '1', '--tag.two', '2'], input, function(err, output) {
68+
t.ifErr(err);
69+
t.same(output, expected);
70+
t.end();
71+
});
72+
});
73+
74+
tap.test('text: timestamps', function(t) {
75+
var input = fs.readFileSync('test/fixtures/basic.in');
76+
var expected = fs.readFileSync('test/fixtures/text-timestamp.grep');
77+
var expectedLines = expected.toString('utf8').split('\n');
78+
var expectedPatterns = expectedLines.map(function(line) { return new RegExp(line); });
79+
common.sltCLI(['--timeStamp'], input, function(err, output) {
80+
var outputLines = output.toString('utf8').split('\n');
81+
t.ifErr(err);
82+
t.same(outputLines.length, expectedPatterns.length, 'correct number of lines');
83+
for (var l = 0; l < expectedLines.length; l++) {
84+
t.match(outputLines[l], expectedPatterns[l]);
85+
}
86+
t.end();
87+
});
88+
});
89+
90+
tap.test('json', function(t) {
91+
var input = fs.readFileSync('test/fixtures/basic.in');
92+
var expected = fs.readFileSync('test/fixtures/basic.json');
93+
common.sltCLI(['--format=json'], input, function(err, output) {
94+
t.ifErr(err);
95+
t.same(output, expected);
96+
t.end();
97+
});
98+
});
99+
100+
tap.test('json: timestamps', function(t) {
101+
var input = fs.readFileSync('test/fixtures/basic.in');
102+
var expected = fs.readFileSync('test/fixtures/json-timestamp.grep');
103+
var expectedLines = expected.toString('utf8').split('\n');
104+
var expectedPatterns = expectedLines.map(function(line) { return new RegExp(line); });
105+
common.sltCLI(['--timeStamp', '--format=json'], input, function(err, output) {
106+
var outputLines = output.toString('utf8').split('\n');
107+
t.ifErr(err);
108+
t.same(outputLines.length, expectedPatterns.length, 'correct number of lines');
109+
for (var l = 0; l < expectedLines.length; l++) {
110+
t.match(outputLines[l], expectedPatterns[l]);
111+
}
112+
t.end();
113+
});
114+
});

0 commit comments

Comments
 (0)