@@ -23,13 +23,89 @@ describe('+ copySync() - prevent copying identical files and dirs', () => {
2323 it ( 'should return an error if src and dest are the same' , ( ) => {
2424 const fileSrc = path . join ( TEST_DIR , 'TEST_fs-extra_copy_sync' )
2525 const fileDest = path . join ( TEST_DIR , 'TEST_fs-extra_copy_sync' )
26+ fs . ensureFileSync ( fileSrc )
27+
2628 try {
2729 fs . copySync ( fileSrc , fileDest )
2830 } catch ( err ) {
2931 assert . strictEqual ( err . message , 'Source and destination must not be the same.' )
3032 }
3133 } )
3234
35+ describe ( 'dest with parent symlink' , ( ) => {
36+ describe ( 'first parent is symlink' , ( ) => {
37+ it ( 'should error when src is file' , ( ) => {
38+ const src = path . join ( TEST_DIR , 'a' , 'file.txt' )
39+ const dest = path . join ( TEST_DIR , 'b' , 'file.txt' )
40+ const srcParent = path . join ( TEST_DIR , 'a' )
41+ const destParent = path . join ( TEST_DIR , 'b' )
42+ fs . ensureFileSync ( src )
43+ fs . ensureSymlinkSync ( srcParent , destParent , 'dir' )
44+
45+ try {
46+ fs . copySync ( src , dest )
47+ } catch ( err ) {
48+ assert . strictEqual ( err . message , 'Source and destination must not be the same.' )
49+ } finally {
50+ assert ( fs . existsSync ( src ) )
51+ }
52+ } )
53+
54+ it ( 'should error when src is directory' , ( ) => {
55+ const src = path . join ( TEST_DIR , 'a' , 'foo' )
56+ const dest = path . join ( TEST_DIR , 'b' , 'foo' )
57+ const srcParent = path . join ( TEST_DIR , 'a' )
58+ const destParent = path . join ( TEST_DIR , 'b' )
59+ fs . ensureDirSync ( src )
60+ fs . ensureSymlinkSync ( srcParent , destParent , 'dir' )
61+
62+ try {
63+ fs . copySync ( src , dest )
64+ } catch ( err ) {
65+ assert . strictEqual ( err . message , 'Source and destination must not be the same.' )
66+ } finally {
67+ assert ( fs . existsSync ( src ) )
68+ }
69+ } )
70+ } )
71+
72+ describe ( 'nested dest' , ( ) => {
73+ it ( 'should error when src is file' , ( ) => {
74+ const src = path . join ( TEST_DIR , 'a' , 'dir' , 'file.txt' )
75+ const dest = path . join ( TEST_DIR , 'b' , 'dir' , 'file.txt' )
76+ const srcParent = path . join ( TEST_DIR , 'a' )
77+ const destParent = path . join ( TEST_DIR , 'b' )
78+ fs . ensureFileSync ( src )
79+ fs . ensureSymlinkSync ( srcParent , destParent , 'dir' )
80+
81+ try {
82+ fs . copySync ( src , dest )
83+ } catch ( err ) {
84+ assert . strictEqual ( err . message , 'Source and destination must not be the same.' )
85+ } finally {
86+ assert ( fs . existsSync ( src ) )
87+ }
88+ } )
89+
90+ it ( 'should error when src is directory' , ( ) => {
91+ const src = path . join ( TEST_DIR , 'a' , 'dir' , 'foo' )
92+ const dest = path . join ( TEST_DIR , 'b' , 'dir' , 'foo' )
93+ const srcParent = path . join ( TEST_DIR , 'a' )
94+ const destParent = path . join ( TEST_DIR , 'b' )
95+ fs . ensureDirSync ( src )
96+ fs . ensureSymlinkSync ( srcParent , destParent , 'dir' )
97+
98+ try {
99+ fs . copySync ( src , dest )
100+ } catch ( err ) {
101+ assert . strictEqual ( err . message , 'Source and destination must not be the same.' )
102+ } finally {
103+ assert ( fs . existsSync ( src ) )
104+ }
105+ } )
106+ } )
107+ } )
108+
33109 // src is directory:
34110 // src is regular, dest is symlink
35111 // src is symlink, dest is regular
@@ -90,7 +166,7 @@ describe('+ copySync() - prevent copying identical files and dirs', () => {
90166 } )
91167
92168 describe ( '>> when src and dest are symlinks that point to the exact same path' , ( ) => {
93- it ( 'should not copy and return ' , ( ) => {
169+ it ( 'should error src and dest are the same ' , ( ) => {
94170 src = path . join ( TEST_DIR , 'src' )
95171 fs . mkdirsSync ( src )
96172 const srcLink = path . join ( TEST_DIR , 'src_symlink' )
@@ -101,7 +177,11 @@ describe('+ copySync() - prevent copying identical files and dirs', () => {
101177 const srclenBefore = klawSync ( srcLink ) . length
102178 const destlenBefore = klawSync ( destLink ) . length
103179
104- fs . copySync ( srcLink , destLink )
180+ try {
181+ fs . copySync ( srcLink , destLink )
182+ } catch ( err ) {
183+ assert . strictEqual ( err . message , 'Source and destination must not be the same.' )
184+ }
105185
106186 const srclenAfter = klawSync ( srcLink ) . length
107187 assert . strictEqual ( srclenAfter , srclenBefore , 'src length should not change' )
@@ -146,8 +226,7 @@ describe('+ copySync() - prevent copying identical files and dirs', () => {
146226 describe ( `>> when src is a symlink that points to a regular dest` , ( ) => {
147227 it ( 'should throw error' , ( ) => {
148228 dest = path . join ( TEST_DIR , 'dest' , 'somefile.txt' )
149- fs . ensureFileSync ( dest )
150- fs . writeFileSync ( dest , 'some data' )
229+ fs . outputFileSync ( dest , 'some data' )
151230
152231 const srcLink = path . join ( TEST_DIR , 'src-symlink' )
153232 fs . symlinkSync ( dest , srcLink , 'file' )
@@ -164,18 +243,21 @@ describe('+ copySync() - prevent copying identical files and dirs', () => {
164243 } )
165244
166245 describe ( '>> when src and dest are symlinks that point to the exact same path' , ( ) => {
167- it ( 'should not copy and return ' , ( ) => {
246+ it ( 'should error src and dest are the same ' , ( ) => {
168247 src = path . join ( TEST_DIR , 'src' , 'srcfile.txt' )
169- fs . ensureFileSync ( src )
170- fs . writeFileSync ( src , 'src data' )
248+ fs . outputFileSync ( src , 'src data' )
171249
172250 const srcLink = path . join ( TEST_DIR , 'src_symlink' )
173251 fs . symlinkSync ( src , srcLink , 'file' )
174252
175253 const destLink = path . join ( TEST_DIR , 'dest_symlink' )
176254 fs . symlinkSync ( src , destLink , 'file' )
177255
178- fs . copySync ( srcLink , destLink )
256+ try {
257+ fs . copySync ( srcLink , destLink )
258+ } catch ( err ) {
259+ assert . strictEqual ( err . message , 'Source and destination must not be the same.' )
260+ }
179261
180262 const srcln = fs . readlinkSync ( srcLink )
181263 assert . strictEqual ( srcln , src )
0 commit comments