Skip to content

Commit 904d78c

Browse files
authored
Fix tests (#205)
* fix(test): destroyOutput regeneration double serialization and output file mutation Problem: When running with destroyOutput set the following problems arise: 1. test/test.js called writeFile(outputFilename, result, options) where result is already a serialized string returned by run(), effectivelly double encoding it. The next test run (`npx jest test/test.js`) would load this corrupted content as the expected snapshot, causing all JSON test cases in test.js to fail. 2. test/__utils__/test-utils.js loadTest() ran the CLI without overriding --output, so the CLI used the output path from options.yaml. Which during a run with `deleteOutput` set, would silently overwrite and mask the corruption from (1) while still producing test failures. Solution: - Write the already-serialized result string directly with `fs.writeFileSync` in test.js. - Pass --output <tmpfile> in loadTest so the CLI writes to a temp file instead of the shared `output`. Output file mutation is now exclusive owned by `test.js`. * fix(test): normalize no-sort/no-bundle option handling in test runner Problem: 1. sort and bundle were unconditionally derived from no-sort/no-bundle, ignoring explicit sort/bundle values set directly in options.yaml 2. The no-bundle delete guard incorrectly checked no-sort as its first condition. Solution: Default sort and bundle to true, and only translate the no-X keys when they are actually present in the options file.
1 parent 10f68cb commit 904d78c

9 files changed

Lines changed: 14 additions & 16 deletions

File tree

test/__utils__/test-utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const fs = require('fs');
2-
const sy = require('@stoplight/yaml');
2+
const os = require('os');
33
const {exec} = require('child_process');
44
const path = require('path');
55
const {parseFile, stringify} = require('../../utils/file');
@@ -13,6 +13,7 @@ async function loadTest(folder, inputType = 'yaml', outType = 'yaml') {
1313
const inputPath = `./test/${folder}/${inputFile}`;
1414
const outputFile = `output.${outType}`;
1515
const outputPath = `./test/${folder}/${outputFile}`;
16+
const tmpOutput = path.join(os.tmpdir(), `openapi-format-${folder}-output.${outType}`);
1617

1718
try {
1819
input = await parseFile(inputPath);
@@ -26,10 +27,10 @@ async function loadTest(folder, inputType = 'yaml', outType = 'yaml') {
2627
// File not found = {} will be used
2728
}
2829

29-
let result = await cli([`${inputFile}`, `--configFile options.yaml`], testPath);
30+
let result = await cli([`${inputFile}`, `--configFile options.yaml`, `--output ${tmpOutput}`], testPath);
3031

3132
try {
32-
outputAfter = await parseFile(outputPath);
33+
outputAfter = await parseFile(tmpOutput);
3334
} catch (err) {
3435
//
3536
}

test/json-default-bug-big-numbers/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@
5959
}
6060
}
6161
}
62-
}
62+
}

test/json-delete-method/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@
3535
}
3636
}
3737
}
38-
}
38+
}

test/json-filter-inverse-operationids-126/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@
7878
}
7979
}
8080
}
81-
}
81+
}

test/json-filter-unused/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,4 @@
141141
}
142142
}
143143
}
144-
}
144+
}

test/overlay-extends-local/output.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ info:
33
title: Base Local
44
version: 1.0.0
55
paths: {}
6-

test/overlay-extends-remote/output.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ info:
55
servers:
66
- url: 'https://api.example.com'
77
description: Default server
8-

test/test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ describe('openapi-format tests', () => {
5252
if (fs.existsSync(configFilename)) {
5353
// Load options file
5454
configFileOptions = await parseFile(configFilename);
55-
configFileOptions.sort = !configFileOptions['no-sort'];
56-
if (configFileOptions['no-sort'] && configFileOptions['no-sort'] === true) {
55+
configFileOptions.sort = configFileOptions.sort ?? true;
56+
configFileOptions.bundle = configFileOptions.bundle ?? true;
57+
if (configFileOptions['no-sort']) {
5758
configFileOptions.sort = !configFileOptions['no-sort'];
5859
delete configFileOptions['no-sort'];
5960
}
60-
configFileOptions.bundle = !configFileOptions['no-bundle'];
61-
if (configFileOptions['no-sort'] && configFileOptions['no-bundle'] === true) {
61+
if (configFileOptions['no-bundle']) {
6262
configFileOptions.bundle = !configFileOptions['no-bundle'];
6363
delete configFileOptions['no-bundle'];
6464
}
@@ -128,8 +128,7 @@ describe('openapi-format tests', () => {
128128

129129
try {
130130
if (!readOutput) {
131-
// Write OpenAPI string to file
132-
await writeFile(outputFilename, result, options);
131+
fs.writeFileSync(outputFilename, result, 'utf8');
133132
}
134133
} catch (error) {
135134
console.error('error', error);

test/yaml-filter-inverse-flagsvalue-value-array/output.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ openapi: 3.0.1
22
servers:
33
- url: 'https://server-e'
44
x-stage:
5-
- e
5+
- e

0 commit comments

Comments
 (0)