...

Source file src/gotest.tools/v3/fs/file.go

Documentation: gotest.tools/v3/fs

     1  /*
     2  Package fs provides tools for creating temporary files, and testing the
     3  contents and structure of a directory.
     4  */
     5  package fs // import "gotest.tools/v3/fs"
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"runtime"
    11  	"strings"
    12  
    13  	"gotest.tools/v3/assert"
    14  	"gotest.tools/v3/internal/cleanup"
    15  )
    16  
    17  // Path objects return their filesystem path. Path may be implemented by a
    18  // real filesystem object (such as File and Dir) or by a type which updates
    19  // entries in a Manifest.
    20  type Path interface {
    21  	Path() string
    22  	Remove()
    23  }
    24  
    25  var (
    26  	_ Path = &Dir{}
    27  	_ Path = &File{}
    28  )
    29  
    30  // File is a temporary file on the filesystem
    31  type File struct {
    32  	path string
    33  }
    34  
    35  type helperT interface {
    36  	Helper()
    37  }
    38  
    39  // NewFile creates a new file in a temporary directory using prefix as part of
    40  // the filename. The PathOps are applied to the before returning the File.
    41  //
    42  // When used with Go 1.14+ the file will be automatically removed when the test
    43  // ends, unless the TEST_NOCLEANUP env var is set to true.
    44  func NewFile(t assert.TestingT, prefix string, ops ...PathOp) *File {
    45  	if ht, ok := t.(helperT); ok {
    46  		ht.Helper()
    47  	}
    48  	tempfile, err := os.CreateTemp("", cleanPrefix(prefix)+"-")
    49  	assert.NilError(t, err)
    50  
    51  	file := &File{path: tempfile.Name()}
    52  	cleanup.Cleanup(t, file.Remove)
    53  
    54  	assert.NilError(t, tempfile.Close())
    55  	assert.NilError(t, applyPathOps(file, ops))
    56  	return file
    57  }
    58  
    59  func cleanPrefix(prefix string) string {
    60  	// windows requires both / and \ are replaced
    61  	if runtime.GOOS == "windows" {
    62  		prefix = strings.Replace(prefix, string(os.PathSeparator), "-", -1)
    63  	}
    64  	return strings.Replace(prefix, "/", "-", -1)
    65  }
    66  
    67  // Path returns the full path to the file
    68  func (f *File) Path() string {
    69  	return f.path
    70  }
    71  
    72  // Remove the file
    73  func (f *File) Remove() {
    74  	_ = os.Remove(f.path)
    75  }
    76  
    77  // Dir is a temporary directory
    78  type Dir struct {
    79  	path string
    80  }
    81  
    82  // NewDir returns a new temporary directory using prefix as part of the directory
    83  // name. The PathOps are applied before returning the Dir.
    84  //
    85  // When used with Go 1.14+ the directory will be automatically removed when the test
    86  // ends, unless the TEST_NOCLEANUP env var is set to true.
    87  func NewDir(t assert.TestingT, prefix string, ops ...PathOp) *Dir {
    88  	if ht, ok := t.(helperT); ok {
    89  		ht.Helper()
    90  	}
    91  	path, err := os.MkdirTemp("", cleanPrefix(prefix)+"-")
    92  	assert.NilError(t, err)
    93  	dir := &Dir{path: path}
    94  	cleanup.Cleanup(t, dir.Remove)
    95  
    96  	assert.NilError(t, applyPathOps(dir, ops))
    97  	return dir
    98  }
    99  
   100  // Path returns the full path to the directory
   101  func (d *Dir) Path() string {
   102  	return d.path
   103  }
   104  
   105  // Remove the directory
   106  func (d *Dir) Remove() {
   107  	_ = os.RemoveAll(d.path)
   108  }
   109  
   110  // Join returns a new path with this directory as the base of the path
   111  func (d *Dir) Join(parts ...string) string {
   112  	return filepath.Join(append([]string{d.Path()}, parts...)...)
   113  }
   114  
   115  // DirFromPath returns a Dir for a path that already exists. No directory is created.
   116  // Unlike NewDir the directory will not be removed automatically when the test exits,
   117  // it is the callers responsibly to remove the directory.
   118  // DirFromPath can be used with Apply to modify an existing directory.
   119  //
   120  // If the path does not already exist, use NewDir instead.
   121  func DirFromPath(t assert.TestingT, path string, ops ...PathOp) *Dir {
   122  	if ht, ok := t.(helperT); ok {
   123  		ht.Helper()
   124  	}
   125  
   126  	dir := &Dir{path: path}
   127  	assert.NilError(t, applyPathOps(dir, ops))
   128  	return dir
   129  }
   130  

View as plain text