Source file src/testing/testing_test.go

     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.
     4  
     5  package testing_test
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  )
    12  
    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.
    16  
    17  func TestMain(m *testing.M) {
    18  	os.Exit(m.Run())
    19  }
    20  
    21  func TestTempDirInCleanup(t *testing.T) {
    22  	var dir string
    23  
    24  	t.Run("test", func(t *testing.T) {
    25  		t.Cleanup(func() {
    26  			dir = t.TempDir()
    27  		})
    28  		_ = t.TempDir()
    29  	})
    30  
    31  	fi, err := os.Stat(dir)
    32  	if fi != nil {
    33  		t.Fatalf("Directory %q from user Cleanup still exists", dir)
    34  	}
    35  	if !os.IsNotExist(err) {
    36  		t.Fatalf("Unexpected error: %v", err)
    37  	}
    38  }
    39  
    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()
    46  			}
    47  		}) {
    48  			t.Fatal("Sub test failure in a benchmark")
    49  		}
    50  	})
    51  }
    52  
    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)
    64  }
    65  
    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
    76  			}
    77  			if err != nil {
    78  				t.Fatal(err)
    79  			}
    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")
    84  			}
    85  		}
    86  	})
    87  
    88  	dir := t.TempDir()
    89  	if dir == "" {
    90  		t.Fatal("expected dir")
    91  	}
    92  	dir2 := t.TempDir()
    93  	if dir == dir2 {
    94  		t.Fatal("subsequent calls to TempDir returned the same directory")
    95  	}
    96  	if filepath.Dir(dir) != filepath.Dir(dir2) {
    97  		t.Fatalf("calls to TempDir do not share a parent; got %q, %q", dir, dir2)
    98  	}
    99  	dirCh <- dir
   100  	fi, err := os.Stat(dir)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	if !fi.IsDir() {
   105  		t.Errorf("dir %q is not a dir", dir)
   106  	}
   107  	files, err := os.ReadDir(dir)
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	if len(files) > 0 {
   112  		t.Errorf("unexpected %d files in TempDir: %v", len(files), files)
   113  	}
   114  
   115  	glob := filepath.Join(dir, "*.txt")
   116  	if _, err := filepath.Glob(glob); err != nil {
   117  		t.Error(err)
   118  	}
   119  }
   120  
   121  func TestSetenv(t *testing.T) {
   122  	tests := []struct {
   123  		name               string
   124  		key                string
   125  		initialValueExists bool
   126  		initialValue       string
   127  		newValue           string
   128  	}{
   129  		{
   130  			name:               "initial value exists",
   131  			key:                "GO_TEST_KEY_1",
   132  			initialValueExists: true,
   133  			initialValue:       "111",
   134  			newValue:           "222",
   135  		},
   136  		{
   137  			name:               "initial value exists but empty",
   138  			key:                "GO_TEST_KEY_2",
   139  			initialValueExists: true,
   140  			initialValue:       "",
   141  			newValue:           "222",
   142  		},
   143  		{
   144  			name:               "initial value is not exists",
   145  			key:                "GO_TEST_KEY_3",
   146  			initialValueExists: false,
   147  			initialValue:       "",
   148  			newValue:           "222",
   149  		},
   150  	}
   151  
   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)
   156  			}
   157  		} else {
   158  			os.Unsetenv(test.key)
   159  		}
   160  
   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)
   165  			}
   166  		})
   167  
   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)
   171  		}
   172  		if exists != test.initialValueExists {
   173  			t.Fatalf("unexpected value after t.Setenv cleanup: got %t, want %t", exists, test.initialValueExists)
   174  		}
   175  	}
   176  }
   177  
   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)
   183  		}
   184  	}()
   185  
   186  	t.Setenv("GO_TEST_KEY_1", "value")
   187  
   188  	t.Parallel()
   189  }
   190  
   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)
   196  		}
   197  	}()
   198  
   199  	t.Parallel()
   200  
   201  	t.Setenv("GO_TEST_KEY_1", "value")
   202  }
   203  

View as plain text