Skip to content

Commit fd0ffe0

Browse files
committed
test: normalize unzip -v output to avoid some of the failed test runs
1 parent 7d1321e commit fd0ffe0

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

test/lib/writeCRX3File.js

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const CWD = process.cwd();
1616
// Port number should be the same as the one found in `example/example-extension` files.
1717
const PORT = 8080;
1818

19-
const NUMBER_OF_FILES_IN_EXAMPLE_ZIP = 2;
19+
const EXAMPLE_ZIP_CONTENTS = 'ad185b2c\texample.js\nef3ea3f0\tmanifest.json';
2020
const DEFAULT_FILE_CHECK_DELAY = 1500;
2121
const HTTP_OK = 200;
2222

@@ -110,6 +110,50 @@ function testWriteCRX3FileWithFilesAndOptions (t) {
110110
.catch(err => t.end(err));
111111
}
112112

113+
function normalizeUnzipOutput (output) {
114+
/*
115+
* Example output:
116+
*
117+
* Archive: /app/example/example-extension.zip
118+
* Length Method Size Cmpr Date Time CRC-32 Name
119+
* -------- ------ ------- ---- ---------- ----- -------- ----
120+
* 277 Defl:N 123 55% 03-24-2019 23:29 ad185b2c example.js
121+
* 320 Defl:N 209 34% 03-24-2019 23:29 ef3ea3f0 manifest.json
122+
* -------- ------- ---- ----
123+
* 597 332 44% 2 files
124+
*/
125+
var fileSection = false;
126+
const files = output
127+
// Split lines...
128+
.split(/[\r\n]+/)
129+
// ... map them so anything that is not about a file is empty...
130+
.map(line => {
131+
const isSeparator = line.startsWith('--');
132+
133+
if (isSeparator) {
134+
fileSection = !fileSection;
135+
return '';
136+
}
137+
138+
if (!fileSection) {
139+
return '';
140+
}
141+
142+
const cols = line.split(/\s+/);
143+
const name = cols.pop();
144+
const crc = cols.pop();
145+
146+
return `${crc} ${name}`;
147+
})
148+
// ... drop empty lines.
149+
.filter(Boolean);
150+
151+
// Sometimes files are in different order, so sort them before returning result.
152+
files.sort();
153+
154+
return files.join('\n');
155+
}
156+
113157
function compareWithExample (t, cfg) {
114158
const examplePath = path.join(CWD, 'example');
115159
const example = {
@@ -132,14 +176,9 @@ function compareWithExample (t, cfg) {
132176
t.ok(cfg.zipPath, 'Promised result should have `zipPath` set');
133177
t.ok(fs.existsSync(cfg.zipPath), `"${cfg.zipPath}" file should exist`);
134178
t.ok(fs.existsSync(example.zip), `"${example.zip}" file should exist`);
135-
const zipExample = tryExec(t, `unzip -v ${example.zip}`, `"${example.zip}" file should be a valid ZIP file`)
136-
.replace(example.zip, '')
137-
.replace(/\s\d{2}:\d{2}\s/g, ' hh:mm ');
138-
const selfTest = zipExample.match(/(ad185b2c\s+example.js|f643ef3e\smanifest.json)/); // eslint-disable-line prefer-named-capture-group
139-
t.ok(selfTest && selfTest.length === NUMBER_OF_FILES_IN_EXAMPLE_ZIP, 'Should pass self-test of unzip output');
140-
const zipTest = tryExec(t, `unzip -v ${cfg.zipPath}`, `"${cfg.zipPath}" file should be a valid ZIP file`)
141-
.replace(cfg.zipPath, '')
142-
.replace(/\s\d{2}:\d{2}\s/g, ' hh:mm ');
179+
const zipExample = normalizeUnzipOutput(tryExec(t, `unzip -v ${example.zip}`, `"${example.zip}" file should be a valid ZIP file`));
180+
t.strictEqual(zipExample, EXAMPLE_ZIP_CONTENTS, 'Should pass self-test of unzip output');
181+
const zipTest = normalizeUnzipOutput(tryExec(t, `unzip -v ${cfg.zipPath}`, `"${cfg.zipPath}" file should be a valid ZIP file`));
143182
const zipMatches = zipTest === zipExample;
144183
t.strictEqual(zipTest, zipExample, `Created "${cfg.zipPath}" should match "${example.zip}"`);
145184

0 commit comments

Comments
 (0)