@cryptotaxi247 / kubo / commits / 23ddc2602

ci: Coverage and Junit xmls in one run

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Oct 27, 2018 at 18:27 UTC 23ddc26025dfbfd3f4cc5af34a05aa51fd23ac14
9 files changed +163 -19
.circleci/config.yml
+7 -3
@@ -51,8 +51,9 @@ jobs:
51
52
53 - run: cd "$HOME/.go_workspace/src/$IMPORT_PATH" && curl -s https://codecov.io/bash > codecov
54 - - run: cd "$HOME/.go_workspace/src/$IMPORT_PATH" && make -j 1 coverage/unit_tests.coverprofile
54 + - run: cd "$HOME/.go_workspace/src/$IMPORT_PATH" && make -j 1 test/unit/gotest.junit.xml
55 - run: cd "$HOME/.go_workspace/src/$IMPORT_PATH" && bash codecov -cF unittests -X search -f coverage/unit_tests.coverprofile
56 + - run: mv "$HOME/.go_workspace/src/${IMPORT_PATH}/test/unit/gotest.junit.xml" /tmp/circleci-test-results
57 - store_test_results:
58 path: /tmp/circleci-test-results
59 # Save artifacts
@@ -89,10 +90,13 @@ jobs:
90 - ~/.go_workspace/src/gx/ipfs
91 - ./node_modules
92
92 -
93 - run: cd "$HOME/.go_workspace/src/$IMPORT_PATH" && curl -s https://codecov.io/bash > codecov
94 - - run: cd "$HOME/.go_workspace/src/$IMPORT_PATH" && make -j 1 coverage/sharness_tests.coverprofile
94 + - run: cd "$HOME/.go_workspace/src/$IMPORT_PATH" && make -j 10 coverage/sharness_tests.coverprofile test/sharness/test-results/sharness.xml TEST_GENERATE_JUNIT=1 CONTINUE_ON_S_FAILURE=1
95 - run: cd "$HOME/.go_workspace/src/$IMPORT_PATH" && bash codecov -cF sharness -X search -f coverage/sharness_tests.coverprofile
96 + - run: mv "$HOME/.go_workspace/src/${IMPORT_PATH}/test/sharness/test-results/sharness.xml" /tmp/circleci-test-results
97 +
98 + # make sure we fail if there are test failures
99 + - run: cd "$HOME/.go_workspace/src/$IMPORT_PATH" && ls test/sharness/test-results/t*-*.sh.*.counts | test/sharness/lib/sharness/aggregate-results.sh | grep 'failed\s*0'
100 - store_test_results:
101 path: /tmp/circleci-test-results
102 # Save artifacts
Rules.mk
+1 -1
@@ -41,7 +41,7 @@ include $(dir)/Rules.mk
41
42 # include this file only if coverage target is executed
43 # it is quite expensive
44 -ifneq ($(filter coverage% clean distclean,$(MAKECMDGOALS)),)
44 +ifneq ($(filter coverage% clean distclean test/unit/gotest.junit.xml,$(MAKECMDGOALS)),)
45 # has to be after cmd/ipfs due to PATH
46 dir := coverage
47 include $(dir)/Rules.mk
bin/circle.sh deleted
-14
@@ -1,14 +0,0 @@
1 -#!/usr/bin/env bash
2 -# the CircleCI build line got a bit out of hands
3 -# thus we have sparate file for it
4 -
5 -curl -s https://codecov.io/bash > codecov
6 -
7 -case $CIRCLE_NODE_INDEX in
8 - 0) make -j 1 coverage/unit_tests.coverprofile &&
9 - bash codecov -cF unittests -X search -f coverage/unit_tests.coverprofile
10 - ;;
11 - 1) make -j 1 coverage/sharness_tests.coverprofile &&
12 - bash codecov -cF sharness -X search -f coverage/sharness_tests.coverprofile
13 - ;;
14 -esac
coverage/Rules.mk
+3 -1
@@ -11,6 +11,7 @@ endif
11
12 # unit tests coverage
13 UTESTS_$(d) := $(shell go list -f '{{if (len .TestGoFiles)}}{{.ImportPath}}{{end}}' $(go-flags-with-tags) ./...)
14 +UTESTS_$(d) += $(shell go list -f '{{if (len .XTestGoFiles)}}{{.ImportPath}}{{end}}' $(go-flags-with-tags) ./... | grep -v go-ipfs/vendor | grep -v go-ipfs/Godeps)
15
16 UCOVER_$(d) := $(addsuffix .coverprofile,$(addprefix $(d)/unitcover/, $(subst /,_,$(UTESTS_$(d)))))
17
@@ -18,7 +19,7 @@ $(UCOVER_$(d)): $(d)/coverage_deps ALWAYS
19 $(eval TMP_PKG := $(subst _,/,$(basename $(@F))))
20 $(eval TMP_DEPS := $(shell go list -f '{{range .Deps}}{{.}} {{end}}' $(go-flags-with-tags) $(TMP_PKG) | sed 's/ /\n/g' | grep ipfs/go-ipfs) $(TMP_PKG))
21 $(eval TMP_DEPS_LIST := $(call join-with,$(comma),$(TMP_DEPS)))
21 - go test $(go-flags-with-tags) $(GOTFLAGS) -covermode=atomic -coverpkg=$(TMP_DEPS_LIST) -coverprofile=$@ $(TMP_PKG)
22 + go test $(go-flags-with-tags) $(GOTFLAGS) -v -covermode=atomic -json -coverpkg=$(TMP_DEPS_LIST) -coverprofile=$@ $(TMP_PKG) | tee -a test/unit/gotest.json
23
24
25 $(d)/unit_tests.coverprofile: $(UCOVER_$(d))
@@ -26,6 +27,7 @@ $(d)/unit_tests.coverprofile: $(UCOVER_$(d))
27
28 TGTS_$(d) := $(d)/unit_tests.coverprofile
29
30 +.PHONY: $(d)/unit_tests.coverprofile
31
32 # sharness tests coverage
33 $(d)/ipfs: GOTAGS += testrunmain
test/Rules.mk
+3
@@ -6,4 +6,7 @@ include $(dir)/Rules.mk
6 dir := $(d)/sharness
7 include $(dir)/Rules.mk
8
9 +dir := $(d)/unit
10 +include $(dir)/Rules.mk
11 +
12 include mk/footer.mk
test/bin/Rules.mk
+4
@@ -18,6 +18,10 @@ $(d)/ma-pipe-unidir: test/dependencies/ma-pipe-unidir
18 $(go-build)
19 TGTS_$(d) += $(d)/ma-pipe-unidir
20
21 +$(d)/json-to-junit: test/dependencies/json-to-junit
22 + $(go-build)
23 +TGTS_$(d) += $(d)/json-to-junit
24 +
25 TGTS_GX_$(d) := hang-fds iptb
26 TGTS_GX_$(d) := $(addprefix $(d)/,$(TGTS_GX_$(d)))
27
test/dependencies/json-to-junit/main.go new
+135
@@ -0,0 +1,135 @@
1 +package main
2 +
3 +import (
4 + "bufio"
5 + "encoding/json"
6 + "encoding/xml"
7 + "log"
8 + "os"
9 +)
10 +
11 +type testMsg struct {
12 + Time string
13 + Action string
14 + Package string
15 + Test string
16 + Output string
17 + Elapsed float64
18 +}
19 +
20 +type testsuites struct {
21 + XMLName xml.Name `xml:"testsuites"`
22 + Name string `xml:"name,attr"`
23 + Testsuites []testsuite
24 +}
25 +
26 +type testsuite struct {
27 + XMLName xml.Name `xml:"testsuite"`
28 + Package string `xml:"package,attr"`
29 + Errors int `xml:"errors,attr"`
30 + Failures int `xml:"failures,attr"`
31 + Tests int `xml:"tests,attr"`
32 + Time float64 `xml:"time,attr"`
33 + Testcases []testcase
34 +}
35 +
36 +type testcase struct {
37 + XMLName xml.Name `xml:"testcase"`
38 + Name string `xml:"name,attr"`
39 + Classname string `xml:"classname,attr"`
40 + Time float64 `xml:"time,attr"`
41 + Sout string `xml:"system-out"`
42 + Serr string `xml:"system-err"`
43 + Skipped string `xml:"skipped,omitempty"`
44 + Failure string `xml:"failure,omitempty"`
45 +}
46 +
47 +func main() {
48 + scanner := bufio.NewScanner(os.Stdin)
49 + tests := make(map[string]map[string]testcase)
50 + var fails int
51 + var packages []testsuite
52 + for scanner.Scan() {
53 + msg := testMsg{}
54 + if err := json.Unmarshal(scanner.Bytes(), &msg); err != nil {
55 + log.Fatal(err)
56 + }
57 +
58 + switch {
59 + case msg.Action == "run":
60 + if tests[msg.Package] == nil {
61 + tests[msg.Package] = make(map[string]testcase)
62 + }
63 +
64 + tests[msg.Package][msg.Test] = testcase{
65 + Name: msg.Test,
66 + Classname: msg.Package,
67 + }
68 + case msg.Action == "output":
69 + if msg.Test == "" {
70 + continue
71 + }
72 +
73 + test := tests[msg.Package][msg.Test]
74 + test.Sout = test.Sout + msg.Output + "\n"
75 + tests[msg.Package][msg.Test] = test
76 + case msg.Action == "skip":
77 + fallthrough
78 + case msg.Action == "fail":
79 + fallthrough
80 + case msg.Action == "pass":
81 + if msg.Test != "" {
82 + test := tests[msg.Package][msg.Test]
83 + test.Time = msg.Elapsed
84 +
85 + if msg.Action == "skip" {
86 + test.Skipped = "skipped"
87 + }
88 +
89 + if msg.Action == "fail" {
90 + fails++
91 + test.Failure = "failed"
92 + }
93 +
94 + tests[msg.Package][msg.Test] = test
95 + continue
96 + }
97 +
98 + ts := testsuite{
99 + Package: msg.Package,
100 + Time: msg.Elapsed,
101 + }
102 +
103 + for _, test := range tests[msg.Package] {
104 + ts.Testcases = append(ts.Testcases, test)
105 + ts.Tests++
106 + if test.Failure != "" {
107 + ts.Failures++
108 + }
109 + }
110 + packages = append(packages, ts)
111 + case msg.Action == "cont":
112 + case msg.Action == "pause":
113 + // ??
114 + default:
115 + log.Fatalf("unknown action %s", msg.Action)
116 + }
117 + }
118 +
119 + if err := scanner.Err(); err != nil {
120 + log.Fatal(err)
121 + }
122 +
123 + out := testsuites{
124 + Name: "go test",
125 + Testsuites: packages,
126 + }
127 +
128 + output, err := xml.MarshalIndent(&out, " ", " ")
129 + if err != nil {
130 + log.Fatalf("error: %v\n", err)
131 + }
132 +
133 + os.Stdout.Write(output)
134 + os.Exit(fails)
135 +}
test/unit/.gitignore new
+2
@@ -0,0 +1,2 @@
1 +gotest.json
2 +gotest.junit.xml
test/unit/Rules.mk new
+8
@@ -0,0 +1,8 @@
1 +include mk/header.mk
2 +
3 +CLEAN += $(d)/gotest.json $(d)/gotest.junit.xml
4 +
5 +$(d)/gotest.junit.xml: clean test/bin/json-to-junit coverage/unit_tests.coverprofile
6 + cat $(@D)/gotest.json | json-to-junit > $(@D)/gotest.junit.xml
7 +
8 +include mk/footer.mk