github.com/dnephin/dobi@v0.15.0/utils/fs/directory_test.go (about) 1 package fs 2 3 import ( 4 "os" 5 "testing" 6 "time" 7 8 "gotest.tools/v3/assert" 9 "gotest.tools/v3/assert/cmp" 10 "gotest.tools/v3/fs" 11 ) 12 13 func TestLastModified_AbsolutePathsForDirectories(t *testing.T) { 14 tmpdir := fs.NewDir(t, "test-directory-last-modified-absolute-paths-for-dir", 15 fs.WithDir("a"), 16 fs.WithDir("b", 17 fs.WithDir("c"))) 18 defer tmpdir.Remove() 19 20 for index, dir := range []string{"a", "b", "b/c"} { 21 mtime := time.Now().AddDate(0, 0, index+10) 22 assert.NilError(t, touch(tmpdir.Join(dir, "file"), mtime)) 23 24 actual, err := LastModified(&LastModifiedSearch{ 25 Root: tmpdir.Path(), 26 Paths: []string{tmpdir.Path()}, 27 }) 28 assert.NilError(t, err) 29 assert.Equal(t, actual, mtime) 30 } 31 } 32 33 func TestLastModified_RelativePathsForDirectories(t *testing.T) { 34 tmpdir := fs.NewDir(t, "test-directory-last-modified-relative-paths-for-dir", 35 fs.WithDir("inner", 36 fs.WithDir("a"), 37 fs.WithDir("b", 38 fs.WithDir("c")))) 39 defer tmpdir.Remove() 40 41 for index, dir := range []string{"a", "b", "b/c"} { 42 mtime := time.Now().AddDate(0, 0, index+10) 43 assert.NilError(t, touch(tmpdir.Join("inner", dir, "file"), mtime)) 44 45 actual, err := LastModified(&LastModifiedSearch{ 46 Root: tmpdir.Join("inner"), 47 Paths: []string{"a", "b", "b/c"}, 48 }) 49 assert.NilError(t, err) 50 assert.Equal(t, actual, mtime) 51 } 52 } 53 54 func TestLastModified_RelativePathsForFile(t *testing.T) { 55 tmpdir := fs.NewDir(t, "test-directory-last-modified-relative-paths-for-file", 56 fs.WithDir("inner", 57 fs.WithDir("a"))) 58 defer tmpdir.Remove() 59 60 mtime := time.Now().AddDate(0, 0, 10) 61 assert.NilError(t, touch(tmpdir.Join("inner", "a", "file"), mtime)) 62 63 actual, err := LastModified(&LastModifiedSearch{ 64 Root: tmpdir.Join("inner"), 65 Paths: []string{"a/file"}, 66 }) 67 assert.NilError(t, err) 68 assert.Equal(t, actual, mtime) 69 } 70 71 func TestLastModifiedExcludesFile(t *testing.T) { 72 tmpdir := fs.NewDir(t, "test-directory-last-modified-excludes-file", 73 fs.WithDir("a"), 74 fs.WithDir("b", 75 fs.WithDir("c"))) 76 defer tmpdir.Remove() 77 78 for index, dir := range []string{"a", "b", "b/c"} { 79 mtime := time.Now().AddDate(0, 0, index+10) 80 assert.Assert(t, cmp.Nil(touch(tmpdir.Join(dir, "file"), mtime))) 81 82 excludedFile := tmpdir.Join(dir, "excluded-file") 83 excludedMtime := time.Now().AddDate(0, 0, index+20) 84 assert.Assert(t, cmp.Nil(touch(excludedFile, excludedMtime))) 85 86 actual, err := LastModified(&LastModifiedSearch{ 87 Root: tmpdir.Path(), 88 Excludes: []string{"**/**/excluded-file"}, 89 Paths: []string{tmpdir.Path()}, 90 }) 91 assert.NilError(t, err) 92 assert.Equal(t, actual, mtime) 93 } 94 } 95 96 func TestLastModifiedExcludesFolder(t *testing.T) { 97 tmpdir := fs.NewDir(t, "test-directory-last-modified-excludes-folder", 98 fs.WithDir("a"), 99 fs.WithDir("b", 100 fs.WithDir("c"))) 101 defer tmpdir.Remove() 102 103 mtime := time.Now().AddDate(0, 0, 0) 104 assert.Assert(t, cmp.Nil(touch(tmpdir.Join("a", "file"), mtime))) 105 106 for index, dir := range []string{"b", "b/c"} { 107 ignoredMtime := time.Now().AddDate(0, 0, index+10) 108 assert.Assert(t, cmp.Nil(touch(tmpdir.Join(dir, "file"), ignoredMtime))) 109 } 110 111 actual, err := LastModified(&LastModifiedSearch{ 112 Excludes: []string{"b/"}, 113 Paths: []string{tmpdir.Path()}, 114 Root: tmpdir.Path(), 115 }) 116 assert.NilError(t, err) 117 assert.Equal(t, actual, mtime) 118 } 119 120 func touch(name string, mtime time.Time) error { 121 w, err := os.Create(name) 122 if err != nil { 123 return err 124 } 125 if err := w.Close(); err != nil { 126 return err 127 } 128 129 return os.Chtimes(name, mtime, mtime) 130 }