142 lines
6.0 KiB
Go
142 lines
6.0 KiB
Go
package releaseidentity
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNormalizeSource(t *testing.T) {
|
|
empty := ""
|
|
tests := []struct {
|
|
name string
|
|
module string
|
|
source *SourceSpec
|
|
want SourceSelection
|
|
invalid bool
|
|
}{
|
|
{name: "legacy", module: "nested", want: SourceSelection{Dir: "nested", GoEnabled: true, Publication: "go-module", VersionFormat: "semver", VersionPrefix: "v"}},
|
|
{name: "explicit root", source: &SourceSpec{}, want: SourceSelection{Dir: ".", Publication: "source", VersionFormat: "semver", VersionPrefix: "v"}},
|
|
{name: "Go application", module: "app", source: &SourceSpec{GoEnabled: true, VersionFormat: "date"}, want: SourceSelection{Dir: "app", GoEnabled: true, Publication: "source", VersionFormat: "date"}},
|
|
{name: "Go module", module: "app/.", source: &SourceSpec{Dir: "app", GoEnabled: true, Publication: "go-module", VersionPrefix: &empty}, want: SourceSelection{Dir: "app", GoEnabled: true, Publication: "go-module", VersionFormat: "semver"}},
|
|
{name: "missing Go", source: &SourceSpec{Publication: "go-module"}, invalid: true},
|
|
{name: "invalid format", source: &SourceSpec{VersionFormat: "other"}, invalid: true},
|
|
{name: "invalid prefix", source: &SourceSpec{VersionPrefix: strPtr("release-")}, invalid: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := NormalizeSource(tt.module, tt.source)
|
|
if (err != nil) != tt.invalid {
|
|
t.Fatalf("NormalizeSource error = %v", err)
|
|
}
|
|
if !tt.invalid && got != tt.want {
|
|
t.Fatalf("NormalizeSource = %+v, want %+v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadSourceUsesSelectedDeclaration(t *testing.T) {
|
|
repo := t.TempDir()
|
|
gitIdentityRun(t, repo, "init", "-q")
|
|
gitIdentityRun(t, repo, "config", "user.name", "ReleaseKit test")
|
|
gitIdentityRun(t, repo, "config", "user.email", "releasekit@example.invalid")
|
|
if err := os.Mkdir(filepath.Join(repo, "app"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mod := filepath.Join(repo, "app", "go.mod")
|
|
if err := os.WriteFile(mod, []byte("module example.com/committed\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink("app", filepath.Join(repo, "alias")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Mkdir(filepath.Join(repo, "config"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(repo, "config", "settings.json"), []byte("{}"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gitIdentityRun(t, repo, "add", ".")
|
|
gitIdentityRun(t, repo, "commit", "-qm", "add module")
|
|
commit := strings.TrimSpace(gitIdentityOutput(t, repo, "rev-parse", "HEAD"))
|
|
if err := os.WriteFile(mod, []byte("module example.com/working\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
spec := &SourceSpec{Dir: "app", GoEnabled: true}
|
|
working, err := LoadWorkingSource(context.Background(), repo, "", spec)
|
|
if err != nil || working.ModulePath != "example.com/working" || working.ModuleSubdir != "app" || working.Publication != "source" {
|
|
t.Fatalf("working source = %+v, error = %v", working, err)
|
|
}
|
|
committed, err := LoadCommittedSource(context.Background(), repo, "", commit, spec)
|
|
if err != nil || committed.ModulePath != "example.com/committed" || committed.ModuleSubdir != "app" {
|
|
t.Fatalf("committed source = %+v, error = %v", committed, err)
|
|
}
|
|
if _, err := LoadCommittedSource(context.Background(), repo, "alias", commit, spec); err != nil {
|
|
t.Fatalf("matching committed directories: %v", err)
|
|
}
|
|
if _, err := LoadCommittedSource(context.Background(), repo, ".", commit, spec); err == nil {
|
|
t.Fatal("expected conflicting committed directories to fail")
|
|
}
|
|
moduleSpec := &SourceSpec{Dir: "app", GoEnabled: true, Publication: "go-module"}
|
|
identity, err := FromCommittedSource(context.Background(), repo, "alias", "v1.2.3", commit, moduleSpec)
|
|
if err != nil || identity.ModulePath != "example.com/committed" || identity.Tag != "app/v1.2.3" {
|
|
t.Fatalf("explicit module identity = %+v, error = %v", identity, err)
|
|
}
|
|
matchingPrefix, conflictingPrefix := "app", "other"
|
|
moduleSpec.TagPrefix = &matchingPrefix
|
|
if _, err := FromCommittedSource(context.Background(), repo, "", "v1.2.3", commit, moduleSpec); err != nil {
|
|
t.Fatalf("matching module tag prefix: %v", err)
|
|
}
|
|
moduleSpec.TagPrefix = &conflictingPrefix
|
|
if _, err := FromCommittedSource(context.Background(), repo, "", "v1.2.3", commit, moduleSpec); err == nil {
|
|
t.Fatal("accepted conflicting module tag prefix")
|
|
}
|
|
config, err := LoadCommittedSource(context.Background(), repo, "", commit, &SourceSpec{Dir: "config"})
|
|
if err != nil || config.ModulePath != "" || config.ModuleSubdir != "config" {
|
|
t.Fatalf("committed source-only selection = %+v, error = %v", config, err)
|
|
}
|
|
if err := os.Remove(mod); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := LoadWorkingSource(context.Background(), repo, "", spec); err == nil {
|
|
t.Fatal("Go selection accepted a missing module declaration")
|
|
}
|
|
if source, err := LoadWorkingSource(context.Background(), repo, "", &SourceSpec{Dir: "app"}); err != nil || source.ModulePath != "" {
|
|
t.Fatalf("source-only selection = %+v, error = %v", source, err)
|
|
}
|
|
}
|
|
|
|
func strPtr(s string) *string { return &s }
|
|
|
|
func TestResolveWorkingSourceWithoutModule(t *testing.T) {
|
|
repo := t.TempDir()
|
|
if err := os.Mkdir(filepath.Join(repo, "config"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := ResolveWorkingSource(repo, "", &SourceSpec{Dir: "config"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.ModuleSubdir != "config" || got.GoEnabled || got.Publication != "source" {
|
|
t.Fatalf("selection = %+v", got)
|
|
}
|
|
if err := os.Symlink("config", filepath.Join(repo, "alias")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := ResolveWorkingSource(repo, "alias", &SourceSpec{Dir: "config"}); err != nil {
|
|
t.Fatalf("matching physical directories: %v", err)
|
|
}
|
|
if _, err := ResolveWorkingSource(repo, ".", &SourceSpec{Dir: "config"}); err == nil {
|
|
t.Fatal("expected conflicting directories to fail")
|
|
}
|
|
if err := os.WriteFile(filepath.Join(repo, "file"), nil, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := ResolveWorkingSource(repo, "", &SourceSpec{Dir: "file"}); err == nil {
|
|
t.Fatal("expected a file selection to fail")
|
|
}
|
|
}
|