Skip to content

Commit

Permalink
Add coverage browser to gopherage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Katharine committed Oct 15, 2018
1 parent 5b33806 commit a70d9ab
Show file tree
Hide file tree
Showing 14 changed files with 440 additions and 208 deletions.
1 change: 1 addition & 0 deletions config/jobs/kubernetes/sig-testing/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ periodics:
bazel run //gopherage -- merge "${ARTIFACTS}"/before/**/*.cov > "${ARTIFACTS}/before/merged.cov"
bazel run //gopherage -- merge "${ARTIFACTS}"/after/**/*.cov > "${ARTIFACTS}/after/merged.cov"
bazel run //gopherage -- diff "${ARTIFACTS}/before/merged.cov" "${ARTIFACTS}/after/merged.cov" > "${ARTIFACTS}/conformance.cov"
bazel run //gopherage -- html "${ARTIFACTS}/conformance.cov" > "${ARTIFACTS}/conformance.html"
env:
- name: KUBE_BUILD_WITH_COVERAGE
value: "true"
Expand Down
157 changes: 0 additions & 157 deletions coverage/browser/browser.js

This file was deleted.

2 changes: 2 additions & 0 deletions gopherage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
visibility = ["//visibility:private"],
deps = [
"//gopherage/cmd/diff:go_default_library",
"//gopherage/cmd/html:go_default_library",
"//gopherage/cmd/merge:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library",
],
Expand All @@ -30,6 +31,7 @@ filegroup(
srcs = [
":package-srcs",
"//gopherage/cmd/diff:all-srcs",
"//gopherage/cmd/html:all-srcs",
"//gopherage/cmd/merge:all-srcs",
"//gopherage/pkg/cov:all-srcs",
"//gopherage/pkg/util:all-srcs",
Expand Down
31 changes: 31 additions & 0 deletions gopherage/cmd/html/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["html.go"],
data = [
"//gopherage/cmd/html/static",
],
importpath = "k8s.io/test-infra/gopherage/cmd/html",
visibility = ["//visibility:public"],
deps = [
"//vendor/github.com/spf13/cobra:go_default_library",
],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//gopherage/cmd/html/static:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
98 changes: 98 additions & 0 deletions gopherage/cmd/html/html.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Copyright 2018 The Kubernetes Authors.
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 html

import (
"html/template"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/spf13/cobra"
)

type flags struct {
OutputFile string
}

// MakeCommand returns a `diff` command.
func MakeCommand() *cobra.Command {
flags := &flags{}
cmd := &cobra.Command{
Use: "html [coverage]",
Short: "Emits an HTML file to browse a coverage file.",
Run: func(cmd *cobra.Command, args []string) {
run(flags, cmd, args)
},
}
cmd.Flags().StringVar(&flags.OutputFile, "o", "-", "output file")
return cmd
}

func run(flags *flags, cmd *cobra.Command, args []string) {
if len(args) != 1 {
log.Fatalln("Usage: gopherage html [coverage]")
}

// This path assumes we're being run using bazel.
resourceDir := "gopherage/cmd/html/static"
if _, err := os.Stat(resourceDir); os.IsNotExist(err) {
log.Fatalln("Resource directory does not exist.")
}

tpl, err := template.ParseFiles(filepath.Join(resourceDir, "browser.html"))
if err != nil {
log.Fatalf("Couldn't read the HTML template: %v", err)
}
script, err := ioutil.ReadFile(filepath.Join(resourceDir, "browser_bundle.es6.js"))
if err != nil {
log.Fatalf("Couldn't read JavaScript: %v", err)
}

// If we're under bazel, move into BUILD_WORKING_DIRECTORY so that manual
// invocations of bazel run are less confusing.
if wd, ok := os.LookupEnv("BUILD_WORKING_DIRECTORY"); ok {
if err := os.Chdir(wd); err != nil {
log.Fatalf("Couldn't chdir into expected working directory.")
}
}

coverage, err := ioutil.ReadFile(args[0])
if err != nil {
log.Fatalf("Couldn't read coverage file: %v", err)
}

outputPath := flags.OutputFile
var output io.Writer
if outputPath == "-" {
output = os.Stdout
} else {
f, err := os.Create(outputPath)
if err != nil {
log.Fatalf("Couldn't open output file: %v", err)
}
defer f.Close()
output = f
}

tpl.Execute(output, struct {
Script template.JS
Coverage string
}{template.JS(script), string(coverage)})
}
45 changes: 45 additions & 0 deletions gopherage/cmd/html/static/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package(default_visibility = ["//visibility:public"])

load("@build_bazel_rules_nodejs//:defs.bzl", "rollup_bundle")
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")

ts_library(
name = "browser",
srcs = glob(
["*.ts"],
exclude = ["*_test.ts"],
),
deps = [
"@npm//:@types/google.visualization",
],
)

rollup_bundle(
name = "browser_bundle",
entry_point = "gopherage/cmd/html/static/browser",
deps = [
":browser",
],
)

filegroup(
name = "static",
srcs = [
"browser.html",
":browser_bundle.es6.js",
],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
<meta charset="UTF-8">
<title>Conformance Code Coverage</title>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="module" crossorigin="use-credentials" src="browser.js"></script>
<script type="text/javascript">
var coverage = "{{ .Coverage }}";
{{ .Script }}
</script>
<style type="text/css">
body {
font-family: sans-serif;
}

.arrow {
float: left;
margin-right: 5px;
}
</style>
</head>
<body>
Expand All @@ -18,4 +26,4 @@ <h2 id="breadcrumbs"></h2>
Loading...
</div>
</body>
</html>
</html>
Loading

0 comments on commit a70d9ab

Please sign in to comment.