Skip to content

Commit 32afa53

Browse files
committed
Compare text output when testing sort
1 parent 8a0967a commit 32afa53

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

test/__utils__/test-utils.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,47 @@ const sy = require("@stoplight/yaml");
33
const {exec} = require("child_process");
44
const path = require("path");
55

6-
async function loadTest(folder, inputType = 'yaml', outType = 'yaml') {
7-
let input, outputBefore, outputAfter = {}
6+
async function loadRawTest(folder, inputType = 'yaml', outType = 'yaml') {
7+
let input = outputBefore = outputAfter = '{}'
88
const testPath = `./test/${folder}`
99
const inputFile = `input.${inputType}`
1010
const inputPath = `./test/${folder}/${inputFile}`
1111
const outputFile = `output.${outType}`
1212
const outputPath = `./test/${folder}/${outputFile}`
1313

1414
try {
15-
if (outType === 'json') {
16-
input = JSON.parse(fs.readFileSync(inputPath, 'utf8'));
17-
} else {
18-
input = sy.parse(fs.readFileSync(inputPath, 'utf8'));
19-
}
15+
input = fs.readFileSync(inputPath, 'utf8');
2016
} catch (err) {
2117
// File not found = {} will be used
2218
}
2319

2420
try {
25-
if (outType === 'json') {
26-
outputBefore = JSON.parse(fs.readFileSync(outputPath, 'utf8'));
27-
} else {
28-
outputBefore = sy.parse(fs.readFileSync(outputPath, 'utf8'));
29-
}
21+
outputBefore = fs.readFileSync(outputPath, 'utf8');
3022
} catch (err) {
3123
// File not found = {} will be used
3224
}
3325

3426
let result = await cli([`${inputFile}`, `--configFile options.yaml`], testPath);
3527

3628
try {
37-
if (outType === 'json') {
38-
outputAfter = JSON.parse(fs.readFileSync(outputPath, 'utf8'));
39-
} else {
40-
outputAfter = sy.parse(fs.readFileSync(outputPath, 'utf8'));
41-
}
29+
outputAfter = fs.readFileSync(outputPath, 'utf8');
4230
} catch (err) {
4331
//
4432
}
4533

4634
return {result: result, input: input, outputBefore: outputBefore, outputAfter: outputAfter}
4735
}
4836

37+
async function loadTest(folder, inputType = 'yaml', outType = 'yaml') {
38+
let raw = await loadRawTest(folder, inputType, outType)
39+
40+
raw.input = (inputType === 'json') ? JSON.parse(raw.input) : sy.parse(raw.input);
41+
raw.outputBefore = (outType === 'json') ? JSON.parse(raw.outputBefore) : sy.parse(raw.outputBefore);
42+
raw.outputAfter = (outType === 'json') ? JSON.parse(raw.outputAfter) : sy.parse(raw.outputAfter);
43+
44+
return raw
45+
}
46+
4947
function cli(args, cwd) {
5048
return new Promise(resolve => {
5149
exec(
@@ -64,6 +62,7 @@ function cli(args, cwd) {
6462
}
6563

6664
module.exports = {
65+
loadRawTest: loadRawTest,
6766
loadTest: loadTest,
6867
cli: cli
6968
};

test/sorting.test.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('openapi-format CLI sorting tests', () => {
99
describe('json-custom', () => {
1010
it('json-custom - should match expected output', async () => {
1111
const testName = 'json-custom'
12-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName, 'json', 'json')
12+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName, 'json', 'json')
1313
// console.log('result',result)
1414
expect(outputAfter).toStrictEqual(outputBefore);
1515
expect(result.code).toBe(0);
@@ -20,7 +20,7 @@ describe('openapi-format CLI sorting tests', () => {
2020
describe('json-custom-yaml', () => {
2121
it('json-custom-yaml - should match expected output', async () => {
2222
const testName = 'json-custom-yaml'
23-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName, 'json', 'yaml')
23+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName, 'json', 'yaml')
2424
// console.log('result',result)
2525
expect(outputAfter).toStrictEqual(outputBefore);
2626
expect(result.code).toBe(0);
@@ -31,7 +31,7 @@ describe('openapi-format CLI sorting tests', () => {
3131
describe('json-default', () => {
3232
it('json-default - should match expected output', async () => {
3333
const testName = 'json-default'
34-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName, 'json', 'json')
34+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName, 'json', 'json')
3535
// console.log('result',result)
3636
expect(result.code).toBe(0);
3737
expect(result.stdout).toContain("formatted successfully");
@@ -42,7 +42,7 @@ describe('openapi-format CLI sorting tests', () => {
4242
describe('json-default-yaml', () => {
4343
it('json-default-yaml - should match expected output', async () => {
4444
const testName = 'json-default-yaml'
45-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName, 'json', 'yaml')
45+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName, 'json', 'yaml')
4646
// console.log('result',result)
4747
expect(result.code).toBe(0);
4848
expect(result.stdout).toContain("formatted successfully");
@@ -53,7 +53,7 @@ describe('openapi-format CLI sorting tests', () => {
5353
describe('json-sort-properties', () => {
5454
it('json-sort-properties - should match expected output', async () => {
5555
const testName = 'json-sort-properties'
56-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName, 'json', 'json')
56+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName, 'json', 'json')
5757
// console.log('result',result)
5858
expect(outputAfter).toStrictEqual(outputBefore);
5959
expect(result.code).toBe(0);
@@ -64,7 +64,7 @@ describe('openapi-format CLI sorting tests', () => {
6464
describe('json-no-sort', () => {
6565
it('json-no-sort - should match expected output', async () => {
6666
const testName = 'json-no-sort'
67-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName, 'json', 'json')
67+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName, 'json', 'json')
6868
// console.log('result',result)
6969
expect(result.code).toBe(0);
7070
expect(result.stdout).toContain("formatted successfully");
@@ -75,7 +75,7 @@ describe('openapi-format CLI sorting tests', () => {
7575
describe('json-rename', () => {
7676
it('json-rename - should match expected output', async () => {
7777
const testName = 'json-rename'
78-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName, 'json', 'json')
78+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName, 'json', 'json')
7979
// console.log('result',result)
8080
expect(result.code).toBe(0);
8181
expect(result.stdout).toContain("formatted successfully");
@@ -86,7 +86,7 @@ describe('openapi-format CLI sorting tests', () => {
8686
describe('json-sort-components', () => {
8787
it('json-sort-components - should match expected output', async () => {
8888
const testName = 'json-sort-components'
89-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName, 'json', 'json')
89+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName, 'json', 'json')
9090
// console.log('result',result)
9191
expect(result.code).toBe(0);
9292
expect(result.stdout).toContain("formatted successfully");
@@ -97,7 +97,7 @@ describe('openapi-format CLI sorting tests', () => {
9797
describe('yaml-custom', () => {
9898
it('yaml-custom - should match expected output', async () => {
9999
const testName = 'yaml-custom'
100-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName)
100+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName)
101101
// console.log('result',result)
102102
expect(result.code).toBe(0);
103103
expect(result.stdout).toContain("formatted successfully");
@@ -108,7 +108,7 @@ describe('openapi-format CLI sorting tests', () => {
108108
describe('yaml-custom-json', () => {
109109
it('yaml-custom-json - should match expected output', async () => {
110110
const testName = 'yaml-custom-json'
111-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName, 'yaml', 'json')
111+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName, 'yaml', 'json')
112112
// console.log('result',result)
113113
expect(result.code).toBe(0);
114114
expect(result.stdout).toContain("formatted successfully");
@@ -119,7 +119,7 @@ describe('openapi-format CLI sorting tests', () => {
119119
describe('yaml-default', () => {
120120
it('yaml-default - should match expected output', async () => {
121121
const testName = 'yaml-default'
122-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName)
122+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName)
123123
// console.log('result',result)
124124
expect(result.code).toBe(0);
125125
expect(result.stdout).toContain("formatted successfully");
@@ -130,7 +130,7 @@ describe('openapi-format CLI sorting tests', () => {
130130
describe('yaml-default-bug-examples-properties', () => {
131131
it('yaml-default-bug-examples-properties - should match expected output', async () => {
132132
const testName = 'yaml-default-bug-examples-properties'
133-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName)
133+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName)
134134
// console.log('result',result)
135135
expect(result.code).toBe(0);
136136
expect(result.stdout).toContain("formatted successfully");
@@ -141,7 +141,7 @@ describe('openapi-format CLI sorting tests', () => {
141141
describe('yaml-default-bug-nested-properties', () => {
142142
it('yaml-default-bug-nested-properties - should match expected output', async () => {
143143
const testName = 'yaml-default-bug-nested-properties'
144-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName)
144+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName)
145145
// console.log('result',result)
146146
expect(result.code).toBe(0);
147147
expect(result.stdout).toContain("formatted successfully");
@@ -152,7 +152,7 @@ describe('openapi-format CLI sorting tests', () => {
152152
describe('yaml-default-json', () => {
153153
it('yaml-default-json - should match expected output', async () => {
154154
const testName = 'yaml-default-json'
155-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName, 'yaml', 'json')
155+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName, 'yaml', 'json')
156156
// console.log('result',result)
157157
expect(result.code).toBe(0);
158158
expect(result.stdout).toContain("formatted successfully");
@@ -163,7 +163,7 @@ describe('openapi-format CLI sorting tests', () => {
163163
describe('yaml-linewidth', () => {
164164
it('yaml-linewidth - should match expected output', async () => {
165165
const testName = 'yaml-linewidth'
166-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName)
166+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName)
167167
// console.log('result',result)
168168
expect(result.code).toBe(0);
169169
expect(result.stdout).toContain("formatted successfully");
@@ -174,7 +174,7 @@ describe('openapi-format CLI sorting tests', () => {
174174
describe('yaml-no-sort', () => {
175175
it('yaml-no-sort - should match expected output', async () => {
176176
const testName = 'yaml-no-sort'
177-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName)
177+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName)
178178
// console.log('result',result)
179179
expect(result.code).toBe(0);
180180
expect(result.stdout).toContain("formatted successfully");
@@ -185,7 +185,7 @@ describe('openapi-format CLI sorting tests', () => {
185185
describe('yaml-rename', () => {
186186
it('yaml-rename - should match expected output', async () => {
187187
const testName = 'yaml-rename'
188-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName)
188+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName)
189189
// console.log('result',result)
190190
expect(result.code).toBe(0);
191191
expect(result.stdout).toContain("formatted successfully");
@@ -196,7 +196,7 @@ describe('openapi-format CLI sorting tests', () => {
196196
describe('yaml-sort-components', () => {
197197
it('yaml-sort-components - should match expected output', async () => {
198198
const testName = 'yaml-sort-components'
199-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName)
199+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName)
200200
// console.log('result',result)
201201
expect(result.code).toBe(0);
202202
expect(result.stdout).toContain("formatted successfully");
@@ -207,7 +207,7 @@ describe('openapi-format CLI sorting tests', () => {
207207
describe('yaml-stoplight-studio-style', () => {
208208
it('yaml-stoplight-studio-style - should match expected output', async () => {
209209
const testName = 'yaml-stoplight-studio-style'
210-
const {result, input, outputBefore, outputAfter} = await testUtils.loadTest(testName)
210+
const {result, input, outputBefore, outputAfter} = await testUtils.loadRawTest(testName)
211211
// console.log('result',result)
212212
expect(result.code).toBe(0);
213213
expect(result.stdout).toContain("formatted successfully");

0 commit comments

Comments
 (0)