Skip to content

Commit

Permalink
test: make file and dir tests OS-agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
brandondyck authored and shoenig committed Aug 19, 2024
1 parent f903659 commit 1244f1b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 44 deletions.
50 changes: 28 additions & 22 deletions must/must_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added must/testdata/dir1/file1
Empty file.
2 changes: 2 additions & 0 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ apply test.go
apply test_test.go
apply examples_test.go

cp -R testdata must/

# rename core test files
mv must/test.go must/must.go
mv must/test_test.go must/must_test.go
Expand Down
50 changes: 28 additions & 22 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
package test

import (
"embed"
"errors"
"io/fs"
"math"
"os"
"path/filepath"
"regexp"
"runtime"
"testing"
Expand All @@ -15,6 +18,10 @@ import (
"github.com/shoenig/test/wait"
)

//go:embed testdata/dir1
var _testdata embed.FS
var testfs, _ = fs.Sub(_testdata, "testdata")

func needsOS(t *testing.T, os string) {
if os != runtime.GOOS {
t.Skip("not supported on this OS")
Expand Down Expand Up @@ -1357,64 +1364,66 @@ func TestFileExistsFS(t *testing.T) {
tc := newCase(t, `expected file to exist`)
t.Cleanup(tc.assert)

FileExistsFS(tc, os.DirFS("/etc"), "hosts2")
FileExistsFS(tc, testfs, "dir1/file2")
}

func TestFileExists(t *testing.T) {
tc := newCase(t, `expected file to exist`)
t.Cleanup(tc.assert)

FileExists(tc, "/etc/hosts2")
FileExists(tc, filepath.Join(t.TempDir(), "fake"))
}

func TestFileNotExistsFS(t *testing.T) {
needsOS(t, "linux")

tc := newCase(t, `expected file to not exist`)
t.Cleanup(tc.assert)

FileNotExistsFS(tc, os.DirFS("/etc"), "hosts")
FileNotExistsFS(tc, testfs, "dir1/file1")
}

func TestFileNotExists(t *testing.T) {
needsOS(t, "linux")
func createTempFile(t *testing.T, name string) (path string) {
path = filepath.Join(t.TempDir(), name)
err := os.WriteFile(path, []byte{}, os.ModePerm)
if err != nil {
t.Fatal("failed to create temp file")
}
return path
}

func TestFileNotExists(t *testing.T) {
tc := newCase(t, `expected file to not exist`)
t.Cleanup(tc.assert)

FileNotExists(tc, "/etc/hosts")
path := createTempFile(t, "test")
FileNotExists(tc, path)
}

func TestDirExistsFS(t *testing.T) {
tc := newCase(t, `expected directory to exist`)
t.Cleanup(tc.assert)

DirExistsFS(tc, os.DirFS("/usr/local"), "bin2")
DirExistsFS(tc, testfs, "dir2")
}

func TestDirExists(t *testing.T) {
tc := newCase(t, `expected directory to exist`)
t.Cleanup(tc.assert)

DirExists(tc, "/usr/local/bin2")
DirExists(tc, filepath.Join(t.TempDir(), "fake"))
}

func TestDirNotExistsFS(t *testing.T) {
needsOS(t, "linux")

tc := newCase(t, `expected directory to not exist`)
t.Cleanup(tc.assert)

DirNotExistsFS(tc, os.DirFS("/usr"), "local")
DirNotExistsFS(tc, testfs, "dir1")
}

func TestDirNotExists(t *testing.T) {
needsOS(t, "linux")

tc := newCase(t, `expected directory to not exist`)
t.Cleanup(tc.assert)

DirNotExists(tc, "/usr/local")
DirNotExists(tc, t.TempDir())
}

func TestFileModeFS(t *testing.T) {
Expand Down Expand Up @@ -1476,21 +1485,18 @@ func TestDirMode(t *testing.T) {
}

func TestFileContainsFS(t *testing.T) {
needsOS(t, "linux")

tc := newCase(t, `expected file contents`)
t.Cleanup(tc.assert)

FileContainsFS(tc, os.DirFS("/etc"), "hosts", "127.0.0.999")
FileContainsFS(tc, testfs, "dir1/file1", "fake data")
}

func TestFileContains(t *testing.T) {
needsOS(t, "linux")

tc := newCase(t, `expected file contents`)
t.Cleanup(tc.assert)

FileContains(tc, "/etc/hosts", "127.0.0.999")
path := createTempFile(t, "test")
FileContains(tc, path, "fake data")
}

func TestFilePathValid(t *testing.T) {
Expand Down
Empty file added testdata/dir1/file1
Empty file.

0 comments on commit 1244f1b

Please sign in to comment.