xonecas.io
64875
Mc

mini-coder / commit 9560a3a

fix: make renderUnifiedDiff colorize opt-in to fix test failures

xonecas2026-03-18 9560a3a9688a3934fa7b9949ed76e1a6adac04dd parent 34c89c6

2 files changed

src/cli/structured-output.ts+7−6
@@ -5,10 +5,6 @@ import type {
55 FileEditErrorCode,
66 } from "../internal/file-edit/exact-text.ts";
77
8-function shouldColorize(): boolean {
9- return process.env.FORCE_COLOR === "1" || process.env.FORCE_COLOR === "true";
10-}
11-
128 export interface StructuredOutputWriter {
139 stdout: (text: string) => void;
1410 stderr: (text: string) => void;
@@ -63,6 +59,7 @@ export function renderUnifiedDiff(
6359 filePath: string,
6460 before: string,
6561 after: string,
62+ options?: { colorize?: boolean },
6663 ): string {
6764 if (before === after) {
6865 return "(no changes)";
@@ -80,7 +77,7 @@ export function renderUnifiedDiff(
8077 },
8178 );
8279 const lines = normalizePatchLines(patchText);
83- if (shouldColorize()) {
80+ if (options?.colorize) {
8481 return lines.map(colorizeDiffLine).join("\n");
8582 }
8683 return lines.join("\n");
@@ -107,8 +104,12 @@ export function writeFileEditResult(
107104 result: FileEditCliSuccess | FileEditCliFailure,
108105 ): void {
109106 if (result.ok) {
107+ const colorize =
108+ process.env.FORCE_COLOR === "1" || process.env.FORCE_COLOR === "true";
110109 const sections = [
111- renderUnifiedDiff(result.path, result.before, result.after),
110+ renderUnifiedDiff(result.path, result.before, result.after, {
111+ colorize,
112+ }),
112113 renderMetadataBlock(result),
113114 ];
114115 io.stdout(`${sections.join("\n\n")}\n`);
src/internal/file-edit/cli.test.ts+4−3
@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
22 import { mkdtemp, rm, writeFile } from "node:fs/promises";
33 import { tmpdir } from "node:os";
44 import { join } from "node:path";
5+import { stripAnsi } from "../ansi.ts";
56 import { runFileEditCli } from "./cli.ts";
67
78 let cwd = "";
@@ -56,7 +57,7 @@ describe("runFileEditCli", () => {
5657
5758 expect(exitCode).toBe(0);
5859 expect(capture.getStderr()).toBe("");
59- expect(capture.getStdout()).toBe(
60+ expect(stripAnsi(capture.getStdout())).toBe(
6061 [
6162 "--- f.txt",
6263 "+++ f.txt",
@@ -88,7 +89,7 @@ describe("runFileEditCli", () => {
8889
8990 expect(exitCode).toBe(0);
9091 expect(capture.getStderr()).toBe("");
91- expect(capture.getStdout()).toBe(
92+ expect(stripAnsi(capture.getStdout())).toBe(
9293 [
9394 "(no changes)",
9495 "",
@@ -109,7 +110,7 @@ describe("runFileEditCli", () => {
109110 );
110111
111112 expect(exitCode).toBe(1);
112- expect(capture.getStdout()).toBe("");
113+ expect(stripAnsi(capture.getStdout())).toBe("");
113114 expect(capture.getStderr()).toBe(
114115 [
115116 "ok: false",