62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
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
|
|
})
|
|
}
|
|
}
|