Daily bump.
[official-gcc.git] / libgo / go / testing / testing_test.go
blob08ae23991f18f6484e0cd10162ec879c5ca9bc90
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.
5 package testing_test
7 import (
8 "os"
9 "path/filepath"
10 "testing"
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) {
18 os.Exit(m.Run())
21 func TestTempDirInCleanup(t *testing.T) {
22 var dir string
24 t.Run("test", func(t *testing.T) {
25 t.Cleanup(func() {
26 dir = t.TempDir()
28 _ = t.TempDir()
31 fi, err := os.Stat(dir)
32 if fi != nil {
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++ {
45 _ = b.TempDir()
47 }) {
48 t.Fatal("Sub test failure in a benchmark")
53 func TestTempDir(t *testing.T) {
54 testTempDir(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)
68 t.Cleanup(func() {
69 // Verify directory has been removed.
70 select {
71 case dir := <-dirCh:
72 fi, err := os.Stat(dir)
73 if os.IsNotExist(err) {
74 // All good
75 return
77 if err != nil {
78 t.Fatal(err)
80 t.Errorf("directory %q still exists: %v, isDir=%v", dir, fi, fi.IsDir())
81 default:
82 if !t.Failed() {
83 t.Fatal("never received dir channel")
88 dir := t.TempDir()
89 if dir == "" {
90 t.Fatal("expected dir")
92 dir2 := t.TempDir()
93 if dir == dir2 {
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)
99 dirCh <- dir
100 fi, err := os.Stat(dir)
101 if err != nil {
102 t.Fatal(err)
104 if !fi.IsDir() {
105 t.Errorf("dir %q is not a dir", dir)
107 files, err := os.ReadDir(dir)
108 if err != nil {
109 t.Fatal(err)
111 if len(files) > 0 {
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 {
117 t.Error(err)
121 func TestSetenv(t *testing.T) {
122 tests := []struct {
123 name string
124 key string
125 initialValueExists bool
126 initialValue string
127 newValue string
130 name: "initial value exists",
131 key: "GO_TEST_KEY_1",
132 initialValueExists: true,
133 initialValue: "111",
134 newValue: "222",
137 name: "initial value exists but empty",
138 key: "GO_TEST_KEY_2",
139 initialValueExists: true,
140 initialValue: "",
141 newValue: "222",
144 name: "initial value is not exists",
145 key: "GO_TEST_KEY_3",
146 initialValueExists: false,
147 initialValue: "",
148 newValue: "222",
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)
157 } else {
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) {
179 defer func() {
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")
188 t.Parallel()
191 func TestSetenvWithParallelBeforeSetenv(t *testing.T) {
192 defer func() {
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)
199 t.Parallel()
201 t.Setenv("GO_TEST_KEY_1", "value")