fix: bundle lint --format doesn't work properly (#406)

This commit is contained in:
Andriy Leliv
2021-10-20 10:27:49 +03:00
committed by GitHub
parent d0d0aa1cff
commit 195fa94187
10 changed files with 244 additions and 17 deletions

View File

@@ -0,0 +1,2 @@
apiDefinitions:
main: ./openapi.yaml

View File

@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`E2E bundle lint format bundle lint: should be formatted by format: codeframe 1`] = `
[1] openapi.yaml:20:11 at #/paths/~1my_post/post/requestBody/content/application~1json
Expected type \`MediaType\` (object) but got \`null\`
18 | requestBody:
19 | content:
20 | application/json:
| ^^^^^^^^^^^^^^^^^
21 |
Error was generated by the spec rule.
❌ Validation failed with 1 error.
run \`openapi lint --generate-ignore-file\` to add all problems to the ignore file.
bundling ./openapi.yaml...
📦 Created a bundle for ./openapi.yaml at /tmp/null.yaml <test>ms.
`;

View File

@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`E2E bundle lint format bundle lint: no format parameter or empty value should be formatted as codeframe 1`] = `
[1] openapi.yaml:20:11 at #/paths/~1my_post/post/requestBody/content/application~1json
Expected type \`MediaType\` (object) but got \`null\`
18 | requestBody:
19 | content:
20 | application/json:
| ^^^^^^^^^^^^^^^^^
21 |
Error was generated by the spec rule.
❌ Validation failed with 1 error.
run \`openapi lint --generate-ignore-file\` to add all problems to the ignore file.
bundling ./openapi.yaml...
📦 Created a bundle for ./openapi.yaml at /tmp/null.yaml <test>ms.
`;

View File

@@ -0,0 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`E2E bundle lint format bundle lint: should be formatted by format: json 1`] = `
{
"totals": {
"errors": 1,
"warnings": 0,
"ignored": 0
},
"version": "1.0.0-beta.63",
"problems": [
{
"ruleId": "spec",
"severity": "error",
"message": "Expected type \`MediaType\` (object) but got \`null\`",
"suggest": [],
"location": [
{
"source": {
"ref": "openapi.yaml"
},
"pointer": "#/paths/~1my_post/post/requestBody/content/application~1json",
"reportOnKey": false
}
]
}
]
}{
"totals": {
"errors": 0,
"warnings": 0,
"ignored": 0
},
"version": "1.0.0-beta.63",
"problems": []
}
❌ Validation failed with 1 error.
run \`openapi lint --generate-ignore-file\` to add all problems to the ignore file.
bundling ./openapi.yaml...
📦 Created a bundle for ./openapi.yaml at /tmp/null.yaml <test>ms.
`;

View File

@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`E2E bundle lint format bundle lint: no format parameter or empty value should be formatted as codeframe 1`] = `
[1] openapi.yaml:20:11 at #/paths/~1my_post/post/requestBody/content/application~1json
Expected type \`MediaType\` (object) but got \`null\`
18 | requestBody:
19 | content:
20 | application/json:
| ^^^^^^^^^^^^^^^^^
21 |
Error was generated by the spec rule.
❌ Validation failed with 1 error.
run \`openapi lint --generate-ignore-file\` to add all problems to the ignore file.
bundling ./openapi.yaml...
📦 Created a bundle for ./openapi.yaml at /tmp/null.yaml <test>ms.
`;

View File

@@ -0,0 +1,20 @@
openapi: 3.1.0
servers:
- url: https://api.example.com/v1
info:
title: Title
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
description: Description
version: 1.0.0
paths:
/my_post:
post:
operationId: my_post
summary: my_post
requestBody:
content:
application/json:

View File

@@ -0,0 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`E2E bundle bundle-lint-format 1`] = `
openapi: 3.1.0
servers:
- url: https://api.example.com/v1
info:
title: Title
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
description: Description
version: 1.0.0
paths:
/my_post:
post:
operationId: my_post
summary: my_post
requestBody:
content:
application/json: null
components: {}
openapi.yaml:
20:11 error spec Expected type \`MediaType\` (object) but got \`null\`
❌ Validation failed with 1 error.
run \`openapi lint --generate-ignore-file\` to add all problems to the ignore file.
bundling ./openapi.yaml...
📦 Created a bundle for ./openapi.yaml at stdout <test>ms.
`;

View File

@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`E2E bundle lint format bundle lint: should be formatted by format: stylish 1`] = `
openapi.yaml:
20:11 error spec Expected type \`MediaType\` (object) but got \`null\`
❌ Validation failed with 1 error.
run \`openapi lint --generate-ignore-file\` to add all problems to the ignore file.
bundling ./openapi.yaml...
📦 Created a bundle for ./openapi.yaml at /tmp/null.yaml <test>ms.
`;

View File

@@ -16,6 +16,12 @@ addSerializer({
print: (v: any) => v as string,
});
function getEntrypoints(folderPath: string) {
const redoclyYamlFile = readFileSync(join(folderPath, ".redocly.yaml"), "utf8");
const redoclyYaml = parseYaml(redoclyYamlFile) as { apiDefinitions: Record<string, string>; };
return Object.keys(redoclyYaml.apiDefinitions);
}
describe('E2E', () => {
describe('lint', () => {
const folderPath = join(__dirname, 'lint');
@@ -43,12 +49,10 @@ describe('E2E', () => {
const out = r.stdout.toString('utf-8');
const err = r.stderr.toString('utf-8');
const result = `${out}\n${err}`;
// @ts-ignore
expect(result).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
(<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
});
}
})
});
describe('join', () => {
const folderPath = join(__dirname, 'join');
@@ -82,12 +86,10 @@ describe('E2E', () => {
const out = r.stdout.toString('utf-8');
const err = r.stderr.toString('utf-8');
const result = `${out}\n${err}`;
// @ts-ignore
expect(result).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
(<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
});
}
})
});
describe('bundle', () => {
const folderPath = join(__dirname, 'bundle');
@@ -100,15 +102,13 @@ describe('E2E', () => {
continue;
}
const redoclyYamlFile = readFileSync(join(testPath, '.redocly.yaml'), 'utf8');
const redoclyYaml = parseYaml(redoclyYamlFile) as { apiDefinitions: Record<string, string> };
const entryPoints = Object.keys(redoclyYaml.apiDefinitions);
const entryPoints = getEntrypoints(testPath);
const args = [
'../../../packages/cli/src/index.ts',
'--lint',
'--max-problems=1',
'bundle',
'--format=stylish',
...entryPoints
];
@@ -125,10 +125,53 @@ describe('E2E', () => {
const out = r.stdout.toString('utf-8');
const err = r.stderr.toString('utf-8');
const result = `${out}\n${err}`;
// @ts-ignore
expect(result).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
(<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
});
}
})
});
describe('bundle lint format', () => {
let args: string[];
let folderPath: string;
function getBundleResult(params: string[]) {
const result = spawnSync('ts-node', params, {
cwd: folderPath,
env: {
...process.env,
NODE_ENV: 'test',
NO_COLOR: 'TRUE',
},
});
const out = result.stdout.toString('utf-8');
const err = result.stderr.toString('utf-8');
return `${out}\n${err}`;
}
beforeAll(() => {
folderPath = join(__dirname, "bundle/bundle-lint-format");
const entryPoints = getEntrypoints(folderPath);
args = [
"../../../packages/cli/src/index.ts",
"--max-problems=1",
"-o=/tmp/null",
"bundle",
"--lint",
...entryPoints,
];
});
test.each(['codeframe','stylish','json'])('bundle lint: should be formatted by format: %s', (format) => {
const params = [...args, `--format=${format}`];
const result = getBundleResult(params);
(<any>expect(result)).toMatchSpecificSnapshot(join(folderPath, `${format}-format-snapshot.js`));
});
test.each(['noFormatParameter','emptyFormatValue'])('bundle lint: no format parameter or empty value should be formatted as codeframe', (format) => {
const formatArgument = format === 'emptyFormatValue' ? ['--format'] : [];
const params = [...args, ... formatArgument];
const result = getBundleResult(params);
(<any>expect(result)).toMatchSpecificSnapshot(join(folderPath, `${format}-snapshot.js`));
});
});
});

View File

@@ -49,7 +49,7 @@ export async function handleBundle(
});
const fileLintTotals = getTotals(results);
formatProblems(results, { format: 'stylish', totals: fileLintTotals, version, maxProblems });
formatProblems(results, { format: argv.format || 'codeframe', totals: fileLintTotals, version, maxProblems });
printLintTotals(fileLintTotals, 2);
}