Skip to content

Commit 41de2ed

Browse files
committed
Tidy up test output
1 parent c229241 commit 41de2ed

1 file changed

Lines changed: 33 additions & 13 deletions

File tree

test/index.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import fs from "node:fs"
22
import path from "node:path"
3+
import { performance } from "node:perf_hooks"
34

45
import staticbuild, { StaticBuildOptions } from "../src/staticbuild"
56
import { scan } from "../src/fs"
7+
import { stdout } from "node:process"
68

7-
function clamp(value: number, min: number, max: number) {
8-
return Math.min(Math.max(value, min), max)
9-
}
9+
const SHOW_INFO_LOGS = false
1010

1111
function expectFileExists(filePath: string) {
1212
if (!fs.existsSync(filePath)) {
@@ -83,21 +83,30 @@ async function test() {
8383
// Remove the first 2 arguments that nodejs provides.
8484
const args = process.argv.splice(2, process.argv.length)
8585

86+
const timings: Record<string, { startTime: number; endTime: number }> = {}
87+
8688
const logger: StaticBuildOptions["logger"] = {
87-
info: (...messages: unknown[]) => {},
88-
warn: console.log,
89-
error: console.log,
90-
time: (name: string) => {},
91-
timeEnd: (name: string) => {},
89+
info: (...messages: unknown[]) =>
90+
SHOW_INFO_LOGS ? stdout.write(`\x1b[38;2;100;100;100m[info] ${messages.join(", ")}\x1b[0m \n`) : null,
91+
warn: (...messages: unknown[]) => stdout.write(`\x1b[38;2;100;100;100m[warn] ${messages.join(", ")}\x1b[0m \n`),
92+
error: (...messages: unknown[]) => stdout.write(`\x1b[38;2;100;100;100m[erro] ${messages.join(", ")}\x1b[0m \n`),
93+
time: (name: string) => {
94+
timings[name] = { startTime: performance.now(), endTime: -1 }
95+
},
96+
timeEnd: (name: string) => {
97+
timings[name] = { ...timings[name], endTime: performance.now() }
98+
},
9299
}
93100

94101
for (const directory of scan("./test", [], { recursive: false })) {
95102
if (args[0] && args[0] !== directory.name) continue
96103

97104
if (!directory.isDirectory) continue
98-
if (directory.name.startsWith("x_")) continue
99105

100-
console.log(`Test: ${directory.name}`)
106+
if (directory.name.startsWith("x_")) {
107+
stdout.write(`\x1b[38;2;100;100;100m[skip]\x1b[0m ${directory.name.replace(/^x_/, "")} \n`)
108+
continue
109+
}
101110

102111
const inputDirectory = path.join(process.cwd(), directory.path, "input")
103112
const outputDirectory = path.join(process.cwd(), directory.path, "output")
@@ -108,10 +117,21 @@ async function test() {
108117

109118
await staticbuild({ inputDirectory, outputDirectory, logger })
110119

111-
const expectedDirectory = path.join(directory.path, "expected")
112-
expectDirectoriesEqual(outputDirectory, expectedDirectory)
120+
try {
121+
const expectedDirectory = path.join(directory.path, "expected")
122+
expectDirectoriesEqual(outputDirectory, expectedDirectory)
123+
124+
stdout.write(`\x1b[38;2;0;255;0m[pass]\x1b[0m ${directory.name} \n`)
125+
} catch (err: unknown) {
126+
stdout.write(`\x1b[38;2;255;0;0m[fail]\x1b[0m ${directory.name} \n`)
127+
break
128+
}
113129

114-
console.log("")
130+
for (const [name, time] of Object.entries(timings)) {
131+
if (time.endTime === -1) continue
132+
133+
stdout.write(`\x1b[38;2;100;100;100m[time] ${name}: ${(time.endTime - time.startTime).toFixed(1)}ms\x1b[0m \n`)
134+
}
115135
}
116136
}
117137

0 commit comments

Comments
 (0)