Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions packages/script/test/check-greys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { afterEach, describe, expect, test } from "bun:test"
import { copyFile, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"
import { tmpdir } from "node:os"
import path from "node:path"

const directories: string[] = []

afterEach(async () => {
await Promise.all(directories.splice(0).map((directory) => rm(directory, { recursive: true, force: true })))
})

describe("check-greys", () => {
test("honors declared path exceptions with Windows separators", async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test can be dropped: the fix is one line, and this test passes on Linux both with and without the fix (I checked by restoring the base script/check-greys.ts), because the temp files only ever have native / paths here. It also copies the script into a temp folder and starts a separate process to run it, and since --json always exits 0, the exitCode check below proves nothing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intended for the Windows full-tier runs, not the Linux-only PR run. I verified it on Windows with Bun 1.4.4: restoring the base checker makes the JSON outcome assertion fail, with both exempt paths reported as problem: not on the scale. With the fix, both report their expected exception reasons. The zero exit check only guards subprocess success; it is not the regression assertion. The second case separately checks exit 1 for a non-exempt file. I kept the real CLI tests to cover that platform behavior without adding a production test API.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's fair. check-test.yml does run the unit tests on Windows for pushes, nightly runs and the release gate, so the JSON outcome check would catch this bug there, even though it can't on the Linux pull-request run. I can't run it on Windows myself, so I'm relying on your result for that. Understood on the exit code: in --json mode the script always exits 0, so that check only confirms the process ran. My remaining point is size: 78 lines that copy the script into a temp folder and start a separate process, to guard a one-line fix. Whether that's worth keeping is up to the maintainers.

const result = await runChecker(
{
"services/site/src/component/font-share/share.css": ".share { color: #777777; }\n",
"services/site/src/style/token/color.css": ":root { --neutral: #777777; }\n",
},
["--json"],
)

expect(result.exitCode).toBe(0)
expect(
JSON.parse(result.stdout.toString())
.entries.map((entry: { outcome: string }) => entry.outcome)
.sort(),
).toEqual(
[
"exception: Font share cards: exported images",
"exception: Legacy tokens: to remove when the dropdown, language picker, 404 and limits graph move to site tokens",
].sort(),
)
})

test("still rejects greys outside declared path exceptions", async () => {
const result = await runChecker({
"services/site/src/component/site/regular.css": ".regular { color: #777777; }\n",
})

expect(result.exitCode).toBe(1)
expect(result.stderr.toString()).toContain("#777777 not on the scale")
})
})

async function runChecker(stylesheets: Record<string, string>, args: string[] = []) {
const directory = await mkdtemp(path.join(tmpdir(), "opencode-greys-"))
directories.push(directory)

const checker = path.join(directory, "script", "check-greys.ts")
await mkdir(path.dirname(checker), { recursive: true })
await copyFile(path.resolve(import.meta.dir, "../../../script/check-greys.ts"), checker)

for (const [file, source] of Object.entries(stylesheets)) {
const target = path.join(directory, file)
await mkdir(path.dirname(target), { recursive: true })
await writeFile(target, source)
}

const home = path.join(directory, "home")
const temp = path.join(directory, "temp")
await Promise.all([mkdir(home), mkdir(temp)])

return Bun.spawnSync({
cmd: [process.execPath, checker, ...args],
cwd: directory,
env: {
...process.env,
HOME: home,
USERPROFILE: home,
TMP: temp,
TEMP: temp,
TMPDIR: temp,
PATH: `${path.dirname(process.execPath)}${path.delimiter}${process.env.PATH ?? ""}`,
},
stdout: "pipe",
stderr: "pipe",
})
}
3 changes: 2 additions & 1 deletion script/check-greys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ const entries = (
await Promise.all(
files.map(async (file) => {
const source = await Bun.file(path.join(repo(), file)).text()
const exception = EXCEPTIONS.find(([re]) => re.test(file))?.[1]
const normalizedFile = file.replaceAll(path.sep, "/")
const exception = EXCEPTIONS.find(([re]) => re.test(normalizedFile))?.[1]
const allowed = allowedRanges(source)
return declarations(source).flatMap((decl) =>
neutrals(decl.value).map((hex) => ({
Expand Down
Loading