Skip to content

Commit f7a5b55

Browse files
committed
trunk fmt
1 parent 8dcc6d8 commit f7a5b55

File tree

7 files changed

+248
-109
lines changed

7 files changed

+248
-109
lines changed

.vscode/launch.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
"runtimeExecutable": "npx",
99
"runtimeArgs": [
1010
"ts-node",
11-
"./src/index.ts",
11+
"./src/index.ts"
1212
// "--transpile-only",
13-
// if you use esm
1413
// "--esm"
1514
],
1615
// "program": "${file}",
@@ -19,11 +18,9 @@
1918
"internalConsoleOptions": "openOnSessionStart",
2019
"skipFiles": ["<node_internals>/**", "node_modules/**"],
2120
"env": {
22-
"path": "./examples", // Replace with your actual path
23-
"test_file_postfix": "_test", // Replace with your actual postfix,
21+
"path": "./examples",
22+
"test_file_postfix": "_test",
2423
"test_mode": "directory"
25-
// You can add more environment variables here
26-
// "ANOTHER_VAR": "another_value"
2724
}
2825
}
2926
]

__tests__/index.test.ts

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
import { ProcessedTestResult, ProcessedCoverageResult } from "../src/interfaces";
2-
import { mockProcessedTestResults, mockProcessedCoverageResults } from "./mockResults";
3-
import { processTestResults, processCoverageReport } from "../src/testResultProcessing";
1+
import {
2+
ProcessedTestResult,
3+
ProcessedCoverageResult,
4+
} from "../src/interfaces";
5+
import {
6+
mockProcessedTestResults,
7+
mockProcessedCoverageResults,
8+
} from "./mockResults";
9+
import {
10+
processTestResults,
11+
processCoverageReport,
12+
} from "../src/testResultProcessing";
413
import { formatResults } from "../src/formatResults";
514

615
import * as path from "path";
716
import * as fs from "fs";
817

918
// Obtained by running `opa test ./spacelift_policies --format=json --v0-compatible`
1019
export const testOutput = fs.readFileSync(
11-
path.join(__dirname, "sample_test_output.txt"),
12-
"utf8",
13-
);
20+
path.join(__dirname, "sample_test_output.txt"),
21+
"utf8",
22+
);
1423

1524
export const coverageOutput = fs.readFileSync(
1625
path.join(__dirname, "sample_coverage_output.txt"),
@@ -61,53 +70,66 @@ describe("processTestResults", () => {
6170
});
6271
});
6372

64-
6573
describe("processCoverageReport", () => {
66-
const parsedCoverageResults = processCoverageReport(JSON.parse(coverageOutput));
74+
const parsedCoverageResults = processCoverageReport(
75+
JSON.parse(coverageOutput),
76+
);
6777

6878
it("should correctly parse coverage output - singular not covered lines", () => {
69-
const targetFile = 'cancel-in-progress-runs.rego';
70-
const result = parsedCoverageResults.find(item => item.file === targetFile);
79+
const targetFile = "cancel-in-progress-runs.rego";
80+
const result = parsedCoverageResults.find(
81+
(item) => item.file === targetFile,
82+
);
7183

7284
expect(result).toBeDefined();
7385
expect(result!.coverage).toBeCloseTo(83.33);
74-
expect(result!.notCoveredLines).toBe('16');
86+
expect(result!.notCoveredLines).toBe("16");
7587
});
7688

7789
it("should correctly parse coverage output - multiple, hyphenated not covered lines", () => {
78-
const targetFile = 'enforce-module-use-policy.rego';
79-
const result = parsedCoverageResults.find(item => item.file === targetFile);
90+
const targetFile = "enforce-module-use-policy.rego";
91+
const result = parsedCoverageResults.find(
92+
(item) => item.file === targetFile,
93+
);
8094

8195
expect(result).toBeDefined();
8296
expect(result!.coverage).toBeCloseTo(47.826);
83-
expect(result!.notCoveredLines).toBe('37, 42, 46, 52, 54, 57, 60-61, 64, 68, 78, 80');
97+
expect(result!.notCoveredLines).toBe(
98+
"37, 42, 46, 52, 54, 57, 60-61, 64, 68, 78, 80",
99+
);
84100
});
85101

86102
it("should correctly parse coverage output - multiple, comma-separated not covered lines", () => {
87-
const targetFile = 'readers-writers-admins-teams.rego';
88-
const result = parsedCoverageResults.find(item => item.file === targetFile);
103+
const targetFile = "readers-writers-admins-teams.rego";
104+
const result = parsedCoverageResults.find(
105+
(item) => item.file === targetFile,
106+
);
89107

90108
expect(result).toBeDefined();
91109
expect(result!.coverage).toBeCloseTo(83.33);
92-
expect(result!.notCoveredLines).toBe('16, 24, 28');
110+
expect(result!.notCoveredLines).toBe("16, 24, 28");
93111
});
94112

95113
it("should correctly parse coverage output - undefined coverage", () => {
96-
const targetFile = 'drift-detection.rego';
97-
const result = parsedCoverageResults.find(item => item.file === targetFile);
114+
const targetFile = "drift-detection.rego";
115+
const result = parsedCoverageResults.find(
116+
(item) => item.file === targetFile,
117+
);
98118

99119
expect(result).toBeDefined();
100120
expect(result!.coverage).toBeUndefined();
101-
expect(result!.notCoveredLines).toBe('3, 5, 8, 11');
121+
expect(result!.notCoveredLines).toBe("3, 5, 8, 11");
102122
});
103123

104124
it("should correctly parse coverage output - 100% coverage with empty not covered lines", () => {
105-
const targetFile = 'tests/cancel-in-progress-runs_test.rego';
106-
const result = parsedCoverageResults.find(item => item.file === targetFile);
125+
const targetFile = "tests/cancel-in-progress-runs_test.rego";
126+
const result = parsedCoverageResults.find(
127+
(item) => item.file === targetFile,
128+
);
107129

108130
expect(result).toBeDefined();
109131
expect(result!.coverage).toBe(100);
110-
expect(result!.notCoveredLines).toBe('');
132+
expect(result!.notCoveredLines).toBe("");
111133
});
112134
});
113135

@@ -126,7 +148,7 @@ describe("formatResults", () => {
126148
},
127149
];
128150
const specificCoverageResult = parsedCoverageResults.filter(
129-
(res) => res.file === "tests/ignore-changes-outside-root.rego"
151+
(res) => res.file === "tests/ignore-changes-outside-root.rego",
130152
);
131153

132154
const result = formatResults(testResults, specificCoverageResult, true);
@@ -201,7 +223,9 @@ describe("formatResults", () => {
201223
const resultRows = result
202224
.split("\n")
203225
.filter((line) => line.startsWith("|") && line.includes("PASS"));
204-
expect(resultRows.length).toBe(parsedTestResults.filter(r => r.status === "PASS").length);
226+
expect(resultRows.length).toBe(
227+
parsedTestResults.filter((r) => r.status === "PASS").length,
228+
);
205229
});
206230

207231
it("should format results without coverage when showCoverage is false", () => {
@@ -244,7 +268,7 @@ describe("formatResults", () => {
244268
},
245269
];
246270
const specificCoverageResult = parsedCoverageResults.filter(
247-
(res) => res.file === "tests/ignore-changes-outside-root.rego"
271+
(res) => res.file === "tests/ignore-changes-outside-root.rego",
248272
);
249273
const result = formatResults(testResults, specificCoverageResult, true);
250274
expect(result).toContain(

__tests__/mockResults.ts

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,94 @@
1-
import { ProcessedTestResult, ProcessedCoverageResult } from "../src/interfaces";
1+
import {
2+
ProcessedTestResult,
3+
ProcessedCoverageResult,
4+
} from "../src/interfaces";
25

36
export const mockProcessedTestResults: ProcessedTestResult[] = [
47
{
58
file: "tests/ignore-changes-outside-root_test.rego",
69
status: "PASS",
710
passed: 12,
811
total: 12,
9-
details: ["✅ test1", "✅ test2", "✅ test3", "✅ test4", "✅ test5", "✅ test6", "✅ test7", "✅ test8", "✅ test9", "✅ test10", "✅ test11", "✅ test12"],
12+
details: [
13+
"✅ test1",
14+
"✅ test2",
15+
"✅ test3",
16+
"✅ test4",
17+
"✅ test5",
18+
"✅ test6",
19+
"✅ test7",
20+
"✅ test8",
21+
"✅ test9",
22+
"✅ test10",
23+
"✅ test11",
24+
"✅ test12",
25+
],
1026
},
1127
{
1228
file: "tests/track-using-labels_test.rego",
1329
status: "PASS",
1430
passed: 8,
1531
total: 8,
16-
details: ["✅ test_label_match", "✅ test_label_no_match", "✅ test_no_labels_on_resource", "✅ test_no_labels_on_constraint", "✅ test_empty_labels_on_resource", "✅ test_empty_labels_on_constraint", "✅ test_multiple_labels_match", "✅ test_multiple_labels_no_match"],
32+
details: [
33+
"✅ test_label_match",
34+
"✅ test_label_no_match",
35+
"✅ test_no_labels_on_resource",
36+
"✅ test_no_labels_on_constraint",
37+
"✅ test_empty_labels_on_resource",
38+
"✅ test_empty_labels_on_constraint",
39+
"✅ test_multiple_labels_match",
40+
"✅ test_multiple_labels_no_match",
41+
],
1742
},
1843
{
1944
file: "tests/enforce-password-length_test.rego",
2045
status: "PASS",
2146
passed: 3,
2247
total: 3,
23-
details: ["✅ test_password_too_short", "✅ test_password_just_right", "✅ test_password_too_long"],
48+
details: [
49+
"✅ test_password_too_short",
50+
"✅ test_password_just_right",
51+
"✅ test_password_too_long",
52+
],
2453
},
2554
{
2655
file: "tests/notification-stack-failure-origins_test.rego",
2756
status: "PASS",
2857
passed: 5,
2958
total: 5,
30-
details: ["✅ test_stack_failure_origin_ami", "✅ test_stack_failure_origin_instance", "✅ test_stack_failure_origin_security_group", "✅ test_stack_failure_origin_vpc", "✅ test_stack_failure_origin_unknown"],
59+
details: [
60+
"✅ test_stack_failure_origin_ami",
61+
"✅ test_stack_failure_origin_instance",
62+
"✅ test_stack_failure_origin_security_group",
63+
"✅ test_stack_failure_origin_vpc",
64+
"✅ test_stack_failure_origin_unknown",
65+
],
3166
},
3267
{
3368
file: "tests/enforce-module-use-policy_test.rego",
3469
status: "PASS",
3570
passed: 4,
3671
total: 4,
37-
details: ["✅ test_valid_module_use", "✅ test_invalid_module_use", "✅ test_no_module_imports", "✅ test_mixed_module_use"],
72+
details: [
73+
"✅ test_valid_module_use",
74+
"✅ test_invalid_module_use",
75+
"✅ test_no_module_imports",
76+
"✅ test_mixed_module_use",
77+
],
3878
},
3979
{
4080
file: "tests/readers-writers-admins-teams_test.rego",
4181
status: "PASS",
4282
passed: 6,
4383
total: 6,
44-
details: ["✅ test_reader_access", "✅ test_writer_access", "✅ test_admin_access", "✅ test_no_access", "✅ test_multiple_roles", "✅ test_nested_teams"],
84+
details: [
85+
"✅ test_reader_access",
86+
"✅ test_writer_access",
87+
"✅ test_admin_access",
88+
"✅ test_no_access",
89+
"✅ test_multiple_roles",
90+
"✅ test_nested_teams",
91+
],
4592
},
4693
{
4794
file: "tests/cancel-in-progress-runs_test.rego",
@@ -86,17 +133,17 @@ export const mockProcessedCoverageResults: ProcessedCoverageResult[] = [
86133
},
87134
{
88135
file: "tests/enforce-password-length.rego",
89-
coverage: 100.00,
136+
coverage: 100.0,
90137
notCoveredLines: "",
91138
},
92139
{
93140
file: "tests/notification-stack-failure-origins.rego",
94-
coverage: 90.00,
141+
coverage: 90.0,
95142
notCoveredLines: "10, 15",
96143
},
97144
{
98145
file: "tests/enforce-module-use-policy.rego",
99-
coverage: 85.00,
146+
coverage: 85.0,
100147
notCoveredLines: "5",
101148
},
102149
{
@@ -111,7 +158,7 @@ export const mockProcessedCoverageResults: ProcessedCoverageResult[] = [
111158
},
112159
{
113160
file: "tests/do-not-delete-stateful-resources.rego",
114-
coverage: 100.00,
161+
coverage: 100.0,
115162
notCoveredLines: "",
116163
},
117164
];

action.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ runs:
6767
version: ${{ inputs.opa_version }}
6868
static: ${{ inputs.opa_static }}
6969

70-
7170
- name: Find Rego files without tests
7271
if: inputs.report_untested_files == 'true'
7372
id: find-no-test

src/index.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { processTestResults, processCoverageReport } from "./testResultProcessing";
2-
import { executeIndividualOpaTests, executeOpaTestByDirectory } from "./opaCommands";
1+
import {
2+
processTestResults,
3+
processCoverageReport,
4+
} from "./testResultProcessing";
5+
import {
6+
executeIndividualOpaTests,
7+
executeOpaTestByDirectory,
8+
} from "./opaCommands";
39
import { formatResults } from "./formatResults";
410

5-
import { ProcessedTestResult, ProcessedCoverageResult } from "./interfaces"
11+
import { ProcessedTestResult, ProcessedCoverageResult } from "./interfaces";
612

713
import * as core from "@actions/core";
814

@@ -12,14 +18,17 @@ const errorString =
1218
export async function main() {
1319
try {
1420
const test_mode = process.env.test_mode || "directory"; // remove default, and others too
15-
const reportNoTestFiles = process.env.report_untested_files === "true" || false;
21+
const reportNoTestFiles =
22+
process.env.report_untested_files === "true" || false;
1623
const noTestFiles = process.env.no_test_files;
1724
const runCoverageReport = process.env.run_coverage_report === "true";
1825
const path = process.env.path || "./examples";
1926
const test_file_postfix = process.env.test_file_postfix || "_test";
2027

2128
if (!path || !test_file_postfix) {
22-
throw new Error("Both 'path' and 'test_file_postfix' environment variables must be set.");
29+
throw new Error(
30+
"Both 'path' and 'test_file_postfix' environment variables must be set.",
31+
);
2332
}
2433

2534
let opaOutput: string = "";
@@ -28,9 +37,19 @@ export async function main() {
2837
let coverageOutput: string | undefined;
2938

3039
if (test_mode === "directory") {
31-
({ output: opaOutput, error: opaError, exitCode: exitCode, coverageOutput: coverageOutput } = await executeOpaTestByDirectory(path, true));
40+
({
41+
output: opaOutput,
42+
error: opaError,
43+
exitCode: exitCode,
44+
coverageOutput: coverageOutput,
45+
} = await executeOpaTestByDirectory(path, true));
3246
} else {
33-
({ output: opaOutput, error: opaError, exitCode: exitCode, coverageOutput: coverageOutput } = await executeIndividualOpaTests(path, test_file_postfix, true));
47+
({
48+
output: opaOutput,
49+
error: opaError,
50+
exitCode: exitCode,
51+
coverageOutput: coverageOutput,
52+
} = await executeIndividualOpaTests(path, test_file_postfix, true));
3453
}
3554

3655
let parsedResults = processTestResults(JSON.parse(opaOutput));

0 commit comments

Comments
 (0)