-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_coverage_test.go
More file actions
145 lines (104 loc) · 3.99 KB
/
Copy pathprocess_coverage_test.go
File metadata and controls
145 lines (104 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//nolint:testpackage // Internal tests for unexported functions.
package testastic
import (
"os"
"path/filepath"
"testing"
)
func TestConvertProcessCoverage(t *testing.T) {
t.Parallel()
t.Run("empty dir produces no output", func(t *testing.T) {
t.Parallel()
// given: an empty coverage directory
coverDir := t.TempDir()
outputPath := filepath.Join(t.TempDir(), "coverage.out")
// when: converting process coverage
err := convertProcessCoverage(coverDir, outputPath)
// then: no output file is created and no error is returned
NoError(t, err)
_, err = os.Stat(outputPath)
True(t, os.IsNotExist(err))
})
t.Run("creates output directory", func(t *testing.T) {
t.Parallel()
// given: an empty coverage directory and nested output path
coverDir := t.TempDir()
outputPath := filepath.Join(t.TempDir(), "nested", "dir", "coverage.out")
// when: converting process coverage
err := convertProcessCoverage(coverDir, outputPath)
// then: no output directory is created because there is no coverage data
NoError(t, err)
// Output dir should exist even though no coverage data was produced
_, err = os.Stat(filepath.Dir(outputPath))
// Dir is not created when there are no entries
True(t, os.IsNotExist(err))
})
}
func TestCollectSubprocessCoverage(t *testing.T) {
t.Run("returns nonzero when conversion fails after passing tests", func(t *testing.T) {
// given: subprocess coverage collection with an invalid output path
blockedDir := filepath.Join(t.TempDir(), "not-a-dir")
writeErr := os.WriteFile(blockedDir, []byte("blocked"), 0o600)
NoError(t, writeErr)
outputPath := filepath.Join(blockedDir, "coverage.out")
// when: tests pass but coverage conversion fails
code := collectSubprocessCoverage(func() int {
err := os.WriteFile(filepath.Join(getSharedCoverDir(), "covdata-entry"), []byte("coverage"), 0o600)
NoError(t, err)
return 0
}, outputPath)
// then: coverage conversion failure fails the run
Equal(t, 1, code)
})
}
func TestSetupCoverDir_sharedDir(t *testing.T) {
t.Run("creates a per-run subdir under the shared dir", func(t *testing.T) {
// given: a shared coverage directory is already configured
dir := t.TempDir()
old := getSharedCoverDir()
setSharedCoverDir(dir)
defer func() { setSharedCoverDir(old) }()
// when: resolving the cover directory without an explicit override
result := setupCoverDir(t, "")
// then: the result is a fresh subdir inside the shared directory
NotEqual(t, dir, result)
HasPrefix(t, result, dir+string(filepath.Separator))
info, err := os.Stat(result)
NoError(t, err)
True(t, info.IsDir())
})
t.Run("each call returns a unique subdir to avoid covmeta races", func(t *testing.T) {
// given: a shared coverage directory is already configured
dir := t.TempDir()
old := getSharedCoverDir()
setSharedCoverDir(dir)
defer func() { setSharedCoverDir(old) }()
// when: setupCoverDir is invoked twice by concurrent runs
first := setupCoverDir(t, "")
second := setupCoverDir(t, "")
// then: the two calls hand out distinct directories (no covmeta.<hash> race)
NotEqual(t, first, second)
})
t.Run("explicit coverDir takes precedence over shared dir", func(t *testing.T) {
// given: both a shared directory and an explicit directory are configured
explicit := t.TempDir()
old := getSharedCoverDir()
setSharedCoverDir(t.TempDir())
defer func() { setSharedCoverDir(old) }()
// when: resolving the cover directory with an explicit override
result := setupCoverDir(t, explicit)
// then: the explicit directory wins
Equal(t, explicit, result)
})
t.Run("falls back to temp dir when shared dir is empty", func(t *testing.T) {
// given: no shared or explicit coverage directory is configured
old := getSharedCoverDir()
setSharedCoverDir("")
defer func() { setSharedCoverDir(old) }()
// when: resolving the cover directory
result := setupCoverDir(t, "")
// then: a temp coverage directory is created
Contains(t, result, "coverage")
NotEqual(t, "", result)
})
}