Skip to content
Merged
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
73 changes: 73 additions & 0 deletions apps/server/src/vcs/GitVcsDriverCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,79 @@ const makeFailedHandle = (stderr: string) =>
stderr: Stream.encodeText(Stream.make(stderr)),
});

describe("Git output collection", () => {
// Git writes in arbitrary chunk sizes; one byte per chunk splits every
// multibyte character across reads.
const byteChunks = (text: string) =>
Stream.fromIterable(Array.from(new TextEncoder().encode(text), (byte) => Uint8Array.of(byte)));
const makeDriver = (output: { stdout?: string; stderr?: string }) =>
makeGitVcsDriverCore().pipe(
Effect.provideService(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make(() =>
Effect.succeed(
ChildProcessSpawner.makeHandle({
...makeSuccessfulHandle(""),
stdout: byteChunks(output.stdout ?? ""),
stderr: byteChunks(output.stderr ?? ""),
}),
),
),
),
);

it.effect("returns the same split UTF-8 and NUL output with and without line callbacks", () =>
Effect.gen(function* () {
const stdout = "a\\b\0雪\nlast ";
const stderr = "Receiving é\r\nUpdating 雪\rdone";
const driver = yield* makeDriver({ stdout, stderr });
const input = { operation: "test.chunkedGitOutput", cwd: "/repo", args: ["status"] };
const expected = {
exitCode: 0,
stdout,
stderr,
stdoutTruncated: false,
stderrTruncated: false,
};

assert.deepEqual(yield* driver.execute(input), expected);

const stdoutLines: string[] = [];
const stderrLines: string[] = [];
const withCallbacks = yield* driver.execute({
...input,
progress: {
onStdoutLine: (line) => Effect.sync(() => void stdoutLines.push(line)),
onStderrLine: (line) => Effect.sync(() => void stderrLines.push(line)),
},
});
assert.deepEqual(withCallbacks, expected);
assert.deepEqual(stdoutLines, ["a\\b\0雪", "last "]);
assert.deepEqual(stderrLines, ["Receiving é", "Updating 雪", "done"]);
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
);

it.effect("caps output without line callbacks", () =>
Effect.gen(function* () {
const driver = yield* makeDriver({ stderr: "é雪\rdone" });
const input = {
operation: "test.cappedGitOutput",
cwd: "/repo",
args: ["status"],
maxOutputBytes: 3,
};

const truncated = yield* driver.execute({ ...input, appendTruncationMarker: true });
assert.equal(truncated.stderr, "é\uFFFD");
assert.isTrue(truncated.stderrTruncated);

const error = yield* driver.execute(input).pipe(Effect.flip);
assert.equal(error.outputLength, 4);
assert.include(error.detail, "exceeded 3 bytes");
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
);
});

const makeTmpDir = (
prefix = "git-vcs-driver-test-",
): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>
Expand Down
14 changes: 9 additions & 5 deletions apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,10 @@ const collectOutput = Effect.fnUntraced(function* (

const decoded = decoder.decode(chunkToDecode, { stream: !truncated });
text += decoded;
lineBuffer += lineDecoder ? lineDecoder.decode(chunk, { stream: true }) : decoded;
yield* emitCompleteLines(false);
if (onLine) {
lineBuffer += lineDecoder ? lineDecoder.decode(chunk, { stream: true }) : decoded;
yield* emitCompleteLines(false);
}
});

yield* Stream.runForEach(stream, processChunk).pipe(
Expand All @@ -917,9 +919,11 @@ const collectOutput = Effect.fnUntraced(function* (

const remainder = truncated ? "" : decoder.decode();
text += remainder;
lineBuffer += remainder;
if (lineDecoder) lineBuffer += lineDecoder.decode();
yield* emitCompleteLines(true);
if (onLine) {
lineBuffer += remainder;
if (lineDecoder) lineBuffer += lineDecoder.decode();
yield* emitCompleteLines(true);
}
return {
text,
truncated,
Expand Down
Loading