2018-06-08 12:23:38 -04:00
|
|
|
/*Package fs provides tools for creating temporary files, and testing the
|
|
|
|
contents and structure of a directory.
|
2017-08-14 14:28:08 -04:00
|
|
|
*/
|
2020-02-22 12:12:14 -05:00
|
|
|
package fs // import "gotest.tools/v3/fs"
|
2017-08-14 14:28:08 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-11-14 06:28:16 -05:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2017-08-14 14:28:08 -04:00
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/internal/cleanup"
|
2017-08-14 14:28:08 -04:00
|
|
|
)
|
|
|
|
|
2018-06-08 12:23:38 -04:00
|
|
|
// Path objects return their filesystem path. Path may be implemented by a
|
|
|
|
// real filesystem object (such as File and Dir) or by a type which updates
|
|
|
|
// entries in a Manifest.
|
2017-08-14 14:28:08 -04:00
|
|
|
type Path interface {
|
|
|
|
Path() string
|
2018-02-28 10:11:02 -05:00
|
|
|
Remove()
|
2017-08-14 14:28:08 -04:00
|
|
|
}
|
|
|
|
|
2018-02-28 10:11:02 -05:00
|
|
|
var (
|
|
|
|
_ Path = &Dir{}
|
|
|
|
_ Path = &File{}
|
|
|
|
)
|
|
|
|
|
2017-08-14 14:28:08 -04:00
|
|
|
// File is a temporary file on the filesystem
|
|
|
|
type File struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2018-02-28 10:11:02 -05:00
|
|
|
type helperT interface {
|
|
|
|
Helper()
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:28:08 -04:00
|
|
|
// NewFile creates a new file in a temporary directory using prefix as part of
|
|
|
|
// the filename. The PathOps are applied to the before returning the File.
|
2020-02-22 12:12:14 -05:00
|
|
|
//
|
|
|
|
// When used with Go 1.14+ the file will be automatically removed when the test
|
|
|
|
// ends, unless the TEST_NOCLEANUP env var is set to true.
|
2018-02-28 10:11:02 -05:00
|
|
|
func NewFile(t assert.TestingT, prefix string, ops ...PathOp) *File {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2018-11-14 06:28:16 -05:00
|
|
|
tempfile, err := ioutil.TempFile("", cleanPrefix(prefix)+"-")
|
2018-02-28 10:11:02 -05:00
|
|
|
assert.NilError(t, err)
|
2020-02-22 12:12:14 -05:00
|
|
|
|
2017-08-14 14:28:08 -04:00
|
|
|
file := &File{path: tempfile.Name()}
|
2020-02-22 12:12:14 -05:00
|
|
|
cleanup.Cleanup(t, file.Remove)
|
|
|
|
|
2018-02-28 10:11:02 -05:00
|
|
|
assert.NilError(t, tempfile.Close())
|
2019-04-12 19:47:37 -04:00
|
|
|
assert.NilError(t, applyPathOps(file, ops))
|
2017-08-14 14:28:08 -04:00
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
2018-11-14 06:28:16 -05:00
|
|
|
func cleanPrefix(prefix string) string {
|
|
|
|
// windows requires both / and \ are replaced
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
prefix = strings.Replace(prefix, string(os.PathSeparator), "-", -1)
|
|
|
|
}
|
|
|
|
return strings.Replace(prefix, "/", "-", -1)
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:28:08 -04:00
|
|
|
// Path returns the full path to the file
|
|
|
|
func (f *File) Path() string {
|
|
|
|
return f.path
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the file
|
|
|
|
func (f *File) Remove() {
|
|
|
|
// nolint: errcheck
|
|
|
|
os.Remove(f.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dir is a temporary directory
|
|
|
|
type Dir struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDir returns a new temporary directory using prefix as part of the directory
|
|
|
|
// name. The PathOps are applied before returning the Dir.
|
2020-02-22 12:12:14 -05:00
|
|
|
//
|
|
|
|
// When used with Go 1.14+ the directory will be automatically removed when the test
|
|
|
|
// ends, unless the TEST_NOCLEANUP env var is set to true.
|
2018-02-28 10:11:02 -05:00
|
|
|
func NewDir(t assert.TestingT, prefix string, ops ...PathOp) *Dir {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2018-11-14 06:28:16 -05:00
|
|
|
path, err := ioutil.TempDir("", cleanPrefix(prefix)+"-")
|
2018-02-28 10:11:02 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-14 14:28:08 -04:00
|
|
|
dir := &Dir{path: path}
|
2020-02-22 12:12:14 -05:00
|
|
|
cleanup.Cleanup(t, dir.Remove)
|
|
|
|
|
2019-04-12 19:47:37 -04:00
|
|
|
assert.NilError(t, applyPathOps(dir, ops))
|
2017-08-14 14:28:08 -04:00
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path returns the full path to the directory
|
|
|
|
func (d *Dir) Path() string {
|
|
|
|
return d.path
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the directory
|
|
|
|
func (d *Dir) Remove() {
|
|
|
|
// nolint: errcheck
|
|
|
|
os.RemoveAll(d.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join returns a new path with this directory as the base of the path
|
|
|
|
func (d *Dir) Join(parts ...string) string {
|
|
|
|
return filepath.Join(append([]string{d.Path()}, parts...)...)
|
|
|
|
}
|