1 // Copyright 2014 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.
13 // This is exactly what a test would do without a TestMain.
14 // It's here only so that there is at least one package in the
15 // standard library with a TestMain, so that code is executed.
17 func TestMain(m
*testing
.M
) {
21 func TestTempDirInCleanup(t
*testing
.T
) {
24 t
.Run("test", func(t
*testing
.T
) {
31 fi
, err
:= os
.Stat(dir
)
33 t
.Fatalf("Directory %q from user Cleanup still exists", dir
)
35 if !os
.IsNotExist(err
) {
36 t
.Fatalf("Unexpected error: %v", err
)
40 func TestTempDirInBenchmark(t
*testing
.T
) {
41 testing
.Benchmark(func(b
*testing
.B
) {
42 if !b
.Run("test", func(b
*testing
.B
) {
43 // Add a loop so that the test won't fail. See issue 38677.
44 for i
:= 0; i
< b
.N
; i
++ {
48 t
.Fatal("Sub test failure in a benchmark")
53 func TestTempDir(t
*testing
.T
) {
55 t
.Run("InSubtest", testTempDir
)
56 t
.Run("test/subtest", testTempDir
)
57 t
.Run("test\\subtest", testTempDir
)
58 t
.Run("test:subtest", testTempDir
)
59 t
.Run("test/..", testTempDir
)
60 t
.Run("../test", testTempDir
)
61 t
.Run("test[]", testTempDir
)
62 t
.Run("test*", testTempDir
)
63 t
.Run("äöüéè", testTempDir
)
66 func testTempDir(t
*testing
.T
) {
67 dirCh
:= make(chan string, 1)
69 // Verify directory has been removed.
72 fi
, err
:= os
.Stat(dir
)
73 if os
.IsNotExist(err
) {
80 t
.Errorf("directory %q still exists: %v, isDir=%v", dir
, fi
, fi
.IsDir())
83 t
.Fatal("never received dir channel")
90 t
.Fatal("expected dir")
94 t
.Fatal("subsequent calls to TempDir returned the same directory")
96 if filepath
.Dir(dir
) != filepath
.Dir(dir2
) {
97 t
.Fatalf("calls to TempDir do not share a parent; got %q, %q", dir
, dir2
)
100 fi
, err
:= os
.Stat(dir
)
105 t
.Errorf("dir %q is not a dir", dir
)
107 files
, err
:= os
.ReadDir(dir
)
112 t
.Errorf("unexpected %d files in TempDir: %v", len(files
), files
)
115 glob
:= filepath
.Join(dir
, "*.txt")
116 if _
, err
:= filepath
.Glob(glob
); err
!= nil {
121 func TestSetenv(t
*testing
.T
) {
125 initialValueExists
bool
130 name
: "initial value exists",
131 key
: "GO_TEST_KEY_1",
132 initialValueExists
: true,
137 name
: "initial value exists but empty",
138 key
: "GO_TEST_KEY_2",
139 initialValueExists
: true,
144 name
: "initial value is not exists",
145 key
: "GO_TEST_KEY_3",
146 initialValueExists
: false,
152 for _
, test
:= range tests
{
153 if test
.initialValueExists
{
154 if err
:= os
.Setenv(test
.key
, test
.initialValue
); err
!= nil {
155 t
.Fatalf("unable to set env: got %v", err
)
158 os
.Unsetenv(test
.key
)
161 t
.Run(test
.name
, func(t
*testing
.T
) {
162 t
.Setenv(test
.key
, test
.newValue
)
163 if os
.Getenv(test
.key
) != test
.newValue
{
164 t
.Fatalf("unexpected value after t.Setenv: got %s, want %s", os
.Getenv(test
.key
), test
.newValue
)
168 got
, exists
:= os
.LookupEnv(test
.key
)
169 if got
!= test
.initialValue
{
170 t
.Fatalf("unexpected value after t.Setenv cleanup: got %s, want %s", got
, test
.initialValue
)
172 if exists
!= test
.initialValueExists
{
173 t
.Fatalf("unexpected value after t.Setenv cleanup: got %t, want %t", exists
, test
.initialValueExists
)
178 func TestSetenvWithParallelAfterSetenv(t
*testing
.T
) {
180 want
:= "testing: t.Parallel called after t.Setenv; cannot set environment variables in parallel tests"
181 if got
:= recover(); got
!= want
{
182 t
.Fatalf("expected panic; got %#v want %q", got
, want
)
186 t
.Setenv("GO_TEST_KEY_1", "value")
191 func TestSetenvWithParallelBeforeSetenv(t
*testing
.T
) {
193 want
:= "testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests"
194 if got
:= recover(); got
!= want
{
195 t
.Fatalf("expected panic; got %#v want %q", got
, want
)
201 t
.Setenv("GO_TEST_KEY_1", "value")