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
|
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/matryer/is"
)
func TestModule(t *testing.T) {
tmp := t.TempDir()
tt := []struct {
Name string
Path string
Expected string
}{
{
Name: "gitea",
Path: "gitea.com/user1/repo",
Expected: "gitea.com/user1/repo",
},
{
Name: "gitea_sub",
Path: "gitea.com/user2/repo/subpkg",
Expected: "gitea.com/user2/repo/subpkg",
},
{
Name: "no_uri",
Path: "giteacom/user3/repo",
Expected: "%s/giteacom/user3/repo",
},
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
assert := is.New(t)
dir := filepath.Join(tmp, tc.Path)
err := os.MkdirAll(dir, os.ModePerm)
assert.NoErr(err) // Should create temp dir
err = os.Chdir(dir)
assert.NoErr(err) // Should chdir to temp dir
name, err := module()
assert.NoErr(err) // Should get module name
expected := tc.Expected
if strings.Contains(expected, "%s") {
expected = fmt.Sprintf(expected, tmp)
}
assert.Equal(name, expected) // Module name should match expected name
})
}
}
|