mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-13 07:23:39 +00:00
os: implement and test os.MkdirTemp
This commit is contained in:
+116
-2
@@ -8,12 +8,19 @@ import (
|
||||
"errors"
|
||||
. "os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateTemp(t *testing.T) {
|
||||
nonexistentDir := filepath.Join("_also_not_exists_", "_not_exists_")
|
||||
dir, err := MkdirTemp("", "TestCreateTempBadDir")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer RemoveAll(dir)
|
||||
|
||||
nonexistentDir := filepath.Join(dir, "_not_exists_")
|
||||
f, err := CreateTemp(nonexistentDir, "foo")
|
||||
if f != nil || err == nil {
|
||||
t.Errorf("CreateTemp(%q, `foo`) = %v, %v", nonexistentDir, f, err)
|
||||
@@ -43,7 +50,11 @@ func TestCreateTempPattern(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateTempBadPattern(t *testing.T) {
|
||||
tmpDir := TempDir()
|
||||
tmpDir, err := MkdirTemp("", t.Name())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer RemoveAll(tmpDir)
|
||||
|
||||
const sep = string(PathSeparator)
|
||||
tests := []struct {
|
||||
@@ -77,3 +88,106 @@ func TestCreateTempBadPattern(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMkdirTemp(t *testing.T) {
|
||||
name, err := MkdirTemp("/_not_exists_", "foo")
|
||||
if name != "" || err == nil {
|
||||
t.Errorf("MkdirTemp(`/_not_exists_`, `foo`) = %v, %v", name, err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
pattern string
|
||||
wantPrefix, wantSuffix string
|
||||
}{
|
||||
{"tempfile_test", "tempfile_test", ""},
|
||||
{"tempfile_test*", "tempfile_test", ""},
|
||||
{"tempfile_test*xyz", "tempfile_test", "xyz"},
|
||||
}
|
||||
|
||||
dir := filepath.Clean(TempDir())
|
||||
|
||||
runTestMkdirTemp := func(t *testing.T, pattern, wantRePat string) {
|
||||
name, err := MkdirTemp(dir, pattern)
|
||||
if name == "" || err != nil {
|
||||
t.Fatalf("MkdirTemp(dir, `tempfile_test`) = %v, %v", name, err)
|
||||
}
|
||||
defer Remove(name)
|
||||
|
||||
re := regexp.MustCompile(wantRePat)
|
||||
if !re.MatchString(name) {
|
||||
t.Errorf("MkdirTemp(%q, %q) created bad name\n\t%q\ndid not match pattern\n\t%q", dir, pattern, name, wantRePat)
|
||||
}
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.pattern, func(t *testing.T) {
|
||||
wantRePat := "^" + regexp.QuoteMeta(filepath.Join(dir, tt.wantPrefix)) + "[0-9]+" + regexp.QuoteMeta(tt.wantSuffix) + "$"
|
||||
runTestMkdirTemp(t, tt.pattern, wantRePat)
|
||||
})
|
||||
}
|
||||
|
||||
// Separately testing "*xyz" (which has no prefix). That is when constructing the
|
||||
// pattern to assert on, as in the previous loop, using filepath.Join for an empty
|
||||
// prefix filepath.Join(dir, ""), produces the pattern:
|
||||
// ^<DIR>[0-9]+xyz$
|
||||
// yet we just want to match
|
||||
// "^<DIR>/[0-9]+xyz"
|
||||
t.Run("*xyz", func(t *testing.T) {
|
||||
wantRePat := "^" + regexp.QuoteMeta(filepath.Join(dir)) + regexp.QuoteMeta(string(filepath.Separator)) + "[0-9]+xyz$"
|
||||
runTestMkdirTemp(t, "*xyz", wantRePat)
|
||||
})
|
||||
}
|
||||
|
||||
// test that we return a nice error message if the dir argument to TempDir doesn't
|
||||
// exist (or that it's empty and TempDir doesn't exist)
|
||||
func TestMkdirTempBadDir(t *testing.T) {
|
||||
dir, err := MkdirTemp("", "MkdirTempBadDir")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer RemoveAll(dir)
|
||||
|
||||
badDir := filepath.Join(dir, "not-exist")
|
||||
_, err = MkdirTemp(badDir, "foo")
|
||||
// TODO: when we drop support for go 1.15, PathError should move to fs
|
||||
if pe, ok := err.(*PathError); !ok || !IsNotExist(err) || pe.Path != badDir {
|
||||
t.Errorf("TempDir error = %#v; want PathError for path %q satisifying IsNotExist", err, badDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMkdirTempBadPattern(t *testing.T) {
|
||||
tmpDir, err := MkdirTemp("", t.Name())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer RemoveAll(tmpDir)
|
||||
|
||||
const sep = string(PathSeparator)
|
||||
tests := []struct {
|
||||
pattern string
|
||||
wantErr bool
|
||||
}{
|
||||
{"ioutil*test", false},
|
||||
{"tempfile_test*foo", false},
|
||||
{"tempfile_test" + sep + "foo", true},
|
||||
{"tempfile_test*" + sep + "foo", true},
|
||||
{"tempfile_test" + sep + "*foo", true},
|
||||
{sep + "tempfile_test" + sep + "*foo", true},
|
||||
{"tempfile_test*foo" + sep, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.pattern, func(t *testing.T) {
|
||||
_, err := MkdirTemp(tmpDir, tt.pattern)
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Errorf("MkdirTemp(..., %#q) succeeded, expected error", tt.pattern)
|
||||
}
|
||||
if !errors.Is(err, ErrPatternHasSeparator) {
|
||||
t.Errorf("MkdirTemp(..., %#q): %v, expected ErrPatternHasSeparator", tt.pattern, err)
|
||||
}
|
||||
} else if err != nil {
|
||||
t.Errorf("MkdirTemp(..., %#q): %v", tt.pattern, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user