// The GitRef rule table, one named test per case. Each refusal names the rule // git's refs.c applies first; every row was checked against git 2.56.0's // `git check-ref-format`, and integration/GitRef.parity.test.ts holds the // validity of generated names to the installed git. import { assert, describe, it } from "@effect/vitest"; import { Effect, Option, Schema } from "effect"; import type { GitRefProblem } from "../src/GitRef.js"; import { GitRef } from "../src/GitRef.js"; const problemOf = (option: Option.Option): GitRefProblem | undefined => Option.getOrUndefined(option); describe("GitRef.branchNameProblem", () => { const refused: ReadonlyArray = [ ["the empty name", "", "empty"], ["a space", "a b", "whitespace-or-control"], ["a tab", "a\tb", "whitespace-or-control"], ["a newline", "a\nb", "whitespace-or-control"], ["a NUL", "a\u0000b", "whitespace-or-control"], ["a C0 control", "a\u0001b", "whitespace-or-control"], ["DEL", "a\u007fb", "whitespace-or-control"], ["`..`", "a..b", "double-dot"], ["`..` across nothing else", "..", "double-dot"], ["a leading `-`", "-a", "leading-dash"], ["a lone `-`", "-", "leading-dash"], ["`~`", "a~b", "forbidden-char"], ["`^`", "a^b", "forbidden-char"], ["`:`", "a:b", "forbidden-char"], ["`?`", "a?b", "forbidden-char"], ["`*`", "a*b", "forbidden-char"], ["`[`", "a[b", "forbidden-char"], ["`\\`", "a\\b", "forbidden-char"], ["`@{`", "a@{b", "at-brace"], ["`@{-1}`, which git expands only inside a repository", "@{-1}", "at-brace"], ["a trailing `.`", "a.", "trailing-dot"], ["a trailing `.` after a slash", "a/b.", "trailing-dot"], ["`//`", "a//b", "empty-component"], ["a leading `/`", "/a", "empty-component"], ["a trailing `/`", "a/", "empty-component"], ["a leading `.`", ".a", "component-leading-dot"], ["a component starting with `.`", "a/.b", "component-leading-dot"], ["a `.lock` suffix", "a.lock", "component-lock-suffix"], ["a `.lock` suffix on a middle component", "a.lock/b", "component-lock-suffix"], ["`HEAD`", "HEAD", "head"], ]; for (const [label, name, problem] of refused) { it(`refuses ${label} as ${problem}`, () => { assert.strictEqual(problemOf(GitRef.branchNameProblem(name)), problem); assert.isFalse(GitRef.isBranchName(name)); }); } const accepted: ReadonlyArray = [ ["a lone `@`", "@"], ["`@` inside a name", "a@b"], ["`@` as a component", "a/@"], ["a version", "v1.0"], ["a release line", "release/1.x"], ["`.lock` not at the end", "a.lockb"], ["`#`", "feature/#1"], ["`{` without `@`", "a{b"], ["`@` then `}`", "a@}"], ["non-ASCII letters", "fünf/日本"], ["non-ASCII whitespace, which git judges as bytes", "a b"], ["a dot ending a middle component", "a./b"], ["`HEAD` as a prefix", "HEAD/x"], ["one component", "main"], ["a name beginning `refs/`", "refs/heads/x"], ]; for (const [label, name] of accepted) { it(`accepts ${label}`, () => { assert.isTrue(Option.isNone(GitRef.branchNameProblem(name))); assert.isTrue(GitRef.isBranchName(name)); }); } it("reports the first problem git meets, scanning left to right", () => { assert.strictEqual(problemOf(GitRef.branchNameProblem("a~/b..c")), "forbidden-char"); assert.strictEqual(problemOf(GitRef.branchNameProblem("a..~")), "double-dot"); assert.strictEqual(problemOf(GitRef.branchNameProblem("-a..b")), "leading-dash"); assert.strictEqual(problemOf(GitRef.branchNameProblem(".a.lock")), "component-leading-dot"); }); }); describe("GitRef.tagNameProblem", () => { it("refuses a leading `-`", () => assert.strictEqual(problemOf(GitRef.tagNameProblem("-v1")), "leading-dash")); it("refuses `HEAD`", () => assert.strictEqual(problemOf(GitRef.tagNameProblem("HEAD")), "head")); it("refuses `@{`", () => assert.strictEqual(problemOf(GitRef.tagNameProblem("v@{1")), "at-brace")); it("refuses the empty name", () => assert.strictEqual(problemOf(GitRef.tagNameProblem("")), "empty")); it("accepts a version", () => assert.isTrue(GitRef.isTagName("v1.2.3"))); it("accepts a lone `@`", () => assert.isTrue(GitRef.isTagName("@"))); it("refuses what a branch name refuses", () => assert.isFalse(GitRef.isTagName("a..b"))); }); describe("GitRef.refNameProblem", () => { it("refuses one component without allowOneLevel", () => assert.strictEqual(problemOf(GitRef.refNameProblem("main")), "one-level")); it("accepts one component with allowOneLevel", () => assert.isTrue(GitRef.isRefName("main", { allowOneLevel: true }))); it("accepts a full branch ref", () => assert.isTrue(GitRef.isRefName("refs/heads/main"))); it("refuses a lone `@`, even with allowOneLevel", () => assert.strictEqual(problemOf(GitRef.refNameProblem("@", { allowOneLevel: true })), "lone-at")); it("accepts `HEAD` with allowOneLevel: the shorthand rules do not apply", () => assert.isTrue(GitRef.isRefName("HEAD", { allowOneLevel: true }))); it("accepts a leading `-`: the shorthand rules do not apply", () => assert.isTrue(GitRef.isRefName("-a/b"))); it("refuses the empty name", () => assert.strictEqual(problemOf(GitRef.refNameProblem("")), "empty")); it("refuses a component problem before the one-level rule", () => assert.strictEqual(problemOf(GitRef.refNameProblem("a..b")), "double-dot")); it("refuses a trailing `.` before the one-level rule", () => assert.strictEqual(problemOf(GitRef.refNameProblem("a.")), "trailing-dot")); it("refuses `.lock` on a full ref", () => assert.strictEqual(problemOf(GitRef.refNameProblem("refs/heads/a.lock")), "component-lock-suffix")); }); describe("GitRef.describe", () => { it("gives a clause for a problem", () => assert.strictEqual(GitRef.describe("double-dot"), "contains `..`")); }); describe("GitRef schemas", () => { it.effect("BranchName decodes a valid name to itself", () => Effect.gen(function* () { assert.strictEqual(yield* Schema.decodeUnknownEffect(GitRef.BranchName)("release/1.x"), "release/1.x"); }), ); it.effect("BranchName refuses `HEAD` with the rule in the message", () => Effect.gen(function* () { const error = yield* Effect.flip(Schema.decodeUnknownEffect(GitRef.BranchName)("HEAD")); assert.include(error.message, "not a valid branch name: is `HEAD`"); }), ); it.effect("BranchName refuses a non-string", () => Effect.gen(function* () { const error = yield* Effect.flip(Schema.decodeUnknownEffect(GitRef.BranchName)(1)); assert.strictEqual(error._tag, "SchemaError"); }), ); it.effect("TagName refuses a leading `-` with the rule in the message", () => Effect.gen(function* () { const error = yield* Effect.flip(Schema.decodeUnknownEffect(GitRef.TagName)("-v1")); assert.include(error.message, "not a valid tag name: starts with `-`"); }), ); it.effect("TagName decodes a version", () => Effect.gen(function* () { assert.strictEqual(yield* Schema.decodeUnknownEffect(GitRef.TagName)("v1.0"), "v1.0"); }), ); }); // okfit round 2, item 4: `.annotate` after a custom check lands on the check, // and core's JSON Schema compiler drops a filter's annotations unless the // filter declares a `toJsonSchema` fragment. These pin that a consumer's // title, description and examples survive into the generated document both // ways: annotating the exported schema, and checking the exported filter onto // the consumer's own annotated string. describe("GitRef schemas keep consumer annotations in JSON Schema", () => { const annotations = { title: "Branch", description: "The branch to publish from.", examples: ["main", "release/1.x"], } as const; const jsonOf = (schema: Schema.Top) => Schema.toJsonSchemaDocument(schema).schema; it("BranchName.annotate keeps title, description and examples", () => { const schema = jsonOf(GitRef.BranchName.annotate(annotations)); assert.deepStrictEqual(schema, { type: "string", ...annotations }); }); it("TagName.annotate keeps title, description and examples", () => { const schema = jsonOf(GitRef.TagName.annotate(annotations)); assert.deepStrictEqual(schema, { type: "string", ...annotations }); }); it("branchNameCheck onto an annotated string keeps its annotations", () => { const schema = jsonOf(Schema.String.annotate(annotations).check(GitRef.branchNameCheck)); assert.deepStrictEqual(schema, { type: "string", ...annotations }); }); it("tagNameCheck onto an annotated string keeps its annotations", () => { const schema = jsonOf(Schema.String.annotate(annotations).check(GitRef.tagNameCheck)); assert.deepStrictEqual(schema, { type: "string", ...annotations }); }); it("the exported schemas emit a plain string with no annotation of their own", () => { assert.deepStrictEqual(jsonOf(GitRef.BranchName), { type: "string" }); assert.deepStrictEqual(jsonOf(GitRef.TagName), { type: "string" }); }); it.effect("branchNameCheck refuses what BranchName refuses, with the same message", () => Effect.gen(function* () { const schema = Schema.String.annotate(annotations).check(GitRef.branchNameCheck); const error = yield* Effect.flip(Schema.decodeUnknownEffect(schema)("HEAD")); assert.include(error.message, "not a valid branch name: is `HEAD`"); assert.strictEqual(yield* Schema.decodeUnknownEffect(schema)("main"), "main"); }), ); it.effect("tagNameCheck refuses a leading `-`", () => Effect.gen(function* () { const error = yield* Effect.flip(Schema.decodeUnknownEffect(Schema.String.check(GitRef.tagNameCheck))("-v1")); assert.include(error.message, "not a valid tag name: starts with `-`"); }), ); });