Resolve source and diagnostic release identities
This commit is contained in:
@@ -6,11 +6,9 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"go.maximumdirect.net/releasekit/internal/candidate"
|
||||
"go.maximumdirect.net/releasekit/internal/gotool"
|
||||
"go.maximumdirect.net/releasekit/internal/releaseidentity"
|
||||
"go.maximumdirect.net/releasekit/internal/tempdir"
|
||||
)
|
||||
|
||||
@@ -25,21 +23,6 @@ func candidateCallback(spec *Spec, output io.Writer, remove func(string) error)
|
||||
selected := *spec
|
||||
selected.Targets = append([]Target(nil), spec.Targets...)
|
||||
return func(ctx context.Context, captured candidate.Candidate, owner *gotool.Owner) (resultErr error) {
|
||||
moduleSubdir, err := filepath.Rel(captured.RepositoryDir, captured.ModuleDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve captured module path: %w", err)
|
||||
}
|
||||
identity := releaseidentity.Identity{
|
||||
Selection: releaseidentity.Selection{
|
||||
RepositoryDir: captured.RepositoryDir,
|
||||
ModuleDir: captured.ModuleDir,
|
||||
ModuleSubdir: filepath.ToSlash(moduleSubdir),
|
||||
},
|
||||
ModulePath: captured.ModulePath,
|
||||
Version: captured.Version,
|
||||
Tag: captured.Tag,
|
||||
NotePath: filepath.ToSlash(filepath.Join(moduleSubdir, "docs", "releases", captured.Version+".md")),
|
||||
}
|
||||
temporaryDir, err := tempdir.Outside(captured.RepositoryDir, "releasekit-candidate-build-")
|
||||
if err != nil {
|
||||
return fmt.Errorf("create temporary candidate build output: %w", err)
|
||||
@@ -50,7 +33,7 @@ func candidateCallback(spec *Spec, output io.Writer, remove func(string) error)
|
||||
}
|
||||
}()
|
||||
_, err = BuildResolved(ctx, Request{
|
||||
Identity: identity,
|
||||
Identity: captured.Identity,
|
||||
Spec: selected,
|
||||
OutputDir: temporaryDir,
|
||||
Output: output,
|
||||
|
||||
@@ -109,7 +109,7 @@ func TestCandidateBuildCleanupPreservesOutcome(t *testing.T) {
|
||||
_ = os.RemoveAll(path)
|
||||
return cleanupErr
|
||||
})
|
||||
err := callback(ctx, candidate.Candidate{RepositoryDir: identity.RepositoryDir, ModuleDir: identity.ModuleDir, ModulePath: identity.ModulePath, Version: identity.Version, Tag: identity.Tag}, nil)
|
||||
err := callback(ctx, candidate.Candidate{Identity: identity, RepositoryDir: identity.RepositoryDir, ModuleDir: identity.ModuleDir, ModulePath: identity.ModulePath, Version: identity.Version, Tag: identity.Tag}, nil)
|
||||
if !errors.Is(err, cleanupErr) || removed == "" || !strings.Contains(err.Error(), removed) {
|
||||
t.Fatalf("candidate cleanup = %q, error = %v", removed, err)
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
var ErrInvalidVersion = errors.New("invalid stable version")
|
||||
|
||||
type Candidate struct {
|
||||
Identity releaseidentity.Identity
|
||||
RepositoryDir string
|
||||
ModuleDir string
|
||||
ModulePath string
|
||||
@@ -93,12 +94,16 @@ func Capture(ctx context.Context, request Request) (Candidate, error) {
|
||||
}
|
||||
|
||||
identity, err := releaseidentity.FromCommittedSource(ctx, repositoryDir, request.ModuleDir, request.Version, state.commit, request.Source)
|
||||
captured.Identity = identity
|
||||
captured.ModuleDir = identity.ModuleDir
|
||||
captured.ModulePath = identity.ModulePath
|
||||
captured.Tag = identity.Tag
|
||||
if err != nil {
|
||||
return captured, fmt.Errorf("resolve selected module release identity: %w", err)
|
||||
}
|
||||
if identity.Publication == "source" {
|
||||
return captured, fmt.Errorf("general source candidate validation is not yet supported")
|
||||
}
|
||||
note, err := git.ReadCommitFile(ctx, identity.RepositoryDir, state.commit, identity.NotePath)
|
||||
if err != nil {
|
||||
return captured, fmt.Errorf("read committed release note %q: %w", identity.NotePath, err)
|
||||
|
||||
@@ -93,6 +93,9 @@ func deliver(ctx context.Context, request Request, remove func(string) error) (r
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("resolve committed delivery identity: %w", err)
|
||||
}
|
||||
if identity.Publication == "source" {
|
||||
return result, errors.New("general source delivery is not yet supported")
|
||||
}
|
||||
result.RepositoryDir = identity.RepositoryDir
|
||||
result.ModuleDir = identity.ModuleDir
|
||||
result.ModulePath = identity.ModulePath
|
||||
|
||||
43
internal/releaseidentity/diagnostic.go
Normal file
43
internal/releaseidentity/diagnostic.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package releaseidentity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go.maximumdirect.net/releasekit/internal/git"
|
||||
)
|
||||
|
||||
// NewDiagnostic constructs a unique diagnostic identity for an already
|
||||
// resolved source. Diagnostic versions are independent of production formats;
|
||||
// Publication records the source selection, not a module publication claim.
|
||||
func NewDiagnostic(ctx context.Context, source ResolvedSource, selector, commit string) (Identity, string, error) {
|
||||
identity := Identity{
|
||||
Selection: source.Selection, Kind: "diagnostic", SourceDir: source.ModuleDir, SourceSubdir: source.ModuleSubdir,
|
||||
GoEnabled: source.GoEnabled, Publication: source.Publication, VersionFormat: source.VersionFormat,
|
||||
VersionPrefix: source.VersionPrefix, ModulePath: source.ModulePath, Commit: commit,
|
||||
}
|
||||
if source.RepositoryDir == "" || source.ModuleDir == "" || source.ModuleSubdir == "" {
|
||||
return identity, "", fmt.Errorf("incomplete source selection")
|
||||
}
|
||||
if selector == "" || strings.Contains(selector, "/") {
|
||||
return identity, "", fmt.Errorf("diagnostic selector must be one Git ref component")
|
||||
}
|
||||
if !source.GoEnabled {
|
||||
identity.ModuleDir, identity.ModuleSubdir = "", ""
|
||||
}
|
||||
var random [16]byte
|
||||
if _, err := rand.Read(random[:]); err != nil {
|
||||
return identity, "", fmt.Errorf("generate diagnostic attempt ID: %w", err)
|
||||
}
|
||||
attempt := hex.EncodeToString(random[:])
|
||||
identity.Tag = "releasekit-test/" + selector + "/" + attempt
|
||||
identity.Version = "releasekit-test-" + attempt
|
||||
identity.Note = fmt.Sprintf("# ReleaseKit diagnostic %s\n\nSelector: %s\nCommit: %s\n", attempt, selector, commit)
|
||||
if err := git.CheckTagRef(ctx, source.RepositoryDir, identity.Tag); err != nil {
|
||||
return identity, "", fmt.Errorf("invalid diagnostic selector %q: %w", selector, err)
|
||||
}
|
||||
return identity, attempt, nil
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
// Package releaseidentity resolves and derives the identity of one Go module
|
||||
// release within a repository.
|
||||
// Package releaseidentity resolves source release identities within a repository.
|
||||
package releaseidentity
|
||||
|
||||
import (
|
||||
@@ -24,13 +23,22 @@ type Selection struct {
|
||||
ModuleSubdir string
|
||||
}
|
||||
|
||||
// Identity is the release identity derived from one selected module.
|
||||
// Identity is the resolved identity of a production or diagnostic release.
|
||||
type Identity struct {
|
||||
Selection
|
||||
ModulePath string
|
||||
Version string
|
||||
Tag string
|
||||
NotePath string
|
||||
Kind string
|
||||
SourceDir string
|
||||
SourceSubdir string
|
||||
GoEnabled bool
|
||||
Publication string
|
||||
VersionFormat string
|
||||
VersionPrefix string
|
||||
Commit string
|
||||
ModulePath string
|
||||
Version string
|
||||
Tag string
|
||||
NotePath string
|
||||
Note string
|
||||
}
|
||||
|
||||
// ResolveSelection normalizes a repository-relative module selection and
|
||||
@@ -75,11 +83,10 @@ func FromCommitted(ctx context.Context, repositoryDir, moduleDir, version, commi
|
||||
return FromCommittedSource(ctx, repositoryDir, moduleDir, version, commit, nil)
|
||||
}
|
||||
|
||||
// FromCommittedSource resolves source selection and the declaration captured at
|
||||
// commit before deriving the currently supported module publication identity.
|
||||
// FromCommittedSource resolves an identity from source at the captured commit.
|
||||
func FromCommittedSource(ctx context.Context, repositoryDir, moduleDir, version, commit string, source *SourceSpec) (Identity, error) {
|
||||
selection, err := LoadCommittedSource(ctx, repositoryDir, moduleDir, commit, source)
|
||||
return deriveSelectedModule(selection, version, err)
|
||||
return deriveSelected(ctx, selection, version, commit, err)
|
||||
}
|
||||
|
||||
// ResolveCommittedSelection resolves directory selection without reading the
|
||||
@@ -152,34 +159,62 @@ func FromWorkingTree(ctx context.Context, repositoryDir, moduleDir, version stri
|
||||
return FromWorkingSource(ctx, repositoryDir, moduleDir, version, nil)
|
||||
}
|
||||
|
||||
// FromWorkingSource resolves source selection and a working declaration before
|
||||
// deriving the currently supported module publication identity.
|
||||
// FromWorkingSource resolves an identity from working-tree source.
|
||||
func FromWorkingSource(ctx context.Context, repositoryDir, moduleDir, version string, source *SourceSpec) (Identity, error) {
|
||||
selection, err := LoadWorkingSource(ctx, repositoryDir, moduleDir, source)
|
||||
return deriveSelectedModule(selection, version, err)
|
||||
return deriveSelected(ctx, selection, version, "", err)
|
||||
}
|
||||
|
||||
func deriveSelectedModule(source ResolvedSource, version string, err error) (Identity, error) {
|
||||
identity := Identity{Selection: source.Selection, ModulePath: source.ModulePath, Version: version}
|
||||
func deriveSelected(ctx context.Context, source ResolvedSource, version, commit string, err error) (Identity, error) {
|
||||
identity := Identity{
|
||||
Selection: source.Selection, Kind: "production", SourceDir: source.ModuleDir, SourceSubdir: source.ModuleSubdir,
|
||||
GoEnabled: source.GoEnabled, Publication: source.Publication, VersionFormat: source.VersionFormat,
|
||||
VersionPrefix: source.VersionPrefix, ModulePath: source.ModulePath, Version: version, Commit: commit,
|
||||
}
|
||||
if !source.GoEnabled {
|
||||
identity.ModuleDir, identity.ModuleSubdir = "", ""
|
||||
}
|
||||
if err != nil {
|
||||
return identity, err
|
||||
}
|
||||
if source.Publication != "go-module" {
|
||||
return identity, fmt.Errorf("general source release identity is not yet supported")
|
||||
if !versioncontract.ValidIdentifier(version, source.VersionFormat, source.VersionPrefix) {
|
||||
return identity, fmt.Errorf("invalid %s release identifier %q", source.VersionFormat, version)
|
||||
}
|
||||
if source.Publication == "source" {
|
||||
prefix := source.ModuleSubdir
|
||||
if prefix == "." {
|
||||
prefix = ""
|
||||
}
|
||||
if source.TagPrefix != nil {
|
||||
prefix = *source.TagPrefix
|
||||
}
|
||||
identity.Tag = version
|
||||
if prefix != "" {
|
||||
identity.Tag = prefix + "/" + version
|
||||
}
|
||||
identity.NotePath = path.Join(source.ModuleSubdir, "docs", "releases", version+".md")
|
||||
if err := git.CheckTagRef(ctx, source.RepositoryDir, identity.Tag); err != nil {
|
||||
return identity, fmt.Errorf("invalid source release tag: %w", err)
|
||||
}
|
||||
return identity, nil
|
||||
}
|
||||
if source.VersionFormat != "semver" || source.VersionPrefix != "v" {
|
||||
return identity, fmt.Errorf("Go module publication requires prefixed semantic versions and the module-derived tag")
|
||||
}
|
||||
identity, err = deriveModule(source.Selection, source.ModulePath, version)
|
||||
moduleIdentity, err := deriveModule(source.Selection, source.ModulePath, version)
|
||||
if err != nil {
|
||||
return identity, err
|
||||
}
|
||||
identity.Tag, identity.NotePath = moduleIdentity.Tag, moduleIdentity.NotePath
|
||||
if source.TagPrefix != nil {
|
||||
prefix := strings.TrimSuffix(strings.TrimSuffix(identity.Tag, version), "/")
|
||||
if *source.TagPrefix != prefix {
|
||||
return identity, fmt.Errorf("source tag prefix %q conflicts with Go module tag prefix %q", *source.TagPrefix, prefix)
|
||||
}
|
||||
}
|
||||
if err := git.CheckTagRef(ctx, source.RepositoryDir, identity.Tag); err != nil {
|
||||
return identity, fmt.Errorf("invalid module release tag: %w", err)
|
||||
}
|
||||
return identity, nil
|
||||
}
|
||||
|
||||
@@ -199,7 +234,11 @@ func Derive(selection Selection, declaration []byte, version string) (Identity,
|
||||
}
|
||||
|
||||
func deriveModule(selection Selection, modulePath, version string) (Identity, error) {
|
||||
identity := Identity{Selection: selection, ModulePath: modulePath, Version: version}
|
||||
identity := Identity{
|
||||
Selection: selection, Kind: "production", SourceDir: selection.ModuleDir, SourceSubdir: selection.ModuleSubdir,
|
||||
GoEnabled: true, Publication: "go-module", VersionFormat: "semver", VersionPrefix: "v",
|
||||
ModulePath: modulePath, Version: version,
|
||||
}
|
||||
_, pathMajor, ok := module.SplitPathVersion(modulePath)
|
||||
if !ok {
|
||||
return identity, fmt.Errorf("module path %q has an invalid major-version suffix", modulePath)
|
||||
@@ -220,13 +259,8 @@ func deriveModule(selection Selection, modulePath, version string) (Identity, er
|
||||
tag = path.Join(tagPrefix, version)
|
||||
}
|
||||
notePath := path.Join(selection.ModuleSubdir, "docs", "releases", version+".md")
|
||||
return Identity{
|
||||
Selection: selection,
|
||||
ModulePath: modulePath,
|
||||
Version: version,
|
||||
Tag: tag,
|
||||
NotePath: notePath,
|
||||
}, nil
|
||||
identity.Tag, identity.NotePath = tag, notePath
|
||||
return identity, nil
|
||||
}
|
||||
|
||||
func modulePathFromDeclaration(declaration []byte) (string, error) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package releaseidentity
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -59,6 +60,12 @@ func TestLoadSourceUsesSelectedDeclaration(t *testing.T) {
|
||||
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"))
|
||||
@@ -70,6 +77,10 @@ func TestLoadSourceUsesSelectedDeclaration(t *testing.T) {
|
||||
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)
|
||||
@@ -85,6 +96,9 @@ func TestLoadSourceUsesSelectedDeclaration(t *testing.T) {
|
||||
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 {
|
||||
@@ -98,6 +112,45 @@ func TestLoadSourceUsesSelectedDeclaration(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -1,25 +1,71 @@
|
||||
package versioncontract
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ValidStableVersion reports whether version uses the selected vMAJOR.MINOR.PATCH form.
|
||||
func ValidStableVersion(version string) bool {
|
||||
if !strings.HasPrefix(version, "v") {
|
||||
return ValidIdentifier(version, "semver", "v")
|
||||
}
|
||||
|
||||
// ValidIdentifier checks a production identifier without changing its spelling.
|
||||
func ValidIdentifier(identifier, format, prefix string) bool {
|
||||
if (prefix != "" && prefix != "v") || !strings.HasPrefix(identifier, prefix) {
|
||||
return false
|
||||
}
|
||||
components := strings.Split(version[1:], ".")
|
||||
if len(components) != 3 {
|
||||
return false
|
||||
}
|
||||
for _, component := range components {
|
||||
if component == "" || len(component) > 1 && component[0] == '0' {
|
||||
value := strings.TrimPrefix(identifier, prefix)
|
||||
switch format {
|
||||
case "semver":
|
||||
components := strings.Split(value, ".")
|
||||
return len(components) == 3 && decimal(components[0], true) && decimal(components[1], true) && decimal(components[2], true)
|
||||
case "date":
|
||||
parts := strings.Split(value, ".")
|
||||
if len(parts) > 2 || (len(parts) == 2 && (!decimal(parts[1], true) || parts[1] == "0")) {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(component); i++ {
|
||||
if component[i] < '0' || component[i] > '9' {
|
||||
return false
|
||||
}
|
||||
date := strings.Split(parts[0], "-")
|
||||
if len(date) != 3 || len(date[0]) != 4 || len(date[1]) != 2 || len(date[2]) != 2 {
|
||||
return false
|
||||
}
|
||||
year, month, ok := yearMonth(date[0], date[1])
|
||||
if !ok || !decimal(date[2], false) {
|
||||
return false
|
||||
}
|
||||
day, _ := strconv.Atoi(date[2])
|
||||
parsed := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
|
||||
return parsed.Year() == year && int(parsed.Month()) == month && parsed.Day() == day
|
||||
case "year-month":
|
||||
parts := strings.Split(value, ".")
|
||||
if len(parts) != 3 || len(parts[0]) != 4 || len(parts[1]) != 2 || !decimal(parts[2], true) {
|
||||
return false
|
||||
}
|
||||
_, _, ok := yearMonth(parts[0], parts[1])
|
||||
return ok
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func decimal(value string, canonical bool) bool {
|
||||
if value == "" || (canonical && len(value) > 1 && value[0] == '0') {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(value); i++ {
|
||||
if value[i] < '0' || value[i] > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func yearMonth(yearText, monthText string) (int, int, bool) {
|
||||
if !decimal(yearText, false) || !decimal(monthText, false) {
|
||||
return 0, 0, false
|
||||
}
|
||||
year, _ := strconv.Atoi(yearText)
|
||||
month, _ := strconv.Atoi(monthText)
|
||||
return year, month, year >= 1 && year <= 9999 && month >= 1 && month <= 12
|
||||
}
|
||||
|
||||
45
internal/versioncontract/version_test.go
Normal file
45
internal/versioncontract/version_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package versioncontract
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestValidIdentifier(t *testing.T) {
|
||||
tests := []struct {
|
||||
format, prefix, value string
|
||||
want bool
|
||||
}{
|
||||
{"semver", "v", "v1.2.3", true},
|
||||
{"semver", "", "123456789012345678901234567890.0.3", true},
|
||||
{"semver", "v", "1.2.3", false},
|
||||
{"semver", "", "v1.2.3", false},
|
||||
{"semver", "v", "v1.02.3", false},
|
||||
{"semver", "v", "v1.2.3-rc.1", false},
|
||||
{"date", "", "2024-02-29", true},
|
||||
{"date", "v", "v2000-02-29.123456789012345678901234567890", true},
|
||||
{"date", "", "1900-02-29", false},
|
||||
{"date", "", "2023-02-29", false},
|
||||
{"date", "", "0000-01-01", false},
|
||||
{"date", "", "0001-01-01", true},
|
||||
{"date", "", "9999-12-31", true},
|
||||
{"date", "", "2024-2-29", false},
|
||||
{"date", "", "2024-02-00", false},
|
||||
{"date", "", "2024-02-29.0", false},
|
||||
{"date", "", "2024-02-29.01", false},
|
||||
{"date", "", "2024-02-29.1.2", false},
|
||||
{"year-month", "", "2024.02.0", true},
|
||||
{"year-month", "v", "v2024.12.123456789012345678901234567890", true},
|
||||
{"year-month", "", "2024.00.0", false},
|
||||
{"year-month", "", "2024.13.0", false},
|
||||
{"year-month", "", "0000.01.0", false},
|
||||
{"year-month", "", "2024.1.0", false},
|
||||
{"year-month", "", "2024.01.00", false},
|
||||
{"year-month", "", "2024.01", false},
|
||||
{"date", "", "٢٠٢٤-02-29", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.format+"/"+tt.prefix+"/"+tt.value, func(t *testing.T) {
|
||||
if got := ValidIdentifier(tt.value, tt.format, tt.prefix); got != tt.want {
|
||||
t.Fatalf("ValidIdentifier(%q, %q, %q) = %v, want %v", tt.value, tt.format, tt.prefix, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user