Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test some utils #101

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions utils/testutils.go
Original file line number Diff line number Diff line change
@@ -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
}
96 changes: 96 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -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)
}