github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/utils/osutil/osutils_test.go (about) 1 package osutil 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 // both stop file and target file exist in working directory 13 func TestFindFile_ExistingInWorkingDir(t *testing.T) { 14 st := "stopFile1.txt" 15 _, err := os.Create(st) 16 require.NoError(t, err, "error creating temp stop file") 17 18 t.Cleanup(func() { 19 _ = os.Remove(st) 20 }) 21 22 tmpfile, err := os.Create("example") 23 require.NoError(t, err, "error creating temp test file") 24 25 t.Cleanup(func() { 26 _ = os.Remove("example") 27 }) 28 29 foundPath, err := FindFile("example", st, 10) 30 require.NoError(t, err, "error calling FindFile") 31 32 wd, err := os.Getwd() 33 require.NoError(t, err, "error getting working directory") 34 expectedPath := filepath.Join(wd, tmpfile.Name()) 35 36 require.Equal(t, expectedPath, expectedPath, "expected %v, got %v", expectedPath, foundPath) 37 } 38 39 // stop file exists in working directory 40 // target file exists in sub directory 41 func TestFindFile_ExistsInSubDir(t *testing.T) { 42 st := "stopFile2.txt" 43 wd, err := os.Getwd() 44 require.NoError(t, err, "error getting working directory") 45 46 b := "baseDir2" 47 s := "subDir2" 48 err = os.Mkdir(b, 0755) 49 require.NoError(t, err, "error creating temp base dir") 50 t.Cleanup(func() { 51 os.RemoveAll(filepath.Join(wd, b)) 52 _ = os.Chdir(wd) 53 }) 54 55 baseDir := filepath.Join(wd, b) 56 subDir := filepath.Join(baseDir, s) 57 err = os.Mkdir(subDir, 0755) 58 require.NoError(t, err, "error creating sub dir") 59 60 // create stop file in base dir 61 stopFile := filepath.Join(baseDir, st) 62 _, err = os.Create(stopFile) 63 require.NoError(t, err, "error creating temp stop file") 64 65 // create nested file in sub dir 66 targetFileName := "target2.txt" 67 targetFile := filepath.Join(subDir, targetFileName) 68 _, err = os.Create(targetFile) 69 require.NoError(t, err, "error creating temp test file") 70 71 err = os.Chdir(subDir) 72 require.NoError(t, err, "error changing working directory") 73 74 foundPath, err := FindFile(targetFileName, st, 2) 75 require.NoError(t, err, "error calling FindFile") 76 require.Equal(t, targetFile, foundPath, "expected %v, got %v", targetFile, foundPath) 77 } 78 79 // stop file exists in parent directory relative to working directory 80 // target file exists in a sub directory 81 func TestFindFile_ExistsInSameDirAsStop(t *testing.T) { 82 st := "stopFile3.txt" 83 wd, err := os.Getwd() 84 require.NoError(t, err, "error getting working directory") 85 86 b := "baseDir3" 87 err = os.Mkdir(b, 0755) 88 require.NoError(t, err, "error creating temp base dir") 89 t.Cleanup(func() { 90 os.RemoveAll(filepath.Join(wd, b)) 91 }) 92 93 baseDir := filepath.Join(wd, b) 94 // create stop file in base dir 95 stopFile := filepath.Join(wd, st) 96 _, err = os.Create(stopFile) 97 require.NoError(t, err, "error creating temp stop file") 98 99 t.Cleanup(func() { 100 os.Remove(stopFile) 101 _ = os.Chdir(wd) 102 }) 103 104 // create nested file in base dir 105 targetFileName := "target3.txt" 106 targetFile := filepath.Join(baseDir, targetFileName) 107 _, err = os.Create(targetFile) 108 require.NoError(t, err, "error creating temp test file") 109 110 err = os.Chdir(baseDir) 111 require.NoError(t, err, "error changing working directory") 112 113 foundPath, err := FindFile(targetFileName, st, 2) 114 require.NoError(t, err, "error calling FindFile") 115 require.Equal(t, targetFile, foundPath, "expected %v, got %v", targetFile, foundPath) 116 } 117 118 // file doesn't exist anywhere 119 func TestFindFile_FileDoesNotExist(t *testing.T) { 120 st := "stopFile4.txt" 121 _, err := os.Create(st) 122 require.NoError(t, err, "error creating temp stop file") 123 124 t.Cleanup(func() { 125 _ = os.Remove(st) 126 }) 127 128 _, err = FindFile("nonExistentFile.txt", st, 2) 129 require.Error(t, err, "expected error calling FindFile") 130 require.Contains(t, "file does not exist", err.Error(), "got wrong error") 131 } 132 133 // stop file doesn't exist at all 134 func TestFindFile_StopFileDoesNotExist(t *testing.T) { 135 _, err := FindFile("somefile.txt", "nonExistentStopFile.txt", 2) 136 require.Error(t, err, "expected error calling FindFile") 137 require.Contains(t, err.Error(), ErrStopFileNotFoundWithinLimit, "got wrong error") 138 } 139 140 // stop file doesn't exist within search limit 141 func TestFindFile_stopFileNotFoundWithinLimit(t *testing.T) { 142 st := "stopFile5.txt" 143 b := "baseDir5" 144 wd, err := os.Getwd() 145 require.NoError(t, err, "error getting working directory") 146 147 err = os.Mkdir(b, 0755) 148 require.NoError(t, err, "error creating temp base dir") 149 t.Cleanup(func() { 150 os.RemoveAll(filepath.Join(wd, b)) 151 _ = os.Chdir(wd) 152 }) 153 154 baseDir := filepath.Join(wd, b) 155 156 currentDir := baseDir 157 for i := 0; i <= 3; i++ { 158 currentDir = filepath.Join(currentDir, fmt.Sprintf("nested%d", i)) 159 err = os.Mkdir(currentDir, 0755) 160 require.NoError(t, err, "error creating temp nested dir") 161 } 162 163 stopFile := filepath.Join(baseDir, st) 164 _, err = os.Create(stopFile) 165 require.NoError(t, err, "error creating temp stop file") 166 167 err = os.Chdir(currentDir) 168 require.NoError(t, err, "error changing working directory") 169 170 tg := "target5.txt" 171 _, err = FindFile(tg, st, 2) 172 require.Error(t, err, "expected error calling FindFile") 173 require.Contains(t, err.Error(), ErrStopFileNotFoundWithinLimit, "got wrong error") 174 }