Skip to content

Commit

Permalink
include input in summary message
Browse files Browse the repository at this point in the history
Signed-off-by: Itay Shakury <[email protected]>
  • Loading branch information
itaysk committed May 29, 2020
1 parent 1033fee commit 8e265f3
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,28 @@ function setFileCoverage(result: any) {
});
}

function setEvalOutput(provider: JSONProvider, uri: vscode.Uri, error: string, result: any) {
function setEvalOutput(provider: JSONProvider, uri: vscode.Uri, error: string, result: any, inputPath: string) {
if (error !== '') {
opaOutputShowError(error);
} else {
opaOutputHide();
let inputMessage: string
if (inputPath === '') {
inputMessage = 'no input file'
} else {
inputMessage = inputPath.replace('file://','');
inputMessage = vscode.workspace.asRelativePath(inputMessage);
}
if (result.result === undefined) {
provider.set(outputUri, `// No results found. Took ${getPrettyTime(result.metrics.timer_rego_query_eval_ns)}.`, undefined);
provider.set(outputUri, `// No results found. Took ${getPrettyTime(result.metrics.timer_rego_query_eval_ns)}. Used ${inputMessage} as input.`, undefined);
} else {
let output: any;
if (result.result[0].bindings === undefined) {
output = result.result.map((x: any) => x.expressions.map((x: any) => x.value));
} else {
output = result.result.map((x: any) => x.bindings);
}
provider.set(uri, `// Found ${result.result.length} result${result.result.length === 1 ? "" : "s"} in ${getPrettyTime(result.metrics.timer_rego_query_eval_ns)}.`, output);
provider.set(uri, `// Found ${result.result.length} result${result.result.length === 1 ? "" : "s"} in ${getPrettyTime(result.metrics.timer_rego_query_eval_ns)} using ${inputMessage} as input.`, output);
}
}
}
Expand Down Expand Up @@ -253,19 +260,12 @@ function activateEvalPackage(context: vscode.ExtensionContext) {
let inputPath = getInputPath();
if (existsSync(inputPath)) {
args.push('--input', inputPath);
} else {
inputPath = '';
}

opa.run('opa', args, 'data.' + pkg, (error: string, result: any) => {
if (error !== '') {
opaOutputShowError(error);
} else {
opaOutputHide();
if (result.result === undefined) {
provider.set(outputUri, `// No results found. Took ${getPrettyTime(result.metrics.timer_rego_query_eval_ns)}.`, undefined);
} else {
provider.set(outputUri, `// Evaluated package in ${getPrettyTime(result.metrics.timer_rego_query_eval_ns)}.`, result.result[0].expressions[0].value);
}
}
setEvalOutput(provider, outputUri, error, result, inputPath);
});
}, (error: string) => {
opaOutputShowError(error);
Expand Down Expand Up @@ -293,6 +293,8 @@ function activateEvalSelection(context: vscode.ExtensionContext) {
let inputPath = getInputPath();
if (existsSync(inputPath)) {
args.push('--input', inputPath);
} else {
inputPath = '';
}

imports.forEach((x: string) => {
Expand All @@ -302,7 +304,7 @@ function activateEvalSelection(context: vscode.ExtensionContext) {
let text = editor.document.getText(editor.selection);

opa.run('opa', args, text, (error: string, result: any) => {
setEvalOutput(provider, outputUri, error, result);
setEvalOutput(provider, outputUri, error, result, inputPath);
});
}, (error: string) => {
opaOutputShowError(error);
Expand Down Expand Up @@ -342,6 +344,8 @@ function activateEvalCoverage(context: vscode.ExtensionContext) {
let inputPath = getInputPath();
if (existsSync(inputPath)) {
args.push('--input', inputPath);
} else {
inputPath = '';
}

imports.forEach((x: string) => {
Expand All @@ -351,7 +355,7 @@ function activateEvalCoverage(context: vscode.ExtensionContext) {
let text = editor.document.getText(editor.selection);

opa.run('opa', args, text, (error: string, result: any) => {
setEvalOutput(provider, outputUri, error, result);
setEvalOutput(provider, outputUri, error, result, inputPath);
setFileCoverage(result.coverage);
showCoverageForWindow();
});
Expand Down

0 comments on commit 8e265f3

Please sign in to comment.