-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check: use Go 1.15's TempDir() code in preference to our own if avail…
…able.
- Loading branch information
1 parent
6ceb58a
commit 2ac1ab7
Showing
5 changed files
with
85 additions
and
76 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,9 @@ | ||
// +build go1.15 | ||
|
||
package check | ||
|
||
type tempDir struct{} | ||
|
||
func (td *tempDir) removeAll() { | ||
// nothing | ||
} |
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,58 @@ | ||
// +build !go1.15 | ||
|
||
package check | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"sync" | ||
) | ||
|
||
type tempDir struct { | ||
sync.Mutex | ||
path string | ||
counter int | ||
} | ||
|
||
func (td *tempDir) newPath() string { | ||
td.Lock() | ||
defer td.Unlock() | ||
if td.path == "" { | ||
path, err := ioutil.TempDir("", "check-") | ||
if err != nil { | ||
panic("Couldn't create temporary directory: " + err.Error()) | ||
} | ||
td.path = path | ||
} | ||
result := filepath.Join(td.path, strconv.Itoa(td.counter)) | ||
td.counter++ | ||
return result | ||
} | ||
|
||
func (td *tempDir) removeAll() { | ||
td.Lock() | ||
defer td.Unlock() | ||
if td.path != "" { | ||
err := os.RemoveAll(td.path) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "WARNING: Error cleaning up temporaries: "+err.Error()) | ||
} | ||
} | ||
} | ||
|
||
// TempDir returns a temporary directory for the test to use. The | ||
// directory is automatically cleaned up when the suite is complete. | ||
// Each subsequent call to c.TempDir returns a unique directory; if | ||
// the directory creation fails, TempDir terminates the test by | ||
// calling Fatal. | ||
func (c *C) TempDir() string { | ||
c.Helper() | ||
path := c.tempDir.newPath() | ||
if err := os.Mkdir(path, 0700); err != nil { | ||
c.Fatalf("Couldn't create temporary directory %s: %s", path, err.Error()) | ||
} | ||
return path | ||
} |
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