...
1
2
3
4 package storage
5
6 import (
7 "context"
8 "os"
9 "path/filepath"
10 "testing"
11 "time"
12 )
13
14 func Test_WaitForFileMatchingPattern_Success(t *testing.T) {
15 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
16 defer cancel()
17
18 testDir := t.TempDir()
19
20 actualPath := filepath.Join(testDir, "path1")
21 err := os.Mkdir(actualPath, 0777)
22 if err != nil {
23 t.Fatalf("unexpected error creating test path: %v", err)
24 }
25
26 pathPattern := filepath.Join(testDir, "path*")
27 pathsToTest := []string{actualPath, pathPattern}
28 for _, p := range pathsToTest {
29 result, err := WaitForFileMatchingPattern(ctx, p)
30 if err != nil {
31 t.Fatalf("expected to find path %v but got error: %v", p, err)
32 }
33 if result != actualPath {
34 t.Fatalf("expected to return path %s, instead go %s", actualPath, result)
35 }
36 }
37 }
38
39 func Test_WaitForFileMatchingPattern_Multiple_Matches(t *testing.T) {
40 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
41 defer cancel()
42
43 testDir := t.TempDir()
44
45 actualPaths := []string{"path1", "path2"}
46 for _, p := range actualPaths {
47 fullPath := filepath.Join(testDir, p)
48 err := os.Mkdir(fullPath, 0777)
49 if err != nil {
50 t.Fatalf("unexpected error creating test path: %v", err)
51 }
52 }
53
54 pathPattern := filepath.Join(testDir, "path*")
55 _, err := WaitForFileMatchingPattern(ctx, pathPattern)
56 if err == nil {
57 t.Fatalf("expected to fail due to multiple matching files")
58 }
59 }
60
61 func Test_WaitForFileMatchingPattern_No_Matches(t *testing.T) {
62 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
63 defer cancel()
64
65 testDir := t.TempDir()
66
67 actualPath := filepath.Join(testDir, "path1")
68 err := os.Mkdir(actualPath, 0777)
69 if err != nil {
70 t.Fatalf("unexpected error creating test path: %v", err)
71 }
72
73 badTestPath := filepath.Join(testDir, "path2")
74 _, err = WaitForFileMatchingPattern(ctx, badTestPath)
75 if err == nil {
76 t.Fatalf("expected to fail due to no matching files")
77 }
78 }
79
View as plain text