Skip to content

Commit

Permalink
allow sub-folders in pixel definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
nshuba committed Dec 11, 2024
1 parent 51b109c commit cdc0fc4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
22 changes: 16 additions & 6 deletions bin/validate_schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,34 @@ const argv = yargs(hideBin(process.argv))

// 1) Validate common params and suffixes
const mainDir = argv.dirPath;
const pixelsDir = path.join(mainDir, 'pixels');
const commonParams = JSON5.parse(fs.readFileSync(`${mainDir}/common_params.json`));
const commonSuffixes = JSON5.parse(fs.readFileSync(`${mainDir}/common_suffixes.json`));
const validator = new DefinitionsValidator(commonParams, commonSuffixes);
logErrors('ERROR in common_params.json:', validator.validateCommonParamsDefinition());
logErrors('ERROR in common_suffixes.json:', validator.validateCommonSuffixesDefinition());

// 2) Validate pixels and params
const pixelsDir = path.join(mainDir, 'pixels');
function validateFile(file) {
console.log(`Validating pixels definition: ${file}`);
const pixelsDef = JSON5.parse(fs.readFileSync(path.join(pixelsDir, file)));
const pixelsDef = JSON5.parse(fs.readFileSync(file));
logErrors(`ERROR in ${file}:`, validator.validatePixelsDefinition(pixelsDef));
}

function validateFolder(folder) {
fs.readdirSync(folder).forEach((file) => {
const fullPath = path.join(folder, file);
if (fs.statSync(fullPath).isDirectory()) {
validateFolder(fullPath);
return;
}

validateFile(fullPath);
});
}

if (argv.file) {
validateFile(argv.file);
validateFile(path.join(pixelsDir, argv.file));
} else {
fs.readdirSync(pixelsDir).forEach((file) => {
validateFile(file);
});
validateFolder(pixelsDir);
}
18 changes: 11 additions & 7 deletions tests/integration_test.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { exec } from 'child_process';
import { expect } from 'chai';
import path from 'path';

const timeout = 5000;
const validDefsPath = path.join('tests', 'test_data', 'valid');
const invalidDefsPath = path.join('tests', 'test_data', 'invalid');
describe('Invalid defs', () => {
it('should output all required params', (done) => {
exec('npm run validate-ddg-pixel-defs tests/test_data/invalid', (error, _, stderr) => {
exec(`npm run validate-ddg-pixel-defs ${invalidDefsPath}`, (error, _, stderr) => {
const pixelPath = path.join(invalidDefsPath, 'pixels', 'pixels.json');
const expectedErrors = [
"ERROR in pixels.json: /invalid_pixel must have required property 'description'",
"ERROR in pixels.json: /invalid_pixel must have required property 'owners'",
"ERROR in pixels.json: /invalid_pixel must have required property 'triggers'",
`ERROR in ${pixelPath}: /invalid_pixel must have required property 'description'`,
`ERROR in ${pixelPath}: /invalid_pixel must have required property 'owners'`,
`ERROR in ${pixelPath}: /invalid_pixel must have required property 'triggers'`,
];

const errors = stderr.trim().split('\n');
Expand All @@ -22,7 +26,7 @@ describe('Invalid defs', () => {

describe('Valid defs', () => {
it('should exit normally', (done) => {
exec('npm run validate-ddg-pixel-defs tests/test_data/valid', (error, _, stderr) => {
exec(`npm run validate-ddg-pixel-defs ${validDefsPath}`, (error, _, stderr) => {
expect(stderr.length).to.equal(0);
expect(error).to.equal(null);

Expand All @@ -34,7 +38,7 @@ describe('Valid defs', () => {
describe('Invalid live pixel', () => {
it('should output extra property error', (done) => {
exec(
'npm run validate-ddg-live-pixel tests/test_data/valid test_pixels.json m.netp.tunnel.stop.failure /t/m_netp_tunnel_stop_failure_d_ios_phone?x=1',
`npm run validate-ddg-live-pixel ${validDefsPath} pixel_subfolder/test_pixels.json m.netp.tunnel.stop.failure /t/m_netp_tunnel_stop_failure_d_ios_phone?x=1`,
(error, _, stderr) => {
const expectedErrors = ["ERROR: must NOT have additional properties. Found extra property 'x'"];

Expand All @@ -51,7 +55,7 @@ describe('Invalid live pixel', () => {
describe('Valid live pixel', () => {
it('should exit normally', (done) => {
exec(
'npm run validate-ddg-live-pixel tests/test_data/valid test_pixels.json m.netp.tunnel.stop.failure /t/m_netp_tunnel_stop_failure_d_ios_phone?ud5=1',
`npm run validate-ddg-live-pixel ${validDefsPath} pixel_subfolder/test_pixels.json m.netp.tunnel.stop.failure /t/m_netp_tunnel_stop_failure_d_ios_phone?ud5=1`,
(error, _, stderr) => {
expect(stderr.length).to.equal(0);
expect(error).to.equal(null);
Expand Down

0 comments on commit cdc0fc4

Please sign in to comment.