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

Update validation.js (and comments) #726

Merged
merged 8 commits into from
Jan 9, 2024
Merged
Changes from 7 commits
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
78 changes: 51 additions & 27 deletions bin/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const yargs = require('yargs');
const fs = require('fs');
const yaml = require('js-yaml');
const sizeOf = require('image-size');
const { exec } = require('child_process');
const { spawn } = require('child_process');
const isEqual = require('lodash.isequal');
const readChunk = require('read-chunk');
const imageType = require('image-type');
Expand Down Expand Up @@ -68,9 +68,8 @@ async function requireDimensions(path) {
});
}

function validatePayloadCodecs(vendorId, payloadEncoding) {
var runs = [];
var promises = [];
async function validatePayloadCodecs(vendorId, payloadEncoding) {
const runs = [];

[
{ def: payloadEncoding.uplinkDecoder, routine: 'decodeUplink' },
Expand All @@ -80,7 +79,6 @@ function validatePayloadCodecs(vendorId, payloadEncoding) {
if (d.def) {
const { routine } = d;
const fileName = `${vendorId}/${d.def.fileName}`;
promises.push(requireFile(fileName));
if (d.def.examples) {
d.def.examples.forEach((e) => {
const { input, output, description, normalizedOutput } = e;
Expand All @@ -99,8 +97,6 @@ function validatePayloadCodecs(vendorId, payloadEncoding) {
input: output,
output: normalizedOutput,
transformOutput: (output) => {
// The normalizer can return an object or an array of objects.
// If it's an object, convert it to an array with a single item.
if (output.data && !Array.isArray(output.data)) {
output.data = [output.data];
}
Expand All @@ -113,16 +109,38 @@ function validatePayloadCodecs(vendorId, payloadEncoding) {
}
});

runs.forEach((r) => {
promises.push(
new Promise((resolve, reject) => {
exec(
`bin/runscript -codec-path "${r.fileName}" -routine ${r.routine} -input '${JSON.stringify(r.input)}'`,
(err, stdout, stderr) => {
if (err) {
reject(err);
} else if (stderr) {
reject(stderr);
for (let index = 0; index < runs.length; index++) {
const r = runs[index];
await new Promise((resolve, reject) => {
setTimeout(async () => {
Copy link
Member

Choose a reason for hiding this comment

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

You can remove this

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've made a change that should hopefully be what your intention is.

try {
const childProcess = spawn('bin/runscript', [
'-codec-path',
r.fileName,
'-routine',
r.routine,
'-input',
JSON.stringify(r.input),
]);

let stdout = '';
let stderr = '';

childProcess.stdout.on('data', (data) => {
stdout += data.toString();
});

childProcess.stderr.on('data', (data) => {
stderr += data.toString();
});

childProcess.on('error', (error) => {
reject(error);
});

childProcess.on('close', (code) => {
if (code !== 0) {
reject(`Process exited with code ${code}\n${stderr}`);
} else {
const expected = r.output;
let actual = JSON.parse(stdout);
Expand All @@ -140,12 +158,13 @@ function validatePayloadCodecs(vendorId, payloadEncoding) {
);
}
}
}
);
})
);
});
return Promise.all(promises);
});
} catch (error) {
reject(error);
}
});
});
}
}

function validateImageExtension(filename) {
Expand All @@ -167,11 +186,16 @@ function validateImageExtension(filename) {
function requireImageDecode(fileName) {
// Test https://golang.org/pkg/image/png/#Decode and https://golang.org/pkg/image/jpeg/#Decode are possible
return new Promise((resolve, reject) => {
exec(`bin/validate-image ${fileName}`, (err) => {
if (err) {
reject(err);
const childProcess = spawn('bin/validate-image', [fileName]);
childProcess.on('error', (error) => {
reject(error);
});
childProcess.on('close', (code) => {
if (code !== 0) {
reject(`Process exited with code ${code}`);
} else {
resolve();
}
resolve();
});
});
}
Expand Down