Skip to content

Commit 1e2174b

Browse files
committed
test: replicate shell tests in JS
The original tests were done as a minimal effort approach to fully exercising the API via the simple CLI wrapper. This approach does not work on Windows. While everything in this module is completely platform agnostic, it is better to be certain. Signed-off-by: Ryan Graham <[email protected]>
1 parent 10118a2 commit 1e2174b

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-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/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)