-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdash-licenses-wrapper.spec.js
More file actions
242 lines (216 loc) · 9.69 KB
/
Copy pathdash-licenses-wrapper.spec.js
File metadata and controls
242 lines (216 loc) · 9.69 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Mocha tests for dash-licenses-wrapper
const chai = require('chai');
const should = chai.should();
const expect = chai.expect;
// const fs = require('fs');
const path = require('path');
const cp = require('child_process');
const wrapper = path.resolve("src", "dash-licenses-wrapper.js");
/** configuration types enum */
const configsEnum = {
/** effective configuration */
"EFFECTIVE": "effective",
/** Parsed CLI */
"CLI": "CLI",
/** Parsed config file */
"FILE": "file"
};
/**
* Regular expressions to parse wrapper output and extract the different configurations.
* In all cases, the matching configuration will be capture in regexp group 1.
*/
// const traceLevelsRegexp = /(INFO||DEBUG):/i;
const configRegexps = {
/** effective configuration regexp */
[configsEnum.EFFECTIVE]: /^INFO: Effective configuration:(.*{.*})$/ms,
/** Parsed CLI regexp */
[configsEnum.CLI]: /^DEBUG: Parsed CLI:(.*?{.*?})$/ms,
/** Parsed config file regexp */
[configsEnum.FILE]: /^DEBUG: Parsed config file:(.*?{.*?})$/ms
}
/**
* Runs the dash-licenses-wrapper and extracts useful info from its output
* @param {string[]} args CLI arguments to pass to dash-licenses-wrapper
* @returns
*/
function runWrapper(args) {
const command = `${wrapper}`;
// const opts = { stdio: ['ignore', 'inherit', 'inherit']};
const opts = { stdio: 'pipe'};
let status = cp.spawnSync(command, args, opts);
const output = status.stdout.toString();
// TODO: handle STDERR trace "Debugger attached.\nWaiting for the debugger to disconnect...\n"
// that's present when debugging in vscode, and can interfere with some tests
const error = status.stderr.toString();
const cfgFile = extractConfigurationFromStdout(output, configsEnum.FILE);
const cfgCLI = extractConfigurationFromStdout(output, configsEnum.CLI);
const cfgFinal = extractConfigurationFromStdout(output, configsEnum.EFFECTIVE);
return [status.status, output, error, cfgFile, cfgCLI, cfgFinal];
}
describe("dash-licenses-wrapper tests", function() {
describe("test CLI parameters", function() {
it("test CLI param --help", function() {
const [status, output, error] = runWrapper(["--help"]);
expect(status).to.equal(0);
// expect(error).to.be.empty;
expect(output).to.match(/^.*Usage: dash-licenses-wrapper.js \[options\]/);
});
it("test output is in color by default", function() {
const [status, output, error] = runWrapper(["--help"]);
expect(status).to.equal(0);
// expect(error).to.be.empty;
expect(output).to.match(/^\x1b\[/);
});
it("test CLI param --noColor results in no color in output", function() {
const [status, output, error] = runWrapper(["--noColor", "--help"]);
expect(status).to.equal(0);
// expect(error).to.be.empty;
expect(output).to.not.match(/^\x1b\[/);
});
it("test CLI param --dryRun results in exit before calling dash-licenses", function() {
const [status, output, error] = runWrapper(["--noColor", "--dryRun"]);
expect(status).to.equal(0);
// expect(error).to.be.empty;
expect(output).to.match(/Dry-run mode enabled/);
});
it("test that dry run mode is disabled by default", function() {
const [status, output, error] = runWrapper(["--noColor", "--help"]);
expect(status).to.equal(0);
// expect(error).to.be.empty;
expect(output).to.not.match(/Dry-run mode enabled/);
});
it("test \"--debug\" CLI param enables extra traces", function() {
// const [status, output, error] = runWrapper(["--debug", "--noColor", "--dryRun"]);
const [status, output, error, cfgFile, cfgCLI, cfgFinal] = runWrapper(
[
"--dryRun",
"--noColor",
"--verbose"
]);
expect(status).to.equal(0);
expect(cfgCLI.dryRun).to.equal(true);
expect(cfgCLI.noColor).to.equal(true);
expect(cfgCLI.verbose).to.equal(true);
});
it("test \"--configFile\" CLI param works to load configurations from file", function() {
const [status, output, error, cfgFile, cfgCLI, cfgFinal] = runWrapper(
[
"--noColor",
"--dryRun",
"--verbose",
"--configFile=examples/license-check-config.json"
]);
expect(status).to.equal(0);
expect(cfgFinal.configFile).to.equal(cfgCLI.configFile).to.equal("examples/license-check-config.json");
expect(cfgFinal.project).to.equal(cfgFile.project).to.equal("ecd.cdt-cloud");
expect(cfgFinal.inputFile).to.equal(cfgFile.inputFile).to.equal("examples/package-lock.json");
expect(cfgFinal.batch).to.equal(cfgFile.batch).to.equal(51);
expect(cfgFinal.timeout).to.equal(cfgFile.timeout).to.equal(241);
expect(cfgFinal.exclusions).to.equal(cfgFile.exclusions).to.equal("examples/license-check-exclusions.json");
expect(cfgFinal.summary).to.equal(cfgFile.summary).to.equal("license-check-summary.txt");
});
});
describe("Run dash-licenses with example yarn lock file", function() {
// it("", function() {
// const yarnLockExample = path.resolve("..", "core", "src", "test", "java", "test_data_yarn.lock");
// const [status, output, error] = runWrapper(["--inputfile", "yarnlockExample"]);
// console.log(output);
// expect(status).to.equal(0);
// });
});
});
// Utility functions:
function extractConfigurationFromStdout(output, type) {
// console.log(`********* ${output} `)
const match = output.match(configRegexps[type]);
// const match = output.match(/^INFO: Effective configuration:(.*{.*})$/ms);
// const config = match[1];
const config = match? match[1] : undefined;
// console.log(`^^^^^^^^^:${config}`);
if (!config) {
return;
}
try {
return JSON.parse(config);
} catch (e) {
console.error("extractConfigurationFromStdout(): " + e);
}
}
/**
* Returns the effective value of a given dash-licenses-wrapper parameter. i.e. the value that will
* end-up being used once defaults, config file and CLI have been resolved.
* @param {string} output of dash-licenses-wrapper. The effective configuration is always printed except when using "--help"
* @param {string} configParamName optional name of a parameter to extract from the effective configuration.
* @returns {string | any | undefined} If configParamName is defined, return the corresponding value extracted from the tool's output. Otherwise, return the entire extracted effective configuration in the form of a JSON object.
*/
function extractEffectiveConfigurationFromStdout(output, configParamName) {
// console.log(`********* ${output} `)
const match = output.match(/^INFO: Effective configuration:(.*{.*})$/ms);
const config = match[1];
// console.log(`^^^^^^^^^:${config}`);
let obj;
try {
obj = JSON.parse(config);
} catch (e) {
console.error("extractFileConfigurationFromStdout()" + e);
}
if (config && configParamName) {
return obj[`${configParamName}`];
} else {
return obj;
}
// if (config && configParamName) {
// const match = config.match(new RegExp(`^\\s+(\\"${configParamName}.*?),?$`,'m'));
// const param = match[1];
// // console.log(`$$$$$ extracted: ${param}`);
// return param;
// } else {
// return config;
// }
}
/**
*
* @param {string} output of dash-licenses-wrapper. Debug mode has to be enabled to see CLI configuration
* @param {string} configParamName optional name of a parameter to extract from the CLI configuration. If omitted, the entire CLI configuration is returned as a JSON object.
* @returns {string | any | undefined} If configParamName is defined, return the corresponding value extracted from the tool's output. Otherwise, return the entire extracted CLI configuration in the form of a JSON object.
*/
function extractCLIConfigurationFromStdout(output, configParamName) {
// console.log(`*********${output} `)
const match = output.match(/^DEBUG: Parsed CLI:(.*?{.*?})$/ms);
const config = match[1];
// console.log(`^^^^^^^^^:${config}`);
let obj;
try {
obj = JSON.parse(config);
} catch (e) {
console.error("extractCLIConfigurationFromStdout(): " + e);
}
if (config && configParamName) {
return obj[`${configParamName}`];
} else {
return obj;
}
}
/**
*
* @param {string} output of dash-licenses-wrapper. Debug mode has to be enabled to see config file configuration
* @param {string} configParamName optional: name of a config file parameter to extract from the configuration.
* @returns {string | any} If configParamName is defined, return the corresponding value extracted from the tool's output. Otherwise, return the entire extracted config file configuration in the form of a JSON object
*/
function extractFileConfigurationFromStdout(output, configParamName) {
// console.log(`*********${output} `)
const match = output.match(/^DEBUG: Parsed config file:(.*?{.*?})$/ms);
const config = match[1];
// console.log(`^^^^^^^^^:${config}`);
let obj;
try {
obj = JSON.parse(config);
} catch (e) {
console.error("extractFileConfigurationFromStdout()" + e);
}
if (config && configParamName) {
return obj[`${configParamName}`];
} else {
return obj;
}
}