Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.generate_exes
.get_deps
bin/
**/.test/
53 changes: 53 additions & 0 deletions internal/test/util/testfs/testfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package testfs

import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

type TestFs struct {
t *testing.T
d string
}

func New(t *testing.T) *TestFs {
d := fmt.Sprintf(".test/%s", t.Name())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for specifying a custom base, instead of relying on ioutil to pick the correct OS-specific tmp dir?

d, err := filepath.Abs(d)
require.NoError(t, err)
err = os.RemoveAll(d)
if err != nil && !os.IsNotExist(err) {
t.Fatalf("Error while removing old test directory: %s", err)
}

err = os.MkdirAll(d, os.ModePerm)
require.NoError(t, err)

return &TestFs{
t: t,
d: d,
}
}

func (tfs *TestFs) TempPath(name string) string {
outPath := path.Join(tfs.d, name)
tmpFile, err := ioutil.TempFile(tfs.d, name)
require.NoError(tfs.t, err)
err = tmpFile.Close()
require.NoError(tfs.t, err)
err = os.Rename(tmpFile.Name(), outPath)
require.NoError(tfs.t, err)
return outPath
}

func (tfs *TestFs) TempDir(name string) string {
outPath := path.Join(tfs.d, name)
err := os.MkdirAll(outPath, os.ModePerm)
require.NoError(tfs.t, err)
return outPath
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning for this package? It seems to wrap ioutil, but with some extras in there?
It'd be great if we can just use ioutil for this, to save having additional code to maintain.

https://golang.org/pkg/io/ioutil/#TempDir