Skip to content

Commit

Permalink
Ensure we only ever import the test version of the packag efor covera…
Browse files Browse the repository at this point in the history
…ge (#13)

* Ensure we only ever import the test version of the packag efor coverage

* fix test

* Fix tests
  • Loading branch information
Tatskaari authored Apr 6, 2022
1 parent 9952601 commit 4c1f27b
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2
- name: Build
run: ./pleasew build -p -v notice //package:release_files
run: ./pleasew build --profile release -p -v notice //package:release_files
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
5 changes: 4 additions & 1 deletion .plzconfig
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ Repeatable = true
Optional = true

[FeatureFlags]
ExcludeGoRules = true
ExcludeGoRules = true

[BuildConfig]
Release = False
2 changes: 2 additions & 0 deletions .plzconfig.release
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[BuildConfig]
Release = true
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Version 0.2.3
-------------
* Import the test version of internal packages when calculating
coverage

Version 0.2.2
-------------
* Handle hyphens in source file names when generating cover vars
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.2
0.2.3
4 changes: 2 additions & 2 deletions build_defs/go.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -909,8 +909,8 @@ def go_test_main(name:str, srcs:list, test_package:str="", test_only:bool=False,
"""
cover = cover and (CONFIG.BUILD_CONFIG == "cover")
test_package = test_package or _get_import_path()

cmd = f'"$TOOLS_PLZ" testmain --test_package "{test_package}" -o $OUT'
external_flag = "--external" if external else ""
cmd = f'"$TOOLS_PLZ" testmain --test_package "{test_package}" {external_flag} -o $OUT'
if benchmark:
cmd = f'{cmd} --benchmark'
cmds = {
Expand Down
3 changes: 2 additions & 1 deletion package/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def cross_compile(arch):
srcs = [f"///{arch}//tools/please_go"],
outs = [f"please_go-{VERSION}-{arch}"],
cmd = "mv $SRC $OUT",
labels = ["manual"],
)

filegroup(
Expand All @@ -17,5 +18,5 @@ filegroup(
"linux_amd64",
"linux_arm64",
]],
labels = ["hlink:plz-out/package"],
labels = ["hlink:plz-out/package", "manual"],
)
5 changes: 3 additions & 2 deletions third_party/go/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
subinclude("//build_defs:go")


go_toolchain(
name = "toolchain",
architectures = [
Expand All @@ -9,7 +8,9 @@ go_toolchain(
"freebsd_amd64",
"linux_amd64",
"linux_arm64",
],
# Installing takes a while so only do it when we need it for release
# This extra check can be removed once we fix booleans in build config
] if CONFIG.RELEASE and CONFIG.RELEASE != "False" else [],
version = "1.18",
tags = [
"osusergo",
Expand Down
3 changes: 2 additions & 1 deletion tools/please_go/please_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var opts = struct {
Output string `short:"o" long:"output" description:"Output filename" required:"true"`
TestPackage string `short:"t" long:"test_package" description:"The import path of the test package"`
Benchmark bool `short:"b" long:"benchmark" description:"Whether to run benchmarks instead of tests"`
External bool `long:"external" description:"Whether the test is external or not"`
Args struct {
Sources []string `positional-arg-name:"sources" description:"Test source files" required:"true"`
} `positional-args:"true" required:"true"`
Expand Down Expand Up @@ -92,7 +93,7 @@ var subCommands = map[string]func() int{
return 0
},
"testmain": func() int {
test.PleaseGoTest(opts.Test.GoTool, opts.Test.Dir, opts.Test.TestPackage, opts.Test.Output, opts.Test.Args.Sources, opts.Test.Exclude, opts.Test.Benchmark)
test.PleaseGoTest(opts.Test.GoTool, opts.Test.Dir, opts.Test.TestPackage, opts.Test.Output, opts.Test.Args.Sources, opts.Test.Exclude, opts.Test.Benchmark, opts.Test.External)
return 0
},
"covervars": func() int {
Expand Down
15 changes: 9 additions & 6 deletions tools/please_go/test/find_cover_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type CoverVar struct {
}

// FindCoverVars searches the given directory recursively to find all Go files with coverage variables.
func FindCoverVars(dir string, excludedDirs []string) ([]CoverVar, error) {
func FindCoverVars(dir, testPackage string, external bool, excludedDirs []string) ([]CoverVar, error) {
if dir == "" {
return nil, nil
}
Expand All @@ -35,7 +35,7 @@ func FindCoverVars(dir string, excludedDirs []string) ([]CoverVar, error) {
return filepath.SkipDir
}
} else if strings.HasSuffix(name, ".cover_vars") {
vars, err := parseCoverVars(name)
vars, err := parseCoverVars(name, testPackage, external)
if err != nil {
return err
}
Expand All @@ -47,7 +47,7 @@ func FindCoverVars(dir string, excludedDirs []string) ([]CoverVar, error) {
}

// parseCoverVars parses the coverage variables file for all cover vars
func parseCoverVars(filepath string) ([]CoverVar, error) {
func parseCoverVars(filepath, testPackage string, external bool) ([]CoverVar, error) {
dir := strings.TrimRight(path.Dir(filepath), "/")
if dir == "" {
dir = "."
Expand All @@ -68,8 +68,12 @@ func parseCoverVars(filepath string) ([]CoverVar, error) {
if len(parts) != 2 {
return nil, fmt.Errorf("malformed cover var line in %v: %v", filepath, line)
}

ret = append(ret, coverVar(dir, parts[0], parts[1]))
if external || dir != os.Getenv("PKG_DIR") {
ret = append(ret, coverVar(dir, parts[0], parts[1]))
} else {
// We recompile the test package, so override the import path here to get the right version
ret = append(ret, coverVar(dir, testPackage, parts[1]))
}
}

return ret, nil
Expand All @@ -88,4 +92,3 @@ func coverVar(dir, importPath, v string) CoverVar {
File: f,
}
}

8 changes: 4 additions & 4 deletions tools/please_go/test/find_cover_vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ var coverageVars = []CoverVar{{
}}

func TestFindCoverVars(t *testing.T) {
vars, err := FindCoverVars("tools/please_go/test/test_data", []string{"tools/please_go/test/test_data/x", "tools/please_go/test/test_data/binary"})
vars, err := FindCoverVars("tools/please_go/test/test_data", "", false, []string{"tools/please_go/test/test_data/x", "tools/please_go/test/test_data/binary"})
assert.NoError(t, err)
assert.Equal(t, coverageVars, vars)
}

func TestFindCoverVarsFailsGracefully(t *testing.T) {
_, err := FindCoverVars("wibble", nil)
_, err := FindCoverVars("wibble", "", false, nil)
assert.Error(t, err)
}

func TestFindCoverVarsReturnsNothingForEmptyPath(t *testing.T) {
vars, err := FindCoverVars("", nil)
vars, err := FindCoverVars("", "", false, nil)
assert.NoError(t, err)
assert.Equal(t, 0, len(vars))
}
Expand All @@ -39,7 +39,7 @@ func TestFindBinaryCoverVars(t *testing.T) {
Var: "GoCover_lock_go",
File: "tools/please_go/test/test_data/binary/lock.go",
}}
vars, err := FindCoverVars("tools/please_go/test/test_data/binary", nil)
vars, err := FindCoverVars("tools/please_go/test/test_data/binary", "", false, nil)
assert.NoError(t, err)
assert.Equal(t, expected, vars)
}
4 changes: 2 additions & 2 deletions tools/please_go/test/gotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

// PleaseGoTest will generate the test main for the provided sources
func PleaseGoTest(goTool, dir, testPackage, output string, sources, exclude []string, isBenchmark bool) {
coverVars, err := FindCoverVars(dir, exclude)
func PleaseGoTest(goTool, dir, testPackage, output string, sources, exclude []string, isBenchmark, external bool) {
coverVars, err := FindCoverVars(dir, testPackage, external, exclude)
if err != nil {
log.Fatalf("Error scanning for coverage: %s", err)
}
Expand Down

0 comments on commit 4c1f27b

Please sign in to comment.