11import * as NodePath from "@effect/platform-node/NodePath" ;
22import * as NodeServices from "@effect/platform-node/NodeServices" ;
33import { assert , describe , it } from "@effect/vitest" ;
4+ import { HostProcessPlatform } from "@t3tools/shared/hostProcess" ;
45import * as Effect from "effect/Effect" ;
56import * as FileSystem from "effect/FileSystem" ;
67import * as Path from "effect/Path" ;
@@ -19,6 +20,43 @@ function makeTempHomeDir() {
1920}
2021
2122describe ( "ssh config" , ( ) => {
23+ const hostPlatform = HostProcessPlatform . defaultValue ( ) ;
24+ // Verified with OpenSSH 10.2's `ssh -G -F <config> <alias>`.
25+ for ( const { name, filename, quoted } of [
26+ { name : "unquoted" , filename : "plain.conf" , quoted : false } ,
27+ { name : "quoted" , filename : "quoted.conf" , quoted : true } ,
28+ { name : "space" , filename : "with space.conf" , quoted : true } ,
29+ { name : "equals" , filename : "with=equals.conf" , quoted : true } ,
30+ { name : "hash" , filename : "with#hash.conf" , quoted : true } ,
31+ { name : "escaped-quote" , filename : 'with"quote.conf' , quoted : true } ,
32+ ] ) {
33+ it . effect . skipIf ( name === "escaped-quote" && hostPlatform === "win32" ) (
34+ `discovers hosts from a ${ name } Include path` ,
35+ ( ) =>
36+ Effect . gen ( function * ( ) {
37+ const fs = yield * FileSystem . FileSystem ;
38+ const path = yield * Path . Path ;
39+ const homeDir = yield * makeTempHomeDir ( ) ;
40+ const sshDir = path . join ( homeDir , ".ssh" ) ;
41+ yield * fs . makeDirectory ( sshDir ) ;
42+ const includedPath = path . join ( homeDir , filename ) ;
43+ yield * fs . writeFileString ( includedPath , `Host ${ name } \n HostName ${ name } .example.com\n` ) ;
44+ const argument = quoted ? `"${ includedPath . replaceAll ( '"' , '\\"' ) } "` : includedPath ;
45+ yield * fs . writeFileString ( path . join ( sshDir , "config" ) , `Include ${ argument } # comment\n` ) ;
46+
47+ assert . deepEqual ( yield * discoverSshHosts ( { homeDir } ) , [
48+ {
49+ alias : name ,
50+ hostname : name ,
51+ username : null ,
52+ port : null ,
53+ source : "ssh-config" ,
54+ } ,
55+ ] ) ;
56+ } ) . pipe ( Effect . provide ( NodeServices . layer ) , Effect . scoped ) ,
57+ ) ;
58+ }
59+
2260 it . effect ( "discovers ssh config hosts across included files" , ( ) =>
2361 Effect . gen ( function * ( ) {
2462 const fs = yield * FileSystem . FileSystem ;
0 commit comments