diff --git a/cmd/restic/cmd_docs_test.go b/cmd/restic/cmd_docs_test.go index bd62f156c..2c61db74a 100644 --- a/cmd/restic/cmd_docs_test.go +++ b/cmd/restic/cmd_docs_test.go @@ -2,14 +2,17 @@ package main import ( "bytes" + "fmt" "os" "os/exec" "strings" "testing" + + "github.com/restic/restic/internal/global" ) func TestNewDocsCommand(t *testing.T) { - cmd := newDocsCommand() + cmd := newDocsCommand(&global.Options{}) if cmd.Use != "docs" { t.Errorf("expected command Use 'docs' got %q", cmd.Use) @@ -39,7 +42,82 @@ func TestNewDocsCommand(t *testing.T) { } } +func TestDocsURLForVersion(t *testing.T) { + // Dynamically build the expected URLs using the package's base constant + stableURL := fmt.Sprintf("%s/stable", ResticURL) + latestURL := fmt.Sprintf("%s/latest", ResticURL) + tagURL := func(tag string) string { + return fmt.Sprintf("%s/v%s", ResticURL, tag) + } + + tests := []struct { + name string + version string + want string + }{ + // --- 1. Stable Tag Releases --- + { + name: "Exact Release Tag", + version: "0.18.1", + want: tagURL("0.18.1"), + }, + { + name: "Release Tag with Patch", + version: "1.23.4", + want: tagURL("1.23.4"), + }, + + // --- 2. Development & Bleeding Edge Builds --- + { + name: "Dev Build with Suffix", + version: "0.18.1-dev", + want: latestURL, + }, + { + name: "Manually Compiled Binary", + version: "0.18.1 (compiled manually)", + want: latestURL, + }, + { + name: "Pure Dev Keyword", + version: "dev", + want: latestURL, + }, + + // --- 3. Fallbacks & Unknown States --- + { + name: "Explicit Unknown Keyword", + version: "unknown", + want: stableURL, + }, + { + name: "Empty String Fallback", + version: "", + want: stableURL, + }, + { + name: "Malformed Version Fallback", + version: "my-custom-version-string", + want: stableURL, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := docsURLForVersion(tt.version); got != tt.want { + t.Errorf("docsURLForVersion(%q) = %q, want %q", tt.version, got, tt.want) + } + }) + } +} + func TestOpenDocs(t *testing.T) { + stableURL := fmt.Sprintf("%s/stable", ResticURL) + latestURL := fmt.Sprintf("%s/latest", ResticURL) + tagURL := func(tag string) string { + return fmt.Sprintf("%s/v%s", ResticURL, tag) + } + tests := []struct { name string goos string @@ -48,12 +126,17 @@ func TestOpenDocs(t *testing.T) { wantBin string wantArg string }{ - {"Linux User", "linux", ResticDocsURL, "user", "xdg-open", ResticDocsURL}, - {"Linux Dev", "linux", ResticDevDocsURL, "developer", "xdg-open", ResticDevDocsURL}, - {"Mac User", "darwin", ResticDocsURL, "user", "open", ResticDocsURL}, - {"Mac Dev", "darwin", ResticDevDocsURL, "developer", "open", ResticDevDocsURL}, - {"Windows User", "windows", ResticDocsURL, "user", "rundll32", "url.dll,FileProtocolHandler"}, - {"Windows Dev", "windows", ResticDevDocsURL, "developer", "rundll32", "url.dll,FileProtocolHandler"}, + {"Linux version", "linux", tagURL("v0.18.1"), "user", "xdg-open", tagURL("v0.18.1")}, + {"Linux User", "linux", stableURL, "user", "xdg-open", stableURL}, + {"Linux Dev", "linux", latestURL, "developer", "xdg-open", latestURL}, + + {"Mac Version", "darwin", tagURL("v0.18.1"), "user", "open", tagURL("v0.18.1")}, + {"Mac User", "darwin", stableURL, "user", "open", stableURL}, + {"Mac Dev", "darwin", latestURL, "developer", "open", latestURL}, + + {"Windows Version", "windows", tagURL("v0.18.1"), "user", "rundll32", "url.dll,FileProtocolHandler"}, + {"Windows User", "windows", stableURL, "user", "rundll32", "url.dll,FileProtocolHandler"}, + {"Windows Dev", "windows", latestURL, "developer", "rundll32", "url.dll,FileProtocolHandler"}, } for _, tt := range tests {