Skip to content

Commit 6c3b1f4

Browse files
committed
test 3.0.1 for gh commenter actions
1 parent bb69c88 commit 6c3b1f4

File tree

5 files changed

+81
-54
lines changed

5 files changed

+81
-54
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ It's super easy to get started and use this GitHub Action to test your OPA Rego
3131
- name: Run OPA Rego Tests
3232
uses: masterpointio/github-action-opa-rego-test@main
3333
with:
34+
path: ./examples
3435
report_untested_files: true # Flag to check & report Rego files that does NOT have corresponding test files. Optional, defaults to false.
3536
```
3637
@@ -61,7 +62,7 @@ jobs:
6162
- name: Run OPA Rego Tests
6263
uses: masterpointio/github-action-opa-rego-test@main
6364
with:
64-
path: "./config/spacelift-policies" # Path of the directory where the OPA Rego policies are stored. Optional, defaults to `.` which is the root directory.
65+
path: "./config/spacelift-policies" # Path of the directory where the OPA Rego policies are stored.
6566
report_untested_files: true # Flag to check & report Rego files without corresponding test files. Optional, defaults to false.
6667
```
6768
@@ -79,7 +80,7 @@ In the example below, all `_test.rego` files' location are valid and will be exe
7980

8081
| Input | Description | Required | Default |
8182
| ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | --------------------------------- |
82-
| `path` | Path to the directory containing OPA Rego files to test | No | `.` (root directory) |
83+
| `path` | Path to the directory containing OPA Rego files to test | Yes | REQUIRED |
8384
| `test_mode` | Whether to test the Rego by an entire directory (including entire package, e.g. `opa test ./`) or by individual files (e.g. `opa test a_test.rego a.rego`). Options of `directory` or `file`. Default is `directory`. | No | `test` |
8485
| `test_file_postfix` | Postfix of the test files to run (e.g. notification.rego <> notification_test.rego) | No | `_test` |
8586
| `write_pr_comment` | Flag to write a user-friendly PR comment with test results | No | `true` |

action.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ branding:
88

99
inputs:
1010
path:
11-
description: "Path to the directory containing OPA Rego files to test. Default to root directory."
12-
required: false
13-
default: "."
11+
description: "Path to the directory containing OPA Rego files to test."
12+
required: true
1413
test_mode:
1514
description: Whether to test the Rego by an entire directory (including entire package, e.g. `opa test ./`) or by individual files (e.g. `opa test a_test.rego a.rego`). Options of `directory` or `file`. Default is `directory`.
1615
required: false

dist/index.js

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26285,12 +26285,12 @@ const errorString = "⛔️⛔️ An unknown error has occurred in generating th
2628526285
function main() {
2628626286
return __awaiter(this, void 0, void 0, function* () {
2628726287
try {
26288-
const test_mode = process.env.test_mode || "directory"; // remove default, and others too
26289-
const reportNoTestFiles = process.env.report_untested_files === "true" || false;
26288+
const test_mode = process.env.test_mode;
26289+
const reportNoTestFiles = process.env.report_untested_files === "true";
2629026290
const noTestFiles = process.env.no_test_files;
2629126291
const runCoverageReport = process.env.run_coverage_report === "true";
26292-
const path = process.env.path || "./examples";
26293-
const test_file_postfix = process.env.test_file_postfix || "_test";
26292+
const path = process.env.path;
26293+
const test_file_postfix = process.env.test_file_postfix;
2629426294
if (!path || !test_file_postfix) {
2629526295
throw new Error("Both 'path' and 'test_file_postfix' environment variables must be set.");
2629626296
}
@@ -26299,10 +26299,20 @@ function main() {
2629926299
let exitCode = 0;
2630026300
let coverageOutput;
2630126301
if (test_mode === "directory") {
26302-
({ output: opaOutput, error: opaError, exitCode: exitCode, coverageOutput: coverageOutput } = yield (0, opaCommands_1.executeOpaTestByDirectory)(path, true));
26302+
({
26303+
output: opaOutput,
26304+
error: opaError,
26305+
exitCode: exitCode,
26306+
coverageOutput: coverageOutput,
26307+
} = yield (0, opaCommands_1.executeOpaTestByDirectory)(path, true));
2630326308
}
2630426309
else {
26305-
({ output: opaOutput, error: opaError, exitCode: exitCode, coverageOutput: coverageOutput } = yield (0, opaCommands_1.executeIndividualOpaTests)(path, test_file_postfix, true));
26310+
({
26311+
output: opaOutput,
26312+
error: opaError,
26313+
exitCode: exitCode,
26314+
coverageOutput: coverageOutput,
26315+
} = yield (0, opaCommands_1.executeIndividualOpaTests)(path, test_file_postfix, true));
2630626316
}
2630726317
let parsedResults = (0, testResultProcessing_1.processTestResults)(JSON.parse(opaOutput));
2630826318
let coverageResults = [];
@@ -26328,6 +26338,7 @@ function main() {
2632826338
if (formattedOutput === "") {
2632926339
formattedOutput = errorString;
2633026340
}
26341+
// This is the output that will be used in the GitHub Pull Request comment.
2633126342
core.setOutput("parsed_results", formattedOutput);
2633226343
const testsFailed = parsedResults.some((result) => result.status === "FAIL");
2633326344
core.setOutput("tests_failed", testsFailed.toString());
@@ -26398,9 +26409,9 @@ const path_1 = __importDefault(__nccwpck_require__(1017));
2639826409
const opaV0CompatibleFlag = "--v0-compatible"; // https://www.openpolicyagent.org/docs/latest/v0-compatibility/
2639926410
function executeOpaTestByDirectory(path_2) {
2640026411
return __awaiter(this, arguments, void 0, function* (path, runCoverageReport = false) {
26401-
let opaOutput = '';
26402-
let opaError = '';
26403-
let opaCoverageOutput = '';
26412+
let opaOutput = "";
26413+
let opaError = "";
26414+
let opaCoverageOutput = "";
2640426415
let exitCode = 0;
2640526416
let coverageExitCode;
2640626417
const options = {
@@ -26410,11 +26421,11 @@ function executeOpaTestByDirectory(path_2) {
2641026421
},
2641126422
stderr: (data) => {
2641226423
opaError += data.toString();
26413-
}
26424+
},
2641426425
},
26415-
ignoreReturnCode: true
26426+
ignoreReturnCode: true,
2641626427
};
26417-
exitCode = yield exec.exec('opa', ['test', path, '--format=json', opaV0CompatibleFlag], options);
26428+
exitCode = yield exec.exec("opa", ["test", path, "--format=json", opaV0CompatibleFlag], options);
2641826429
if (runCoverageReport) {
2641926430
const coverageOptions = {
2642026431
listeners: {
@@ -26423,19 +26434,19 @@ function executeOpaTestByDirectory(path_2) {
2642326434
},
2642426435
stderr: (data) => {
2642526436
opaError += `\nCoverage: ${data.toString()}`;
26426-
}
26437+
},
2642726438
},
26428-
ignoreReturnCode: true
26439+
ignoreReturnCode: true,
2642926440
};
26430-
coverageExitCode = yield exec.exec('opa', ['test', path, '--format=json', '--coverage', opaV0CompatibleFlag], coverageOptions);
26441+
coverageExitCode = yield exec.exec("opa", ["test", path, "--format=json", "--coverage", opaV0CompatibleFlag], coverageOptions);
2643126442
}
2643226443
else {
2643326444
console.log("Coverage reporting skipped due to runCoverageReport flag set to false");
2643426445
}
2643526446
console.log("OPA test commands completed");
2643626447
return Object.assign({ output: opaOutput, error: opaError, exitCode: exitCode }, (runCoverageReport && {
2643726448
coverageOutput: opaCoverageOutput,
26438-
coverageExitCode: coverageExitCode
26449+
coverageExitCode: coverageExitCode,
2643926450
}));
2644026451
});
2644126452
}
@@ -26449,49 +26460,58 @@ function executeOpaTestByDirectory(path_2) {
2644926460
function executeIndividualOpaTests(basePath_1, testFilePostfix_1) {
2645026461
return __awaiter(this, arguments, void 0, function* (basePath, testFilePostfix, runCoverageReport = false) {
2645126462
const allTestResults = [];
26452-
let opaError = '';
26463+
let opaError = "";
2645326464
let exitCode = 0;
2645426465
const coverageFiles = {};
2645526466
let coverageExitCode = 0;
2645626467
// ---------- locate test files ----------
26457-
let findStdout = '';
26458-
let findStderr = '';
26459-
yield exec.exec('find', [basePath, '-type', 'f', '-name', `*${testFilePostfix}.rego`], {
26468+
let findStdout = "";
26469+
let findStderr = "";
26470+
yield exec.exec("find", [basePath, "-type", "f", "-name", `*${testFilePostfix}.rego`], {
2646026471
listeners: {
2646126472
stdout: (b) => (findStdout += b.toString()),
26462-
stderr: (b) => (findStderr += b.toString())
26463-
}
26473+
stderr: (b) => (findStderr += b.toString()),
26474+
},
2646426475
});
2646526476
if (findStderr) {
26466-
opaError += findStderr + '\n';
26477+
opaError += findStderr + "\n";
2646726478
exitCode = 1;
2646826479
}
26469-
const testFiles = findStdout.trim().split('\n').filter(Boolean);
26480+
const testFiles = findStdout.trim().split("\n").filter(Boolean);
2647026481
for (const testFile of testFiles) {
2647126482
const base = path_1.default.basename(testFile, `${testFilePostfix}.rego`);
2647226483
const dir = path_1.default.dirname(testFile);
2647326484
// locate impl file
26474-
let implOut = '';
26475-
yield exec.exec('find', [dir, `${dir}/..`, '-maxdepth', '1', '-type', 'f', '-name', `${base}.rego`], {
26476-
listeners: { stdout: (b) => (implOut += b.toString()) }
26485+
let implOut = "";
26486+
yield exec.exec("find", [
26487+
dir,
26488+
`${dir}/..`,
26489+
"-maxdepth",
26490+
"1",
26491+
"-type",
26492+
"f",
26493+
"-name",
26494+
`${base}.rego`,
26495+
], {
26496+
listeners: { stdout: (b) => (implOut += b.toString()) },
2647726497
});
26478-
const implFile = implOut.trim().split('\n').find(Boolean);
26498+
const implFile = implOut.trim().split("\n").find(Boolean);
2647926499
if (!implFile) {
2648026500
const msg = `Error: Implementation file not found for test: ${testFile}`;
26481-
opaError += msg + '\n';
26501+
opaError += msg + "\n";
2648226502
exitCode = 1;
2648326503
coverageExitCode = 1;
2648426504
continue;
2648526505
}
2648626506
// -------- Running OPA test --------
26487-
let testOutput = '';
26488-
let testErrMsg = '';
26489-
const testExitCode = yield exec.exec('opa', ['test', testFile, implFile, '--format=json', opaV0CompatibleFlag], {
26507+
let testOutput = "";
26508+
let testErrMsg = "";
26509+
const testExitCode = yield exec.exec("opa", ["test", testFile, implFile, "--format=json", opaV0CompatibleFlag], {
2649026510
listeners: {
2649126511
stdout: (b) => (testOutput += b.toString()),
26492-
stderr: (b) => (testErrMsg += b.toString())
26512+
stderr: (b) => (testErrMsg += b.toString()),
2649326513
},
26494-
ignoreReturnCode: true
26514+
ignoreReturnCode: true,
2649526515
});
2649626516
if (testExitCode)
2649726517
exitCode = testExitCode;
@@ -26508,14 +26528,21 @@ function executeIndividualOpaTests(basePath_1, testFilePostfix_1) {
2650826528
}
2650926529
// -------- coverage (optional) --------
2651026530
if (runCoverageReport) {
26511-
let covOut = '';
26512-
let covErr = '';
26513-
const covExit = yield exec.exec('opa', ['test', testFile, implFile, '--coverage', '--format=json', opaV0CompatibleFlag], {
26531+
let covOut = "";
26532+
let covErr = "";
26533+
const covExit = yield exec.exec("opa", [
26534+
"test",
26535+
testFile,
26536+
implFile,
26537+
"--coverage",
26538+
"--format=json",
26539+
opaV0CompatibleFlag,
26540+
], {
2651426541
listeners: {
2651526542
stdout: (b) => (covOut += b.toString()),
26516-
stderr: (b) => (covErr += b.toString())
26543+
stderr: (b) => (covErr += b.toString()),
2651726544
},
26518-
ignoreReturnCode: true
26545+
ignoreReturnCode: true,
2651926546
});
2652026547
coverageExitCode = Math.max(coverageExitCode, covExit);
2652126548
if (covErr)
@@ -26559,7 +26586,7 @@ function processTestResults(opaRawJsonTestResult) {
2655926586
// Group by file
2656026587
const fileMap = new Map();
2656126588
// Group tests by file
26562-
opaRawJsonTestResult.forEach(result => {
26589+
opaRawJsonTestResult.forEach((result) => {
2656326590
const file = result.location.file;
2656426591
if (!fileMap.has(file)) {
2656526592
fileMap.set(file, []);
@@ -26574,10 +26601,10 @@ function processTestResults(opaRawJsonTestResult) {
2657426601
status: "PASS",
2657526602
passed: 0,
2657626603
total: tests.length,
26577-
details: []
26604+
details: [],
2657826605
};
2657926606
// Count passed tests and collect details
26580-
tests.forEach(test => {
26607+
tests.forEach((test) => {
2658126608
const passed = !test.fail;
2658226609
if (passed) {
2658326610
result.passed++;
@@ -26607,7 +26634,7 @@ function processCoverageReport(opaRawJsonCoverageReport) {
2660726634
coverageResults.push({
2660826635
file: filePath,
2660926636
coverage: fileData.coverage,
26610-
notCoveredLines: "" // No uncovered lines
26637+
notCoveredLines: "", // No uncovered lines
2661126638
});
2661226639
continue;
2661326640
}
@@ -26628,14 +26655,14 @@ function processCoverageReport(opaRawJsonCoverageReport) {
2662826655
// Sort numerically
2662926656
notCoveredRanges.sort((a, b) => {
2663026657
// Extract the first number from each range for comparison
26631-
const aStart = parseInt(a.split('-')[0]);
26632-
const bStart = parseInt(b.split('-')[0]);
26658+
const aStart = parseInt(a.split("-")[0]);
26659+
const bStart = parseInt(b.split("-")[0]);
2663326660
return aStart - bStart;
2663426661
});
2663526662
coverageResults.push({
2663626663
file: filePath,
2663726664
coverage: fileData.coverage,
26638-
notCoveredLines: notCoveredRanges.join(', ')
26665+
notCoveredLines: notCoveredRanges.join(", "),
2663926666
});
2664026667
}
2664126668
return coverageResults;

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export async function main() {
2121
const reportNoTestFiles = process.env.report_untested_files === "true";
2222
const noTestFiles = process.env.no_test_files;
2323
const runCoverageReport = process.env.run_coverage_report === "true";
24-
const path = process.env.path || "./examples";
25-
const test_file_postfix = process.env.test_file_postfix || "_test";
24+
const path = process.env.path;
25+
const test_file_postfix = process.env.test_file_postfix;
2626

2727
if (!path || !test_file_postfix) {
2828
throw new Error(

0 commit comments

Comments
 (0)