2025-01-31 17:39:46 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSubmodules(t *testing.T) {
|
|
|
|
tests := []struct {
|
2025-02-02 21:07:51 +01:00
|
|
|
name string
|
|
|
|
file string
|
|
|
|
subs []Submodule
|
|
|
|
has_error bool
|
2025-01-31 17:39:46 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Empty submodules file",
|
|
|
|
file: "",
|
|
|
|
subs: []Submodule{},
|
|
|
|
},
|
|
|
|
{
|
2025-02-02 21:07:51 +01:00
|
|
|
name: "Empty single submodule",
|
|
|
|
file: "[submodule \"Foo\"]",
|
|
|
|
subs: []Submodule{
|
|
|
|
{Name: "Foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid submodule name",
|
|
|
|
file: "[submodule \"Foo']",
|
|
|
|
has_error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid submodule name",
|
|
|
|
file: "[submodule 'Foo']",
|
|
|
|
has_error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid submodule name",
|
|
|
|
file: "[submodule Foo]",
|
|
|
|
has_error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid submodule name",
|
|
|
|
file: "[submodul \"Foo\"]",
|
|
|
|
has_error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid submodule name",
|
|
|
|
file: "[submodule \"Foo\"",
|
|
|
|
has_error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Gerbage input",
|
|
|
|
file: "asdf kjasf[d;fkl",
|
|
|
|
has_error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Submodule with one entry",
|
|
|
|
file: "[submodule \"libfoo\"]\npath = foo\n\n",
|
2025-01-31 17:39:46 +01:00
|
|
|
subs: []Submodule{
|
|
|
|
{
|
|
|
|
Name: "libfoo",
|
2025-02-02 21:07:51 +01:00
|
|
|
Path: "foo",
|
2025-01-31 17:39:46 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2025-02-02 21:07:51 +01:00
|
|
|
/*
|
|
|
|
{
|
|
|
|
name: "Simple submodule",
|
|
|
|
file: `[submodule "libfoo"]
|
|
|
|
path = include/foo
|
|
|
|
url = git://foo.com/git/lib.git
|
|
|
|
|
|
|
|
[submodule "libbar"]
|
|
|
|
path = include/bar
|
|
|
|
url = git://bar.com/git/lib.git`,
|
|
|
|
|
|
|
|
subs: []Submodule{
|
|
|
|
{
|
|
|
|
Name: "libfoo",
|
|
|
|
Path: "include/foo",
|
|
|
|
Url: "git://foo.com/git/lib.git",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "libbar",
|
|
|
|
Path: "include/bar",
|
|
|
|
Url: "git://bar.com/git/lib.git",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
*/
|
2025-01-31 17:39:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2025-02-02 21:07:51 +01:00
|
|
|
mods, err := ParseSubmodulesFile(strings.NewReader(test.file))
|
|
|
|
if test.has_error {
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected an error")
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2025-01-31 17:39:46 +01:00
|
|
|
t.Error("unexpected error:", err)
|
|
|
|
}
|
|
|
|
if !slices.Equal(mods, test.subs) {
|
|
|
|
t.Error("expected", test.subs, "but got", mods)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|