1// Copyright 2010 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package ioutil
6
7import (
8	"os"
9	"path/filepath"
10	"regexp"
11	"testing"
12)
13
14func TestTempFile(t *testing.T) {
15	f, err := TempFile("/_not_exists_", "foo")
16	if f != nil || err == nil {
17		t.Errorf("TempFile(`/_not_exists_`, `foo`) = %v, %v", f, err)
18	}
19
20	dir := os.TempDir()
21	f, err = TempFile(dir, "ioutil_test")
22	if f == nil || err != nil {
23		t.Errorf("TempFile(dir, `ioutil_test`) = %v, %v", f, err)
24	}
25	if f != nil {
26		f.Close()
27		os.Remove(f.Name())
28		re := regexp.MustCompile("^" + regexp.QuoteMeta(filepath.Join(dir, "ioutil_test")) + "[0-9]+$")
29		if !re.MatchString(f.Name()) {
30			t.Errorf("TempFile(`"+dir+"`, `ioutil_test`) created bad name %s", f.Name())
31		}
32	}
33}
34
35func TestTempDir(t *testing.T) {
36	name, err := TempDir("/_not_exists_", "foo")
37	if name != "" || err == nil {
38		t.Errorf("TempDir(`/_not_exists_`, `foo`) = %v, %v", name, err)
39	}
40
41	dir := os.TempDir()
42	name, err = TempDir(dir, "ioutil_test")
43	if name == "" || err != nil {
44		t.Errorf("TempDir(dir, `ioutil_test`) = %v, %v", name, err)
45	}
46	if name != "" {
47		os.Remove(name)
48		re := regexp.MustCompile("^" + regexp.QuoteMeta(filepath.Join(dir, "ioutil_test")) + "[0-9]+$")
49		if !re.MatchString(name) {
50			t.Errorf("TempDir(`"+dir+"`, `ioutil_test`) created bad name %s", name)
51		}
52	}
53}
54