Skip to content
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

.wp-env.json schema: Fix schema and add unit tests #63281

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion schemas/json/wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"$ref": "#/definitions/wpEnvProperties"
},
{
"type": "object",
"properties": {
"$schema": {
"type": "string"
Expand All @@ -91,10 +92,10 @@
"type": "object",
"patternProperties": {
"[a-zA-Z]": {
"type": "object",
"allOf": [
{ "$ref": "#/definitions/wpEnvProperties" },
{
"type": "object",
"propertyNames": {
"$ref": "#/definitions/wpEnvPropertyNames"
}
Expand All @@ -107,6 +108,7 @@
}
},
{
"type": "object",
"propertyNames": {
"anyOf": [
{
Expand Down
43 changes: 43 additions & 0 deletions test/integration/wp-env-schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* External dependencies
*/
import Ajv from 'ajv';
import glob from 'fast-glob';

/**
* Internal dependencies
*/
import wpEnvSchema from '../../schemas/json/wp-env.json';

describe( '.wp-env.json schema', () => {
const jsonFiles = glob.sync( [ '.wp-env.json' ], { onlyFiles: true } );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think any new .wp-env.json files will be added in the future, but I am concerned that there may be a better pattern.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove the jsonFiles array entirely if there's only one.

  • Import the .wp-env.json file just like the schema as an internal dependency.
  • Remove the 'found .wp-env.json files' test.
  • Replace the test.each with a singular test.
  • Replace the require( filepath ) with the imported variable.

const ajv = new Ajv( {
allowMatchingProperties: true,
} );

test( 'strictly adheres to the draft-07 meta schema', () => {
// Use ajv.compile instead of ajv.validateSchema to validate the schema
// because validateSchema only checks syntax, whereas, compile checks
// if the schema is semantically correct with strict mode.
// See https://github.com/ajv-validator/ajv/issues/1434#issuecomment-822982571
const result = ajv.compile( wpEnvSchema );

expect( result.errors ).toBe( null );
} );

test( 'found .wp-env.json files', () => {
expect( jsonFiles.length ).toBeGreaterThan( 0 );
} );

test.each( jsonFiles )( 'validates schema for `%s`', ( filepath ) => {
// We want to validate the .wp-env.json file using the local schema.
const { $schema, ...metadata } = require( filepath );

// we expect the $schema property to be present in the .wp-env.json file
expect( $schema ).toBeTruthy();

const result = ajv.validate( wpEnvSchema, metadata ) || ajv.errors;

expect( result ).toBe( true );
} );
} );
Loading