github.com/grahambrereton-form3/tilt@v0.10.18/internal/testutils/tempdir/temp_dir_fixture.go (about)

     1  package tempdir
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/windmilleng/wmclient/pkg/os/temp"
    11  )
    12  
    13  type TempDirFixture struct {
    14  	t   testing.TB
    15  	dir *temp.TempDir
    16  }
    17  
    18  func NewTempDirFixture(t testing.TB) *TempDirFixture {
    19  	name := t.Name()
    20  	name = strings.Replace(name, "/", "-", -1)
    21  	dir, err := temp.NewDir(name)
    22  	if err != nil {
    23  		t.Fatalf("Error making temp dir: %v", err)
    24  	}
    25  
    26  	return &TempDirFixture{
    27  		t:   t,
    28  		dir: dir,
    29  	}
    30  }
    31  
    32  func (f *TempDirFixture) T() testing.TB {
    33  	return f.t
    34  }
    35  
    36  func (f *TempDirFixture) Path() string {
    37  	return f.dir.Path()
    38  }
    39  
    40  func (f *TempDirFixture) JoinPath(path ...string) string {
    41  	p := []string{}
    42  	isAbs := len(path) > 0 && filepath.IsAbs(path[0])
    43  	if isAbs {
    44  		if !strings.HasPrefix(path[0], f.Path()) {
    45  			f.t.Fatalf("Path outside fixture tempdir are forbidden: %s", path[0])
    46  		}
    47  	} else {
    48  		p = append(p, f.Path())
    49  	}
    50  
    51  	p = append(p, path...)
    52  	return filepath.Join(p...)
    53  }
    54  
    55  func (f *TempDirFixture) JoinPaths(paths []string) []string {
    56  	joined := make([]string, len(paths))
    57  	for i, p := range paths {
    58  		joined[i] = f.JoinPath(p)
    59  	}
    60  	return joined
    61  }
    62  
    63  // Returns the full path to the file written.
    64  func (f *TempDirFixture) WriteFile(path string, contents string) string {
    65  	fullPath := f.JoinPath(path)
    66  	base := filepath.Dir(fullPath)
    67  	err := os.MkdirAll(base, os.FileMode(0777))
    68  	if err != nil {
    69  		f.t.Fatal(err)
    70  	}
    71  	err = ioutil.WriteFile(fullPath, []byte(contents), os.FileMode(0777))
    72  	if err != nil {
    73  		f.t.Fatal(err)
    74  	}
    75  	return fullPath
    76  }
    77  
    78  // Returns the full path to the file written.
    79  func (f *TempDirFixture) CopyFile(originalPath, newPath string) {
    80  	contents, err := ioutil.ReadFile(originalPath)
    81  	if err != nil {
    82  		f.t.Fatal(err)
    83  	}
    84  	f.WriteFile(newPath, string(contents))
    85  }
    86  
    87  func (f *TempDirFixture) WriteSymlink(linkContents, destPath string) {
    88  	fullDestPath := f.JoinPath(destPath)
    89  	err := os.MkdirAll(filepath.Dir(fullDestPath), os.FileMode(0777))
    90  	if err != nil {
    91  		f.t.Fatal(err)
    92  	}
    93  	err = os.Symlink(linkContents, fullDestPath)
    94  	if err != nil {
    95  		f.t.Fatal(err)
    96  	}
    97  }
    98  
    99  func (f *TempDirFixture) MkdirAll(path string) {
   100  	fullPath := f.JoinPath(path)
   101  	err := os.MkdirAll(fullPath, os.FileMode(0777))
   102  	if err != nil {
   103  		f.t.Fatal(err)
   104  	}
   105  }
   106  
   107  func (f *TempDirFixture) TouchFiles(paths []string) {
   108  	for _, p := range paths {
   109  		f.WriteFile(p, "")
   110  	}
   111  }
   112  
   113  func (f *TempDirFixture) Rm(pathInRepo string) {
   114  	fullPath := f.JoinPath(pathInRepo)
   115  	err := os.RemoveAll(fullPath)
   116  	if err != nil {
   117  		f.t.Fatal(err)
   118  	}
   119  }
   120  
   121  func (f *TempDirFixture) NewFile(prefix string) (*os.File, error) {
   122  	return ioutil.TempFile(f.dir.Path(), prefix)
   123  }
   124  
   125  func (f *TempDirFixture) TempDir(prefix string) string {
   126  	name, err := ioutil.TempDir(f.dir.Path(), prefix)
   127  	if err != nil {
   128  		f.t.Fatal(err)
   129  	}
   130  	return name
   131  }
   132  
   133  func (f *TempDirFixture) TearDown() {
   134  	err := f.dir.TearDown()
   135  	if err != nil {
   136  		f.t.Fatal(err)
   137  	}
   138  }