History

Use typed exit codes in VCS process results

- Switch VCS process and git driver results to `ChildProcessSpawner.ExitCode` - Update status checks and fallback results to use the new typed exit code - Refresh tests to match the new result shape

Julius Marminge committed 6 months ago

Commit 0587d8a1 parent 68df27e9 files · +62 −41Browse files

Unified · Split

9 files
  1. apps/server/src/git/GitManager.test.ts +6 −2
  2. apps/server/src/server.test.ts +2 −1
  3. apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts +2 −3
  4. apps/server/src/sourceControl/SourceControlProviderRegistry.ts +1 −1
  5. apps/server/src/vcs/GitVcsDriver.test.ts +2 −1
  6. apps/server/src/vcs/GitVcsDriver.ts +2 −1
  7. apps/server/src/vcs/GitVcsDriverCore.ts +44 −29
  8. apps/server/src/vcs/VcsDriverRegistry.test.ts +2 −1
  9. apps/server/src/vcs/VcsProcess.ts +1 −2
apps/server/src/git/GitManager.test.ts+6−2
@@ -211,7 +211,11 @@ function runGit( · 210 unmodified lines
211211 args: readonly string[],
212212 allowNonZeroExit = false,
213213 ): Effect.Effect<
214− { readonly code: number; readonly stdout: string; readonly stderr: string },
214+ {
215+ readonly exitCode: GitVcsDriver.ExecuteGitResult["exitCode"];
216+ readonly stdout: string;
217+ readonly stderr: string;
218+ },
215219 GitCommandError,
216220 GitVcsDriver.GitVcsDriver
217221 > {
@@ -224,7 +228,7 @@ function runGit( · 6 unmodified lines
224228 allowNonZeroExit,
225229 });
226230 return {
227− code: result.code,
231+ exitCode: result.exitCode,
228232 stdout: result.stdout,
229233 stderr: result.stderr,
230234 };
apps/server/src/server.test.ts+2−1
@@ -39,6 +39,7 @@ import { · 38 unmodified lines
3939 Path,
4040 Stream,
4141 } from "effect";
42+import { ChildProcessSpawner } from "effect/unstable/process";
4243 import {
4344 FetchHttpClient,
4445 HttpBody,
@@ -384,7 +385,7 @@ const buildAppUnderTest = (options?: { · 339 unmodified lines
384385 },
385386 execute: () =>
386387 Effect.succeed({
387− exitCode: 0,
388+ exitCode: ChildProcessSpawner.ExitCode(0),
388389 stdout: "",
389390 stderr: "",
390391 stdoutTruncated: false,
apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts+2−3
@@ -1,5 +1,6 @@
11 import { assert, it } from "@effect/vitest";
22 import { Effect, Layer } from "effect";
3+import { ChildProcessSpawner } from "effect/unstable/process";
34
45 import { GitHubCli } from "./GitHubCli.ts";
56 import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
@@ -8,9 +9,7 @@ import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry. · 2 unmodified lines
89 const processResult = (stdout: string) => ({
910 stdout,
1011 stderr: "",
11− code: 0,
12− signal: null,
13− timedOut: false,
12+ exitCode: ChildProcessSpawner.ExitCode(0),
1413 stdoutTruncated: false,
1514 stderrTruncated: false,
1615 });
apps/server/src/sourceControl/SourceControlProviderRegistry.ts+1−1
@@ -89,7 +89,7 @@ export const make = Effect.fn("makeSourceControlProviderRegistry")(function* () · 88 unmodified lines
8989 })
9090 .pipe(
9191 Effect.map((result) =>
92− result.code === 0 ? firstRemoteUrlFromVerboseOutput(result.stdout) : null,
92+ result.exitCode === 0 ? firstRemoteUrlFromVerboseOutput(result.stdout) : null,
9393 ),
9494 Effect.mapError((error) => providerDetectionError("detectProvider", cwd, error)),
9595 ));
apps/server/src/vcs/GitVcsDriver.test.ts+2−1
@@ -1,5 +1,6 @@
11 import * as NodeServices from "@effect/platform-node/NodeServices";
22 import { Effect, FileSystem, Layer, Path, PlatformError } from "effect";
3+import { ChildProcessSpawner } from "effect/unstable/process";
34 import { assert, it } from "@effect/vitest";
45
56 import { GitCommandError } from "@t3tools/contracts";
@@ -85,7 +86,7 @@ it.effect("GitVcsDriver forwards execute env to the VCS process", () => { · 79 unmodified lines
8586 Effect.sync(() => {
8687 observedEnv = input.env;
8788 return {
88− exitCode: 0,
89+ exitCode: ChildProcessSpawner.ExitCode(0),
8990 stdout: "",
9091 stderr: "",
9192 stdoutTruncated: false,
apps/server/src/vcs/GitVcsDriver.ts+2−1
@@ -1,4 +1,5 @@
11 import { Context, DateTime, Effect, Layer, Option } from "effect";
2+import { ChildProcessSpawner } from "effect/unstable/process";
23
34 import {
45 GitCommandError,
@@ -35,7 +36,7 @@ export interface ExecuteGitInput { · 30 unmodified lines
3536 }
3637
3738 export interface ExecuteGitResult {
38− readonly code: number;
39+ readonly exitCode: ChildProcessSpawner.ExitCode;
3940 readonly stdout: string;
4041 readonly stderr: string;
4142 readonly stdoutTruncated: boolean;
apps/server/src/vcs/GitVcsDriverCore.ts+44−29
@@ -448,7 +448,7 @@ const createTrace2Monitor = Effect.fn("createTrace2Monitor")(function* ( · 447 unmodified lines
448448
449449 if (event === "child_exit") {
450450 hookStartByChildKey.delete(childKey);
451− const code = traceRecord.success.code;
451+ const code = traceRecord.success.exitCode;
452452 const exitCode = typeof code === "number" && Number.isInteger(code) ? code : null;
453453 const now = yield* DateTime.now;
454454 const durationMs = started
@@ -666,7 +666,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 211 unmodified lines
666666 input.progress?.onStderrLine,
667667 ),
668668 child.exitCode.pipe(
669− Effect.map((value) => Number(value)),
670669 Effect.mapError(toGitCommandError(commandInput, "failed to report exit code.")),
671670 ),
672671 input.stdin === undefined
@@ -693,7 +692,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 20 unmodified lines
693692 }
694693
695694 return {
696− code: exitCode,
695+ exitCode,
697696 stdout: stdout.text,
698697 stderr: stderr.text,
699698 stdoutTruncated: stdout.truncated,
@@ -761,7 +760,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 61 unmodified lines
761760 ...(options.progress ? { progress: options.progress } : {}),
762761 }).pipe(
763762 Effect.flatMap((result) => {
764− if (options.allowNonZeroExit || result.code === 0) {
763+ if (options.allowNonZeroExit || result.exitCode === 0) {
765764 return Effect.succeed(result);
766765 }
767766 const stderr = result.stderr.trim();
@@ -778,7 +777,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 10 unmodified lines
778777 operation,
779778 cwd,
780779 args,
781− `${commandLabel(args)} failed: code=${result.code ?? "null"}`,
780+ `${commandLabel(args)} failed: code=${result.exitCode ?? "null"}`,
782781 ),
783782 );
784783 }),
@@ -823,7 +822,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 38 unmodified lines
823822 allowNonZeroExit: true,
824823 timeoutMs: 5_000,
825824 },
826− ).pipe(Effect.map((result) => result.code === 0));
825+ ).pipe(Effect.map((result) => result.exitCode === 0));
827826
828827 const resolveAvailableBranchName = Effect.fn("resolveAvailableBranchName")(function* (
829828 cwd: string,
@@ -939,7 +938,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 109 unmodified lines
939938 { allowNonZeroExit: true },
940939 ).pipe(
941940 Effect.map((result) => {
942− if (result.code !== 0) {
941+ if (result.exitCode !== 0) {
943942 return null;
944943 }
945944 return parseDefaultBranchFromRemoteHeadRef(result.stdout, remoteName);
@@ -958,12 +957,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 12 unmodified lines
958957 {
959958 allowNonZeroExit: true,
960959 },
961− ).pipe(Effect.map((result) => result.code === 0));
960+ ).pipe(Effect.map((result) => result.exitCode === 0));
962961
963962 const originRemoteExists = (cwd: string): Effect.Effect<boolean, GitCommandError> =>
964963 executeGit("GitVcsDriver.originRemoteExists", cwd, ["remote", "get-url", "origin"], {
965964 allowNonZeroExit: true,
966− }).pipe(Effect.map((result) => result.code === 0));
965+ }).pipe(Effect.map((result) => result.exitCode === 0));
967966
968967 const listRemoteNames = (cwd: string): Effect.Effect<ReadonlyArray<string>, GitCommandError> =>
969968 runGitStdout("GitVcsDriver.listRemoteNames", cwd, ["remote"]).pipe(
@@ -1115,7 +1114,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 145 unmodified lines
11151114 ["rev-list", "--count", `${baseRef}..HEAD`],
11161115 { allowNonZeroExit: true },
11171116 );
1118− if (result.code !== 0) {
1117+ if (result.exitCode !== 0) {
11191118 return 0;
11201119 }
11211120
@@ -1140,7 +1139,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 18 unmodified lines
11401139 );
11411140
11421141 const branchLastCommit = new Map<string, number>();
1143− if (branchRecency.code !== 0) {
1142+ if (branchRecency.exitCode !== 0) {
11441143 return branchLastCommit;
11451144 }
11461145
@@ -1173,7 +1172,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 26 unmodified lines
11731172 return NON_REPOSITORY_STATUS_DETAILS;
11741173 }
11751174
1176− if (statusResult.code !== 0) {
1175+ if (statusResult.exitCode !== 0) {
11771176 const stderr = statusResult.stderr.trim();
11781177 return yield* createGitCommandError(
11791178 "GitVcsDriver.statusDetails.status",
@@ -1206,7 +1205,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 26 unmodified lines
12061205 );
12071206 const statusStdout = statusResult.stdout;
12081207 const defaultBranch =
1209− defaultRefResult.code === 0
1208+ defaultRefResult.exitCode === 0
12101209 ? defaultRefResult.stdout.trim().replace(/^refs\/remotes\/origin\//, "")
12111210 : null;
12121211
@@ -1614,7 +1613,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 401 unmodified lines
16141613 ).pipe(
16151614 Effect.catchIf(isMissingGitCwdError, () =>
16161615 Effect.succeed({
1617− code: 128,
1616+ exitCode: ChildProcessSpawner.ExitCode(128),
16181617 stdout: "",
16191618 stderr: "fatal: not a git repository",
16201619 stdoutTruncated: false,
@@ -1623,7 +1622,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 2 unmodified lines
16231622 ),
16241623 );
16251624
1626− if (localBranchResult.code !== 0) {
1625+ if (localBranchResult.exitCode !== 0) {
16271626 const stderr = localBranchResult.stderr.trim();
16281627 if (stderr.toLowerCase().includes("not a git repository")) {
16291628 return {
@@ -1654,7 +1653,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 24 unmodified lines
16541653 Effect.catch((error) =>
16551654 Effect.logWarning(
16561655 `GitVcsDriver.listRefs: remote refName lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote refName list.`,
1657− ).pipe(Effect.as({ code: 1, stdout: "", stderr: "" })),
1656+ ).pipe(
1657+ Effect.as({
1658+ exitCode: ChildProcessSpawner.ExitCode(1),
1659+ stdout: "",
1660+ stderr: "",
1661+ stdoutTruncated: false,
1662+ stderrTruncated: false,
1663+ } satisfies ExecuteGitResult),
1664+ ),
16581665 ),
16591666 );
16601667
@@ -1670,7 +1677,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 9 unmodified lines
16701677 Effect.catch((error) =>
16711678 Effect.logWarning(
16721679 `GitVcsDriver.listRefs: remote name lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote name list.`,
1673− ).pipe(Effect.as({ code: 1, stdout: "", stderr: "" })),
1680+ ).pipe(
1681+ Effect.as({
1682+ exitCode: ChildProcessSpawner.ExitCode(1),
1683+ stdout: "",
1684+ stderr: "",
1685+ stdoutTruncated: false,
1686+ stderrTruncated: false,
1687+ } satisfies ExecuteGitResult),
1688+ ),
16741689 ),
16751690 );
16761691
@@ -1703,25 +1718,25 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 26 unmodified lines
17031718 );
17041719
17051720 const remoteNames =
1706− remoteNamesResult.code === 0 ? parseRemoteNames(remoteNamesResult.stdout) : [];
1707− if (remoteBranchResult.code !== 0 && remoteBranchResult.stderr.trim().length > 0) {
1721+ remoteNamesResult.exitCode === 0 ? parseRemoteNames(remoteNamesResult.stdout) : [];
1722+ if (remoteBranchResult.exitCode !== 0 && remoteBranchResult.stderr.trim().length > 0) {
17081723 yield* Effect.logWarning(
1709− `GitVcsDriver.listRefs: remote refName lookup returned code ${remoteBranchResult.code} for ${input.cwd}: ${remoteBranchResult.stderr.trim()}. Falling back to an empty remote refName list.`,
1724+ `GitVcsDriver.listRefs: remote refName lookup returned code ${remoteBranchResult.exitCode} for ${input.cwd}: ${remoteBranchResult.stderr.trim()}. Falling back to an empty remote refName list.`,
17101725 );
17111726 }
1712− if (remoteNamesResult.code !== 0 && remoteNamesResult.stderr.trim().length > 0) {
1727+ if (remoteNamesResult.exitCode !== 0 && remoteNamesResult.stderr.trim().length > 0) {
17131728 yield* Effect.logWarning(
1714− `GitVcsDriver.listRefs: remote name lookup returned code ${remoteNamesResult.code} for ${input.cwd}: ${remoteNamesResult.stderr.trim()}. Falling back to an empty remote name list.`,
1729+ `GitVcsDriver.listRefs: remote name lookup returned code ${remoteNamesResult.exitCode} for ${input.cwd}: ${remoteNamesResult.stderr.trim()}. Falling back to an empty remote name list.`,
17151730 );
17161731 }
17171732
17181733 const defaultBranch =
1719− defaultRef.code === 0
1734+ defaultRef.exitCode === 0
17201735 ? defaultRef.stdout.trim().replace(/^refs\/remotes\/origin\//, "")
17211736 : null;
17221737
17231738 const worktreeMap = new Map<string, string>();
1724− if (worktreeList.code === 0) {
1739+ if (worktreeList.exitCode === 0) {
17251740 let currentPath: string | null = null;
17261741 for (const line of worktreeList.stdout.split("\n")) {
17271742 if (line.startsWith("worktree ")) {
@@ -1762,7 +1777,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 34 unmodified lines
17621777 });
17631778
17641779 const remoteBranches =
1765− remoteBranchResult.code === 0
1780+ remoteBranchResult.exitCode === 0
17661781 ? remoteBranchResult.stdout
17671782 .split("\n")
17681783 .map(parseBranchLine)
@@ -1943,7 +1958,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 174 unmodified lines
19431958 timeoutMs: 5_000,
19441959 allowNonZeroExit: true,
19451960 },
1946− ).pipe(Effect.map((result) => result.code === 0)),
1961+ ).pipe(Effect.map((result) => result.exitCode === 0)),
19471962 executeGit(
19481963 "GitVcsDriver.switchRef.remoteExists",
19491964 input.cwd,
@@ -1952,7 +1967,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 2 unmodified lines
19521967 timeoutMs: 5_000,
19531968 allowNonZeroExit: true,
19541969 },
1955− ).pipe(Effect.map((result) => result.code === 0)),
1970+ ).pipe(Effect.map((result) => result.exitCode === 0)),
19561971 ],
19571972 { concurrency: "unbounded" },
19581973 );
@@ -1968,7 +1983,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 9 unmodified lines
19681983 },
19691984 ).pipe(
19701985 Effect.map((result) =>
1971− result.code === 0
1986+ result.exitCode === 0
19721987 ? parseTrackingBranchByUpstreamRef(result.stdout, input.refName)
19731988 : null,
19741989 ),
@@ -1986,7 +2001,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* · 11 unmodified lines
19862001 timeoutMs: 5_000,
19872002 allowNonZeroExit: true,
19882003 },
1989− ).pipe(Effect.map((result) => result.code === 0))
2004+ ).pipe(Effect.map((result) => result.exitCode === 0))
19902005 : false;
19912006
19922007 const checkoutArgs = localInputExists
apps/server/src/vcs/VcsDriverRegistry.test.ts+2−1
@@ -1,5 +1,6 @@
11 import { assert, it } from "@effect/vitest";
22 import { Effect, Layer } from "effect";
3+import { ChildProcessSpawner } from "effect/unstable/process";
34 import { describe } from "vitest";
45
56 import { VcsProcess, type VcsProcessInput, type VcsProcessOutput } from "./VcsProcess.ts";
@@ -7,7 +8,7 @@ import { VcsProjectConfig } from "./VcsProjectConfig.ts"; · 1 unmodified line
78 import { VcsDriverRegistry, make as makeVcsDriverRegistry } from "./VcsDriverRegistry.ts";
89
910 const processOutput = (stdout: string): VcsProcessOutput => ({
10− exitCode: 0,
11+ exitCode: ChildProcessSpawner.ExitCode(0),
1112 stdout,
1213 stderr: "",
1314 stdoutTruncated: false,
apps/server/src/vcs/VcsProcess.ts+1−2
@@ -23,7 +23,7 @@ export interface VcsProcessInput { · 22 unmodified lines
2323 }
2424
2525 export interface VcsProcessOutput {
26− readonly exitCode: number;
26+ readonly exitCode: ChildProcessSpawner.ExitCode;
2727 readonly stdout: string;
2828 readonly stderr: string;
2929 readonly stdoutTruncated: boolean;
@@ -147,7 +147,6 @@ export const make = Effect.fn("makeVcsProcess")(function* () { · 117 unmodified lines
147147 input.truncateOutputAtMaxBytes ?? false,
148148 ),
149149 child.exitCode.pipe(
150− Effect.map((value) => Number(value)),
151150 Effect.mapError(
152151 (cause) =>
153152 new VcsOutputDecodeError({

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t