github.phpd.cn/thought-machine/please@v12.2.0+incompatible/tools/please_go_test/gotest/test_data/example_test_main.go (about) 1 // This isn't a 'real' source file, it's test data for //src/build/go:write_test_main_test 2 3 package parse 4 5 import ( 6 "os" 7 "testing" 8 "unsafe" 9 10 "github.com/stretchr/testify/assert" 11 12 "core" 13 ) 14 15 func TestParseSourceBuildLabel(t *testing.T) { 16 src := parseSource("//src/parse/test_data/test_subfolder4:test_py", "src/parse") 17 label := src.Label() 18 assert.NotNil(t, label) 19 assert.Equal(t, label.PackageName, "src/parse/test_data/test_subfolder4") 20 assert.Equal(t, label.Name, "test_py") 21 } 22 23 func TestParseSourceRelativeBuildLabel(t *testing.T) { 24 src := parseSource(":builtin_rules", "src/parse") 25 label := src.Label() 26 assert.NotNil(t, label) 27 assert.Equal(t, "src/parse", label.PackageName) 28 assert.Equal(t, "builtin_rules", label.Name) 29 } 30 31 // Test parsing from a subdirectory that does not contain a build file. 32 func TestParseSourceFromSubdirectory(t *testing.T) { 33 src := parseSource("test_subfolder3/test_py", "src/parse/test_data") 34 assert.Nil(t, src.Label()) 35 paths := src.Paths(nil) 36 assert.Equal(t, 1, len(paths)) 37 assert.Equal(t, "src/parse/test_data/test_subfolder3/test_py", paths[0]) 38 } 39 40 func TestParseSourceFromOwnedSubdirectory(t *testing.T) { 41 assert.Panics(t, func() { parseSource("test_subfolder4/test_py", "src/parse/test_data") }, 42 "Should panic when parsing from a subdirectory that does contain a build file") 43 } 44 45 func TestParseSourceWithParentPath(t *testing.T) { 46 assert.Panics(t, func() { parseSource("test_subfolder4/../test_py", "src/parse/test_data") }, 47 "Should panic when parsing a path with ../ in it") 48 } 49 50 func TestParseSourceWithAbsolutePath(t *testing.T) { 51 assert.Panics(t, func() { parseSource("/test_subfolder4/test_py", "src/parse/test_data") }, 52 "Should panic trying to parse an absolute path") 53 } 54 55 func TestAddTarget(t *testing.T) { 56 pkg := core.NewPackage("src/parse") 57 addTargetTest1 := func(name string, binary, container, test bool, testCmd string) *core.BuildTarget { 58 target := addTarget(unsafe.Pointer(pkg), name, "true", testCmd, binary, test, 59 false, false, container, false, false, false, 0, 0, 0, "Building...") 60 return (*core.BuildTarget)(target) 61 } 62 addTargetTest := func(name string, binary, container bool) *core.BuildTarget { 63 return addTargetTest1(name, binary, container, false, "") 64 } 65 // Test that labels are correctly applied 66 target1 := addTargetTest("target1", false, false) 67 assert.False(t, target1.HasLabel("bin")) 68 assert.False(t, target1.HasLabel("container")) 69 target2 := addTargetTest("target2", true, false) 70 assert.True(t, target2.HasLabel("bin")) 71 assert.False(t, target2.HasLabel("container")) 72 target3 := addTargetTest("target3", true, true) 73 assert.True(t, target3.HasLabel("bin")) 74 assert.True(t, target3.HasLabel("container")) 75 76 assert.Panics(t, func() { addTargetTest("target1", false, false) }, 77 "Should panic attempting to add a new target with the same name") 78 assert.Panics(t, func() { addTargetTest1("target4", false, false, true, "") }, 79 "Should panic attempting to add a test target with no test command") 80 assert.Panics(t, func() { addTargetTest1("target5", false, false, false, "true") }, 81 "Should panic attempting to add a non-test target with a test command") 82 83 assert.Nil(t, core.State.Graph.Target(core.ParseBuildLabel("//src/parse:target1", "")), 84 "Shouldn't have added target to the graph yet") 85 core.State.Graph.AddPackage(pkg) 86 addTargetTest("target6", true, false) 87 target6 := core.State.Graph.Target(core.ParseBuildLabel("//src/parse:target6", "")) 88 assert.NotNil(t, target6, "Should have been added to the graph since the package is added") 89 assert.True(t, target6.HasLabel("bin")) 90 } 91 92 func TestMain(m *testing.M) { 93 // Need to set this before calling parseSource. It's a bit of a hack but whatevs. 94 buildFileNames = []string{"TEST_BUILD"} 95 core.NewBuildState(10, nil, 2, core.DefaultConfiguration()) 96 os.Exit(m.Run()) 97 }