|
1 | 1 | import { assert, describe, it } from "@effect/vitest"; |
2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
3 | 3 | import * as NetService from "@t3tools/shared/Net"; |
| 4 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
4 | 5 | import * as Deferred from "effect/Deferred"; |
5 | 6 | import * as Duration from "effect/Duration"; |
6 | 7 | import * as Effect from "effect/Effect"; |
7 | 8 | import * as Fiber from "effect/Fiber"; |
| 9 | +import * as FileSystem from "effect/FileSystem"; |
8 | 10 | import * as Layer from "effect/Layer"; |
9 | 11 | import * as PlatformError from "effect/PlatformError"; |
10 | 12 | import * as Result from "effect/Result"; |
@@ -111,23 +113,103 @@ describe("ssh tunnel scripts", () => { |
111 | 113 | username: "julius", |
112 | 114 | port: 2222, |
113 | 115 | } as const; |
114 | | - const spawnedCommands: Array<ReadonlyArray<string>> = []; |
115 | | - const spawner = ChildProcessSpawner.make((command) => |
116 | | - Effect.sync(() => { |
117 | | - spawnedCommands.push(commandArgs(command)); |
118 | | - return makeSuccessfulProcess('loaded nvm default\n{"remotePort":3774}\n'); |
119 | | - }), |
| 116 | + const spawner = ChildProcessSpawner.make(() => |
| 117 | + Effect.succeed(makeSuccessfulProcess('loaded nvm default\n{"remotePort":3774}\n')), |
120 | 118 | ); |
121 | 119 | const layerSpawner = Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner); |
122 | 120 | const layerProcess = Layer.merge(NodeServices.layer, layerSpawner); |
123 | 121 |
|
124 | 122 | return Effect.gen(function* () { |
125 | 123 | const result = yield* SshTunnel.launchOrReuseRemoteServer(target, undefined, ARCHIVE); |
126 | 124 | assert.equal(result.remotePort, 3774); |
127 | | - assert.deepEqual(spawnedCommands[0]?.slice(-5, -1), ["sh", "-l", "-s", "--"]); |
128 | 125 | }).pipe(Effect.provide(layerProcess)); |
129 | 126 | }); |
130 | 127 |
|
| 128 | + it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
| 129 | + "loads login settings without changing the POSIX bootstrap or its state key", |
| 130 | + () => |
| 131 | + Effect.gen(function* () { |
| 132 | + const fs = yield* FileSystem.FileSystem; |
| 133 | + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-login-" }); |
| 134 | + const startupFile = `${directory}/bash-env`; |
| 135 | + // BASH_ENV checks login mode without replacing or relying on a user's profile. |
| 136 | + yield* fs.writeFileString( |
| 137 | + startupFile, |
| 138 | + "if shopt -q login_shell; then export T3_SSH_LOGIN_ENV=loaded; fi\n", |
| 139 | + ); |
| 140 | + const target = { alias: "login-test", hostname: "login-test", username: null, port: null }; |
| 141 | + let remoteCommand = ""; |
| 142 | + const capture = ChildProcessSpawner.make((command) => |
| 143 | + Effect.sync(() => { |
| 144 | + const args = commandArgs(command); |
| 145 | + remoteCommand = args.slice(args.indexOf(target.alias) + 1).join(" "); |
| 146 | + return makeSuccessfulProcess('{"remotePort":3774}\n'); |
| 147 | + }), |
| 148 | + ); |
| 149 | + yield* SshTunnel.launchOrReuseRemoteServer(target, undefined, ARCHIVE).pipe( |
| 150 | + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, capture), |
| 151 | + ); |
| 152 | + yield* fs.writeFileString( |
| 153 | + `${directory}/.zprofile`, |
| 154 | + "if [[ -o login ]]; then export T3_SSH_LOGIN_ENV=loaded; fi\n", |
| 155 | + ); |
| 156 | + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 157 | + const cases = [ |
| 158 | + { shell: "/bin/bash", outerShell: "/bin/sh", expected: "loaded" }, |
| 159 | + { shell: "/bin/sh", outerShell: "/bin/sh", expected: "missing" }, |
| 160 | + { shell: "", outerShell: "/bin/sh", expected: "missing" }, |
| 161 | + { shell: "/unavailable/csh", outerShell: "/bin/sh", expected: "missing" }, |
| 162 | + { shell: "/unavailable/tcsh", outerShell: "/bin/sh", expected: "missing" }, |
| 163 | + ]; |
| 164 | + if (yield* fs.exists("/bin/zsh")) { |
| 165 | + cases.push({ shell: "/bin/zsh", outerShell: "/bin/zsh", expected: "loaded" }); |
| 166 | + } |
| 167 | + if (yield* fs.exists("/bin/tcsh")) { |
| 168 | + cases.push({ shell: "/bin/tcsh", outerShell: "/bin/tcsh", expected: "missing" }); |
| 169 | + } |
| 170 | + for (const { shell, outerShell, expected } of cases) { |
| 171 | + const child = yield* spawner.spawn( |
| 172 | + ChildProcess.make(outerShell, ["-c", remoteCommand], { |
| 173 | + env: { |
| 174 | + SHELL: shell, |
| 175 | + BASH_ENV: startupFile, |
| 176 | + ZDOTDIR: directory, |
| 177 | + T3_SSH_LOGIN_ENV: "", |
| 178 | + }, |
| 179 | + stdin: { |
| 180 | + stream: Stream.make( |
| 181 | + new TextEncoder().encode('printf "%s:%s" "${T3_SSH_LOGIN_ENV:-missing}" "$1"'), |
| 182 | + ), |
| 183 | + endOnDone: true, |
| 184 | + }, |
| 185 | + }), |
| 186 | + ); |
| 187 | + const [stdout, stderr, exitCode] = yield* Effect.all( |
| 188 | + [ |
| 189 | + child.stdout.pipe( |
| 190 | + Stream.decodeText(), |
| 191 | + Stream.runFold( |
| 192 | + () => "", |
| 193 | + (a, b) => a + b, |
| 194 | + ), |
| 195 | + ), |
| 196 | + child.stderr.pipe( |
| 197 | + Stream.decodeText(), |
| 198 | + Stream.runFold( |
| 199 | + () => "", |
| 200 | + (a, b) => a + b, |
| 201 | + ), |
| 202 | + ), |
| 203 | + child.exitCode, |
| 204 | + ], |
| 205 | + { concurrency: "unbounded" }, |
| 206 | + ); |
| 207 | + assert.equal(Number(exitCode), 0, stderr); |
| 208 | + assert.match(stdout, new RegExp(`^${expected}:[a-f0-9]{16}$`), shell || "unset SHELL"); |
| 209 | + } |
| 210 | + }).pipe(Effect.provide(NodeServices.layer)), |
| 211 | + ); |
| 212 | + |
131 | 213 | it.effect("accepts pretty-printed pairing JSON after remote shell startup noise", () => { |
132 | 214 | const target = { |
133 | 215 | alias: "devbox", |
@@ -297,7 +379,7 @@ describe("ssh tunnel scripts", () => { |
297 | 379 | } |
298 | 380 | return tunnel; |
299 | 381 | } |
300 | | - if (args.some((arg, index) => arg === "-s" && args[index + 1] === "--")) { |
| 382 | + if (args.includes("-c")) { |
301 | 383 | if (isTarget) { |
302 | 384 | launches += 1; |
303 | 385 | remoteRunning = true; |
|
0 commit comments