"io/fs"
"os"
"path/filepath"
- "reflect"
+ "runtime"
"syscall"
"testing"
- "github.com/google/go-cmp/cmp"
-
ft "ruderich.org/simon/safcm/cmd/safcm-remote/sync/filetest"
+ "ruderich.org/simon/safcm/testutil"
)
func TestHandle(t *testing.T) {
}
_, uid, _, gid := ft.CurrentUserAndGroup()
+ symlinkExists := "open file: too many levels of symbolic links"
+ if runtime.GOOS == "freebsd" {
+ // EMLINK instead of ELOOP
+ symlinkExists = "open file: too many links"
+ }
+
tests := []struct {
name string
path string
},
},
nil,
- fmt.Errorf("open file: too many levels of symbolic links"),
+ fmt.Errorf(symlinkExists),
},
{
"exists: fifo",
}
for _, tc := range tests {
- // Create separate test directory for each test case
- path := filepath.Join(cwd, "testdata", tc.name)
- err = os.Mkdir(path, 0700)
- if err != nil {
- t.Fatal(err)
- }
- err = os.Chdir(path)
- if err != nil {
- t.Fatal(err)
- }
+ t.Run(tc.name, func(t *testing.T) {
+ // Create separate test directory for each test case
+ path := filepath.Join(cwd, "testdata", tc.name)
+ err = os.Mkdir(path, 0700)
+ if err != nil {
+ t.Fatal(err)
+ }
+ err = os.Chdir(path)
+ if err != nil {
+ t.Fatal(err)
+ }
- if tc.prepare != nil {
- tc.prepare()
- }
+ if tc.prepare != nil {
+ tc.prepare()
+ }
- changes, err := handle(tc.path, tc.line, tc.create)
- if !reflect.DeepEqual(tc.expChanges, changes) {
- t.Errorf("%s: changes: %s", tc.name,
- cmp.Diff(tc.expChanges, changes))
- }
- // Ugly but the simplest way to compare errors (including nil)
- if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
- t.Errorf("%s: err = %#v, want %#v",
- tc.name, err, tc.expErr)
- }
+ changes, err := handle(tc.path, tc.line, tc.create)
+ testutil.AssertEqual(t, "changes",
+ changes, tc.expChanges)
+ testutil.AssertErrorEqual(t, "err", err, tc.expErr)
- files, err := ft.WalkDir(path)
- if err != nil {
- t.Fatal(err)
- }
- if !reflect.DeepEqual(tc.expFiles, files) {
- t.Errorf("%s: files: %s", tc.name,
- cmp.Diff(tc.expFiles, files))
- }
+ files, err := ft.WalkDir(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ testutil.AssertEqual(t, "files", files, tc.expFiles)
+ })
}
if !t.Failed() {