Skip to content

Commit

Permalink
tests/core/coverage: migrate to go_bazel_test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Conrod committed Nov 20, 2019
1 parent dd78c74 commit 959f752
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 135 deletions.
98 changes: 3 additions & 95 deletions tests/core/coverage/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,103 +1,11 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@io_bazel_rules_go//tests:bazel_tests.bzl", "bazel_test")
load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")

bazel_test(
name = "coverage_test_test",
args = ["--instrumentation_filter=-coverage:b"],
check = """
if ! grep -q '^coverage: 50.0% of statements' "bazel-testlogs/$RULES_GO_OUTPUT/coverage_test/test.log"; then
echo "error: no coverage output found in test log file" >&2
exit 1
fi
data_file=bazel-testlogs/$RULES_GO_OUTPUT/coverage_test/coverage.dat
if [ ! -e "$data_file" ]; then
echo "error: $data_file: does not exist" >&2
exit 1
fi
if [ ! -s "$data_file" ]; then
echo "warning: $data_file: has size zero. Bazel may have trashed it with lcov." >&2
echo "skipping rest of test" >&2
exit 0
fi

function check_file_included {
if ! grep -q "$1" "$data_file"; then
echo "error: coverage data not found for $1" >&2
exit 1
fi
}
function check_file_excluded {
if grep -q "$1" "$data_file"; then
echo "error: coverage data found for $1, but it should be excluded" >&2
exit 1
fi
}
included_files=(
'github.com/bazelbuild/rules_go/tests/core/coverage/a/a.go:'
'github.com/bazelbuild/rules_go/tests/core/coverage/c/c.go:'
)
excluded_files=(
'github.com/bazelbuild/rules_go/tests/core/coverage/b/b.go:'
)
for i in "${included_files[@]}"; do
check_file_included "$i"
done
for i in "${excluded_files[@]}"; do
check_file_excluded "$i"
done
""",
command = "coverage",
targets = [":coverage_test"],
)
# load("@io_bazel_rules_go//tests:bazel_tests.bzl", "bazel_test")
load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")

go_test(
go_bazel_test(
name = "coverage_test",
srcs = ["coverage_test.go"],
embed = [":a"],
tags = ["manual"],
)

go_library(
name = "a",
srcs = ["a.go"],
importpath = "github.com/bazelbuild/rules_go/tests/core/coverage/a",
deps = [":b"],
)

go_library(
name = "b",
srcs = ["b.go"],
importpath = "github.com/bazelbuild/rules_go/tests/core/coverage/b",
deps = [":c"],
)

go_library(
name = "c",
srcs = ["c.go"],
importpath = "github.com/bazelbuild/rules_go/tests/core/coverage/c",
)

bazel_test(
name = "cross_cover_test_test",
args = [
"--collect_code_coverage",
"--instrumentation_filter=-coverage:b",
],
command = "build",
targets = [":cross_cover_test"],
)

go_test(
name = "cross_cover_test",
srcs = ["coverage_test.go"],
embed = [":a"],
goarch = "386",
goos = "linux",
pure = "on",
tags = ["manual"],
)

go_bazel_test(
Expand Down
10 changes: 2 additions & 8 deletions tests/core/coverage/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@
coverage functionality
======================

coverage_test_test
------------------
coverage_test
-------------

Checks that ``bazel coverage`` on a ``go_test`` produces reasonable output.
Libraries referenced by the test that pass ``--instrumentation_filter`` should
have coverage data. Library excluded with ``--instrumentatiuon_filter`` should
not have coverage data.

coverdata_aspect_test_test
--------------------------

Checks that the ``coverdata`` library is compiled in the same mode as the
test that depends on it.

binary_coverage_test
--------------------

Expand Down
11 changes: 0 additions & 11 deletions tests/core/coverage/a.go

This file was deleted.

11 changes: 0 additions & 11 deletions tests/core/coverage/b.go

This file was deleted.

9 changes: 0 additions & 9 deletions tests/core/coverage/c.go

This file was deleted.

144 changes: 143 additions & 1 deletion tests/core/coverage/coverage_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,149 @@
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package coverage_test

import (
"bytes"
"io/ioutil"
"path/filepath"
"testing"

"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
)

func TestMain(m *testing.M) {
bazel_testing.TestMain(m, bazel_testing.Args{
Main: `
-- BUILD.bazel --
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_test(
name = "a_test",
srcs = ["a_test.go"],
embed = [":a"],
)
go_test(
name = "a_test_cross",
srcs = ["a_test.go"],
embed = [":a"],
goarch = "386",
goos = "linux",
pure = "on",
tags = ["manual"],
)
go_library(
name = "a",
srcs = ["a.go"],
importpath = "example.com/coverage/a",
deps = [":b"],
)
go_library(
name = "b",
srcs = ["b.go"],
importpath = "example.com/coverage/b",
deps = [":c"],
)
go_library(
name = "c",
srcs = ["c.go"],
importpath = "example.com/coverage/c",
)
-- a_test.go --
package a
import "testing"
func TestLive(t *testing.T) {
func TestA(t *testing.T) {
ALive()
}
-- a.go --
package a
import "example.com/coverage/b"
func ALive() int {
return b.BLive()
}
func ADead() int {
return b.BDead()
}
-- b.go --
package b
import "example.com/coverage/c"
func BLive() int {
return c.CLive()
}
func BDead() int {
return c.CDead()
}
-- c.go --
package c
func CLive() int {
return 12
}
func CDead() int {
return 34
}
`,
})
}

func TestCoverage(t *testing.T) {
if err := bazel_testing.RunBazel("coverage", "--instrumentation_filter=-//:b", ":a_test"); err != nil {
t.Fatal(err)
}

coveragePath := filepath.FromSlash("bazel-testlogs/a_test/coverage.dat")
coverageData, err := ioutil.ReadFile(coveragePath)
if err != nil {
t.Fatal(err)
}
for _, include := range []string{
"example.com/coverage/a/a.go:",
"example.com/coverage/c/c.go:",
} {
if !bytes.Contains(coverageData, []byte(include)) {
t.Errorf("%s: does not contain %q\n", coveragePath, include)
}
}
for _, exclude := range []string{
"example.com/coverage/b/b.go:",
} {
if bytes.Contains(coverageData, []byte(exclude)) {
t.Errorf("%s: contains %q\n", coveragePath, exclude)
}
}
}

func TestCrossBuild(t *testing.T) {
if err := bazel_testing.RunBazel("build", "--collect_code_coverage", "--instrumentation_filter=-//:b", "//:a_test_cross"); err != nil {
t.Fatal(err)
}
}

0 comments on commit 959f752

Please sign in to comment.