Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/midden. Archive posts and media across platforms, with tenant accounts and Telegram access
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129#!/usr/bin/env node// Verify that pre-commit checks the index without disturbing partial staging.import assert from "node:assert/strict";import { spawnSync } from "node:child_process";import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync,} from "node:fs";import { tmpdir } from "node:os";import path from "node:path";import { test } from "node:test";import { fileURLToPath } from "node:url";
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");const FORMATTED = '{\n "value": 1\n}\n';const UNFORMATTED = '{"value":1}';
function fixture(t) { const root = mkdtempSync(path.join(tmpdir(), "midden-format-")); t.after(() => rmSync(root, { recursive: true, force: true })); mkdirSync(path.join(root, "scripts")); mkdirSync(path.join(root, ".githooks")); copyFileSync( path.join(ROOT, "scripts/format.mjs"), path.join(root, "scripts/format.mjs"), ); copyFileSync( path.join(ROOT, ".githooks/pre-commit"), path.join(root, ".githooks/pre-commit"), ); chmodSync(path.join(root, ".githooks/pre-commit"), 0o755); symlinkSync( path.join(ROOT, "node_modules"), path.join(root, "node_modules"), "dir", ); function run(command, { check = true } = {}) { const result = spawnSync(command[0], command.slice(1), { cwd: root, encoding: "utf8", }); if (check) assert.equal( result.status, 0, `${command.join(" ")}:\n${result.stdout}\n${result.stderr}`, ); return result; } const git = (...args) => run(["git", ...args]); git("init", "--quiet"); git("config", "core.hooksPath", ".githooks"); git("config", "user.name", "Format Test"); git("config", "user.email", "format-test@example.invalid"); function stage(text) { writeFileSync(path.join(root, "sample.json"), text); git("add", "sample.json"); } return { root, run, git, stage };}
test("reject unformatted index even when the worktree is formatted", (t) => { const { root, run, git, stage } = fixture(t); stage(UNFORMATTED); writeFileSync(path.join(root, "sample.json"), FORMATTED); const index = git("ls-files", "--stage").stdout; const result = run(["git", "commit", "-m", "must reject"], { check: false }); assert.notEqual(result.status, 0); assert.match(result.stdout + result.stderr, /Needs formatting: sample\.json/); assert.equal(git("ls-files", "--stage").stdout, index); assert.equal(readFileSync(path.join(root, "sample.json"), "utf8"), FORMATTED); assert.equal(git("show", ":sample.json").stdout, UNFORMATTED);});
test("accept formatted index without staging worktree changes", (t) => { const { root, git, stage } = fixture(t); stage(FORMATTED); writeFileSync(path.join(root, "sample.json"), UNFORMATTED); git("commit", "-m", "formatted index"); assert.equal(git("show", "HEAD:sample.json").stdout, FORMATTED); assert.equal( readFileSync(path.join(root, "sample.json"), "utf8"), UNFORMATTED, ); assert.equal(git("diff", "--cached").stdout, "");});
test("Go formatting converges before writing", (t) => { const { root, run, git } = fixture(t); if (existsSync(path.join(ROOT, ".tools"))) symlinkSync(path.join(ROOT, ".tools"), path.join(root, ".tools"), "dir"); const filename = path.join(root, "generated.go"); writeFileSync( filename, "// Code generated by test. DO NOT EDIT.\npackage sample\n\nvar a = 1\nvar b = 2\nvar c = []int{\n1,\n}\nvar d = []int{\n2,\n}\n", ); const command = [process.execPath, "scripts/format.mjs"]; run([...command, "--write", "generated.go"]); const formatted = readFileSync(filename, "utf8"); run([...command, "--check", "generated.go"]); run([...command, "--write", "generated.go"]); assert.equal(readFileSync(filename, "utf8"), formatted); git("add", "generated.go"); git("commit", "-m", "formatted generated Go");});
test("staged web files use their local Prettier configuration", (t) => { const { root, run, git } = fixture(t); mkdirSync(path.join(root, "web")); writeFileSync(path.join(root, "web", ".prettierrc.json"), '{"semi":false}'); writeFileSync( path.join(root, "web", "sample.js"), "const value = 1\nconsole.log(value)\n", ); git("add", "web/sample.js"); run([process.execPath, "scripts/format.mjs", "--staged"]); assert.equal( git("show", ":web/sample.js").stdout, "const value = 1\nconsole.log(value)\n", );});