Skip to content

Commit e56214a

Browse files
refactor(ssh): test known-host discovery through its service
1 parent 60498e7 commit e56214a

2 files changed

Lines changed: 28 additions & 28 deletions

File tree

‎packages/ssh/src/config.test.ts‎

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import * as Effect from "effect/Effect";
55
import * as FileSystem from "effect/FileSystem";
66
import * as Path from "effect/Path";
77

8-
import {
9-
discoverSshHosts,
10-
parseKnownHostsHostnames,
11-
resolveSshConfigIncludePattern,
12-
} from "./config.ts";
8+
import { discoverSshHosts, resolveSshConfigIncludePattern } from "./config.ts";
139

1410
function makeTempHomeDir() {
1511
return Effect.gen(function* () {
@@ -132,32 +128,42 @@ describe("ssh config", () => {
132128
);
133129

134130
it.effect("parses known_hosts entries without returning hashed hosts", () =>
135-
Effect.sync(() => {
131+
Effect.gen(function* () {
132+
const fs = yield* FileSystem.FileSystem;
133+
const path = yield* Path.Path;
134+
const homeDir = yield* makeTempHomeDir();
135+
const sshDir = path.join(homeDir, ".ssh");
136+
yield* fs.makeDirectory(sshDir);
137+
yield* fs.writeFileString(
138+
path.join(sshDir, "known_hosts"),
139+
[
140+
"github.com ssh-ed25519 AAAA",
141+
"gitlab.com,gitlab-alias ssh-ed25519 BBBB",
142+
"|1|hashed|entry ssh-ed25519 CCCC",
143+
"@cert-authority authority.example.com,*.example.com ssh-ed25519 DDDD",
144+
"[ssh.example.com]:2200 ssh-ed25519 EEEE",
145+
"port.example.com:22 ssh-ed25519 HHHH",
146+
"::1 ssh-ed25519 FFFF",
147+
"2001:db8::1 ssh-ed25519 GGGG",
148+
"",
149+
].join("\n"),
150+
);
151+
152+
const hosts = yield* discoverSshHosts({ homeDir });
136153
assert.deepEqual(
137-
parseKnownHostsHostnames(
138-
[
139-
"github.com ssh-ed25519 AAAA",
140-
"gitlab.com,gitlab-alias ssh-ed25519 BBBB",
141-
"|1|hashed|entry ssh-ed25519 CCCC",
142-
"@cert-authority *.example.com ssh-ed25519 DDDD",
143-
"[ssh.example.com]:2200 ssh-ed25519 EEEE",
144-
"port.example.com:22 ssh-ed25519 HHHH",
145-
"::1 ssh-ed25519 FFFF",
146-
"2001:db8::1 ssh-ed25519 GGGG",
147-
"",
148-
].join("\n"),
149-
),
154+
hosts.map(({ alias }) => alias),
150155
[
151156
"::1",
152157
"2001:db8::1",
158+
"authority.example.com",
153159
"github.com",
154160
"gitlab-alias",
155161
"gitlab.com",
156-
"port.example.com",
157-
"ssh.example.com",
162+
"ssh://port.example.com:22",
163+
"ssh://ssh.example.com:2200",
158164
],
159165
);
160-
}),
166+
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
161167
);
162168

163169
it.effect("expands tilde-prefixed ssh config include patterns", () =>

‎packages/ssh/src/config.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,6 @@ function parseKnownHostsTargets(raw: string): ReadonlyArray<DesktopDiscoveredSsh
191191
return [...targets.values()].toSorted((left, right) => left.alias.localeCompare(right.alias));
192192
}
193193

194-
export function parseKnownHostsHostnames(raw: string): ReadonlyArray<string> {
195-
return [...new Set(parseKnownHostsTargets(raw).map((target) => target.hostname))].toSorted(
196-
(left, right) => left.localeCompare(right),
197-
);
198-
}
199-
200194
const readKnownHostsTargets = Effect.fnUntraced(function* (filePath: string) {
201195
const fs = yield* FileSystem.FileSystem;
202196
if (!(yield* fs.exists(filePath))) return [];

0 commit comments

Comments
 (0)