Skip to content

Commit

Permalink
Updated dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
seriousme committed Sep 1, 2021
1 parent 5fc7729 commit 238bf71
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## [Unreleased]
### Changed
- Added URI decoding to refs
- Updated dependencies
- node-fetch ^2.6.1 → ^3.0.0
- ajv ^8.6.1 → ^8.6.2
- ajv-formats ^2.1.0 → ^2.1.1

## [v1.3.0] 07-07-2021
### Changed
Expand Down
95 changes: 84 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"validate-api": "./bin/validate-api-cli.js"
},
"dependencies": {
"ajv": "^8.6.1",
"ajv": "^8.6.2",
"ajv-draft-04": "^1.0.0",
"ajv-formats": "^2.1.0",
"ajv-formats": "^2.1.1",
"js-yaml": "^4.1.0"
},
"scripts": {
Expand All @@ -32,7 +32,7 @@
"author": "Hans Klunder",
"license": "MIT",
"devDependencies": {
"node-fetch": "^2.6.1",
"node-fetch": "^3.0.0",
"tap": "^15.0.9"
},
"directories": {
Expand Down
70 changes: 38 additions & 32 deletions test/realworld/realworld.js → test/realworld/realworld.mjs
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
// test the validator against the APIs of https://apis.guru
const Validator = require("../../index.js");
import { createRequire } from 'module';
const importJSON = createRequire(import.meta.url);
const localFile = (fileName) => (new URL(fileName, import.meta.url)).pathname
import Validator from "../../index.js";
const validator = new Validator();
const { writeFileSync } = require("fs");
const fetch = require("node-fetch");
const { argv, exit } = require("process");
const JSYaml = require("js-yaml");
const { createReport } = require("./createReport.js");
const yamlOpts = { schema: JSYaml.JSON_SCHEMA };
const failedFile = `${__dirname}/failed.json`;
const reportFile = `${__dirname}/failed.md`;
const newFailedFile = `${__dirname}/failed.updated.json`;
const newReportFile = `${__dirname}/failed.updated.md`;
import { writeFileSync } from "fs";
import fetch from "node-fetch";
import { argv, exit } from "process";
import { JSON_SCHEMA, load } from "js-yaml";
import { createReport } from "./createReport.js";
const yamlOpts = { schema: JSON_SCHEMA };
const failedFile = localFile('/failed.json');
const reportFile = localFile('/failed.md');
const newFailedFile = localFile('/failed.updated.json');
const newReportFile = localFile('/failed.updated.md');
const defaultPercentage = 10;
const failedMap = loadFailedData(failedFile);


function loadFailedData(fileName) {
const dataMap = new Map();
try {
const data = require(fileName);
const data = importJSON(fileName);
data.failedTests = data.failedTests || [];
data.failedTests.forEach(item => dataMap.set(item.name, item));
data.failedTests.forEach((item) => dataMap.set(item.name, item));
return dataMap;
} catch (_) {
return dataMap;
}
}


function sample(fullMap, percentage) {
const { floor, random } = Math;
const len = fullMap.size;
Expand All @@ -52,7 +53,7 @@ function unescapeJsonPointer(str) {
}

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

function makeRexp(pathItem) {
Expand All @@ -77,7 +78,7 @@ function yamlLine(yamlSpec, path) {
function findArrayItem(lines, num, pathIdx) {
if (num > lines.length - 2) {
return num;
};
}
const firstItem = lines[num + 1];
const match = firstItem.match(/^\s*-/);
if (match === null) {
Expand Down Expand Up @@ -105,9 +106,9 @@ function findItem(lines, num, pathItem) {

function getInstanceValue(yamlSpec, path) {
if (path === "") {
return [false, 'content too large'];
return [false, "content too large"];
}
const obj = JSYaml.load(yamlSpec, yamlOpts);
const obj = load(yamlSpec, yamlOpts);
const paths = path.split("/").slice(1);
const result = paths.reduce((o, n) => o[unescapeJsonPointer(n)], obj);
return [true, result];
Expand All @@ -116,7 +117,7 @@ function getInstanceValue(yamlSpec, path) {
function yamlToGitHub(url) {
return url.replace(
"https://api.apis.guru/v2/specs/",
"https://github.com/APIs-guru/openapi-directory/blob/main/APIs/"
"https://github.com/APIs-guru/openapi-directory/blob/main/APIs/",
);
}

Expand Down Expand Up @@ -146,7 +147,7 @@ async function fetchApiList(percentage, onlyFailed = false) {
}
if (percentage !== 100) {
console.log(
`testing a random set containing ${percentage}% of ${apiMap.size} available APIs`
`testing a random set containing ${percentage}% of ${apiMap.size} available APIs`,
);
return [sample(apiMap, percentage), apiListSize, apiMap.size];
}
Expand All @@ -167,7 +168,10 @@ async function testAPIs(percentage, onlyFailed, ci) {
if (onlyFailed || ci) {
percentage = 100;
}
const [apiList, totalSize, latestSize] = await fetchApiList(percentage, onlyFailed);
const [apiList, totalSize, latestSize] = await fetchApiList(
percentage,
onlyFailed,
);
const failed = new Map();
const results = {
total: apiList.size,
Expand All @@ -188,14 +192,16 @@ async function testAPIs(percentage, onlyFailed, ci) {
const [res, value] = getInstanceValue(spec, item.instancePath);
item.hasInstanceValue = res;
item.instanceValue = value;
item.gitHubUrl = `${api.gitHubUrl}#L${yamlLine(
spec,
item.instancePath
)}`;
item.gitHubUrl = `${api.gitHubUrl}#L${
yamlLine(
spec,
item.instancePath,
)
}`;
});
if (failedMap.has(name)) {
const failedApiErrors = JSON.stringify(
failedMap.get(name).result.errors
failedMap.get(name).result.errors,
);
if (failedApiErrors === JSON.stringify(api.result.errors)) {
results.knownFailed++;
Expand All @@ -208,7 +214,7 @@ async function testAPIs(percentage, onlyFailed, ci) {
}
console.log(
`Finished testing ${results.total} APIs
${results.invalid} tests failed of which ${results.knownFailed} were known failures`
${results.invalid} tests failed of which ${results.knownFailed} were known failures`,
);
if (
results.knownFailed !== results.invalid ||
Expand All @@ -224,20 +230,20 @@ async function testAPIs(percentage, onlyFailed, ci) {
testedAPICount: results.total,
failedAPICount: results.invalid,
knownFailedCount: results.knownFailed,
failedTests: Array.from(failed.values())
}
failedTests: Array.from(failed.values()),
};
console.log(`new/updated failures found`);
console.log(`creating ${jsonFile}`);
writeFileSync(
jsonFile,
JSON.stringify(data, null, 2),
"utf8"
"utf8",
);
console.log(`creating new report ${mdFile}`);
writeFileSync(
mdFile,
createReport(data),
"utf8"
"utf8",
);
}
process.exit(exitCode);
Expand Down

0 comments on commit 238bf71

Please sign in to comment.