File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -113,3 +113,19 @@ func (d *Dir) Remove() {
113113func (d * Dir ) Join (parts ... string ) string {
114114 return filepath .Join (append ([]string {d .Path ()}, parts ... )... )
115115}
116+
117+ // DirFromPath returns a Dir for a path that already exists. No directory is created.
118+ // Unlike NewDir the directory will not be removed automatically when the test exits,
119+ // it is the callers responsibly to remove the directory.
120+ // DirFromPath can be used with Apply to modify an existing directory.
121+ //
122+ // If the path does not already exist, use NewDir instead.
123+ func DirFromPath (t assert.TestingT , path string , ops ... PathOp ) * Dir {
124+ if ht , ok := t .(helperT ); ok {
125+ ht .Helper ()
126+ }
127+
128+ dir := & Dir {path : path }
129+ assert .NilError (t , applyPathOps (dir , ops ))
130+ return dir
131+ }
Original file line number Diff line number Diff line change 11package fs_test
22
33import (
4+ "errors"
5+ "io/ioutil"
46 "os"
7+ "path/filepath"
58 "testing"
69
710 "gotest.tools/v3/assert"
@@ -92,3 +95,24 @@ func TestNewDir_IntegrationWithCleanup(t *testing.T) {
9295 assert .ErrorType (t , err , os .IsNotExist )
9396 })
9497}
98+
99+ func TestDirFromPath (t * testing.T ) {
100+ tmpdir , err := ioutil .TempDir ("" , t .Name ())
101+ assert .NilError (t , err )
102+ t .Cleanup (func () {
103+ os .RemoveAll (tmpdir )
104+ })
105+
106+ dir := fs .DirFromPath (t , tmpdir , fs .WithFile ("newfile" , "" ))
107+
108+ _ , err = os .Stat (dir .Join ("newfile" ))
109+ assert .NilError (t , err )
110+
111+ assert .Equal (t , dir .Path (), tmpdir )
112+ assert .Equal (t , dir .Join ("newfile" ), filepath .Join (tmpdir , "newfile" ))
113+
114+ dir .Remove ()
115+
116+ _ , err = os .Stat (tmpdir )
117+ assert .Assert (t , errors .Is (err , os .ErrNotExist ))
118+ }
You can’t perform that action at this time.
0 commit comments