Skip to content

Commit 5677ea7

Browse files
committed
Enhance exit codes for better clarity
1 parent 3127419 commit 5677ea7

File tree

4 files changed

+82
-13
lines changed

4 files changed

+82
-13
lines changed

cli.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
import * as veritaclis from './src/veritaclis.js'
2-
import * as report from './src/report.js'
2+
import * as report from './src/report/index.js'
3+
4+
// Exit code constants
5+
const EXIT_SUCCESS = 0 // All tests passed
6+
const EXIT_TEST_FAILURE = 1 // One or more tests failed
7+
const EXIT_ERROR = 2 // Internal error or incorrect usage
38

49
if (import.meta.main) {
510
const [path] = Deno.args
611

712
if (!path) {
813
console.error('Please provide a test file or directory path.')
9-
Deno.exit(1)
14+
Deno.exit(EXIT_ERROR)
1015
}
1116

1217
try {
13-
const result = await veritaclis.run(path)
14-
report.viewer(result)
15-
Deno.exit(0)
18+
const simpleReport = await veritaclis.run(path)
19+
20+
const analyzedReport = report.analyze(simpleReport)
21+
22+
report.view(analyzedReport)
23+
24+
Deno.exit(analyzedReport.allPassed ? EXIT_SUCCESS : EXIT_TEST_FAILURE)
1625
} catch (error) {
1726
console.error('Error:', error.message)
18-
Deno.exit(1)
27+
Deno.exit(EXIT_ERROR)
1928
}
2029
}

src/report/analyzer.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export function analyze(simpleReport) {
2+
const results = []
3+
let totalTests = 0
4+
let passedTests = 0
5+
let skippedTests = 0
6+
let failedTests = 0
7+
let errorsCount = 0
8+
9+
for (const test of simpleReport) {
10+
totalTests += 1
11+
12+
const hasFailedPre = Array.isArray(test.pre)
13+
? test.pre.some(p => !p.passed)
14+
: false
15+
16+
const hasFailedPost = Array.isArray(test.post)
17+
? test.post.some(p => !p.passed)
18+
: false
19+
20+
const hasError = Boolean(test.error)
21+
22+
const skipped = hasFailedPre
23+
const passed = !hasError && !skipped && !hasFailedPost
24+
25+
if (hasError) {
26+
errorsCount += 1
27+
}
28+
29+
if (skipped) {
30+
skippedTests += 1
31+
} else if (passed) {
32+
passedTests += 1
33+
} else {
34+
failedTests += 1
35+
}
36+
37+
results.push({
38+
...test,
39+
passed,
40+
skipped,
41+
})
42+
}
43+
44+
const allPassed =
45+
totalTests > 0 &&
46+
passedTests === totalTests &&
47+
errorsCount === 0
48+
49+
return {
50+
results,
51+
totalTests,
52+
passedTests,
53+
skippedTests,
54+
failedTests,
55+
errorsCount,
56+
allPassed,
57+
}
58+
}

src/report/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { analyze } from './analyzer.js'
2+
export { view } from './viewer.js'

src/report.js renamed to src/report/viewer.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { green, gray, red, yellow, cyan, bold } from "https://deno.land/std/fmt/colors.ts"
22

3-
export function viewer(results) {
4-
for (const testResult of results) {
3+
export function view(report) {
4+
for (const testResult of report.results) {
55
console.log(cyan(bold(testResult.path)))
6-
6+
77
if (testResult.description) {
88
console.log(gray(testResult.description))
99
}
@@ -12,7 +12,7 @@ export function viewer(results) {
1212
console.log(` PRE: ${pre.description} ... ${pre.passed ? green("OK") : red("FAIL")}`)
1313
}
1414

15-
if (testResult.pre.some(p => !p.passed)) {
15+
if (testResult.skipped) {
1616
console.log(yellow(" Test skipped due to failed precondition(s)."))
1717
console.log()
1818
continue
@@ -29,7 +29,7 @@ export function viewer(results) {
2929
console.log()
3030
}
3131

32-
const total = results.length
33-
const passed = results.filter(r => r.pre.every(p => p.passed) && r.post.every(p => p.passed) && !r.error).length
34-
console.log(bold(`Summary: ${passed} / ${total} tests passed.`))
32+
const summaryText = `Summary: ${report.passedTests} / ${report.totalTests} tests passed.`
33+
const color = report.allPassed ? green : red
34+
console.log(color(bold(summaryText)))
3535
}

0 commit comments

Comments
 (0)