github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/testing/testing_test.go (about) 1 //go:build !windows 2 3 // TODO: implement readdir for windows, then enable this file 4 5 // Copyright 2014 The Go Authors. All rights reserved. 6 // Use of this source code is governed by a BSD-style 7 // license that can be found in the LICENSE file. 8 9 package testing_test 10 11 import ( 12 "errors" 13 "io/fs" 14 "os" 15 "path/filepath" 16 "runtime" 17 "testing" 18 ) 19 20 // This is exactly what a test would do without a TestMain. 21 // It's here only so that there is at least one package in the 22 // standard library with a TestMain, so that code is executed. 23 24 func TestMain(m *testing.M) { 25 os.Exit(m.Run()) 26 } 27 28 func TestTempDirInCleanup(t *testing.T) { 29 if runtime.GOOS == "wasip1" { 30 t.Log("Skipping. TODO: implement RemoveAll for wasi") 31 return 32 } 33 34 var dir string 35 36 t.Run("test", func(t *testing.T) { 37 t.Cleanup(func() { 38 dir = t.TempDir() 39 }) 40 _ = t.TempDir() 41 }) 42 43 fi, err := os.Stat(dir) 44 if fi != nil { 45 t.Fatalf("Directory %q from user Cleanup still exists", dir) 46 } 47 if !errors.Is(err, fs.ErrNotExist) { 48 t.Fatalf("Unexpected error: %v", err) 49 } 50 } 51 52 func TestTempDirInBenchmark(t *testing.T) { 53 testing.Benchmark(func(b *testing.B) { 54 if !b.Run("test", func(b *testing.B) { 55 // Add a loop so that the test won't fail. See issue 38677. 56 for i := 0; i < b.N; i++ { 57 _ = b.TempDir() 58 } 59 }) { 60 t.Fatal("Sub test failure in a benchmark") 61 } 62 }) 63 } 64 65 func TestTempDir(t *testing.T) { 66 if runtime.GOOS == "wasip1" { 67 t.Log("Skipping. TODO: implement RemoveAll for wasi") 68 return 69 } 70 71 testTempDir(t) 72 t.Run("InSubtest", testTempDir) 73 t.Run("test/subtest", testTempDir) 74 t.Run("test\\subtest", testTempDir) 75 t.Run("test:subtest", testTempDir) 76 t.Run("test/..", testTempDir) 77 t.Run("../test", testTempDir) 78 t.Run("test[]", testTempDir) 79 t.Run("test*", testTempDir) 80 t.Run("äöüéè", testTempDir) 81 } 82 83 func testTempDir(t *testing.T) { 84 dirCh := make(chan string, 1) 85 t.Cleanup(func() { 86 // Verify directory has been removed. 87 select { 88 case dir := <-dirCh: 89 fi, err := os.Stat(dir) 90 if errors.Is(err, fs.ErrNotExist) { 91 // All good 92 return 93 } 94 if err != nil { 95 t.Fatal(err) 96 } 97 t.Errorf("directory %q still exists: %v, isDir=%v", dir, fi, fi.IsDir()) 98 default: 99 if !t.Failed() { 100 t.Fatal("never received dir channel") 101 } 102 } 103 }) 104 105 dir := t.TempDir() 106 if dir == "" { 107 t.Fatal("expected dir") 108 } 109 dir2 := t.TempDir() 110 if dir == dir2 { 111 t.Fatal("subsequent calls to TempDir returned the same directory") 112 } 113 if filepath.Dir(dir) != filepath.Dir(dir2) { 114 t.Fatalf("calls to TempDir do not share a parent; got %q, %q", dir, dir2) 115 } 116 dirCh <- dir 117 fi, err := os.Stat(dir) 118 if err != nil { 119 t.Fatal(err) 120 } 121 if !fi.IsDir() { 122 t.Errorf("dir %q is not a dir", dir) 123 } 124 files, err := os.ReadDir(dir) 125 if err != nil { 126 t.Fatal(err) 127 } 128 if len(files) > 0 { 129 t.Errorf("unexpected %d files in TempDir: %v", len(files), files) 130 } 131 132 glob := filepath.Join(dir, "*.txt") 133 if _, err := filepath.Glob(glob); err != nil { 134 t.Error(err) 135 } 136 137 err = os.Remove(dir) 138 if err != nil { 139 t.Errorf("unexpected files in TempDir") 140 } 141 } 142 143 func TestSetenv(t *testing.T) { 144 tests := []struct { 145 name string 146 key string 147 initialValueExists bool 148 initialValue string 149 newValue string 150 }{ 151 { 152 name: "initial value exists", 153 key: "GO_TEST_KEY_1", 154 initialValueExists: true, 155 initialValue: "111", 156 newValue: "222", 157 }, 158 { 159 name: "initial value exists but empty", 160 key: "GO_TEST_KEY_2", 161 initialValueExists: true, 162 initialValue: "", 163 newValue: "222", 164 }, 165 { 166 name: "initial value is not exists", 167 key: "GO_TEST_KEY_3", 168 initialValueExists: false, 169 initialValue: "", 170 newValue: "222", 171 }, 172 } 173 174 for _, test := range tests { 175 if test.initialValueExists { 176 if err := os.Setenv(test.key, test.initialValue); err != nil { 177 t.Fatalf("unable to set env: got %v", err) 178 } 179 } else { 180 os.Unsetenv(test.key) 181 } 182 183 t.Run(test.name, func(t *testing.T) { 184 t.Setenv(test.key, test.newValue) 185 if os.Getenv(test.key) != test.newValue { 186 t.Fatalf("unexpected value after t.Setenv: got %s, want %s", os.Getenv(test.key), test.newValue) 187 } 188 }) 189 190 got, exists := os.LookupEnv(test.key) 191 if got != test.initialValue { 192 t.Fatalf("unexpected value after t.Setenv cleanup: got %s, want %s", got, test.initialValue) 193 } 194 if exists != test.initialValueExists { 195 t.Fatalf("unexpected value after t.Setenv cleanup: got %t, want %t", exists, test.initialValueExists) 196 } 197 } 198 } 199 200 func TestTesting(t *testing.T) { 201 if !testing.Testing() { 202 t.Error("Expected testing.Testing() to return true while in a test") 203 } 204 }