-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers_test.go
More file actions
114 lines (101 loc) · 2.73 KB
/
helpers_test.go
File metadata and controls
114 lines (101 loc) · 2.73 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
package main
import (
"fmt"
"maps"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func selectionNames(selection *packageSelection) []string {
if selection == nil {
return nil
}
return slices.Sorted(maps.Keys(selection.Tests))
}
// mustPackageInventory builds a packageInventory for the synthetic "pkg"
// directory and "sample" package used throughout the test suite.
func mustPackageInventory(t *testing.T, files map[string]string) packageInventory {
t.Helper()
const packageName = "sample"
inventory := packageInventory{
Key: packageKey{Dir: "pkg", Name: packageName},
Tests: map[string]struct{}{},
}
for _, content := range files {
snapshot, err := parseFileSnapshot([]byte(content))
require.NoError(t, err)
require.Equal(t, packageName, snapshot.packageName)
for testName := range snapshot.tests {
inventory.Tests[testName] = struct{}{}
}
}
return inventory
}
func mustFileSnapshot(t *testing.T, data []byte) fileSnapshot {
t.Helper()
snapshot, err := parseFileSnapshot(data)
require.NoError(t, err)
return snapshot
}
func mustOptionalFileSnapshot(t *testing.T, data []byte) *fileSnapshot {
t.Helper()
if data == nil {
return nil
}
snapshot := mustFileSnapshot(t, data)
return &snapshot
}
func diffForChange(oldRange, newRange lineRange) string {
return fmt.Sprintf("@@ -%s +%s @@\n", formatDiffRange(oldRange), formatDiffRange(newRange))
}
func formatDiffRange(r lineRange) string {
if !r.hasLines() {
start := r.Start
if start == 0 {
start = 1
}
return fmt.Sprintf("%d,0", start)
}
count := r.End - r.Start + 1
if count == 1 {
return fmt.Sprintf("%d", r.Start)
}
return fmt.Sprintf("%d,%d", r.Start, count)
}
func singleLineRange(t *testing.T, content, needle string) lineRange {
t.Helper()
line := lineNumberForSubstring(t, content, needle)
return lineRange{Start: line, End: line}
}
func rangeSpan(start, end lineRange) lineRange {
return lineRange{Start: start.Start, End: end.End}
}
func emptyRangeAt(start int) lineRange {
return lineRange{Start: start, End: start - 1}
}
func lineNumberForSubstring(t *testing.T, content, needle string) int {
t.Helper()
lineNumber := 0
for index, line := range strings.Split(content, "\n") {
if !strings.Contains(line, needle) {
continue
}
if lineNumber != 0 {
t.Fatalf("needle %q matched more than once", needle)
}
lineNumber = index + 1
}
if lineNumber == 0 {
t.Fatalf("needle %q not found", needle)
}
return lineNumber
}
func writeTestFile(t *testing.T, root, relativePath, content string) {
t.Helper()
path := filepath.Join(root, filepath.FromSlash(relativePath))
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o750))
require.NoError(t, os.WriteFile(path, []byte(content), 0o600))
}