@@ -3,7 +3,9 @@ import { assert, it } from "@effect/vitest";
33import * as Effect from "effect/Effect" ;
44import * as Exit from "effect/Exit" ;
55import * as FileSystem from "effect/FileSystem" ;
6+ import * as Layer from "effect/Layer" ;
67import * as Path from "effect/Path" ;
8+ import * as PlatformError from "effect/PlatformError" ;
79
810import { writeFileStringAtomically } from "./atomicWrite.ts" ;
911
@@ -64,6 +66,29 @@ it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => {
6466 } ) ,
6567 ) ;
6668
69+ it . effect ( "resolves a relative link through a symlinked parent directory" , ( ) =>
70+ Effect . gen ( function * ( ) {
71+ const fs = yield * FileSystem . FileSystem ;
72+ const path = yield * Path . Path ;
73+ const root = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-atomic-write-" } ) ;
74+ const destination = path . join ( root , "dotfiles" , "config" , "settings.json" ) ;
75+ const linkedState = path . join ( root , "dotfiles" , "state" ) ;
76+ const home = path . join ( root , "home" ) ;
77+ const link = path . join ( home , "state" , "settings.json" ) ;
78+ yield * fs . makeDirectory ( path . dirname ( destination ) , { recursive : true } ) ;
79+ yield * fs . makeDirectory ( linkedState , { recursive : true } ) ;
80+ yield * fs . makeDirectory ( home , { recursive : true } ) ;
81+ yield * fs . symlink ( linkedState , path . join ( home , "state" ) ) ;
82+ yield * fs . writeFileString ( destination , "before" ) ;
83+ yield * fs . symlink ( "../config/settings.json" , link ) ;
84+
85+ yield * writeFileStringAtomically ( { filePath : link , contents : "after" } ) ;
86+
87+ assert . strictEqual ( yield * fs . readLink ( link ) , "../config/settings.json" ) ;
88+ assert . strictEqual ( yield * fs . readFileString ( destination ) , "after" ) ;
89+ } ) ,
90+ ) ;
91+
6792 it . effect ( "creates a missing file and its directory" , ( ) =>
6893 Effect . gen ( function * ( ) {
6994 const fs = yield * FileSystem . FileSystem ;
@@ -77,3 +102,38 @@ it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => {
77102 } ) ,
78103 ) ;
79104} ) ;
105+
106+ it . effect ( "surfaces an unreadable link instead of writing over it" , ( ) =>
107+ Effect . gen ( function * ( ) {
108+ const readLinkFailure = PlatformError . systemError ( {
109+ _tag : "Unknown" ,
110+ module : "FileSystem" ,
111+ method : "readLink" ,
112+ pathOrDescriptor : "/home/settings.json" ,
113+ } ) ;
114+
115+ const result = yield * Effect . exit (
116+ writeFileStringAtomically ( { filePath : "/home/settings.json" , contents : "after" } ) ,
117+ ) ;
118+
119+ assert . deepStrictEqual ( result , Exit . fail ( readLinkFailure ) ) ;
120+ } ) . pipe (
121+ Effect . provide (
122+ Layer . mergeAll (
123+ Path . layer ,
124+ FileSystem . layerNoop ( {
125+ readLink : ( ) =>
126+ Effect . fail (
127+ PlatformError . systemError ( {
128+ _tag : "Unknown" ,
129+ module : "FileSystem" ,
130+ method : "readLink" ,
131+ pathOrDescriptor : "/home/settings.json" ,
132+ } ) ,
133+ ) ,
134+ rename : ( ) => Effect . die ( "an unreadable link must not be replaced" ) ,
135+ } ) ,
136+ ) ,
137+ ) ,
138+ ) ,
139+ ) ;
0 commit comments