TestRandomUnixTmpDirPath platform agnostic

Signed-off-by: John Howard <jhoward@microsoft.com>
This commit is contained in:
John Howard 2015-09-24 09:37:07 -07:00
parent f09705e6aa
commit b5e2a90aa3
2 changed files with 15 additions and 6 deletions

View file

@ -9,8 +9,9 @@ import (
"io" "io"
"os" "os"
"os/exec" "os/exec"
"path" "path/filepath"
"reflect" "reflect"
"runtime"
"strings" "strings"
"syscall" "syscall"
"time" "time"
@ -244,10 +245,14 @@ func ListTar(f io.Reader) ([]string, error) {
} }
} }
// RandomUnixTmpDirPath provides a temporary unix path with rand string appended. // RandomTmpDirPath provides a temporary path with rand string appended.
// does not create or checks if it exists. // does not create or checks if it exists.
func RandomUnixTmpDirPath(s string) string { func RandomTmpDirPath(s string) string {
return path.Join("/tmp", fmt.Sprintf("%s.%s", s, stringutils.GenerateRandomAlphaOnlyString(10))) tmp := "/tmp"
if runtime.GOOS == "windows" {
tmp = os.Getenv("TEMP")
}
return filepath.Join(tmp, fmt.Sprintf("%s.%s", s, stringutils.GenerateRandomAlphaOnlyString(10)))
} }
// ConsumeWithSpeed reads chunkSize bytes from reader after every interval. // ConsumeWithSpeed reads chunkSize bytes from reader after every interval.

View file

@ -6,6 +6,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path" "path"
"runtime"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -341,10 +342,13 @@ func TestListTar(t *testing.T) {
} }
} }
func TestRandomUnixTmpDirPath(t *testing.T) { func TestRandomTmpDirPath(t *testing.T) {
path := RandomUnixTmpDirPath("something") path := RandomTmpDirPath("something")
prefix := "/tmp/something" prefix := "/tmp/something"
if runtime.GOOS == "windows" {
prefix = os.Getenv("TEMP") + `\something`
}
expectedSize := len(prefix) + 11 expectedSize := len(prefix) + 11
if !strings.HasPrefix(path, prefix) { if !strings.HasPrefix(path, prefix) {