From cdc0fc46f5ef0bb8b8a8d3fbc2b7e15da63e2f8d Mon Sep 17 00:00:00 2001 From: Anastasia Shuba Date: Wed, 11 Dec 2024 12:10:39 -0800 Subject: [PATCH 1/2] allow sub-folders in pixel definitions --- bin/validate_schema.mjs | 22 ++++++++++++++----- tests/integration_test.mjs | 18 +++++++++------ .../{ => pixel_subfolder}/test_pixels.json | 0 3 files changed, 27 insertions(+), 13 deletions(-) rename tests/test_data/valid/pixels/{ => pixel_subfolder}/test_pixels.json (100%) diff --git a/bin/validate_schema.mjs b/bin/validate_schema.mjs index b13687b..3f945d5 100644 --- a/bin/validate_schema.mjs +++ b/bin/validate_schema.mjs @@ -32,6 +32,7 @@ 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); @@ -39,17 +40,26 @@ logErrors('ERROR in common_params.json:', validator.validateCommonParamsDefiniti 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); } diff --git a/tests/integration_test.mjs b/tests/integration_test.mjs index 3d36fc1..14820ad 100644 --- a/tests/integration_test.mjs +++ b/tests/integration_test.mjs @@ -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'); @@ -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); @@ -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'"]; @@ -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); diff --git a/tests/test_data/valid/pixels/test_pixels.json b/tests/test_data/valid/pixels/pixel_subfolder/test_pixels.json similarity index 100% rename from tests/test_data/valid/pixels/test_pixels.json rename to tests/test_data/valid/pixels/pixel_subfolder/test_pixels.json From 2fbcdfbfe78f3c52efc8d862bef2069dd0a5f8b5 Mon Sep 17 00:00:00 2001 From: Anastasia Shuba Date: Thu, 12 Dec 2024 16:44:26 -0800 Subject: [PATCH 2/2] code review --- bin/validate_schema.mjs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/validate_schema.mjs b/bin/validate_schema.mjs index 3f945d5..367cb3d 100644 --- a/bin/validate_schema.mjs +++ b/bin/validate_schema.mjs @@ -47,10 +47,9 @@ function validateFile(file) { } function validateFolder(folder) { - fs.readdirSync(folder).forEach((file) => { + fs.readdirSync(folder, { recursive: true }).forEach((file) => { const fullPath = path.join(folder, file); if (fs.statSync(fullPath).isDirectory()) { - validateFolder(fullPath); return; }