test: Allow using an environment variable to override the test dir

Some build systems may not permit writing to . during build, so allow
that path to be overridden with an environment variable.
This commit is contained in:
Matthew Garrett 2017-04-13 11:35:01 -07:00
parent 93776cd69e
commit cb1fb5dded
3 changed files with 22 additions and 7 deletions

View file

@ -13,9 +13,14 @@ import (
)
func TestXattr(t *testing.T) {
// a bit dirty to create/destory a directory in cwd, but often /tmp is
// mounted tmpfs and doesn't support xattrs
dir, err := ioutil.TempDir(".", "test.xattrs.")
testDir, present := os.LookupEnv("MTREE_TESTDIR")
if present == false {
// a bit dirty to create/destory a directory in cwd,
// but often /tmp is mounted tmpfs and doesn't support
// xattrs
testDir = "."
}
dir, err := ioutil.TempDir(testDir, "test.xattrs.")
if err != nil {
t.Fatal(err)
}

View file

@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"testing"
"time"
@ -78,11 +79,16 @@ func TestTar(t *testing.T) {
t.Fatal("expected a DirectoryHierarchy struct, but got nil")
}
fh, err = os.Create("./testdata/test.mtree")
testDir, present := os.LookupEnv("MTREE_TESTDIR")
if present == false {
testDir = "."
}
testPath := filepath.Join(testDir, "test.mtree")
fh, err = os.Create(testPath)
if err != nil {
t.Fatal(err)
}
defer os.Remove("./testdata/test.mtree")
defer os.Remove(testPath)
// put output of tar walk into test.mtree
_, err = tdh.WriteTo(fh)
@ -92,7 +98,7 @@ func TestTar(t *testing.T) {
fh.Close()
// now simulate gomtree -T testdata/test.tar -f testdata/test.mtree
fh, err = os.Open("./testdata/test.mtree")
fh, err = os.Open(testPath)
if err != nil {
t.Fatal(err)
}

View file

@ -10,7 +10,11 @@ import (
)
func TestXattr(t *testing.T) {
fh, err := ioutil.TempFile(".", "xattr.")
testDir, present := os.LookupEnv("MTREE_TESTDIR")
if present == false {
testDir = "."
}
fh, err := ioutil.TempFile(testDir, "xattr.")
if err != nil {
t.Fatal(err)
}