@@ -36,6 +36,37 @@ describe('changeFile', () => {
3636 )
3737 } )
3838
39+ test ( 'accepts absolute paths inside the project for string replacements' , async ( ) => {
40+ const fs = createMockFs ( {
41+ files : {
42+ '/repo/src/file.ts' : 'const value = 1\n' ,
43+ } ,
44+ } )
45+
46+ const result = await changeFile ( {
47+ parameters : {
48+ type : 'patch' ,
49+ path : '/repo/src/file.ts' ,
50+ content : '@@ -1,1 +1,1 @@\n-const value = 1\n+const value = 2\n' ,
51+ } ,
52+ cwd : '/repo' ,
53+ fs,
54+ } )
55+
56+ expect ( result ) . toEqual ( [
57+ {
58+ type : 'json' ,
59+ value : {
60+ file : 'src/file.ts' ,
61+ message : 'String replace applied successfully.' ,
62+ } ,
63+ } ,
64+ ] )
65+ expect ( await fs . readFile ( '/repo/src/file.ts' , 'utf-8' ) ) . toBe (
66+ 'const value = 2\n' ,
67+ )
68+ } )
69+
3970 test ( 'returns a simple success message for new file writes' , async ( ) => {
4071 const fs = createMockFs ( )
4172
@@ -63,6 +94,58 @@ describe('changeFile', () => {
6394 )
6495 } )
6596
97+ test ( 'accepts absolute paths inside the project for file writes' , async ( ) => {
98+ const fs = createMockFs ( )
99+
100+ const result = await changeFile ( {
101+ parameters : {
102+ type : 'file' ,
103+ path : '/repo/src/file.ts' ,
104+ content : 'const value = 1\n' ,
105+ } ,
106+ cwd : '/repo' ,
107+ fs,
108+ } )
109+
110+ expect ( result ) . toEqual ( [
111+ {
112+ type : 'json' ,
113+ value : {
114+ file : 'src/file.ts' ,
115+ message : 'Created file successfully.' ,
116+ } ,
117+ } ,
118+ ] )
119+ expect ( await fs . readFile ( '/repo/src/file.ts' , 'utf-8' ) ) . toBe (
120+ 'const value = 1\n' ,
121+ )
122+ } )
123+
124+ test ( 'accepts paths whose file names start with two dots inside the project' , async ( ) => {
125+ const fs = createMockFs ( )
126+
127+ const result = await changeFile ( {
128+ parameters : {
129+ type : 'file' ,
130+ path : '/repo/..config' ,
131+ content : 'value = true\n' ,
132+ } ,
133+ cwd : '/repo' ,
134+ fs,
135+ } )
136+
137+ expect ( result ) . toEqual ( [
138+ {
139+ type : 'json' ,
140+ value : {
141+ file : '..config' ,
142+ message : 'Created file successfully.' ,
143+ } ,
144+ } ,
145+ ] )
146+ expect ( await fs . readFile ( '/repo/..config' , 'utf-8' ) ) . toBe ( 'value = true\n' )
147+ } )
148+
66149 test ( 'returns a simple success message for overwritten file writes' , async ( ) => {
67150 const fs = createMockFs ( {
68151 files : {
@@ -93,4 +176,20 @@ describe('changeFile', () => {
93176 'const value = 2\n' ,
94177 )
95178 } )
179+
180+ test ( 'rejects absolute paths outside the project' , async ( ) => {
181+ const fs = createMockFs ( )
182+
183+ await expect (
184+ changeFile ( {
185+ parameters : {
186+ type : 'file' ,
187+ path : '/outside/file.ts' ,
188+ content : 'const value = 1\n' ,
189+ } ,
190+ cwd : '/repo' ,
191+ fs,
192+ } ) ,
193+ ) . rejects . toThrow ( 'file path is outside the project directory' )
194+ } )
96195} )
0 commit comments