2010-06-03 17:29:34 -06:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ioutil_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "io/ioutil"
|
|
|
|
"os"
|
2010-06-30 14:52:34 -06:00
|
|
|
"regexp"
|
2010-06-03 17:29:34 -06:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTempFile(t *testing.T) {
|
|
|
|
f, err := TempFile("/_not_exists_", "foo")
|
|
|
|
if f != nil || err == nil {
|
|
|
|
t.Errorf("TempFile(`/_not_exists_`, `foo`) = %v, %v", f, err)
|
|
|
|
}
|
|
|
|
|
2010-06-30 14:52:34 -06:00
|
|
|
dir := os.TempDir()
|
|
|
|
f, err = TempFile(dir, "ioutil_test")
|
2010-06-03 17:29:34 -06:00
|
|
|
if f == nil || err != nil {
|
2010-06-30 14:52:34 -06:00
|
|
|
t.Errorf("TempFile(dir, `ioutil_test`) = %v, %v", f, err)
|
2010-06-03 17:29:34 -06:00
|
|
|
}
|
2010-06-30 14:52:34 -06:00
|
|
|
if f != nil {
|
2011-03-03 12:51:49 -07:00
|
|
|
f.Close()
|
|
|
|
os.Remove(f.Name())
|
2010-10-28 17:54:24 -06:00
|
|
|
re := regexp.MustCompile("^" + regexp.QuoteMeta(dir) + "/ioutil_test[0-9]+$")
|
2010-06-30 14:52:34 -06:00
|
|
|
if !re.MatchString(f.Name()) {
|
|
|
|
t.Errorf("TempFile(`"+dir+"`, `ioutil_test`) created bad name %s", f.Name())
|
|
|
|
}
|
2010-06-03 17:29:34 -06:00
|
|
|
}
|
2011-03-03 12:51:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTempDir(t *testing.T) {
|
|
|
|
name, err := TempDir("/_not_exists_", "foo")
|
|
|
|
if name != "" || err == nil {
|
|
|
|
t.Errorf("TempDir(`/_not_exists_`, `foo`) = %v, %v", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := os.TempDir()
|
|
|
|
name, err = TempDir(dir, "ioutil_test")
|
|
|
|
if name == "" || err != nil {
|
|
|
|
t.Errorf("TempDir(dir, `ioutil_test`) = %v, %v", name, err)
|
|
|
|
}
|
|
|
|
if name != "" {
|
|
|
|
os.Remove(name)
|
|
|
|
re := regexp.MustCompile("^" + regexp.QuoteMeta(dir) + "/ioutil_test[0-9]+$")
|
|
|
|
if !re.MatchString(name) {
|
|
|
|
t.Errorf("TempDir(`"+dir+"`, `ioutil_test`) created bad name %s", name)
|
|
|
|
}
|
|
|
|
}
|
2010-06-03 17:29:34 -06:00
|
|
|
}
|