Creates a temporary directory with the structure below. Returns a cleanup function that will remove the directory and all of its contents. The cleanup function should be wrapped in a defer statement. | root |-- rootFile.txt |-- subDir1 | |-- subDir1_file.txt | |-- subDir2 |-- subDir2_file.g
(t *testing.T)
| 270 | // |-- subDir2_file.go |
| 271 | // |-- subDir2_file.txt |
| 272 | func createTestDir(t *testing.T) (cleanupFn func()) { |
| 273 | t.Helper() |
| 274 | // Make Directories |
| 275 | rootDir := t.TempDir() |
| 276 | |
| 277 | // Move workspace to temporary directory |
| 278 | t.Chdir(rootDir) |
| 279 | |
| 280 | // Make subdirectories |
| 281 | err := os.Mkdir(filepath.Join(rootDir, "subDir1"), 0755) |
| 282 | if err != nil { |
| 283 | t.Fatal(err) |
| 284 | } |
| 285 | |
| 286 | err = os.Mkdir(filepath.Join(rootDir, "subDir2"), 0755) |
| 287 | if err != nil { |
| 288 | t.Fatal(err) |
| 289 | } |
| 290 | |
| 291 | // Make Files |
| 292 | err = os.WriteFile(filepath.Join(rootDir, "rootFile.txt"), []byte(""), 0644) |
| 293 | if err != nil { |
| 294 | t.Fatal(err) |
| 295 | } |
| 296 | |
| 297 | err = os.WriteFile(filepath.Join(rootDir, "subDir1", "subDir1_file.txt"), []byte(""), 0o644) |
| 298 | if err != nil { |
| 299 | t.Fatal(err) |
| 300 | } |
| 301 | |
| 302 | err = os.WriteFile(filepath.Join(rootDir, "subDir2", "subDir2_file.go"), []byte(""), 0o644) |
| 303 | if err != nil { |
| 304 | t.Fatal(err) |
| 305 | } |
| 306 | |
| 307 | err = os.WriteFile(filepath.Join(rootDir, "subDir2", "subDir2_file.txt"), []byte(""), 0o644) |
| 308 | if err != nil { |
| 309 | t.Fatal(err) |
| 310 | } |
| 311 | |
| 312 | cleanupFn = func() { |
| 313 | os.RemoveAll(rootDir) |
| 314 | } |
| 315 | return cleanupFn |
| 316 | } |
no test coverage detected