-
-
Notifications
You must be signed in to change notification settings - Fork 275
Add normalized hash-based test IDs to draft2020-12/enum.json (POC for #698) #796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
AnirudhJindal
wants to merge
27
commits into
json-schema-org:main
Choose a base branch
from
AnirudhJindal:add-test-ids
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
44fc7b2
Add test IDs to draft2020-12/enum.json using normalized schema hash (…
AnirudhJindal d2980e4
Allow optional id property on tests in test-schema
AnirudhJindal 8b990ad
Fix script based on reviews
AnirudhJindal 7a2270b
adding jsoc-parser and updating according to the reviews
AnirudhJindal 6cc0f8f
minor changes as per review
AnirudhJindal 7b86302
Remove accidentally committed node_modules
AnirudhJindal 90d4ebc
added a github action for automating addition of test-ids
AnirudhJindal ab25581
changed the script restore method to the path filtring
AnirudhJindal a236d61
Add test IDs to draft2020-12/enum.json using normalized schema hash (…
AnirudhJindal 0ae5f26
Allow optional id property on tests in test-schema
AnirudhJindal 44ab305
Fix script based on reviews
AnirudhJindal 927bd9b
adding jsoc-parser and updating according to the reviews
AnirudhJindal 9ca9e3b
minor changes as per review
AnirudhJindal c7799ef
Remove accidentally committed node_modules
AnirudhJindal 343d4ef
added a github action for automating addition of test-ids
AnirudhJindal ac90ab4
changed the script restore method to the path filtring
AnirudhJindal ef1c568
Fix some remote categorization for easier dialect-specific loading
jdesrosiers 9cb5e89
All dependencies should be devDependencies
jdesrosiers 61f1940
Fix issues with v1 tests
jdesrosiers ed50aef
Add alternate version of script and action
jdesrosiers 4d98634
Disable generating test ids for now
jdesrosiers 470010d
Try to fix action checkout bug
jdesrosiers 46f5725
accept upstream changes and simplyfying apply-test-ids.yml
AnirudhJindal 323a738
fix: accept incoming changes for package files
AnirudhJindal db70c2c
removed the not needed second test file filter
AnirudhJindal bbb9c24
changed the node version to latest
AnirudhJindal d14cdd4
fix: updated the script
AnirudhJindal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,21 @@ | ||
| { | ||
| "name": "json-schema-test-suite", | ||
| "version": "0.1.0", | ||
| "type": "module", | ||
| "description": "A language agnostic test suite for the JSON Schema specifications", | ||
| "repository": "github:json-schema-org/JSON-Schema-Test-Suite", | ||
| "keywords": [ | ||
| "json-schema", | ||
| "tests" | ||
| ], | ||
| "author": "http://json-schema.org", | ||
| "license": "MIT" | ||
| "license": "MIT", | ||
| "dependencies": { | ||
| "@hyperjump/browser": "^1.3.1", | ||
| "@hyperjump/json-pointer": "^1.1.1", | ||
| "@hyperjump/json-schema": "^1.17.2", | ||
| "@hyperjump/pact": "^1.4.0", | ||
| "@hyperjump/uri": "^1.3.2", | ||
| "json-stringify-deterministic": "^1.0.12" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import * as fs from "node:fs"; | ||
| import * as crypto from "node:crypto"; | ||
| import jsonStringify from "json-stringify-deterministic"; | ||
| import { parse, modify, applyEdits } from "jsonc-parser"; | ||
| import { normalize } from "./normalize.js"; | ||
| import { loadRemotes } from "./load-remotes.js"; | ||
|
|
||
|
|
||
| const DIALECT_MAP = { | ||
| "draft2020-12": "https://json-schema.org/draft/2020-12/schema", | ||
| "draft2019-09": "https://json-schema.org/draft/2019-09/schema", | ||
| "draft7": "http://json-schema.org/draft-07/schema#", | ||
| "draft6": "http://json-schema.org/draft-06/schema#", | ||
| "draft4": "http://json-schema.org/draft-04/schema#" | ||
| }; | ||
|
|
||
|
|
||
| function generateTestId(normalizedSchema, testData, testValid) { | ||
| return crypto | ||
| .createHash("md5") | ||
| .update( | ||
| jsonStringify(normalizedSchema) + | ||
| jsonStringify(testData) + | ||
| testValid | ||
| ) | ||
| .digest("hex"); | ||
| } | ||
|
|
||
| async function addIdsToFile(filePath, dialectUri) { | ||
| console.log("Reading:", filePath); | ||
|
|
||
| const text = fs.readFileSync(filePath, "utf8"); | ||
| const tests = parse(text); | ||
| let edits = []; | ||
| let added = 0; | ||
|
|
||
| for (let i = 0; i < tests.length; i++) { | ||
| const testCase = tests[i]; | ||
| const normalizedSchema = await normalize(testCase.schema, dialectUri); | ||
|
|
||
| for (let j = 0; j < testCase.tests.length; j++) { | ||
| const test = testCase.tests[j]; | ||
|
|
||
| if (!test.id) { | ||
| const id = generateTestId( | ||
| normalizedSchema, | ||
| test.data, | ||
| test.valid | ||
| ); | ||
|
|
||
| const path = [i, "tests", j, "id"]; | ||
|
|
||
| edits.push( | ||
| ...modify(text, path, id, { | ||
| formattingOptions: { | ||
| insertSpaces: true, | ||
| tabSize: 2 | ||
| } | ||
| }) | ||
| ); | ||
|
|
||
| added++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (added > 0) { | ||
| const updatedText = applyEdits(text, edits); | ||
| fs.writeFileSync(filePath, updatedText); | ||
| console.log(` Added ${added} IDs`); | ||
| } else { | ||
| console.log(" All tests already have IDs"); | ||
| } | ||
| } | ||
|
|
||
| //CLI stuff | ||
|
|
||
| const dialectArg = process.argv[2]; | ||
| if (!dialectArg || !DIALECT_MAP[dialectArg]) { | ||
| console.error("Usage: node add-test-ids.js <dialect> [file-path]"); | ||
| console.error("Available dialects:", Object.keys(DIALECT_MAP).join(", ")); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const dialectUri = DIALECT_MAP[dialectArg]; | ||
| const filePath = process.argv[3]; | ||
|
|
||
| // Load remotes only for the specified dialect | ||
| loadRemotes(dialectUri, "./remotes"); | ||
|
|
||
| if (filePath) { | ||
| await addIdsToFile(filePath, dialectUri); | ||
| } else { | ||
| const testDir = `tests/${dialectArg}`; | ||
| const files = fs.readdirSync(testDir).filter(f => f.endsWith(".json")); | ||
|
|
||
| for (const file of files) { | ||
| await addIdsToFile(`${testDir}/${file}`, dialectUri); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import * as fs from "node:fs"; | ||
| import * as path from "node:path"; | ||
| import * as crypto from "node:crypto"; | ||
| import jsonStringify from "json-stringify-deterministic"; | ||
| import { normalize } from "./normalize.js"; | ||
| import { loadRemotes } from "./load-remotes.js"; | ||
|
|
||
|
|
||
| // Helpers | ||
|
|
||
| function* jsonFiles(dir) { | ||
| for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { | ||
| const full = path.join(dir, entry.name); | ||
| if (entry.isDirectory()) { | ||
| yield* jsonFiles(full); | ||
| } else if (entry.isFile() && entry.name.endsWith(".json")) { | ||
| yield full; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function dialectFromDir(dir) { | ||
| const draft = path.basename(dir); | ||
|
|
||
| switch (draft) { | ||
| case "draft2020-12": | ||
| return "https://json-schema.org/draft/2020-12/schema"; | ||
| case "draft2019-09": | ||
| return "https://json-schema.org/draft/2019-09/schema"; | ||
| case "draft7": | ||
| return "http://json-schema.org/draft-07/schema#"; | ||
| case "draft6": | ||
| return "http://json-schema.org/draft-06/schema#"; | ||
| case "draft4": | ||
| return "http://json-schema.org/draft-04/schema#"; | ||
| default: | ||
| throw new Error(`Unknown draft directory: ${draft}`); | ||
| } | ||
| } | ||
|
|
||
| function generateTestId(normalizedSchema, testData, testValid) { | ||
| return crypto | ||
| .createHash("md5") | ||
| .update( | ||
| jsonStringify(normalizedSchema) + | ||
| jsonStringify(testData) + | ||
| testValid | ||
| ) | ||
| .digest("hex"); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| async function checkVersion(dir) { | ||
| const missingIdFiles = new Set(); | ||
| const mismatchedIdFiles = new Set(); | ||
|
|
||
| const dialectUri = dialectFromDir(dir); | ||
|
|
||
| console.log(`Checking tests in ${dir}...`); | ||
| console.log(`Using dialect: ${dialectUri}`); | ||
|
|
||
| // Load remotes ONCE for this dialect | ||
| const remotesPath = "./remotes"; | ||
| if (fs.existsSync(remotesPath)) { | ||
|
||
| loadRemotes(dialectUri, remotesPath); | ||
| } | ||
|
|
||
| for (const file of jsonFiles(dir)) { | ||
| const testCases = JSON.parse(fs.readFileSync(file, "utf8")); | ||
|
|
||
| for (const testCase of testCases) { | ||
| const normalizedSchema = await normalize(testCase.schema, dialectUri); | ||
|
|
||
| for (const test of testCase.tests) { | ||
| if (!test.id) { | ||
| missingIdFiles.add(file); | ||
| console.log( | ||
| ` ✗ Missing ID: ${file} | ${testCase.description} | ${test.description}` | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| const expectedId = generateTestId( | ||
| normalizedSchema, | ||
| test.data, | ||
| test.valid | ||
| ); | ||
|
|
||
| if (test.id !== expectedId) { | ||
| mismatchedIdFiles.add(file); | ||
| console.log(` ✗ Mismatched ID: ${file}`); | ||
| console.log( | ||
| ` Test: ${testCase.description} | ${test.description}` | ||
| ); | ||
| console.log(` Current ID: ${test.id}`); | ||
| console.log(` Expected ID: ${expectedId}`); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //Summary | ||
| console.log("\n" + "=".repeat(60)); | ||
| console.log("Summary:"); | ||
| console.log("=".repeat(60)); | ||
|
|
||
| console.log("\nFiles with missing IDs:"); | ||
| missingIdFiles.size === 0 | ||
| ? console.log(" ✓ None") | ||
| : [...missingIdFiles].forEach(f => console.log(` - ${f}`)); | ||
|
|
||
| console.log("\nFiles with mismatched IDs:"); | ||
| mismatchedIdFiles.size === 0 | ||
| ? console.log(" ✓ None") | ||
| : [...mismatchedIdFiles].forEach(f => console.log(` - ${f}`)); | ||
|
|
||
| const hasErrors = | ||
| missingIdFiles.size > 0 || mismatchedIdFiles.size > 0; | ||
|
|
||
| console.log("\n" + "=".repeat(60)); | ||
| if (hasErrors) { | ||
| console.log("❌ Check failed - issues found"); | ||
| process.exit(1); | ||
| } else { | ||
| console.log("✅ All checks passed!"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // CLI | ||
|
|
||
|
|
||
| const dir = process.argv[2]; | ||
| if (!dir) { | ||
| console.error("Usage: node scripts/check-test-ids.js <tests/draftXXXX>"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| await checkVersion(dir); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // scripts/load-remotes.js | ||
| import * as fs from "node:fs"; | ||
| import { toAbsoluteIri } from "@hyperjump/uri"; | ||
| import { registerSchema } from "@hyperjump/json-schema/draft-2020-12"; | ||
|
|
||
| // Keep track of which remote URLs we've already registered | ||
| const loadedRemotes = new Set(); | ||
|
|
||
| export const loadRemotes = (dialectId, filePath, url = "") => { | ||
| if (!fs.existsSync(filePath)) { | ||
| console.warn(`Warning: Remotes path not found: ${filePath}`); | ||
| return; | ||
| } | ||
|
|
||
| fs.readdirSync(filePath, { withFileTypes: true }).forEach((entry) => { | ||
| if (entry.isFile() && entry.name.endsWith(".json")) { | ||
| const remotePath = `${filePath}/${entry.name}`; | ||
| const remoteUrl = `http://localhost:1234${url}/${entry.name}`; | ||
|
|
||
| // Skip if already registered | ||
| if (loadedRemotes.has(remoteUrl)) { | ||
| return; | ||
| } | ||
|
|
||
| const remote = JSON.parse(fs.readFileSync(remotePath, "utf8")); | ||
|
|
||
| // FIXEDhere | ||
| if (typeof remote.$id === "string" && remote.$id.startsWith("file:")) { | ||
| remote.$id = remote.$id.replace(/^file:/, "x-file:"); | ||
| } | ||
|
|
||
| // Only register if $schema matches dialect OR there's no $schema | ||
| if (!remote.$schema || toAbsoluteIri(remote.$schema) === dialectId) { | ||
| registerSchema(remote, remoteUrl, dialectId); | ||
| loadedRemotes.add(remoteUrl); | ||
| } | ||
| } else if (entry.isDirectory()) { | ||
| loadRemotes( | ||
| dialectId, | ||
| `${filePath}/${entry.name}`, | ||
| `${url}/${entry.name}` | ||
| ); | ||
| } | ||
| }); | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The convention for tabSize in this repo is 4, not 2.