import dedent from "dedent" import { describe, it, expect, beforeEach, afterEach } from "vitest" import { writeFile, unlink, mkdir, rm, readFile } from "fs/promises" import { join, resolve } from "path" import { spawn } from "child_process" import { execBinary, expectExitCode } from "./cli/cli-helpers" import type { ExecResult } from "./cli/cli-helpers" const execGit = (args: string[], cwd: string): Promise => { return new Promise((resolvePromise) => { const child = spawn("git", args, { cwd, stdio: ["ignore", "pipe", "pipe"], env: { ...process.env, NO_COLOR: "1", GIT_CONFIG_GLOBAL: "/dev/null", GIT_CONFIG_SYSTEM: "/dev/null" } }) let stdout = "" let stderr = "" child.stdout.on("data", (data) => { stdout += data.toString() }) child.stderr.on("data", (data) => { stderr += data.toString() }) child.on("close", (code) => { resolvePromise({ stdout, stderr, exitCode: code || 0 }) }) }) } const setupGitRepositoryWithPreCommitHook = async (directory: string, formatArgs: string) => { const binary = resolve(process.cwd(), "bin/herb-format") await mkdir(join(directory, ".git-hooks"), { recursive: true }) await writeFile(join(directory, "unformatted.html.erb"), '

Not formatted

') await writeFile( join(directory, ".git-hooks", "pre-commit"), `#!/bin/sh\nset -e\n"${process.execPath}" "${binary}" ${formatArgs}\n`.trimEnd() + "\n", { mode: 0o755 } ) await execGit(["init", "--quiet"], directory) await execGit(["config", "core.hooksPath", ".git-hooks"], directory) await execGit(["config", "commit.gpgsign", "false"], directory) await execGit(["config", "user.email", "test@example.com"], directory) await execGit(["config", "user.name", "Test"], directory) await execGit(["add", "-A"], directory) } describe("CLI Binary", () => { beforeEach(async () => { await cleanup() }) afterEach(async () => { await cleanup() }) const cleanup = async () => { const testFiles = [ "test-format.html.erb", "test-unchanged.html.erb", "test-changed.html.erb" ] for (const file of testFiles) { await unlink(file).catch(() => {}) } await rm("test-dir", { recursive: true }).catch(() => {}) } it("should show help when --help flag is provided", async () => { const result = await execBinary(["--help"]) expectExitCode(result, 0) expect(result.stdout).toContain("Usage: herb-format") expect(result.stdout).toContain("files|directories|glob-patterns") expect(result.stdout).toContain("Arguments:") expect(result.stdout).toContain("Options:") expect(result.stdout).toContain("Examples:") }) it("should show help when -h flag is provided", async () => { const result = await execBinary(["-h"]) expectExitCode(result, 0) expect(result.stdout).toContain("Usage: herb-format") expect(result.stdout).toContain("files|directories|glob-patterns") expect(result.stdout).toContain("Arguments:") expect(result.stdout).toContain("Options:") expect(result.stdout).toContain("Examples:") }) it("should show version when --version flag is provided", async () => { const result = await execBinary(["--version"]) expectExitCode(result, 0) expect(result.stdout).toContain("Versions:") expect(result.stdout).toContain("@herb-tools/formatter@") expect(result.stdout).toContain("@herb-tools/printer@") }) it("should show version when -v flag is provided", async () => { const result = await execBinary(["-v"]) expectExitCode(result, 0) expect(result.stdout).toContain("Versions:") expect(result.stdout).toContain("@herb-tools/formatter@") expect(result.stdout).toContain("@herb-tools/printer@") }) it("should format HTML/ERB from stdin", async () => { const input = '
<%= user.name %>
' const result = await execBinary([], input) expectExitCode(result, 0) expect(result.stdout).toContain('
') expect(result.stdout).toContain('
<%= user.name %>
') expect(result.stdout).toContain('
') }) it("should return stdin unchanged when formatting is disabled", async () => { const configFile = "test-dir/.herb.yml" const input = '
<%= user.name %>
' await mkdir("test-dir", { recursive: true }) await writeFile(configFile, dedent` version: 0.11.2 formatter: enabled: false `) const result = await execBinary(["--config-file", configFile], input) expectExitCode(result, 0) expect(result.stdout).toBe(input) expect(result.stderr).toContain("Formatter is disabled in .herb.yml configuration.") }) it("should return large stdin unchanged to a pipe when formatting is disabled", async () => { const configFile = "test-dir/.herb.yml" const input = '
<%= user.name %>
\n'.repeat(4000) await mkdir("test-dir", { recursive: true }) await writeFile(configFile, dedent` version: 0.11.2 formatter: enabled: false `) const result = await execBinary(["--config-file", configFile], input) expectExitCode(result, 0) expect(result.stdout.length).toBe(input.length) expect(result.stdout).toBe(input) }) it("should format HTML/ERB from file", async () => { const testFile = "test-format.html.erb" const input = '
<%= "Hello" %>

World

' await writeFile(testFile, input) try { const result = await execBinary([testFile]) expectExitCode(result, 0) expect(result.stdout).toContain('Formatted: test-format.html.erb') const formattedContent = await readFile(testFile, 'utf-8') expect(formattedContent).toBe(dedent`
<%= "Hello" %>

World

` + '\n') } finally { await unlink(testFile).catch(() => {}) } }) it("should show experimental preview message on stderr", async () => { const result = await execBinary([]) expect(result.stderr).toContain("⚠️ Experimental Preview") expect(result.stderr).toContain("early development") expect(result.stderr).toContain("github.com/marcoroth/herb/issues") }) it("should not include experimental preview message in stdout", async () => { const input = '
test
' const result = await execBinary([], input) expectExitCode(result, 0) expect(result.stdout).not.toContain("⚠️ Experimental Preview") expect(result.stdout).toContain('
test
') }) it("stdout should contain only formatted output without warnings", async () => { const input = '

Hello

' const result = await execBinary([], input) expectExitCode(result, 0) expect(result.stderr).toContain("⚠️ Experimental Preview") expect(result.stdout).not.toContain("⚠️") expect(result.stdout).not.toContain("Experimental") expect(result.stdout).toBe('
\n

Hello

\n
\n') }) it("should suppress the experimental preview message with --quiet", async () => { const input = '

Hello

' const result = await execBinary(["--quiet"], input) expectExitCode(result, 0) expect(result.stderr).not.toContain("Experimental Preview") expect(result.stdout).toBe('
\n

Hello

\n
\n') }) it("should suppress the experimental preview message with -q", async () => { const input = '

Hello

' const result = await execBinary(["-q"], input) expectExitCode(result, 0) expect(result.stderr).not.toContain("Experimental Preview") expect(result.stdout).toBe('
\n

Hello

\n
\n') }) it("should suppress the config file notice with --quiet", async () => { const directory = "test-quiet-config" const configFile = join(directory, ".herb.yml") const input = '

Hello

' await mkdir(directory, { recursive: true }) await writeFile(configFile, dedent` version: 0.10.3 formatter: enabled: true `) try { const result = await execBinary(["--quiet", "--config-file", configFile], input) expectExitCode(result, 0) expect(result.stderr).not.toContain("Using Herb config file") expect(result.stderr).not.toContain("Experimental Preview") expect(result.stdout).toBe('
\n

Hello

\n
\n') } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) it("should keep the misnamed config warning with --quiet", async () => { const directory = "test-quiet-misnamed" const configFile = join(directory, ".herb.yml") const input = '

Hello

' await mkdir(directory, { recursive: true }) await writeFile(configFile, dedent` version: 0.10.3 formatter: enabled: true `) await writeFile(join(directory, ".herb.yaml"), "") try { const result = await execBinary(["--quiet", "--config-file", configFile], input) expectExitCode(result, 0) expect(result.stderr).toContain(`⚠ Ignoring ${resolve(directory, ".herb.yaml")}: Herb only reads \`.herb.yml\``) expect(result.stderr).not.toContain("Using Herb config file") expect(result.stdout).toBe('
\n

Hello

\n
\n') } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) it("should keep the forcing warning with --quiet", async () => { const directory = "test-quiet-force" const configFile = join(directory, ".herb.yml") const input = '

Hello

' await mkdir(directory, { recursive: true }) await writeFile(configFile, dedent` version: 0.10.3 formatter: enabled: false `) try { const result = await execBinary(["--quiet", "--force", "--config-file", configFile], input) expectExitCode(result, 0) expect(result.stderr).toContain("⚠️ Forcing formatter run (disabled in .herb.yml)") expect(result.stderr).not.toContain("Experimental Preview") expect(result.stderr).not.toContain("Using Herb config file") expect(result.stdout).toBe('
\n

Hello

\n
\n') } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) it("should show --quiet option in help", async () => { const result = await execBinary(["--help"]) expectExitCode(result, 0) expect(result.stdout).toContain("-q, --quiet") }) it("should format empty input from stdin when no args provided", async () => { const result = await execBinary([], "") expectExitCode(result, 0) expect(result.stderr).toContain("⚠️ Experimental Preview") expect(result.stdout).toBe("\n") }) it("should format configured files when stdin is a non-TTY null device", async () => { const directory = "test-non-tty-null-stdin" const input = '

Not formatted

' await mkdir(directory, { recursive: true }) await writeFile(join(directory, "unformatted.html.erb"), input) try { const result = await execBinary([], undefined, { cwd: directory, stdin: "ignore" }) expectExitCode(result, 0) expect(result.stdout).toContain("Formatted: unformatted.html.erb") const formattedContent = await readFile(join(directory, "unformatted.html.erb"), "utf-8") expect(formattedContent).not.toBe(input) expect(formattedContent).toContain("
") } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) it("should handle no files found in empty directory", async () => { await mkdir("test-empty-dir", { recursive: true }) try { const result = await execBinary(["test-empty-dir"]) expectExitCode(result, 0) expect(result.stderr).toContain("⚠️ Experimental Preview") expect(result.stdout).toContain("No files found matching pattern") } finally { await rm("test-empty-dir", { recursive: true }).catch(() => {}) } }) it("should only show formatted message when file changes", async () => { const testFile = "test-unchanged.html.erb" const input = '
Already formatted
\n' await writeFile(testFile, input) try { const result = await execBinary([testFile]) expectExitCode(result, 0) expect(result.stdout).not.toContain(`Formatted: ${testFile}`) const fileContent = await readFile(testFile, 'utf-8') expect(fileContent).toBe(input) } finally { await unlink(testFile).catch(() => {}) } }) it("should show formatted message when file changes", async () => { const testFile = "test-changed.html.erb" const input = '

Unformatted

' await writeFile(testFile, input) try { const result = await execBinary([testFile]) expectExitCode(result, 0) expect(result.stdout).toContain(`Formatted: ${testFile}`) const fileContent = await readFile(testFile, 'utf-8') expect(fileContent).not.toBe(input) } finally { await unlink(testFile).catch(() => {}) } }) it("should handle directory with no files", async () => { await mkdir("test-dir", { recursive: true }) try { const result = await execBinary(["test-dir"]) expectExitCode(result, 0) expect(result.stdout).toContain("No files found matching pattern") } finally { await rm("test-dir", { recursive: true }).catch(() => {}) } }) it("should handle directory with files", async () => { await mkdir("test-dir", { recursive: true }) const testFile = join("test-dir", "test.html.erb") const input = '

Test

' await writeFile(testFile, input) try { const result = await execBinary(["test-dir"]) expectExitCode(result, 0) expect(result.stdout).toContain("1 formatted") expect(result.stdout).toContain(`Formatted: ${testFile}`) } finally { await rm("test-dir", { recursive: true }).catch(() => {}) } }) it("should use proper singular/plural forms", async () => { await mkdir("test-dir", { recursive: true }) const testFile1 = join("test-dir", "test1.html.erb") const testFile2 = join("test-dir", "test2.html.erb") const input = '

Test

' await writeFile(testFile1, input) await writeFile(testFile2, input) try { const result = await execBinary(["test-dir"]) expectExitCode(result, 0) expect(result.stdout).toContain("2 formatted") expect(result.stdout).not.toContain("file(s)") } finally { await rm("test-dir", { recursive: true }).catch(() => {}) } }) it("should handle non-existent file gracefully", async () => { const result = await execBinary(["non-existent-file.html.erb"]) expectExitCode(result, 1) expect(result.stderr).toContain("Error: Cannot access 'non-existent-file.html.erb'") }) it("should show --check option in help", async () => { const result = await execBinary(["--help"]) expectExitCode(result, 0) expect(result.stdout).toContain("--check") expect(result.stdout).toContain("check if files are formatted") expect(result.stdout).toContain("herb-format --check") }) it("should reject --check with stdin", async () => { const result = await execBinary(["--check", "-"], "
test
") expectExitCode(result, 1) expect(result.stderr).toContain("Error: --check mode is not supported with stdin") }) it("should reject -c with stdin", async () => { const result = await execBinary(["-c", "-"], "
test
") expectExitCode(result, 1) expect(result.stderr).toContain("Error: --check mode is not supported with stdin") }) for (const stdin of ["pipe", "ignore"] as const) { it(`should check configured files with --check when stdin is a non-TTY ${stdin}`, async () => { const directory = `test-non-tty-${stdin}` const input = '

Not formatted

' await mkdir(directory, { recursive: true }) await writeFile(join(directory, "unformatted.html.erb"), input) try { const result = await execBinary(["--check"], undefined, { cwd: directory, stdin }) expect(result.stderr).not.toContain("--check mode is not supported with stdin") expectExitCode(result, 1) expect(result.stdout).toContain("unformatted.html.erb") expect(result.stdout).toContain("not formatted") } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) } it("should skip files that don't match the configured file patterns", async () => { const directory = "test-unsupported-files" const script = "function greet(name) {\n if (name < 3) {\n return 'hi'\n }\n}\n" await mkdir(directory, { recursive: true }) await writeFile(join(directory, "script.js"), script) await writeFile(join(directory, "page.html.erb"), '

Not formatted

') try { const result = await execBinary(["script.js", "page.html.erb"], undefined, { cwd: directory }) expectExitCode(result, 0) expect(result.stderr).toContain("script.js") expect(result.stderr).toContain("Use --force to format it anyway") expect(result.stdout).toContain("Formatted: page.html.erb") expect(await readFile(join(directory, "script.js"), "utf-8")).toBe(script) expect(await readFile(join(directory, "page.html.erb"), "utf-8")).toBe("
\n

Not formatted

\n
\n") } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) it("should format files that don't match the configured file patterns with --force", async () => { const directory = "test-unsupported-files-force" const script = "function greet(name) {\n if (name < 3) {\n return 'hi'\n }\n}\n" await mkdir(directory, { recursive: true }) await writeFile(join(directory, "script.js"), script) try { const result = await execBinary(["--force", "script.js"], undefined, { cwd: directory }) expectExitCode(result, 0) expect(result.stdout).toContain("Formatted: script.js") expect(await readFile(join(directory, "script.js"), "utf-8")).not.toBe(script) } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) it("should format files added to the include patterns in .herb.yml", async () => { const directory = "test-include-config" await mkdir(directory, { recursive: true }) await writeFile(join(directory, ".herb.yml"), dedent` version: 0.11.2 formatter: enabled: true files: include: - "**/*.xml.erb" `) await writeFile(join(directory, "feed.xml.erb"), '') try { const result = await execBinary(["feed.xml.erb"], undefined, { cwd: directory }) expectExitCode(result, 0) expect(result.stderr).not.toContain("match the configured file patterns") expect(result.stdout).toContain("Formatted: feed.xml.erb") const formatted = await readFile(join(directory, "feed.xml.erb"), "utf-8") expect(formatted).toContain("") } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) it("should still skip files outside the include patterns in .herb.yml", async () => { const directory = "test-include-config-skip" const script = "function greet(name) {\n if (name < 3) {\n return 'hi'\n }\n}\n" await mkdir(directory, { recursive: true }) await writeFile(join(directory, ".herb.yml"), dedent` version: 0.11.2 formatter: enabled: true files: include: - "**/*.xml.erb" `) await writeFile(join(directory, "script.js"), script) try { const result = await execBinary(["script.js"], undefined, { cwd: directory }) expectExitCode(result, 0) expect(result.stderr).toContain("script.js") expect(result.stderr).toContain("Use --force to format it anyway") expect(await readFile(join(directory, "script.js"), "utf-8")).toBe(script) } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) it("should accept every default file pattern", async () => { const directory = "test-default-patterns" const files = ["a.herb", "b.html.erb", "c.html.herb", "d.html", "e.html+phone.erb", "f.rhtml", "g.turbo_stream.erb"] await mkdir(directory, { recursive: true }) for (const file of files) { await writeFile(join(directory, file), '

Not formatted

') } try { const result = await execBinary(["--check", ...files], undefined, { cwd: directory }) expectExitCode(result, 1) expect(result.stderr).not.toContain("match the configured file patterns") for (const file of files) { expect(result.stdout).toContain(file) } } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) it("should check configured files with --check from a git pre-commit hook", async () => { const directory = "test-git-hook-check" try { await setupGitRepositoryWithPreCommitHook(directory, "--check") const result = await execGit(["commit", "-m", "test"], directory) const output = result.stdout + result.stderr expect(output).not.toContain("--check mode is not supported with stdin") expect(result.exitCode).toBe(1) expect(output).toContain("unformatted.html.erb") expect(output).toContain("not formatted") } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) it("should format configured files from a git pre-commit hook", async () => { const directory = "test-git-hook-format" try { await setupGitRepositoryWithPreCommitHook(directory, "") const result = await execGit(["commit", "-m", "test"], directory) const output = result.stdout + result.stderr expect(result.exitCode).toBe(0) expect(output).toContain("Formatted: unformatted.html.erb") const formattedContent = await readFile(join(directory, "unformatted.html.erb"), "utf-8") expect(formattedContent).toBe("
\n

Not formatted

\n
\n") } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) it("should pass --check with a non-TTY stdin when all files are formatted", async () => { const directory = "test-non-tty-stdin-formatted" const input = '
\n

Already formatted

\n
\n' await mkdir(directory, { recursive: true }) await writeFile(join(directory, "formatted.html.erb"), input) try { const result = await execBinary(["--check"], undefined, { cwd: directory, stdin: "pipe" }) expectExitCode(result, 0) expect(result.stdout).toContain("clean") } finally { await rm(directory, { recursive: true }).catch(() => {}) } }) it("should pass --check when file is already formatted", async () => { const testFile = "test-formatted.html.erb" const input = '
\n

Already formatted

\n
\n' await writeFile(testFile, input) try { const result = await execBinary(["--check", testFile]) expectExitCode(result, 0) expect(result.stdout).toContain("clean") } finally { await unlink(testFile).catch(() => {}) } }) it("should fail --check when file is not formatted", async () => { const testFile = "test-unformatted.html.erb" const input = '

Not formatted

' await writeFile(testFile, input) try { const result = await execBinary(["--check", testFile]) expectExitCode(result, 1) expect(result.stdout).toContain("not formatted") } finally { await unlink(testFile).catch(() => {}) } }) it("should handle --check with directory containing mixed files", async () => { await mkdir("test-dir", { recursive: true }) const formattedFile = join("test-dir", "formatted.html.erb") const unformattedFile = join("test-dir", "unformatted.html.erb") await writeFile(formattedFile, '
\n

Formatted

\n
\n') await writeFile(unformattedFile, '

Unformatted

') try { const result = await execBinary(["--check", "test-dir"]) expectExitCode(result, 1) expect(result.stdout).toContain("The following") expect(result.stdout).toContain("not formatted") expect(result.stdout).toContain("unformatted.html.erb") expect(result.stdout).toContain("1 unformatted") } finally { await rm("test-dir", { recursive: true }).catch(() => {}) } }) it("CLI output should end with trailing newline", async () => { const input = '
Hello
' const result = await execBinary([], input) expectExitCode(result, 0) expect(result.stdout.endsWith('\n')).toBe(true) expect(result.stderr).toContain("⚠️ Experimental Preview") expect(result.stdout).toBe('
Hello
\n') }) it("CLI should preserve existing trailing newline", async () => { const input = '
Hello
\n' const result = await execBinary([], input) expectExitCode(result, 0) expect(result.stdout.endsWith('\n')).toBe(true) expect(result.stderr).toContain("⚠️ Experimental Preview") expect(result.stdout).toBe('
Hello
\n') }) it("CLI should add trailing newline to empty input", async () => { const input = '' const result = await execBinary([], input) expectExitCode(result, 0) expect(result.stderr).toContain("⚠️ Experimental Preview") expect(result.stdout).toBe('\n') }) it("should show --indent-width option in help", async () => { const result = await execBinary(["--help"]) expectExitCode(result, 0) expect(result.stdout).toContain("herb-format --indent-width") expect(result.stdout).toContain("number of spaces per indentation level") }) it("should accept valid --indent-width", async () => { const input = '
\n

Test

\n
' const result = await execBinary(["--indent-width", "4"], input) expectExitCode(result, 0) expect(result.stdout).toContain("

Test

") // 4 spaces instead of 2 }) it("should show --indent-style option in help", async () => { const result = await execBinary(["--help"]) expectExitCode(result, 0) expect(result.stdout).toContain("herb-format --indent-style") expect(result.stdout).toContain("character used for indentation") }) it("should accept valid --indent-style", async () => { const input = '
\n

Test

\n
' const result = await execBinary(["--indent-style", "tab"], input) expectExitCode(result, 0) expect(result.stdout).toContain("\t

Test

") }) it("should reject invalid --indent-style", async () => { const result = await execBinary(["--indent-style", "nope"], '
') expectExitCode(result, 1) expect(result.stderr).toContain("Invalid indent-style") }) it("should show --max-line-length option in help", async () => { const result = await execBinary(["--help"]) expectExitCode(result, 0) expect(result.stdout).toContain("herb-format --max-line-length") expect(result.stdout).toContain("maximum line length before wrapping") }) it("should accept valid --max-line-length", async () => { const input = '

Short text that wraps

' const result = await execBinary(["--max-line-length", "20"], input) expectExitCode(result, 0) expect(result.stdout).toContain("Short text that") expect(result.stdout).toContain("wraps") }) describe("Glob Pattern Support", () => { beforeEach(async () => { await mkdir("test-fixtures", { recursive: true }) await mkdir("test-fixtures/nested", { recursive: true }) }) afterEach(async () => { await rm("test-fixtures", { recursive: true }).catch(() => {}) }) it("should format single .xml.erb file", async () => { const content = '\n<% if true %><% end %>' const expectedContent = dedent` <% if true %> <% end %> ` await writeFile("test-fixtures/test.xml.erb", content) const result = await execBinary(["--force", "test-fixtures/test.xml.erb"]) expectExitCode(result, 0) expect(result.stdout).toContain("Formatted: test-fixtures/test.xml.erb") const actualContent = await readFile("test-fixtures/test.xml.erb", "utf-8") expect(actualContent.trim()).toBe(expectedContent) }) it("should handle glob pattern for .xml.erb files", async () => { const content1 = '' const content2 = '' await writeFile("test-fixtures/file1.xml.erb", content1) await writeFile("test-fixtures/file2.xml.erb", content2) await writeFile("test-fixtures/ignored.html.erb", "
") const result = await execBinary(["--force", "test-fixtures/*.xml.erb"]) expectExitCode(result, 0) expect(result.stdout).toContain("Formatted: test-fixtures/file1.xml.erb") expect(result.stdout).toContain("Formatted: test-fixtures/file2.xml.erb") expect(result.stdout).not.toContain("ignored.html.erb") expect(result.stdout).toContain("2 formatted") }) it("should handle recursive glob pattern", async () => { const content = '' await writeFile("test-fixtures/top.xml.erb", content) await writeFile("test-fixtures/nested/deep.xml.erb", content) const result = await execBinary(["--force", "test-fixtures/**/*.xml.erb"]) expectExitCode(result, 0) expect(result.stdout).toContain("Formatted: test-fixtures/top.xml.erb") expect(result.stdout).toContain("Formatted: test-fixtures/nested/deep.xml.erb") expect(result.stdout).toContain("2 formatted") }) it("should handle mixed file extensions with glob", async () => { const xmlContent = '' const htmlContent = '

' await writeFile("test-fixtures/file.xml.erb", xmlContent) await writeFile("test-fixtures/file.html.erb", htmlContent) const result = await execBinary(["--force", "test-fixtures/*.erb"]) expectExitCode(result, 0) expect(result.stdout).toContain("Formatted: test-fixtures/file.xml.erb") expect(result.stdout).toContain("Formatted: test-fixtures/file.html.erb") expect(result.stdout).toContain("2 formatted") }) it("should handle glob with --check mode", async () => { const wellFormattedContent = dedent` <% if true %> <% end %> ` const poorlyFormattedContent = '<% if true %><% end %>' await writeFile("test-fixtures/good.xml.erb", wellFormattedContent + '\n') await writeFile("test-fixtures/bad.xml.erb", poorlyFormattedContent) const result = await execBinary(["--check", "--force", "test-fixtures/*.xml.erb"]) expectExitCode(result, 1) expect(result.stdout).toContain("The following") expect(result.stdout).toContain("not formatted") expect(result.stdout).toContain("bad.xml.erb") expect(result.stdout).not.toContain("good.xml.erb") expect(result.stdout).toContain("1 unformatted") }) it("should handle no files matching glob pattern", async () => { const result = await execBinary(["test-fixtures/*.nonexistent"]) expectExitCode(result, 0) expect(result.stdout).toContain("No files found matching pattern") expect(result.stdout).toContain("test-fixtures/*.nonexistent") }) it("should error for non-existent specific file", async () => { const result = await execBinary(["test-fixtures/nonexistent.xml.erb"]) expectExitCode(result, 1) expect(result.stderr).toContain("Error: Cannot access 'test-fixtures/nonexistent.xml.erb'") }) it("should handle relative path glob patterns", async () => { const content = '' await writeFile("test-fixtures/test.xml.erb", content) const result = await execBinary(["--force", "./test-fixtures/*.xml.erb"]) expectExitCode(result, 0) expect(result.stdout).toContain("Formatted: test-fixtures/test.xml.erb") expect(result.stdout).toContain("1 formatted") }) }) describe("Advanced CLI Patterns", () => { beforeEach(async () => { await mkdir("test-advanced", { recursive: true }) await mkdir("test-advanced/sub1", { recursive: true }) await mkdir("test-advanced/sub2", { recursive: true }) }) afterEach(async () => { await rm("test-advanced", { recursive: true }).catch(() => {}) }) it("should handle complex nested glob patterns", async () => { const content = '' await writeFile("test-advanced/root.xml.erb", content) await writeFile("test-advanced/sub1/file1.xml.erb", content) await writeFile("test-advanced/sub1/file2.xml.erb", content) await writeFile("test-advanced/sub2/file3.xml.erb", content) await writeFile("test-advanced/sub2/ignore.html.erb", "
") const result = await execBinary(["--force", "test-advanced/**/file*.xml.erb"]) expectExitCode(result, 0) expect(result.stdout).toContain("Formatted: test-advanced/sub1/file1.xml.erb") expect(result.stdout).toContain("Formatted: test-advanced/sub1/file2.xml.erb") expect(result.stdout).toContain("Formatted: test-advanced/sub2/file3.xml.erb") expect(result.stdout).not.toContain("root.xml.erb") expect(result.stdout).not.toContain("ignore.html.erb") expect(result.stdout).toContain("3 formatted") }) it("should handle brace expansion patterns", async () => { const content = '' await writeFile("test-advanced/config.xml.erb", content) await writeFile("test-advanced/manifest.xml.erb", content) await writeFile("test-advanced/other.erb", content) const result = await execBinary(["--force", "test-advanced/{config,manifest}.xml.erb"]) expectExitCode(result, 0) expect(result.stdout).toContain("Formatted: test-advanced/config.xml.erb") expect(result.stdout).toContain("Formatted: test-advanced/manifest.xml.erb") expect(result.stdout).not.toContain("other.erb") expect(result.stdout).toContain("2 formatted") }) it("should handle directory argument with mixed file types", async () => { const xmlContent = '' const htmlContent = '

' await writeFile("test-advanced/file.xml.erb", xmlContent) await writeFile("test-advanced/page.html.erb", htmlContent) await writeFile("test-advanced/readme.txt", "plain text") const result = await execBinary(["test-advanced/"]) expectExitCode(result, 0) expect(result.stdout).toContain("Formatted: test-advanced/page.html.erb") expect(result.stdout).not.toContain("file.xml.erb") // Only .html.erb by default for directories expect(result.stdout).not.toContain("readme.txt") expect(result.stdout).toContain("1 formatted") }) it("should handle empty directory gracefully", async () => { const result = await execBinary(["test-advanced/"]) expectExitCode(result, 0) expect(result.stdout).toContain("No files found matching pattern") }) it("should handle mixed file arguments", async () => { const xmlContent = '' const htmlContent = '

' await writeFile("test-advanced/specific.xml.erb", xmlContent) await writeFile("test-advanced/another.html.erb", htmlContent) const result1 = await execBinary(["--force", "test-advanced/specific.xml.erb"]) expectExitCode(result1, 0) expect(result1.stdout).toContain("Formatted: test-advanced/specific.xml.erb") const result2 = await execBinary(["test-advanced/another.html.erb"]) expectExitCode(result2, 0) expect(result2.stdout).toContain("Formatted: test-advanced/another.html.erb") }) it("should preserve glob patterns in error messages", async () => { const result = await execBinary(["test-advanced/non-existent-*.xml.erb"]) expectExitCode(result, 0) expect(result.stdout).toContain("No files found matching pattern") expect(result.stdout).toContain("test-advanced/non-existent-*.xml.erb") }) it("should handle check mode with complex patterns", async () => { const formattedContent = dedent` ` const unformattedContent = '' await writeFile("test-advanced/good1.xml.erb", formattedContent + '\n') await writeFile("test-advanced/good2.xml.erb", formattedContent + '\n') await writeFile("test-advanced/sub1/bad.xml.erb", unformattedContent) const result = await execBinary(["--check", "--force", "test-advanced/**/*.xml.erb"]) expectExitCode(result, 1) expect(result.stdout).toContain("The following") expect(result.stdout).toContain("not formatted") expect(result.stdout).toContain("bad.xml.erb") expect(result.stdout).not.toContain("good1.xml.erb") expect(result.stdout).not.toContain("good2.xml.erb") expect(result.stdout).toContain("1 unformatted") }) }) describe("Multiple File Arguments", () => { beforeEach(async () => { await mkdir("test-multi", { recursive: true }) }) afterEach(async () => { await rm("test-multi", { recursive: true }).catch(() => {}) }) it("should format multiple files successfully", async () => { const unformattedContent = '

Test

' await writeFile("test-multi/file1.html.erb", unformattedContent) await writeFile("test-multi/file2.html.erb", unformattedContent) const result = await execBinary(["test-multi/file1.html.erb", "test-multi/file2.html.erb"]) expectExitCode(result, 0) expect(result.stdout).toContain("Formatted: test-multi/file1.html.erb") expect(result.stdout).toContain("Formatted: test-multi/file2.html.erb") expect(result.stdout).toContain("2 formatted") }) it("should handle multiple files with mixed formatting states", async () => { const formattedContent = '
\n

Test

\n
\n' const unformattedContent = '

Test

' await writeFile("test-multi/formatted.html.erb", formattedContent) await writeFile("test-multi/unformatted.html.erb", unformattedContent) const result = await execBinary(["test-multi/formatted.html.erb", "test-multi/unformatted.html.erb"]) expectExitCode(result, 0) expect(result.stdout).not.toContain("Formatted: test-multi/formatted.html.erb") expect(result.stdout).toContain("Formatted: test-multi/unformatted.html.erb") expect(result.stdout).toContain("1 formatted") }) it("should exit with error if one file doesn't exist", async () => { const content = '

Test

' await writeFile("test-multi/exists.html.erb", content) const result = await execBinary(["test-multi/exists.html.erb", "test-multi/nonexistent.html.erb"]) expectExitCode(result, 1) expect(result.stderr).toContain("Cannot access") expect(result.stderr).toContain("nonexistent.html.erb") }) it("should deduplicate files passed multiple times", async () => { const unformattedContent = '

Test

' await writeFile("test-multi/duplicate.html.erb", unformattedContent) const result = await execBinary(["test-multi/duplicate.html.erb", "test-multi/duplicate.html.erb"]) expectExitCode(result, 0) // Should only format once expect(result.stdout).toContain("1 formatted") }) it("should handle --check with multiple files", async () => { const formattedContent = '
\n

Test

\n
\n' const unformattedContent = '

Test

' await writeFile("test-multi/file1.html.erb", formattedContent) await writeFile("test-multi/file2.html.erb", unformattedContent) const result = await execBinary(["--check", "test-multi/file1.html.erb", "test-multi/file2.html.erb"]) expectExitCode(result, 1) expect(result.stdout).toContain("The following") expect(result.stdout).toContain("not formatted") expect(result.stdout).toContain("file2.html.erb") expect(result.stdout).not.toContain("file1.html.erb") expect(result.stdout).toContain("1 unformatted") }) it("should reject stdin mixed with file arguments", async () => { const content = '

Test

' await writeFile("test-multi/file.html.erb", content) const result = await execBinary(["-", "test-multi/file.html.erb"]) expectExitCode(result, 1) expect(result.stderr).toContain("Cannot mix stdin ('-') with file arguments") }) it("should handle mix of files and globs", async () => { const unformattedContent = '

Test

' await writeFile("test-multi/specific.html.erb", unformattedContent) await writeFile("test-multi/pattern1.xml.erb", '') await writeFile("test-multi/pattern2.xml.erb", '') const result = await execBinary(["--force", "test-multi/specific.html.erb", "test-multi/*.xml.erb"]) expectExitCode(result, 0) expect(result.stdout).toContain("Formatted: test-multi/specific.html.erb") expect(result.stdout).toContain("Formatted: test-multi/pattern1.xml.erb") expect(result.stdout).toContain("Formatted: test-multi/pattern2.xml.erb") expect(result.stdout).toContain("3 formatted") }) }) describe("Config Auto-Discovery", () => { const configContent = dedent` files: include: - "**/*.html.erb" formatter: enabled: true indentWidth: 4 maxLineLength: 120 ` beforeEach(async () => { await mkdir("test-config-discovery", { recursive: true }) await mkdir("test-config-discovery/subdir", { recursive: true }) await writeFile("test-config-discovery/.herb.yml", configContent) }) afterEach(async () => { await rm("test-config-discovery", { recursive: true }).catch(() => {}) }) it("should auto-discover config file in current directory", async () => { const testFile = "test-config-discovery/test.html.erb" const input = '

Test

' await writeFile(testFile, input) const result = await execBinary([testFile]) expectExitCode(result, 0) expect(result.stderr).toContain("Using Herb config file at") expect(result.stderr).toContain(".herb.yml") const formatted = await readFile(testFile, 'utf-8') expect(formatted).toContain('

Test

') expect(formatted).not.toMatch(/^\s{2}

/m) }) it("should auto-discover config file from subdirectory", async () => { const testFile = "test-config-discovery/subdir/nested.html.erb" const input = '

Nested

' await writeFile(testFile, input) const result = await execBinary([testFile]) expectExitCode(result, 0) expect(result.stderr).toContain("Using Herb config file at") expect(result.stderr).toContain(".herb.yml") const formatted = await readFile(testFile, 'utf-8') expect(formatted).toContain('

Nested

') }) it("should use config maxLineLength setting", async () => { const testFile = "test-config-discovery/long-line.html.erb" const input = '

This is a pretty long line of text that is exactly one hundred and fifteen characters long

' await writeFile(testFile, input) const result = await execBinary([testFile]) expectExitCode(result, 0) const formatted = await readFile(testFile, 'utf-8') expect(formatted).toContain('This is a pretty long line') }) it("should override config with CLI flags", async () => { const testFile = "test-config-discovery/override.html.erb" const input = '

Test

' await writeFile(testFile, input) const result = await execBinary([testFile, '--indent-width', '2']) expectExitCode(result, 0) const formatted = await readFile(testFile, 'utf-8') expect(formatted).toContain('

Test

') expect(formatted).not.toMatch(/^\s{4}

/m) }) it("should work without config file (use defaults)", async () => { await mkdir("test-no-config", { recursive: true }) const testFile = "test-no-config/test.html.erb" const input = '

Test

' await writeFile(testFile, input) const result = await execBinary([testFile]) expectExitCode(result, 0) expect(result.stderr).not.toContain("Using Herb config file") const formatted = await readFile(testFile, 'utf-8') expect(formatted).toContain('

Test

') await rm("test-no-config", { recursive: true }) }) it("should honor explicit --config-file flag", async () => { await mkdir("test-explicit-config", { recursive: true }) const altConfigContent = dedent` files: include: - "**/*.html.erb" formatter: enabled: true indentWidth: 8 ` await writeFile("test-explicit-config/.herb.yml", altConfigContent) const testFile = "test-explicit-config/explicit.html.erb" const input = '

Test

' await writeFile(testFile, input) const result = await execBinary([testFile, '--config-file', 'test-explicit-config/.herb.yml']) expectExitCode(result, 0) expect(result.stderr).toContain("Using Herb config file") const formatted = await readFile(testFile, 'utf-8') expect(formatted).toContain('

Test

') await rm("test-explicit-config", { recursive: true }) }) it("should format multiple files with config from project root", async () => { const file1 = "test-config-discovery/file1.html.erb" const file2 = "test-config-discovery/subdir/file2.html.erb" const input = '

Test

' await writeFile(file1, input) await writeFile(file2, input) const result = await execBinary([file1, file2]) expectExitCode(result, 0) expect(result.stderr).toContain("Using Herb config file") expect(result.stdout).toContain("2 formatted") const formatted1 = await readFile(file1, 'utf-8') const formatted2 = await readFile(file2, 'utf-8') expect(formatted1).toContain('

Test

') expect(formatted2).toContain('

Test

') }) }) describe("Config Auto-Discovery Regression Tests (Issue #1177)", () => { const configContent = dedent` files: include: - "**/*.html.erb" formatter: enabled: true indentWidth: 3 maxLineLength: 100 ` beforeEach(async () => { await mkdir("test-issue-1177", { recursive: true }) await mkdir("test-issue-1177/deep/nested/path", { recursive: true }) await writeFile("test-issue-1177/.herb.yml", configContent) }) afterEach(async () => { await rm("test-issue-1177", { recursive: true }).catch(() => {}) }) it("should auto-discover config when formatting a single file in project root", async () => { const testFile = "test-issue-1177/index.html.erb" const input = '

Test

' await writeFile(testFile, input) const result = await execBinary([testFile]) expectExitCode(result, 0) expect(result.stderr).toContain("Using Herb config file") expect(result.stderr).toContain("test-issue-1177/.herb.yml") const formatted = await readFile(testFile, 'utf-8') expect(formatted).toContain('

Test

') expect(formatted).toMatch(/^\s{3}

/m) }) it("should auto-discover config when formatting a deeply nested file", async () => { const testFile = "test-issue-1177/deep/nested/path/deep.html.erb" const input = '

Deep nested file

' await writeFile(testFile, input) const result = await execBinary([testFile]) expectExitCode(result, 0) expect(result.stderr).toContain("Using Herb config file") expect(result.stderr).toContain("test-issue-1177/.herb.yml") const formatted = await readFile(testFile, 'utf-8') expect(formatted).toContain('

Deep nested file

') }) it("should use config settings without requiring --config-file flag", async () => { const testFile = "test-issue-1177/test.html.erb" const input = '

This is a line that is exactly ninety five characters long for testing config

' await writeFile(testFile, input) const result = await execBinary([testFile]) expectExitCode(result, 0) expect(result.stderr).toContain("Using Herb config file") const formatted = await readFile(testFile, 'utf-8') expect(formatted).toContain('ninety five characters long for testing config') expect(formatted).toContain('

') }) it("should consistently use config across multiple file arguments", async () => { const file1 = "test-issue-1177/file1.html.erb" const file2 = "test-issue-1177/deep/file2.html.erb" const file3 = "test-issue-1177/deep/nested/file3.html.erb" const input = '

Test

' await writeFile(file1, input) await writeFile(file2, input) await writeFile(file3, input) const result = await execBinary([file1, file2, file3]) expectExitCode(result, 0) expect(result.stderr).toContain("Using Herb config file") expect(result.stdout).toContain("3 files") const formatted1 = await readFile(file1, 'utf-8') const formatted2 = await readFile(file2, 'utf-8') const formatted3 = await readFile(file3, 'utf-8') expect(formatted1).toContain('

Test

') expect(formatted2).toContain('

Test

') expect(formatted3).toContain('

Test

') }) it("should respect config when using glob patterns", async () => { await writeFile("test-issue-1177/a.html.erb", '

A

') await writeFile("test-issue-1177/b.html.erb", '

B

') await writeFile("test-issue-1177/deep/c.html.erb", '

C

') const result = await execBinary(["test-issue-1177/**/*.html.erb"]) expectExitCode(result, 0) expect(result.stderr).toContain("Using Herb config file") const formattedA = await readFile("test-issue-1177/a.html.erb", 'utf-8') const formattedB = await readFile("test-issue-1177/b.html.erb", 'utf-8') const formattedC = await readFile("test-issue-1177/deep/c.html.erb", 'utf-8') expect(formattedA).toContain('

A

') expect(formattedB).toContain('

B

') expect(formattedC).toContain('

C

') }) it("should use config in --check mode", async () => { const testFile = "test-issue-1177/check.html.erb" const alreadyFormatted = '
\n

Test

\n
\n' await writeFile(testFile, alreadyFormatted) const result = await execBinary(["--check", testFile]) expectExitCode(result, 0) expect(result.stderr).toContain("Using Herb config file") expect(result.stdout).toContain("clean") }) it("should detect unformatted files correctly with config settings", async () => { const testFile = "test-issue-1177/unformatted.html.erb" const wrongFormatting = '
\n

Test

\n
\n' await writeFile(testFile, wrongFormatting) const result = await execBinary(["--check", testFile]) expectExitCode(result, 1) expect(result.stderr).toContain("Using Herb config file") expect(result.stdout).toContain("not formatted") expect(result.stdout).toContain("unformatted.html.erb") }) }) })