Skip to content

Commit 1fd214f

Browse files
authored
Auto decompress files as part of test running (#195)
This simplifies running tests locally and should reduce errors due to outdated local files. - Add npm run test:prepare script that decompresses files by default - Run test:prepare for each test - Add --quiet flag to compress script
1 parent 216f62e commit 1fd214f

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ jobs:
3939
- name: Install Node Packages
4040
run: npm ci
4141

42-
- name: Decompress compressed files
43-
run: npm run decompress
44-
4542
- name: Cache jsvu Binaries
4643
uses: actions/cache@v4
4744
with:

package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
"scripts": {
1818
"server": "node tests/server.mjs",
1919
"compress": "node utils/compress.mjs",
20-
"decompress": "node utils/compress.mjs -d -k",
20+
"decompress": "node utils/compress.mjs --decompress --keep ",
2121
"lint:check": "eslint **/*.{js,mjs,jsx,ts,tsx}",
2222
"pretty:check": "prettier --check ./",
2323
"pretty:format": "prettier --write ./",
2424
"format:check": "npm run pretty:check && npm run lint:check",
25-
"test:chrome": "node tests/run-browser.mjs --browser chrome",
26-
"test:firefox": "node tests/run-browser.mjs --browser firefox",
27-
"test:safari": "node tests/run-browser.mjs --browser safari",
28-
"test:edge": "node tests/run-browser.mjs --browser edge",
29-
"test:shell": "npm run test:v8 && npm run test:jsc && npm run test:spidermonkey",
30-
"test:v8": "node tests/run-shell.mjs --shell v8",
31-
"test:jsc": "node tests/run-shell.mjs --shell jsc",
32-
"test:spidermonkey": "node tests/run-shell.mjs --shell spidermonkey"
25+
"test:prepare": "npm run decompress -- --quiet",
26+
"test:chrome": "npm run test:prepare && node tests/run-browser.mjs --browser chrome",
27+
"test:firefox": "npm run test:prepare && node tests/run-browser.mjs --browser firefox",
28+
"test:safari": "npm run test:prepare && node tests/run-browser.mjs --browser safari",
29+
"test:edge": "npm run test:prepare && node tests/run-browser.mjs --browser edge",
30+
"test:shell": "npm run test:prepare && npm run test:v8 && npm run test:jsc && npm run test:spidermonkey",
31+
"test:v8": "npm run test:prepare && node tests/run-shell.mjs --shell v8",
32+
"test:jsc": "npm run test:prepare && node tests/run-shell.mjs --shell jsc",
33+
"test:spidermonkey": "npm run test:prepare && node tests/run-shell.mjs --shell spidermonkey"
3334
},
3435
"devDependencies": {
3536
"@actions/core": "^1.11.1",

utils/compress.mjs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import zlib from 'zlib';
55
import fs from 'fs';
66
import path from 'path';
77

8+
let log = console.log
9+
810
function parseCommandLineArgs() {
911
const optionDefinitions = [
1012
{ name: 'decompress', alias: 'd', type: Boolean, description: 'Decompress files (default: compress).' },
1113
{ name: 'keep', alias: 'k', type: Boolean, description: 'Keep input files after processing (default: delete).' },
1214
{ name: 'help', alias: 'h', type: Boolean, description: 'Print this usage guide.' },
15+
{ name: 'quiet', alias: 'q', type: Boolean, description: 'Quite output, only print errors.' },
1316
{ name: 'globs', type: String, multiple: true, defaultOption: true, description: 'Glob patterns of files to process.' },
1417
];
1518
const options = commandLineArgs(optionDefinitions);
@@ -32,10 +35,14 @@ function parseCommandLineArgs() {
3235
process.exit(0);
3336
}
3437

38+
if (options.quiet) {
39+
log = () => {};
40+
}
41+
3542
if (options.globs === undefined) {
3643
if (options.decompress) {
3744
const defaultGlob = '**/*.z';
38-
console.log(`No input glob pattern given, using default: ${defaultGlob}`);
45+
log(`No input glob pattern given, using default: ${defaultGlob}`);
3946
options.globs = [defaultGlob];
4047
} else {
4148
// For compression, require the user to specify explicit input file patterns.
@@ -61,9 +68,9 @@ function compress(inputData) {
6168
const originalSize = inputData.length;
6269
const compressedSize = compressedData.length;
6370
const compressionRatio = calculateCompressionRatio(originalSize, compressedSize);
64-
console.log(` Original size: ${String(originalSize).padStart(8)} bytes`);
65-
console.log(` Compressed size: ${String(compressedSize).padStart(8)} bytes`);
66-
console.log(` Compression ratio: ${compressionRatio.toFixed(2).padStart(8)}%`);
71+
log(` Original size: ${String(originalSize).padStart(8)} bytes`);
72+
log(` Compressed size: ${String(compressedSize).padStart(8)} bytes`);
73+
log(` Compression ratio: ${compressionRatio.toFixed(2).padStart(8)}%`);
6774

6875
return compressedData;
6976
}
@@ -74,18 +81,19 @@ function decompress(inputData) {
7481
const compressedSize = inputData.length;
7582
const decompressedSize = decompressedData.length;
7683
const expansionRatio = calculateExpansionRatio(compressedSize, decompressedSize);
77-
console.log(` Compressed size: ${String(compressedSize).padStart(8)} bytes`);
78-
console.log(` Decompressed size: ${String(decompressedSize).padStart(8)} bytes`);
79-
console.log(` Expansion ratio: ${expansionRatio.toFixed(2).padStart(8)}%`);
84+
log(` Compressed size: ${String(compressedSize).padStart(8)} bytes`);
85+
log(` Decompressed size: ${String(decompressedSize).padStart(8)} bytes`);
86+
log(` Expansion ratio: ${expansionRatio.toFixed(2).padStart(8)}%`);
8087

8188
return decompressedData;
8289
}
8390

8491
function globsToFiles(globs) {
8592
let files = [];
86-
console.assert(globs.length > 0);
93+
console.assert(globs.length > 0) ;
94+
const globtions = { nodir: true, ignore: '**/node_modules/**' };
8795
for (const glob of globs) {
88-
const matches = globSync(glob, { nodir: true });
96+
const matches = globSync(glob, globtions);
8997
files = files.concat(matches);
9098
}
9199
files = Array.from(new Set(files)).sort();
@@ -94,7 +102,7 @@ function globsToFiles(globs) {
94102

95103
function processFiles(files, isDecompress, keep) {
96104
const verb = isDecompress ? 'decompress' : 'compress';
97-
console.log(`Found ${files.length} files to ${verb}` + (files.length ? ':' : '.'));
105+
log(`Found ${files.length} files to ${verb}` + (files.length ? ':' : '.'));
98106

99107
// For printing overall statistics at the end.
100108
let totalInputSize = 0;
@@ -111,7 +119,7 @@ function processFiles(files, isDecompress, keep) {
111119
} else {
112120
outputFilename = inputFilename.slice(0, -2);
113121
}
114-
console.log(` Decompressing to: ${outputFilename}`);
122+
log(` Decompressing to: ${outputFilename}`);
115123
} else {
116124
if (path.extname(inputFilename) === '.z') {
117125
console.warn(` Warning: Input file already has a .z extension.`);
@@ -130,7 +138,7 @@ function processFiles(files, isDecompress, keep) {
130138

131139
if (!keep) {
132140
fs.unlinkSync(inputFilename);
133-
console.log(` Deleted input file.`);
141+
log(` Deleted input file.`);
134142
}
135143
} catch (err) {
136144
console.error(`Error ${verb}ing ${inputFilename}:`, err);
@@ -140,14 +148,14 @@ function processFiles(files, isDecompress, keep) {
140148
if (files.length > 1) {
141149
if (isDecompress) {
142150
const totalExpansionRatio = calculateExpansionRatio(totalInputSize, totalOutputSize);
143-
console.log(`Total compressed sizes: ${String(totalInputSize).padStart(9)} bytes`);
144-
console.log(`Total decompressed sizes: ${String(totalOutputSize).padStart(9)} bytes`);
145-
console.log(`Average expansion ratio: ${totalExpansionRatio.toFixed(2).padStart(9)}%`);
151+
log(`Total compressed sizes: ${String(totalInputSize).padStart(9)} bytes`);
152+
log(`Total decompressed sizes: ${String(totalOutputSize).padStart(9)} bytes`);
153+
log(`Average expansion ratio: ${totalExpansionRatio.toFixed(2).padStart(9)}%`);
146154
} else {
147155
const totalCompressionRatio = calculateCompressionRatio(totalInputSize, totalOutputSize);
148-
console.log(`Total original sizes: ${String(totalInputSize).padStart(9)} bytes`);
149-
console.log(`Total compressed sizes: ${String(totalOutputSize).padStart(9)} bytes`);
150-
console.log(`Average compression ratio: ${totalCompressionRatio.toFixed(2).padStart(9)}%`);
156+
log(`Total original sizes: ${String(totalInputSize).padStart(9)} bytes`);
157+
log(`Total compressed sizes: ${String(totalOutputSize).padStart(9)} bytes`);
158+
log(`Average compression ratio: ${totalCompressionRatio.toFixed(2).padStart(9)}%`);
151159
}
152160
}
153161
}

0 commit comments

Comments
 (0)