summaryrefslogtreecommitdiffstats
path: root/src/cmd/go/testdata/script/test_main.txt
blob: 25d02e4465cb4c72d5028927dc74af574f09da54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Test TestMain
go test standalone_main_normal_test.go
! stdout '^ok.*\[no tests to run\]'
! stderr '^ok.*\[no tests to run\]'
stdout '^ok'

# Test TestMain sees testing flags
go test standalone_testmain_flag_test.go
stdout '^ok.*\[no tests to run\]'

# Test TestMain with wrong signature (Issue #22388)
! go test standalone_main_wrong_test.go
stderr 'wrong signature for TestMain, must be: func TestMain\(m \*testing.M\)'

# Test TestMain does not call os.Exit (Issue #34129)
! go test standalone_testmain_not_call_os_exit_test.go
! stdout '^ok'

-- standalone_main_normal_test.go --
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package standalone_main_normal_test

import "testing"

func TestMain(t *testing.T) {
}
-- standalone_main_wrong_test.go --
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package standalone_main_wrong_test

import "testing"

func TestMain(m *testing.Main) {
}
-- standalone_testmain_flag_test.go --
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package standalone_testmain_flag_test

import (
	"flag"
	"fmt"
	"os"
	"testing"
)

func TestMain(m *testing.M) {
	// A TestMain should be able to access testing flags if it calls
	// flag.Parse without needing to use testing.Init.
	flag.Parse()
	found := false
	flag.VisitAll(func(f *flag.Flag) {
		if f.Name == "test.count" {
			found = true
		}
	})
	if !found {
		fmt.Println("testing flags not registered")
		os.Exit(1)
	}
	os.Exit(m.Run())
}
-- standalone_testmain_not_call_os_exit_test.go --
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package standalone_testmain_not_call_os_exit_test

import (
	"testing"
)

func TestWillFail(t *testing.T) {
	t.Error("this test will fail.")
}

func TestMain(m *testing.M) {
	defer func() {
		recover()
	}()
	exit := m.Run()
	panic(exit)
}