Skip to content

Commit 8f9e4dc

Browse files
committed
Test: Fix relative path and update done() callback in CLI test spec
1 parent eb809ec commit 8f9e4dc

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

test/unit/cli/cli.spec.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const assert = require('node:assert');
22
const { exec } = require('node:child_process');
3+
const path = require('node:path');
34
const fs = require('node:fs');
45
const { UAParser } = require('../../../src/main/ua-parser');
56
const uap = new UAParser();
@@ -10,27 +11,38 @@ const input = [
1011
];
1112
const output = input.map(x => uap.setUA(x).getResult());
1213

14+
const testDir = path.join(__dirname);
15+
const inputFile = path.join(testDir, 'input.txt');
16+
const outputFile = path.join(testDir, 'output.json');
17+
1318
describe('npx ua-parser-js <string>', () => {
14-
it ('print result to stdout', () => {
19+
it ('print result to stdout', (done) => {
1520
exec('npx ua-parser-js "TEST"', (err, stdout, stderr) => {
21+
if (err) return done(err);
1622
assert.deepEqual(JSON.parse(stdout), JSON.parse(JSON.stringify([uap.setUA("TEST").getResult()])));
23+
done();
1724
});
1825
})
1926
});
2027

2128
describe('npx ua-parser-js --input-file=<filepath>', () => {
22-
it ('load file and print result to stdout', () => {
23-
exec('npx ua-parser-js --input-file="../test/unit/cli/input.txt"', (err, stdout, stderr) => {
29+
it ('load file and print result to stdout', (done) => {
30+
exec(`npx ua-parser-js --input-file="${inputFile}"`, (err, stdout, stderr) => {
31+
if (err) return done(err);
2432
assert.deepEqual(JSON.parse(stdout), JSON.parse(JSON.stringify(output)));
33+
done();
2534
});
2635
});
2736
});
2837

2938
describe('npx ua-parser-js --input-file=<filepath> --output-file=<filepath>', () => {
30-
it ('load file and save result to file', () => {
31-
exec('npx ua-parser-js --input-file="../test/unit/cli/input.txt" --output-file="../test/unit/cli/output.json"', (err, stdout, stderr) => {
32-
fs.readFile('test/unit/cli/output.json', (err, data) => {
39+
it ('load file and save result to file', (done) => {
40+
exec(`npx ua-parser-js --input-file="${inputFile}" --output-file="${outputFile}"`, (err, stdout, stderr) => {
41+
if (err) return done(err);
42+
fs.readFile(outputFile, (err, data) => {
43+
if (err) return done(err);
3344
assert.deepEqual(JSON.parse(data), JSON.parse(JSON.stringify(output)));
45+
done();
3446
});
3547
});
3648
});

0 commit comments

Comments
 (0)