From 9040c699063e402f393293fc208c3834780a4c09 Mon Sep 17 00:00:00 2001 From: Samim Mirhosseini Date: Wed, 12 Feb 2020 18:40:02 -0500 Subject: [PATCH] feat: adding jq queries in contains check --- lib/inspect/checks/contains.js | 40 ++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/inspect/checks/contains.js b/lib/inspect/checks/contains.js index 88fd650..b6333e6 100644 --- a/lib/inspect/checks/contains.js +++ b/lib/inspect/checks/contains.js @@ -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, @@ -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); } }