forked from cortesi/modd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better, cleaner temp dir handler for testing.
- Loading branch information
Showing
4 changed files
with
49 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package utils | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
) | ||
|
||
// WithTempDir creates a temp directory, changes the current working directory | ||
// to it, and returns a function that can be called to clean up. Use it like | ||
// this: | ||
// defer WithTempDir(t)() | ||
func WithTempDir(t *testing.T) func() { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatalf("TempDir: %v", err) | ||
} | ||
tmpdir, err := ioutil.TempDir("", "") | ||
if err != nil { | ||
t.Fatalf("TempDir: %v", err) | ||
} | ||
err = os.Chdir(tmpdir) | ||
if err != nil { | ||
t.Fatalf("Chdir: %v", err) | ||
} | ||
return func() { | ||
err := os.Chdir(cwd) | ||
if err != nil { | ||
t.Fatalf("Chdir: %v", err) | ||
} | ||
err = os.RemoveAll(tmpdir) | ||
if err != nil { | ||
t.Fatalf("Removing tmpdir: %s", err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters