package releaseidentity import ( "context" "encoding/hex" "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) } if err := os.MkdirAll(filepath.Join(repo, "product", "v2"), 0755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(repo, "product", "v2", "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) } goDiagnostic, _, err := NewDiagnostic(context.Background(), working, "app", commit) if err != nil || !goDiagnostic.GoEnabled || goDiagnostic.ModulePath != "example.com/working" || goDiagnostic.ModuleDir != working.ModuleDir { t.Fatalf("Go diagnostic identity = %+v, error = %v", goDiagnostic, 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) } if _, err := FromCommittedSource(context.Background(), repo, "", "v2.0.0", commit, moduleSpec); err == nil { t.Fatal("accepted module version incompatible with import-path major") } 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) } diagnostic, attempt, err := NewDiagnostic(context.Background(), config, "config", commit) if err != nil || len(attempt) != 32 || diagnostic.Kind != "diagnostic" || diagnostic.Version != "releasekit-test-"+attempt || diagnostic.Tag != "releasekit-test/config/"+attempt || diagnostic.SourceSubdir != "config" || diagnostic.ModuleDir != "" || diagnostic.Commit != commit { t.Fatalf("diagnostic identity = %+v, attempt = %q, error = %v", diagnostic, attempt, err) } if _, err := hex.DecodeString(attempt); err != nil || attempt != strings.ToLower(attempt) { t.Fatalf("invalid diagnostic attempt ID %q", attempt) } if !strings.Contains(diagnostic.Note, attempt) || !strings.Contains(diagnostic.Note, commit) || diagnostic.NotePath != "" { t.Fatalf("diagnostic note not bound to its attempt and commit: %+v", diagnostic) } for _, selector := range []string{"", "a/b", ".invalid", "invalid..name"} { if _, _, err := NewDiagnostic(context.Background(), config, selector, commit); err == nil { t.Fatalf("accepted diagnostic selector %q", selector) } } configIdentity, err := FromCommittedSource(context.Background(), repo, "", "2024-02-29.1", commit, &SourceSpec{Dir: "config", VersionFormat: "date"}) if err != nil || configIdentity.Kind != "production" || configIdentity.SourceSubdir != "config" || configIdentity.ModuleDir != "" || configIdentity.ModulePath != "" || configIdentity.Commit != commit || configIdentity.Tag != "config/2024-02-29.1" || configIdentity.NotePath != "config/docs/releases/2024-02-29.1.md" { t.Fatalf("source identity = %+v, error = %v", configIdentity, err) } application, err := FromCommittedSource(context.Background(), repo, "", "v2.0.0", commit, spec) if err != nil || application.ModulePath != "example.com/committed" || application.Tag != "app/v2.0.0" { t.Fatalf("application identity = %+v, error = %v", application, err) } calendarApp, err := FromCommittedSource(context.Background(), repo, "", "2026.12.0", commit, &SourceSpec{Dir: "app", GoEnabled: true, VersionFormat: "year-month"}) if err != nil || calendarApp.ModulePath != "example.com/committed" || calendarApp.Tag != "app/2026.12.0" || calendarApp.NotePath != "app/docs/releases/2026.12.0.md" { t.Fatalf("calendar application identity = %+v, error = %v", calendarApp, err) } noPrefix, badPrefix := "", "bad..name" plain, err := FromCommittedSource(context.Background(), repo, "", "v1.2.3", commit, &SourceSpec{Dir: "config", TagPrefix: &noPrefix}) if err != nil || plain.Tag != "v1.2.3" { t.Fatalf("unprefixed identity = %+v, error = %v", plain, err) } majorDir, err := FromCommittedSource(context.Background(), repo, "", "v2.0.0", commit, &SourceSpec{Dir: "product/v2"}) if err != nil || majorDir.Tag != "product/v2/v2.0.0" { t.Fatalf("physical major directory identity = %+v, error = %v", majorDir, err) } if _, err := FromCommittedSource(context.Background(), repo, "", "v1.2.3", commit, &SourceSpec{Dir: "config", TagPrefix: &badPrefix}); err == nil { t.Fatal("accepted invalid Git tag prefix") } 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") } }