github.com/AndrienkoAleksandr/go@v0.0.19/src/testing/testing_test.go (about)

     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  	"bytes"
     9  	"internal/testenv"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  )
    14  
    15  // This is exactly what a test would do without a TestMain.
    16  // It's here only so that there is at least one package in the
    17  // standard library with a TestMain, so that code is executed.
    18  
    19  func TestMain(m *testing.M) {
    20  	os.Exit(m.Run())
    21  }
    22  
    23  func TestTempDirInCleanup(t *testing.T) {
    24  	var dir string
    25  
    26  	t.Run("test", func(t *testing.T) {
    27  		t.Cleanup(func() {
    28  			dir = t.TempDir()
    29  		})
    30  		_ = t.TempDir()
    31  	})
    32  
    33  	fi, err := os.Stat(dir)
    34  	if fi != nil {
    35  		t.Fatalf("Directory %q from user Cleanup still exists", dir)
    36  	}
    37  	if !os.IsNotExist(err) {
    38  		t.Fatalf("Unexpected error: %v", err)
    39  	}
    40  }
    41  
    42  func TestTempDirInBenchmark(t *testing.T) {
    43  	testing.Benchmark(func(b *testing.B) {
    44  		if !b.Run("test", func(b *testing.B) {
    45  			// Add a loop so that the test won't fail. See issue 38677.
    46  			for i := 0; i < b.N; i++ {
    47  				_ = b.TempDir()
    48  			}
    49  		}) {
    50  			t.Fatal("Sub test failure in a benchmark")
    51  		}
    52  	})
    53  }
    54  
    55  func TestTempDir(t *testing.T) {
    56  	testTempDir(t)
    57  	t.Run("InSubtest", testTempDir)
    58  	t.Run("test/subtest", testTempDir)
    59  	t.Run("test\\subtest", testTempDir)
    60  	t.Run("test:subtest", testTempDir)
    61  	t.Run("test/..", testTempDir)
    62  	t.Run("../test", testTempDir)
    63  	t.Run("test[]", testTempDir)
    64  	t.Run("test*", testTempDir)
    65  	t.Run("äöüéè", testTempDir)
    66  }
    67  
    68  func testTempDir(t *testing.T) {
    69  	dirCh := make(chan string, 1)
    70  	t.Cleanup(func() {
    71  		// Verify directory has been removed.
    72  		select {
    73  		case dir := <-dirCh:
    74  			fi, err := os.Stat(dir)
    75  			if os.IsNotExist(err) {
    76  				// All good
    77  				return
    78  			}
    79  			if err != nil {
    80  				t.Fatal(err)
    81  			}
    82  			t.Errorf("directory %q still exists: %v, isDir=%v", dir, fi, fi.IsDir())
    83  		default:
    84  			if !t.Failed() {
    85  				t.Fatal("never received dir channel")
    86  			}
    87  		}
    88  	})
    89  
    90  	dir := t.TempDir()
    91  	if dir == "" {
    92  		t.Fatal("expected dir")
    93  	}
    94  	dir2 := t.TempDir()
    95  	if dir == dir2 {
    96  		t.Fatal("subsequent calls to TempDir returned the same directory")
    97  	}
    98  	if filepath.Dir(dir) != filepath.Dir(dir2) {
    99  		t.Fatalf("calls to TempDir do not share a parent; got %q, %q", dir, dir2)
   100  	}
   101  	dirCh <- dir
   102  	fi, err := os.Stat(dir)
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	if !fi.IsDir() {
   107  		t.Errorf("dir %q is not a dir", dir)
   108  	}
   109  	files, err := os.ReadDir(dir)
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	if len(files) > 0 {
   114  		t.Errorf("unexpected %d files in TempDir: %v", len(files), files)
   115  	}
   116  
   117  	glob := filepath.Join(dir, "*.txt")
   118  	if _, err := filepath.Glob(glob); err != nil {
   119  		t.Error(err)
   120  	}
   121  }
   122  
   123  func TestSetenv(t *testing.T) {
   124  	tests := []struct {
   125  		name               string
   126  		key                string
   127  		initialValueExists bool
   128  		initialValue       string
   129  		newValue           string
   130  	}{
   131  		{
   132  			name:               "initial value exists",
   133  			key:                "GO_TEST_KEY_1",
   134  			initialValueExists: true,
   135  			initialValue:       "111",
   136  			newValue:           "222",
   137  		},
   138  		{
   139  			name:               "initial value exists but empty",
   140  			key:                "GO_TEST_KEY_2",
   141  			initialValueExists: true,
   142  			initialValue:       "",
   143  			newValue:           "222",
   144  		},
   145  		{
   146  			name:               "initial value is not exists",
   147  			key:                "GO_TEST_KEY_3",
   148  			initialValueExists: false,
   149  			initialValue:       "",
   150  			newValue:           "222",
   151  		},
   152  	}
   153  
   154  	for _, test := range tests {
   155  		if test.initialValueExists {
   156  			if err := os.Setenv(test.key, test.initialValue); err != nil {
   157  				t.Fatalf("unable to set env: got %v", err)
   158  			}
   159  		} else {
   160  			os.Unsetenv(test.key)
   161  		}
   162  
   163  		t.Run(test.name, func(t *testing.T) {
   164  			t.Setenv(test.key, test.newValue)
   165  			if os.Getenv(test.key) != test.newValue {
   166  				t.Fatalf("unexpected value after t.Setenv: got %s, want %s", os.Getenv(test.key), test.newValue)
   167  			}
   168  		})
   169  
   170  		got, exists := os.LookupEnv(test.key)
   171  		if got != test.initialValue {
   172  			t.Fatalf("unexpected value after t.Setenv cleanup: got %s, want %s", got, test.initialValue)
   173  		}
   174  		if exists != test.initialValueExists {
   175  			t.Fatalf("unexpected value after t.Setenv cleanup: got %t, want %t", exists, test.initialValueExists)
   176  		}
   177  	}
   178  }
   179  
   180  func TestSetenvWithParallelAfterSetenv(t *testing.T) {
   181  	defer func() {
   182  		want := "testing: t.Parallel called after t.Setenv; cannot set environment variables in parallel tests"
   183  		if got := recover(); got != want {
   184  			t.Fatalf("expected panic; got %#v want %q", got, want)
   185  		}
   186  	}()
   187  
   188  	t.Setenv("GO_TEST_KEY_1", "value")
   189  
   190  	t.Parallel()
   191  }
   192  
   193  func TestSetenvWithParallelBeforeSetenv(t *testing.T) {
   194  	defer func() {
   195  		want := "testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests"
   196  		if got := recover(); got != want {
   197  			t.Fatalf("expected panic; got %#v want %q", got, want)
   198  		}
   199  	}()
   200  
   201  	t.Parallel()
   202  
   203  	t.Setenv("GO_TEST_KEY_1", "value")
   204  }
   205  
   206  func TestSetenvWithParallelParentBeforeSetenv(t *testing.T) {
   207  	t.Parallel()
   208  
   209  	t.Run("child", func(t *testing.T) {
   210  		defer func() {
   211  			want := "testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests"
   212  			if got := recover(); got != want {
   213  				t.Fatalf("expected panic; got %#v want %q", got, want)
   214  			}
   215  		}()
   216  
   217  		t.Setenv("GO_TEST_KEY_1", "value")
   218  	})
   219  }
   220  
   221  func TestSetenvWithParallelGrandParentBeforeSetenv(t *testing.T) {
   222  	t.Parallel()
   223  
   224  	t.Run("child", func(t *testing.T) {
   225  		t.Run("grand-child", func(t *testing.T) {
   226  			defer func() {
   227  				want := "testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests"
   228  				if got := recover(); got != want {
   229  					t.Fatalf("expected panic; got %#v want %q", got, want)
   230  				}
   231  			}()
   232  
   233  			t.Setenv("GO_TEST_KEY_1", "value")
   234  		})
   235  	})
   236  }
   237  
   238  // testingTrueInInit is part of TestTesting.
   239  var testingTrueInInit = false
   240  
   241  // testingTrueInPackageVarInit is part of TestTesting.
   242  var testingTrueInPackageVarInit = testing.Testing()
   243  
   244  // init is part of TestTesting.
   245  func init() {
   246  	if testing.Testing() {
   247  		testingTrueInInit = true
   248  	}
   249  }
   250  
   251  var testingProg = `
   252  package main
   253  
   254  import (
   255  	"fmt"
   256  	"testing"
   257  )
   258  
   259  func main() {
   260  	fmt.Println(testing.Testing())
   261  }
   262  `
   263  
   264  func TestTesting(t *testing.T) {
   265  	if !testing.Testing() {
   266  		t.Errorf("testing.Testing() == %t, want %t", testing.Testing(), true)
   267  	}
   268  	if !testingTrueInInit {
   269  		t.Errorf("testing.Testing() called by init function == %t, want %t", testingTrueInInit, true)
   270  	}
   271  	if !testingTrueInPackageVarInit {
   272  		t.Errorf("testing.Testing() variable initialized as %t, want %t", testingTrueInPackageVarInit, true)
   273  	}
   274  
   275  	if testing.Short() {
   276  		t.Skip("skipping building a binary in short mode")
   277  	}
   278  	testenv.MustHaveGoRun(t)
   279  
   280  	fn := filepath.Join(t.TempDir(), "x.go")
   281  	if err := os.WriteFile(fn, []byte(testingProg), 0644); err != nil {
   282  		t.Fatal(err)
   283  	}
   284  
   285  	cmd := testenv.Command(t, testenv.GoToolPath(t), "run", fn)
   286  	out, err := cmd.CombinedOutput()
   287  	if err != nil {
   288  		t.Fatalf("%v failed: %v\n%s", cmd, err, out)
   289  	}
   290  
   291  	s := string(bytes.TrimSpace(out))
   292  	if s != "false" {
   293  		t.Errorf("in non-test testing.Test() returned %q, want %q", s, "false")
   294  	}
   295  }