-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodfmt_test.go
71 lines (61 loc) · 1.54 KB
/
modfmt_test.go
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
package main
import (
"os"
"testing"
"golang.org/x/mod/modfile"
)
const (
testFileName = "testdata/go.mod"
updatedTestFileName = "testdata/updated_go.mod"
)
func TestMergeRequires(t *testing.T) {
// fmt the go.mod file
updatedContents, err := MergeRequires(testFileName)
if err != nil {
t.Fatal(err)
}
// create a new file with the updated contents
newFile, err := os.Create(updatedTestFileName)
if err != nil {
t.Fatal(err)
}
defer newFile.Close()
if _, err = newFile.Write(updatedContents); err != nil {
t.Fatal(err)
}
// parse the old go.mod file
oldContents, err := os.ReadFile(testFileName)
if err != nil {
t.Fatal(err)
}
oldmod, err := modfile.ParseLax(testFileName, oldContents, nil)
if err != nil {
t.Fatal(err)
}
// parse the updated go.mod file
newContents, err := os.ReadFile(updatedTestFileName)
if err != nil {
t.Fatal(err)
}
newmod, err := modfile.ParseLax(updatedTestFileName, newContents, nil)
if err != nil {
t.Fatal(err)
}
// Check if both mod reqs have the same length
if len(oldmod.Require) != len(newmod.Require) {
t.Errorf("Require length mismatch: %d != %d", len(oldmod.Require), len(newmod.Require))
}
// Check if both mod reqs have the same content
for _, req := range oldmod.Require {
found := false
for _, newReq := range newmod.Require {
if req.Mod.Path == newReq.Mod.Path && req.Mod.Version == newReq.Mod.Version {
found = true
break
}
}
if !found {
t.Errorf("Require mismatch: %s@%s not found in updated go.mod", req.Mod.Path, req.Mod.Version)
}
}
}