-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_test.go
96 lines (69 loc) · 2.58 KB
/
mod_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
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
// SPDX-FileCopyrightText: 2020-2024 caixw
//
// SPDX-License-Identifier: MIT
package source
import (
"io/fs"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/issue9/assert/v4"
)
func TestPkgSourceDir(t *testing.T) {
a := assert.New(t, false)
// std
dir, err := PkgSourceDir("encoding/json", "./", false)
a.NotError(err).FileExists(dir)
dir, err = PkgSourceDir("note-exists", "./", false)
a.NotError(err).FileNotExists(dir)
// require
dir, err = PkgSourceDir("github.com/issue9/assert/v4", "./", false)
a.NotError(err).FileExists(dir).
True(strings.HasSuffix(filepath.ToSlash(dir), "assert/[email protected]"))
dir, err = PkgSourceDir("github.com/issue9/assert/v4/rest", "./", false)
a.NotError(err).FileExists(dir).
True(strings.HasSuffix(filepath.ToSlash(dir), "assert/[email protected]/rest"))
dir, err = PkgSourceDir("github.com/issue9/assertxx", "./", false)
a.ErrorIs(err, fs.ErrNotExist).Empty(dir)
// replace
dir, err = PkgSourceDir("github.com/issue9/web/v2", "./testdata/go.mod", true)
a.NotError(err).NotEmpty(dir) // 此处 dir 可能不存在,因为 go.mod 关于 web 的包是随便指定的
dir, err = PkgSourceDir("github.com/issue9/source", "./testdata/go.mod", true)
a.NotError(err).FileExists(dir)
// not exist
dir, err = PkgSourceDir("github.com/issue9/not-exists", "./testdata/go.mod", true)
a.ErrorIs(err, fs.ErrNotExist).Empty(dir)
}
func TestModFile(t *testing.T) {
a := assert.New(t, false)
p, f, err := ModFile("./")
a.NotError(err).NotNil(f).Equal(f.Module.Mod.Path, "github.com/issue9/source").NotEmpty(p)
p, f, err = ModFile("./testdata")
a.NotError(err).NotNil(f).Equal(f.Module.Mod.Path, "github.com/issue9/source").NotEmpty(p)
// NOTE: 可能不存在 c:\windows\system32 或是 c:\windows\system32 下正好存在一个 go.mod
dir := "/windows/system32"
if runtime.GOOS == "windows" {
dir = "c:\\windows\\system32"
}
p, f, err = ModFile(dir)
a.Error(err).Nil(f).Empty(p)
}
func TestModDir(t *testing.T) {
a := assert.New(t, false)
d, err := ModDir("./")
a.NotError(err).NotEmpty(d)
}
func TestPkgPath(t *testing.T) {
a := assert.New(t, false)
p, err := PkgPath("./")
a.NotError(err).Equal(p, "github.com/issue9/source")
p, err = PkgPath("./testdata")
a.NotError(err).Equal(p, "github.com/issue9/source/testdata")
p, err = PkgPath("./testdata/go.mod/sub/")
a.NotError(err).Equal(p, "github.com/issue9/source/mod/sub")
p, err = PkgPath("./testdata/go.mod/sub/sub.go")
a.NotError(err).Equal(p, "github.com/issue9/source/mod/sub")
p, err = PkgPath("./testdata/go.mod/go.mod")
a.NotError(err).Equal(p, "github.com/issue9/source/mod")
}