Skip to content

Commit b3028c7

Browse files
authored
add integration test (#2)
1 parent 0418c25 commit b3028c7

File tree

9 files changed

+214
-12
lines changed

9 files changed

+214
-12
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: integration-test
2+
on: [push]
3+
jobs:
4+
test:
5+
name: test
6+
permissions:
7+
contents: write
8+
packages: write
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: checkout
12+
uses: actions/checkout@v3
13+
- name: setup go
14+
uses: actions/setup-go@v3
15+
with:
16+
go-version-file: go.mod
17+
18+
- name: Set action image version to dev
19+
run: |
20+
yq e -i '.runs.image = "docker://ghcr.io/vladopajic/go-test-coverage:dev"' action.yml
21+
image=$(yq '.runs.image' action.yml)
22+
echo "Image: $image"
23+
24+
- name: Setup buildx
25+
uses: docker/setup-buildx-action@v2
26+
27+
- name: Login to GitHub container registry
28+
uses: docker/login-action@v2
29+
with:
30+
registry: ghcr.io
31+
username: ${{ github.repository_owner }}
32+
password: ${{ secrets.GITHUB_TOKEN }}
33+
34+
- name: Build and push
35+
uses: docker/build-push-action@v4
36+
with:
37+
push: true
38+
build-args: |
39+
VERSION=dev
40+
tags: |
41+
ghcr.io/vladopajic/go-test-coverage:dev
42+
43+
- name: generate test coverage
44+
run: go test ./... -coverprofile=./cover.out -covermode=atomic
45+
46+
- name: Test total coverage 0% (config)
47+
uses: ./
48+
with:
49+
config: ./.github/workflows/testdata/zero.yml
50+
51+
- name: Test total coverage 100% (config)
52+
uses: ./
53+
id: should-fail-1
54+
continue-on-error: true
55+
with:
56+
config: ./.github/workflows/testdata/total100.yml
57+
58+
- name: Check previous step failed
59+
if: steps.should-fail-1.outcome != 'failure'
60+
shell: bash
61+
run: |
62+
echo "Previous step should have failed"
63+
exit 1
64+
65+
- name: Test total coverage 0% (inputs)
66+
uses: ./
67+
with:
68+
profile: cover.out
69+
local-prefix: "github.com/vladopajic/go-test-coverage"
70+
threshold-file: 0
71+
threshold-package: 0
72+
threshold-total: 0
73+
74+
- name: Test total coverage 100% (inputs)
75+
uses: ./
76+
id: should-fail-2
77+
continue-on-error: true
78+
with:
79+
profile: cover.out
80+
local-prefix: "github.com/vladopajic/go-test-coverage"
81+
threshold-file: 0
82+
threshold-package: 0
83+
threshold-total: 100
84+
85+
- name: Check previous step failed
86+
if: steps.should-fail-2.outcome != 'failure'
87+
shell: bash
88+
run: |
89+
echo "Previous step should have failed"
90+
exit 1

.github/workflows/test.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,3 @@ jobs:
1313
go-version-file: go.mod
1414
- name: test
1515
run: go test -timeout=30s -race -count=10 -failfast ./...
16-
- name: test (report coverage)
17-
run: go test ./... -coverprofile=./cover.out -covermode=atomic
18-
- name: upload code coverage
19-
uses: codecov/codecov-action@v3.1.0
20-
if: contains(github.ref, 'main')
21-
with:
22-
file: ./cover.out
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This file is used for integration tests
2+
3+
profile: cover.out
4+
github-action-output: true
5+
local-refix: "github.com/vladopajic/go-test-coverage"
6+
threshold:
7+
file: 0
8+
package: 0
9+
total: 100

.github/workflows/testdata/zero.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This file is used for integration tests
2+
3+
profile: cover.out
4+
github-action-output: true
5+
local-refix: "github.com/vladopajic/go-test-coverage"
6+
threshold:
7+
file: 0
8+
package: 0
9+
total: 0

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,32 @@ inputs:
55
config:
66
description: Path of configuration file.
77
required: false
8+
default: "''"
89
type: string
910
profile:
1011
description: Path of coverage profile file.
1112
required: false
13+
default: "''"
1214
type: string
1315
local-prefix:
1416
description: When specified reported file paths will not contain local prefix in the output.
1517
required: false
18+
default: "''"
1619
type: string
1720
threshold-file:
1821
description: The minimum coverage that each file should have.
1922
required: false
23+
default: 0
2024
type: number
2125
threshold-package:
2226
description: The minimum coverage that each package should have.
2327
required: false
28+
default: 0
2429
type: number
2530
threshold-total:
2631
description: The minimum total coverage project should have.
2732
required: false
33+
default: 50
2834
type: number
2935
outputs:
3036
total_coverage:

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ go 1.20
44

55
require (
66
github.com/alexflint/go-arg v1.4.3
7+
github.com/stretchr/testify v1.7.0
78
golang.org/x/tools v0.5.0
89
gopkg.in/yaml.v3 v3.0.1
910
)
1011

11-
require github.com/alexflint/go-scalar v1.1.0 // indirect
12+
require (
13+
github.com/alexflint/go-scalar v1.1.0 // indirect
14+
github.com/davecgh/go-spew v1.1.1 // indirect
15+
github.com/pmezard/go-difflib v1.0.0 // indirect
16+
)

main.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ func (args) Version() string {
4040
func (a *args) toConfig() testcoverage.Config {
4141
cfg := testcoverage.NewConfig()
4242

43-
cfg.Profile = a.Profile
43+
cfg.Profile = fromMagicToEmpty(a.Profile)
4444
cfg.GithubActionOutput = a.GithubActionOutput
45-
cfg.LocalPrefix = a.LocalPrefix
45+
cfg.LocalPrefix = fromMagicToEmpty(a.LocalPrefix)
4646
cfg.Threshold.File = a.ThresholdFile
4747
cfg.Threshold.Package = a.ThresholdPackage
4848
cfg.Threshold.Total = a.ThresholdTotal
@@ -93,8 +93,9 @@ func readConfig() (testcoverage.Config, error) {
9393
}
9494
arg.MustParse(&cmdArgs)
9595

96-
if cmdArgs.ConfigPath != "" {
97-
err := testcoverage.ConfigFromFile(&cfg, cmdArgs.ConfigPath)
96+
cfgPath := fromMagicToEmpty(cmdArgs.ConfigPath)
97+
if cfgPath != "" {
98+
err := testcoverage.ConfigFromFile(&cfg, cfgPath)
9899
if err != nil {
99100
return testcoverage.Config{}, fmt.Errorf("failed loading config from file: %w", err)
100101
}
@@ -108,3 +109,11 @@ func readConfig() (testcoverage.Config, error) {
108109

109110
return cfg, nil
110111
}
112+
113+
func fromMagicToEmpty(s string) string {
114+
if s == `''` {
115+
return ""
116+
}
117+
118+
return s
119+
}

pkg/testcoverage/config_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package testcoverage_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
. "github.com/vladopajic/go-test-coverage/pkg/testcoverage"
9+
)
10+
11+
func Test_NewConfig(t *testing.T) {
12+
t.Parallel()
13+
14+
cfg := NewConfig()
15+
16+
assert.Empty(t, cfg.Profile)
17+
assert.Empty(t, cfg.LocalPrefix)
18+
assert.False(t, cfg.GithubActionOutput)
19+
assert.Equal(t, DefaultFileThreshold, cfg.Threshold.File)
20+
assert.Equal(t, DefaultPackageThreshold, cfg.Threshold.Package)
21+
assert.Equal(t, DefaultTotalThreshold, cfg.Threshold.Total)
22+
}
23+
24+
func Test_Config_Validate(t *testing.T) {
25+
t.Parallel()
26+
27+
newValidCfg := func() Config {
28+
cfg := NewConfig()
29+
cfg.Profile = "cover.out"
30+
31+
return cfg
32+
}
33+
34+
cfg := newValidCfg()
35+
assert.NoError(t, cfg.Validate())
36+
37+
cfg = newValidCfg()
38+
cfg.Profile = ""
39+
assert.ErrorIs(t, cfg.Validate(), ErrCoverageProfileNotSpecified)
40+
41+
cfg = newValidCfg()
42+
cfg.Threshold.File = 101
43+
assert.ErrorIs(t, cfg.Validate(), ErrThresholdNotInRange)
44+
45+
cfg = newValidCfg()
46+
cfg.Threshold.File = -1
47+
assert.ErrorIs(t, cfg.Validate(), ErrThresholdNotInRange)
48+
49+
cfg = newValidCfg()
50+
cfg.Threshold.Package = 101
51+
assert.ErrorIs(t, cfg.Validate(), ErrThresholdNotInRange)
52+
53+
cfg = newValidCfg()
54+
cfg.Threshold.Package = -1
55+
assert.ErrorIs(t, cfg.Validate(), ErrThresholdNotInRange)
56+
57+
cfg = newValidCfg()
58+
cfg.Threshold.Total = 101
59+
assert.ErrorIs(t, cfg.Validate(), ErrThresholdNotInRange)
60+
61+
cfg = newValidCfg()
62+
cfg.Threshold.Total = -1
63+
assert.ErrorIs(t, cfg.Validate(), ErrThresholdNotInRange)
64+
}
65+
66+
func Test_ConfigFromFile(t *testing.T) {
67+
t.Parallel()
68+
69+
cfg := NewConfig()
70+
cfgBefore := NewConfig()
71+
err := ConfigFromFile(&cfg, t.TempDir())
72+
assert.Error(t, err)
73+
assert.Equal(t, cfgBefore, cfg)
74+
}

pkg/testcoverage/export_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package testcoverage
2+
3+
const (
4+
DefaultFileThreshold = defaultFileThreshold
5+
DefaultPackageThreshold = defaultPackageThreshold
6+
DefaultTotalThreshold = defaultTotalThreshold
7+
)

0 commit comments

Comments
 (0)