github.com/please-build/go-rules/tools/please_go@v0.0.0-20240319165128-ea27d6f5caba/test/write_test_main_test.go (about) 1 package test 2 3 import ( 4 "go/parser" 5 "go/token" 6 "io/ioutil" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestParseTestSources(t *testing.T) { 13 descr, err := parseTestSources([]string{"tools/please_go/test/test_data/test/example_test.go"}) 14 assert.NoError(t, err) 15 assert.Equal(t, "buildgo", descr.Package) 16 assert.Equal(t, "", descr.Main) 17 functions := []string{ 18 "TestReadPkgdef", 19 "TestReadCopiedPkgdef", 20 "TestFindCoverVars", 21 "TestFindCoverVarsFailsGracefully", 22 "TestFindCoverVarsReturnsNothingForEmptyPath", 23 } 24 assert.Equal(t, functions, descr.TestFunctions) 25 } 26 27 func TestParseTestSourcesWithMain(t *testing.T) { 28 descr, err := parseTestSources([]string{"tools/please_go/test/test_data/main/example_test_main.go"}) 29 assert.NoError(t, err) 30 assert.Equal(t, "parse", descr.Package) 31 assert.Equal(t, "TestMain", descr.Main) 32 functions := []string{ 33 "TestParseSourceBuildLabel", 34 "TestParseSourceRelativeBuildLabel", 35 "TestParseSourceFromSubdirectory", 36 "TestParseSourceFromOwnedSubdirectory", 37 "TestParseSourceWithParentPath", 38 "TestParseSourceWithAbsolutePath", 39 "TestAddTarget", 40 } 41 assert.Equal(t, functions, descr.TestFunctions) 42 } 43 44 func TestParseTestSourcesFailsGracefully(t *testing.T) { 45 _, err := parseTestSources([]string{"wibble"}) 46 assert.Error(t, err) 47 } 48 49 func TestWriteTestMain(t *testing.T) { 50 err := WriteTestMain("test_pkg", []string{"tools/please_go/test/test_data/test/example_test.go"}, "test.go", false, []CoverVar{}, false, true, false) 51 assert.NoError(t, err) 52 // It's not really practical to assert the contents of the file in great detail. 53 // We'll do the obvious thing of asserting that it is valid Go source. 54 f, err := parser.ParseFile(token.NewFileSet(), "test.go", nil, 0) 55 assert.NoError(t, err) 56 assert.Equal(t, "main", f.Name.Name) 57 } 58 59 func TestWriteTestMainWithCoverage(t *testing.T) { 60 err := WriteTestMain("test_package", []string{"tools/please_go/test/test_data/test/example_test.go"}, "test.go", true, []CoverVar{{ 61 Dir: "tools/please_go/test/test_data", 62 ImportPath: "core", 63 Var: "GoCover_lock_go", 64 File: "tools/please_go/test/test_data/lock.go", 65 }}, false, false, false) 66 assert.NoError(t, err) 67 // It's not really practical to assert the contents of the file in great detail. 68 // We'll do the obvious thing of asserting that it is valid Go source. 69 f, err := parser.ParseFile(token.NewFileSet(), "test.go", nil, 0) 70 assert.NoError(t, err) 71 assert.Equal(t, "main", f.Name.Name) 72 } 73 74 func TestWriteTestMainWithBenchmark(t *testing.T) { 75 err := WriteTestMain("test_package", []string{"tools/please_go/test/test_data/bench/example_benchmark_test.go"}, "test.go", true, []CoverVar{}, true, true, false) 76 assert.NoError(t, err) 77 // It's not really practical to assert the contents of the file in great detail. 78 // We'll do the obvious thing of asserting that it is valid Go source. 79 f, err := parser.ParseFile(token.NewFileSet(), "test.go", nil, 0) 80 assert.NoError(t, err) 81 assert.Equal(t, "main", f.Name.Name) 82 83 test, err := ioutil.ReadFile("test.go") 84 assert.NoError(t, err) 85 assert.Contains(t, string(test), "BenchmarkExample") 86 } 87 88 func TestExtraImportPaths(t *testing.T) { 89 assert.Equal(t, extraImportPaths("core", "core", []CoverVar{ 90 {ImportPath: "core"}, 91 {ImportPath: "output"}, 92 }), []string{ 93 "core \"core\"", 94 "_cover0 \"core\"", 95 "_cover1 \"output\"", 96 }) 97 } 98 99 func TestExtraImportPathsWithImportPath(t *testing.T) { 100 assert.Equal(t, extraImportPaths("core", "core", []CoverVar{ 101 {ImportPath: "github.com/thought-machine/please/src/core"}, 102 {ImportPath: "github.com/thought-machine/please/output"}, 103 }), []string{ 104 "core \"core\"", 105 "_cover0 \"github.com/thought-machine/please/src/core\"", 106 "_cover1 \"github.com/thought-machine/please/output\"", 107 }) 108 }