diff --git a/utils/testutils.go b/utils/testutils.go new file mode 100644 index 0000000..0cf2ce1 --- /dev/null +++ b/utils/testutils.go @@ -0,0 +1,34 @@ +package utils + +import ( + "os" + "path/filepath" + "testing" + + "go.viam.com/test" +) + +// MockViamDirs replaces utils.ViamDirs entries with t.TempDir for duration of test. +func MockViamDirs(t *testing.T) { + t.Helper() + old := ViamDirs + t.Cleanup(func() { + ViamDirs = old + }) + td := t.TempDir() + ViamDirs = map[string]string{ + "viam": td, + } + for _, subdir := range []string{"bin", "cache", "tmp", "etc"} { + ViamDirs[subdir] = filepath.Join(td, subdir) + err := os.Mkdir(ViamDirs[subdir], 0o750) + test.That(t, err, test.ShouldBeNil) + } +} + +// Touch is equivalent to unix touch; creates an empty file at path. +func Touch(t *testing.T, path string) { + f, err := os.Create(path) //nolint:gosec + test.That(t, err, test.ShouldBeNil) + f.Close() //nolint:gosec,errcheck +} diff --git a/utils/utils_test.go b/utils/utils_test.go new file mode 100644 index 0000000..f813e75 --- /dev/null +++ b/utils/utils_test.go @@ -0,0 +1,96 @@ +package utils + +import ( + "os/exec" + "path/filepath" + "testing" + + "go.viam.com/test" +) + +func TestDecompressFile(t *testing.T) { + MockViamDirs(t) + td := t.TempDir() + if _, err := exec.LookPath("xz"); err != nil { + t.Skip("no xz command") + } + orig := filepath.Join(td, "plaintext") + + // compress an empty file + Touch(t, orig) + _, err := exec.Command("xz", orig).Output() + test.That(t, err, test.ShouldBeNil) + + // decompress + path, err := DecompressFile(orig + ".xz") + test.That(t, err, test.ShouldBeNil) + test.That(t, path, test.ShouldResemble, filepath.Join(ViamDirs["cache"], "plaintext")) +} + +func TestGetFileSum(t *testing.T) { + td := t.TempDir() + path := filepath.Join(td, "checkme") + + Touch(t, path) + + _, err := GetFileSum(path) + test.That(t, err, test.ShouldBeNil) +} + +func TestCheckIfSame(t *testing.T) { + td := t.TempDir() + + path1 := filepath.Join(td, "path1") + path2 := filepath.Join(td, "path2") + Touch(t, path1) + Touch(t, path2) + + link := filepath.Join(td, "link") + test.That(t, ForceSymlink(path1, link), test.ShouldBeNil) + + check := func(path1, path2 string, expected bool) { + same, err := CheckIfSame(path1, path2) + test.That(t, err, test.ShouldBeNil) + test.That(t, same == expected, test.ShouldBeTrue) + } + + check(path1, path1, true) + check(path1, path2, false) + check(link, link, true) + check(link, path1, true) + check(link, path2, false) +} + +func TestForceSymlink(t *testing.T) { + td := t.TempDir() + path := filepath.Join(td, "link") + target := filepath.Join(td, "target") + + // test initial case + err := ForceSymlink(target, path) + test.That(t, err, test.ShouldBeNil) + + // test already-exists case + err = ForceSymlink(target, path) + test.That(t, err, test.ShouldBeNil) +} + +func TestWriteFileIfNew(t *testing.T) { + contents := []byte("hello") + path := filepath.Join(t.TempDir(), "writeme") + + // write new + written, err := WriteFileIfNew(path, contents) + test.That(t, err, test.ShouldBeNil) + test.That(t, written, test.ShouldBeTrue) + + // unchanged + written, err = WriteFileIfNew(path, contents) + test.That(t, err, test.ShouldBeNil) + test.That(t, written, test.ShouldBeFalse) + + // changed + written, err = WriteFileIfNew(path, []byte("other contents")) + test.That(t, err, test.ShouldBeNil) + test.That(t, written, test.ShouldBeTrue) +}