Skip to content

Commit

Permalink
feat: adding jq queries in contains check
Browse files Browse the repository at this point in the history
  • Loading branch information
ssmirr committed Feb 12, 2020
1 parent 736a27b commit 9040c69
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions lib/inspect/checks/contains.js
Expand Up @@ -4,22 +4,44 @@ const Check = require('./check');

class ContainsCheck extends Check {
async check(context, args) {
let expect = args.expect !== 'undefined' ? args.expect : true;
return this.getContainsStatus(context, args.file, args.string, expect);
let expect = args.expect != undefined ? args.expect : true;
return this.getContainsStatus(context, args.file, args.string, args.jq, expect);
}

async getContainsStatus(context, file, string, expect) {
async getContainsStatus(context, file, string, jq, expect) {

let message = 'NA';
let status;
try {
status = await this.connector.contains(context, file, string, expect);
} catch (error) {
message = error;
status = false;

// if given a jq query
if (jq) {
// checking if jq is in $PATH
let hasjq = (await this.connector.exec(`which jq`)).exitCode == 0;

if (hasjq) {
let queryResult = (await this.connector.exec(`jq -r "${jq}" ${file}`)).stdout;
status = (queryResult === string) === expect;
}

else {
status = false;
message = 'error: jq is not installed on the target machine.'
}
}

// if just matching string in the file
else {
try {
status = await this.connector.contains(context, file, string, expect);
} catch (error) {
message = error;
status = false;
}
}

return {
file: `...${path.basename(file)}`,
jq,
string,
message,
status,
Expand All @@ -33,7 +55,7 @@ class ContainsCheck extends Check {

// Escape carriage returns and line breaks.
let results_string = results.string.replace('\r', '\\r').replace('\n', '\\n');
let message = chalk`{gray [${results.file}]} {white ${expected} contain${plural}} {gray [${results_string}]} {white status:} {gray ${results.status}} {white message:} {gray ${results.message}}`;
let message = chalk`{gray [${results.file}]} {white jq query:} {gray ${results.jq}} {white ${expected} contain${plural}:} {gray [${results_string}]} {white status:} {gray ${results.status}} {white message:} {gray ${results.message}}`;
this.reporter.report(message, results.status);
}
}
Expand Down

0 comments on commit 9040c69

Please sign in to comment.