github.com/soypat/rebed@v0.2.3/rebed_test.go (about) 1 package rebed 2 3 import ( 4 "embed" 5 "fmt" 6 "io/fs" 7 "os" 8 "path/filepath" 9 "testing" 10 ) 11 12 // defer os.Chdir(strings.Repeat("../", len(filepath.SplitList(testDir)))) 13 14 //go:embed testFS/* 15 var testFS embed.FS 16 17 // this is where filesystems are created 18 const testDir = "testdata/testdir" 19 20 func setup(path string, t *testing.T) { 21 os.RemoveAll(path) 22 os.MkdirAll(path, 0777) 23 } 24 func TestTree(t *testing.T) { 25 tDir := filepath.Join(testDir, t.Name()) 26 setup(tDir, t) 27 defer os.RemoveAll(tDir) 28 err := Tree(testFS, tDir) 29 if err != nil { 30 t.Error(err) 31 } 32 // We search our embedded directories and check if our created filesystem has the entries 33 err = Walk(testFS, ".", func(path string, de fs.DirEntry) error { 34 pathToCreated := filepath.Join(path, de.Name()) 35 if de.IsDir() { 36 info, err := os.Stat(pathToCreated) 37 if os.IsNotExist(err) { 38 t.Errorf("folder %q not found", pathToCreated) 39 } 40 if !info.IsDir() { 41 t.Errorf("expected a folder %q, got file", pathToCreated) 42 } 43 if err != nil { 44 t.Error(err) 45 } 46 } 47 return nil 48 }) 49 if err != nil { 50 t.Error(err) 51 } 52 } 53 54 func TestTouch(t *testing.T) { 55 testFileCreation(Touch, t) 56 } 57 58 func TestWrite(t *testing.T) { 59 testFileCreation(Write, t) 60 } 61 62 func TestPatch(t *testing.T) { 63 testFileCreation(Patch, t) 64 } 65 66 func TestCreate(t *testing.T) { 67 testFileCreation(Create, t) 68 } 69 70 func TestCreateError(t *testing.T) { 71 tDir := filepath.Join(testDir, t.Name()) 72 setup(tDir, t) 73 defer os.RemoveAll(tDir) 74 err := Create(testFS, tDir) 75 if err != nil { 76 t.Errorf("Create failed with new directory %s", err) 77 } 78 err = Create(testFS, tDir) 79 if err == nil || !os.IsExist(err) { 80 t.Errorf("Create should have failed during conflicting filesystem creation: %s", err) 81 } 82 } 83 84 func TestWalkError(t *testing.T) { 85 theError := fmt.Errorf("oops") 86 err := Walk(testFS, ".", func(path string, de fs.DirEntry) error { 87 return theError 88 }) 89 if err != theError { 90 t.Errorf("Expected specific error, got %s", err) 91 } 92 } 93 94 func TestWalkDirError(t *testing.T) { 95 err := Walk(testFS, "nonexistent", func(path string, de fs.DirEntry) error { 96 return nil 97 }) 98 if err == nil { 99 t.Errorf("expected error while walking non-existent directory") 100 } 101 } 102 103 func testFileCreation(rebedder func(embed.FS, string) error, t *testing.T) { 104 tDir := filepath.Join(testDir, t.Name()) 105 setup(tDir, t) 106 defer os.RemoveAll(tDir) 107 err := rebedder(testFS, tDir) 108 if err != nil { 109 t.Error(err) 110 } 111 err = Walk(testFS, ".", func(path string, de fs.DirEntry) error { 112 pathToCreated := filepath.Join(path, de.Name()) 113 info, err := os.Stat(pathToCreated) 114 if err != nil { 115 return err 116 } 117 if de.IsDir() != info.IsDir() { 118 t.Errorf("expected folder/file got file/folder %q", pathToCreated) 119 } 120 return nil 121 }) 122 if err != nil { 123 t.Error(err) 124 } 125 } 126 127 func TestEmbedCopyErrors(t *testing.T) { 128 err := embedCopyToFile(testFS, "nonexistent/File/path", "outputPath") 129 if err == nil { 130 t.Error("file should not have existed!") 131 } 132 err = embedCopyToFile(testFS, "testFS/file", "nonexistentfolder/file") 133 if err == nil { 134 t.Error("copying to a nonexistent folder should error") 135 } 136 }