@@ -2,6 +2,7 @@ package move
22
33import (
44 "fmt"
5+ "io/fs"
56 "path/filepath"
67 "testing"
78
@@ -89,6 +90,68 @@ func TestCopyFolderWithTechnologyFiltering(t *testing.T) {
8990 })
9091}
9192
93+ func TestCopyByList (t * testing.T ) {
94+ dirs := []string {
95+ "./folder" ,
96+ "./folder/sub" ,
97+ "./folder/sub/child" ,
98+ }
99+ dirModes := []fs.FileMode {
100+ 0777 ,
101+ 0776 ,
102+ 0775 ,
103+ }
104+
105+ filesNames := []string {
106+ "f1.txt" ,
107+ "runtime" ,
108+ "log" ,
109+ }
110+ fileModes := []fs.FileMode {
111+ 0764 ,
112+ 0773 ,
113+ 0772 ,
114+ }
115+
116+ fs := afero.Afero {Fs : afero .NewMemMapFs ()}
117+ // create an FS where there are multiple sub dirs and files, each with their own file modes
118+ for i := range len (dirs ) {
119+ err := fs .Mkdir (dirs [i ], dirModes [i ])
120+ require .NoError (t , err )
121+ err = fs .WriteFile (filepath .Join (dirs [i ], filesNames [i ]), []byte (fmt .Sprintf ("%d" , i )), fileModes [i ])
122+ require .NoError (t , err )
123+ }
124+
125+ // reverse the list, so the longest path is the first
126+ fileList := []string {}
127+ for i := len (dirs ) - 1 ; i >= 0 ; i -- {
128+ fileList = append (fileList , filepath .Join (dirs [i ], filesNames [i ]))
129+ }
130+
131+ targetDir := "./target"
132+
133+ err := copyByList (testLog , fs , "./" , targetDir , fileList )
134+ require .NoError (t , err )
135+
136+ for i := range len (dirs ) {
137+ targetStat , err := fs .Stat (filepath .Join (targetDir , dirs [i ]))
138+ require .NoError (t , err )
139+ assert .Equal (t , dirModes [i ], targetStat .Mode ().Perm (), targetStat .Name ())
140+
141+ sourceStat , err := fs .Stat (dirs [i ])
142+ require .NoError (t , err )
143+ assert .Equal (t , sourceStat .Mode (), targetStat .Mode (), targetStat .Name ())
144+
145+ targetStat , err = fs .Stat (filepath .Join (targetDir , dirs [i ], filesNames [i ]))
146+ require .NoError (t , err )
147+ assert .Equal (t , fileModes [i ], targetStat .Mode ().Perm (), targetStat .Name ())
148+
149+ sourceStat , err = fs .Stat (filepath .Join (dirs [i ], filesNames [i ]))
150+ require .NoError (t , err )
151+ assert .Equal (t , sourceStat .Mode (), targetStat .Mode (), targetStat .Name ())
152+ }
153+ }
154+
92155func TestFilterFilesByTechnology (t * testing.T ) {
93156 fs := afero.Afero {Fs : afero .NewMemMapFs ()}
94157
@@ -111,25 +174,22 @@ func TestFilterFilesByTechnology(t *testing.T) {
111174 }
112175 }`
113176 _ = afero .WriteFile (fs , filepath .Join (sourceDir , "manifest.json" ), []byte (manifestContent ), 0644 )
114- _ = afero .WriteFile (fs , filepath .Join (sourceDir , "fileA1.txt" ), []byte ("a1 content" ), 0644 )
115- _ = afero .WriteFile (fs , filepath .Join (sourceDir , "fileA2.txt" ), []byte ("a2 content" ), 0644 )
116- _ = afero .WriteFile (fs , filepath .Join (sourceDir , "fileB1.txt" ), []byte ("b1 content" ), 0644 )
117177
118178 t .Run ("filter single technology" , func (t * testing.T ) {
119179 paths , err := filterFilesByTechnology (testLog , fs , sourceDir , []string {"java" })
120180 require .NoError (t , err )
121181 assert .ElementsMatch (t , []string {
122- filepath .Join (sourceDir , "fileA1.txt" ),
123- filepath .Join (sourceDir , "fileA2.txt" ),
182+ filepath .Join ("fileA1.txt" ),
183+ filepath .Join ("fileA2.txt" ),
124184 }, paths )
125185 })
126186 t .Run ("filter multiple technologies" , func (t * testing.T ) {
127187 paths , err := filterFilesByTechnology (testLog , fs , sourceDir , []string {"java" , "python" })
128188 require .NoError (t , err )
129189 assert .ElementsMatch (t , []string {
130- filepath .Join (sourceDir , "fileA1.txt" ),
131- filepath .Join (sourceDir , "fileA2.txt" ),
132- filepath .Join (sourceDir , "fileB1.txt" ),
190+ filepath .Join ("fileA1.txt" ),
191+ filepath .Join ("fileA2.txt" ),
192+ filepath .Join ("fileB1.txt" ),
133193 }, paths )
134194 })
135195 t .Run ("not filter non-existing technology" , func (t * testing.T ) {
0 commit comments