-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmod_test.go
More file actions
57 lines (47 loc) · 1.03 KB
/
mod_test.go
File metadata and controls
57 lines (47 loc) · 1.03 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
package mod
import (
"errors"
"os"
"path/filepath"
"testing"
)
func TestGetModFile(t *testing.T) {
dir := t.TempDir()
err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte(`
module example.com/foo
require example.com/whatever v1.2.3
tool example.com/whatever/cmd/thing
go 1.23.4
`), 0o644)
if err != nil {
t.Fatal(err)
}
mod, err := GetModFile(dir)
if err != nil {
t.Fatal(err)
}
if mod.Module.Mod.Path != "example.com/foo" {
t.Fatalf("unexpected module path: %v", mod.Module.Mod.Path)
}
}
func TestGetModFileErrors(t *testing.T) {
t.Run("DoesNotExist", func(t *testing.T) {
_, err := GetModFile(t.TempDir())
if err == nil {
t.Fatal("expected error")
}
if !errors.Is(err, os.ErrNotExist) {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("SyntaxError", func(t *testing.T) {
dir := t.TempDir()
err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("syntax error"), 0o644)
if err != nil {
t.Fatal(err)
}
if _, err := GetModFile(dir); err == nil {
t.Fatal("expected error")
}
})
}