11package hash
22
33import (
4+ "fmt"
45 "os"
6+ "path/filepath"
7+ "sort"
8+ "strings"
59 "testing"
610 "time"
711
12+ "github.com/ActiveState/cli/internal/errs"
13+ "github.com/ActiveState/cli/internal/fileutils"
14+ "github.com/ActiveState/cli/internal/osutils"
815 "github.com/patrickmn/go-cache"
916 "github.com/stretchr/testify/assert"
17+ "github.com/stretchr/testify/require"
1018)
1119
1220type testCache struct {
@@ -31,23 +39,39 @@ func (tc *testCache) Set(key string, value interface{}, expiration time.Duration
3139}
3240
3341func TestFileHasher_HashFiles (t * testing.T ) {
34- file1 := createTempFile (t , "file1" )
35- file2 := createTempFile (t , "file2" )
42+ dir := fileutils .TempDirUnsafe ()
43+ file1 := createTempFile (t , dir , "file1.txt" )
44+ file2 := createTempFile (t , dir , "file2.info" )
45+ subfile1 := createTempFile (t , dir , "dir1/subfile1.txt" )
3646
3747 hasher := NewFileHasher ()
3848
39- hash1 , err := hasher .HashFiles ([]string {file1 , file2 })
40- assert .NoError (t , err )
49+ hash1 , files1 , err := hasher .HashFiles (dir , []string {file1 , file2 , subfile1 })
50+ require .NoError (t , err )
4151
42- hash2 , err := hasher .HashFiles ([]string {file1 , file2 })
43- assert .NoError (t , err )
52+ hash2 , files2 , err := hasher .HashFiles (dir , []string {"./**/*" })
53+ require .NoError (t , err , errs .JoinMessage (err ))
54+
55+ for _ , f := range files1 {
56+ assert .False (t , strings .HasPrefix (f .Path , dir ), fmt .Sprintf ("'%s' should not be prefixed with '%s'" , f .Path , dir ))
57+ }
58+
59+ sort .Slice (files1 , func (i , j int ) bool { return files1 [i ].Path < files1 [j ].Path })
60+ sort .Slice (files2 , func (i , j int ) bool { return files2 [i ].Path < files2 [j ].Path })
61+ require .Len (t , files2 , 3 )
62+ require .Len (t , files2 , len (files1 ))
63+
64+ for i , f := range files1 {
65+ assert .Equal (t , f .Path , files2 [i ].Path )
66+ assert .Equal (t , f .Hash , files2 [i ].Hash )
67+ }
4468
4569 assert .Equal (t , hash1 , hash2 )
4670}
4771
4872func TestFileHasher_CacheHit (t * testing.T ) {
49- file1 := createTempFile (t , "file1" )
50- file2 := createTempFile (t , "file2" )
73+ file1 := createTempFile (t , "" , " file1" )
74+ file2 := createTempFile (t , "" , " file2" )
5175
5276 tc := & testCache {
5377 cache : cache .New (cache .NoExpiration , cache .NoExpiration ),
@@ -57,10 +81,10 @@ func TestFileHasher_CacheHit(t *testing.T) {
5781 cache : tc ,
5882 }
5983
60- hash1 , err := hasher .HashFiles ([]string {file1 , file2 })
84+ hash1 , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 })
6185 assert .NoError (t , err )
6286
63- hash2 , err := hasher .HashFiles ([]string {file1 , file2 })
87+ hash2 , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 })
6488 assert .NoError (t , err )
6589
6690 assert .Equal (t , hash1 , hash2 )
@@ -69,8 +93,8 @@ func TestFileHasher_CacheHit(t *testing.T) {
6993}
7094
7195func TestFileHasher_CacheMiss (t * testing.T ) {
72- file1 := createTempFile (t , "file1" )
73- file2 := createTempFile (t , "file2" )
96+ file1 := createTempFile (t , "" , " file1" )
97+ file2 := createTempFile (t , "" , " file2" )
7498
7599 tc := & testCache {
76100 cache : cache .New (cache .NoExpiration , cache .NoExpiration ),
@@ -80,7 +104,7 @@ func TestFileHasher_CacheMiss(t *testing.T) {
80104 cache : tc ,
81105 }
82106
83- hash1 , err := hasher .HashFiles ([]string {file1 , file2 })
107+ hash1 , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 })
84108 assert .NoError (t , err )
85109
86110 if err := os .Chtimes (file1 , time .Now (), time .Now ()); err != nil {
@@ -92,7 +116,7 @@ func TestFileHasher_CacheMiss(t *testing.T) {
92116 err = file .Sync ()
93117 assert .NoError (t , err )
94118
95- hash2 , err := hasher .HashFiles ([]string {file1 , file2 })
119+ hash2 , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 })
96120 assert .NoError (t , err )
97121
98122 assert .Equal (t , hash1 , hash2 )
@@ -102,11 +126,11 @@ func TestFileHasher_CacheMiss(t *testing.T) {
102126
103127func TestFileHasher_ContentAgnostic (t * testing.T ) {
104128 // Files have same content but different names and modification times
105- file1 := createTempFile (t , "file1" )
129+ file1 := createTempFile (t , "" , " file1" )
106130
107131 // Ensure mod times are different
108132 time .Sleep (1 * time .Millisecond )
109- file2 := createTempFile (t , "file1" )
133+ file2 := createTempFile (t , "" , " file1" )
110134
111135 tc := & testCache {
112136 cache : cache .New (cache .NoExpiration , cache .NoExpiration ),
@@ -116,10 +140,10 @@ func TestFileHasher_ContentAgnostic(t *testing.T) {
116140 cache : tc ,
117141 }
118142
119- hash1 , err := hasher .HashFiles ([]string {file1 , file2 })
143+ hash1 , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 })
120144 assert .NoError (t , err )
121145
122- hash2 , err := hasher .HashFiles ([]string {file1 , file2 })
146+ hash2 , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 })
123147 assert .NoError (t , err )
124148
125149 assert .Equal (t , hash1 , hash2 )
@@ -128,9 +152,9 @@ func TestFileHasher_ContentAgnostic(t *testing.T) {
128152}
129153
130154func TestFileHasher_NotEqualFileAdded (t * testing.T ) {
131- file1 := createTempFile (t , "file1" )
132- file2 := createTempFile (t , "file2" )
133- file3 := createTempFile (t , "file3" )
155+ file1 := createTempFile (t , "" , " file1" )
156+ file2 := createTempFile (t , "" , " file2" )
157+ file3 := createTempFile (t , "" , " file3" )
134158
135159 tc := & testCache {
136160 cache : cache .New (cache .NoExpiration , cache .NoExpiration ),
@@ -140,10 +164,10 @@ func TestFileHasher_NotEqualFileAdded(t *testing.T) {
140164 cache : tc ,
141165 }
142166
143- hash1 , err := hasher .HashFiles ([]string {file1 , file2 })
167+ hash1 , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 })
144168 assert .NoError (t , err )
145169
146- hash2 , err := hasher .HashFiles ([]string {file1 , file2 , file3 })
170+ hash2 , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 , file3 })
147171 assert .NoError (t , err )
148172
149173 assert .NotEqual (t , hash1 , hash2 )
@@ -152,9 +176,9 @@ func TestFileHasher_NotEqualFileAdded(t *testing.T) {
152176}
153177
154178func TestFileHasher_NotEqualFileRemoved (t * testing.T ) {
155- file1 := createTempFile (t , "file1" )
156- file2 := createTempFile (t , "file2" )
157- file3 := createTempFile (t , "file3" )
179+ file1 := createTempFile (t , "" , " file1" )
180+ file2 := createTempFile (t , "" , " file2" )
181+ file3 := createTempFile (t , "" , " file3" )
158182
159183 tc := & testCache {
160184 cache : cache .New (cache .NoExpiration , cache .NoExpiration ),
@@ -164,10 +188,10 @@ func TestFileHasher_NotEqualFileRemoved(t *testing.T) {
164188 cache : tc ,
165189 }
166190
167- hash1 , err := hasher .HashFiles ([]string {file1 , file2 , file3 })
191+ hash1 , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 , file3 })
168192 assert .NoError (t , err )
169193
170- hash2 , err := hasher .HashFiles ([]string {file1 , file2 })
194+ hash2 , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 })
171195 assert .NoError (t , err )
172196
173197 assert .NotEqual (t , hash1 , hash2 )
@@ -176,8 +200,8 @@ func TestFileHasher_NotEqualFileRemoved(t *testing.T) {
176200}
177201
178202func TestFileHasher_NotEqualContentChanged (t * testing.T ) {
179- file1 := createTempFile (t , "file1" )
180- file2 := createTempFile (t , "file2" )
203+ file1 := createTempFile (t , "" , " file1" )
204+ file2 := createTempFile (t , "" , " file2" )
181205
182206 tc := & testCache {
183207 cache : cache .New (cache .NoExpiration , cache .NoExpiration ),
@@ -187,10 +211,10 @@ func TestFileHasher_NotEqualContentChanged(t *testing.T) {
187211 cache : tc ,
188212 }
189213
190- hash1 , err := hasher .HashFiles ([]string {file1 , file2 })
214+ hash1 , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 })
191215 assert .NoError (t , err )
192216
193- hash2 , err := hasher .HashFiles ([]string {file1 , file2 })
217+ hash2 , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 })
194218 assert .NoError (t , err )
195219
196220 assert .Equal (t , hash1 , hash2 )
@@ -203,26 +227,28 @@ func TestFileHasher_NotEqualContentChanged(t *testing.T) {
203227 t .Fatal (err )
204228 }
205229
206- hash2Modified , err := hasher .HashFiles ([]string {file1 , file2 })
230+ hash2Modified , _ , err := hasher .HashFiles (osutils . GetwdUnsafe (), []string {file1 , file2 })
207231 assert .NoError (t , err )
208232
209233 assert .NotEqual (t , hash1 , hash2Modified )
210234 assert .Len (t , tc .hits , 3 )
211235 assert .Len (t , tc .misses , 3 )
212236}
213237
214- func createTempFile (t * testing.T , content string ) string {
215- tmpfile , err := os .CreateTemp ("" , "testfile" )
216- if err != nil {
217- t .Fatal (err )
218- }
219-
220- if _ , err := tmpfile .Write ([]byte (content )); err != nil {
221- t .Fatal (err )
238+ func createTempFile (t * testing.T , dir , path string ) string {
239+ if dir == "" {
240+ dir = t .TempDir ()
222241 }
223- if err := tmpfile .Close (); err != nil {
224- t .Fatal (err )
242+ if path == "" {
243+ tmpfile , err := os .CreateTemp (dir , "" )
244+ if err != nil {
245+ t .Fatal (err )
246+ }
247+ path = tmpfile .Name ()
248+ tmpfile .Close ()
225249 }
250+ err := fileutils .WriteFile (filepath .Join (dir , path ), []byte (path )) // Contents aren't important so long as they're consistent
251+ require .NoError (t , err , errs .JoinMessage (err ))
226252
227- return tmpfile . Name ()
253+ return path
228254}
0 commit comments