Skip to content

Commit

Permalink
fix: improve version matching #43
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisparnin committed Jan 10, 2019
1 parent b504cba commit 10e9a8f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/harness/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class LocalConnector {
child_process.exec(cmd, (error, stdout, stderr) => {
if (error || stderr) {
// console.error(`=> ${error}, ${stderr}`);
reject(stderr);
resolve(stderr);
} else {
resolve(stdout);
}
Expand Down
37 changes: 23 additions & 14 deletions lib/inspect/checks/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,46 @@ class VersionCheck extends Check {
status: false,
};
}
// output currently has extra newline
// output = output.split('\n')[0];

let result = this.tryMatch(expectedRange, output, /[0-9]+\.[0-9]+\.[0-9]+/);
if( !result.status )
{
// If we fail to match, try with less restrictive number set (e.g. ViM 8.0):
result = this.tryMatch(expectedRange, output, /[0-9]+\.[0-9]+/)
}

let results = {
cmd,
actual: result.matchedVersion,
expected: expectedRange,
status: result.status,
};
return results;
}

tryMatch(expectedRange, output, matchRegex)
{
let status = false;
for (let o of output.split(/(\s+)/g)) {
for (let o of output.split(/[^-_.a-zA-Z0-9]+/g)) {
// _ is used in several version ids, but is not semver friendly.
// " is in output of some version output
o = o.replace(/_/g, '-').replace(/"/g, '');
// console.log(o)
// Only check tokens with numbers
if (o.match(/[0-9]+\.[0-9]+\.[0-9]+/)) {
if (o.match(matchRegex)) {
// console.log(o) // <- For debugging, helpful for seeing what partial tokens are being matched.
// Some versions, such as google chrome, have four version units. This will ignore anything after 3rd version unit.
o = semver.coerce(o);

if (semver.valid(o)
&& (semver.satisfies(o, expectedRange) || semver.gtr(o, expectedRange))) {
output = o;
status = true;
// Use first valid version found
break;
return {matchedVersion: o, status: true};
}
}
}

let results = {
cmd,
actual: output,
expected: expectedRange,
status,
};
return results;
return false;
}

async report(results) {
Expand Down
16 changes: 16 additions & 0 deletions test/opunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- group:
description: "Editor support"
checks:
- version:
comment: vim is useful for quick edits in the terminal
cmd: vim --version
range: ^8.0.x
- version:
cmd: mysql --version
range: ^5.7.x
- version:
cmd: node --version
range: ^10.15.x
- version:
cmd: java -version
range: ^1.7.x

0 comments on commit 10e9a8f

Please sign in to comment.