4bbeaae8f0
Agents leave their managed worktrees behind, and `agentws rm` takes one path at a time with no idea whether a branch's work is safely upstream, so clearing an accumulation by hand risks destroying unmerged commits. - classify every managed worktree: dirty, PR open, upstream, or unproven - remove only what is safe; delete the local branch only when work is upstream - prove "upstream" with merge-base and git cherry, so squash merges count - match a PR by head.label, which survives the branch deletion a merge does - dry run by default; --yes applies, --keep-branches spares every branch - read the Gitea path from origin's URL rather than assuming the owner
184 lines
5.0 KiB
Go
184 lines
5.0 KiB
Go
package agent
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestParsePRRef(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
wantErr bool
|
|
owner string
|
|
repo string
|
|
num int
|
|
}{
|
|
{"unkin/argocd-apps#42", false, "unkin", "argocd-apps", 42},
|
|
{"unkin/argocd-apps:42", false, "unkin", "argocd-apps", 42},
|
|
{" unkin/repo#1 ", false, "unkin", "repo", 1},
|
|
{"unkin/repo#0", true, "", "", 0},
|
|
{"unkin/repo#-3", true, "", "", 0},
|
|
{"unkin/repo#abc", true, "", "", 0},
|
|
{"unkin/repo", true, "", "", 0},
|
|
{"unkinrepo#3", true, "", "", 0},
|
|
{"unkin/a/b#3", true, "", "", 0},
|
|
{"/repo#3", true, "", "", 0},
|
|
{"unkin/#3", true, "", "", 0},
|
|
}
|
|
for _, tt := range tests {
|
|
got, err := ParsePRRef(tt.in)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Errorf("ParsePRRef(%q): expected error, got %+v", tt.in, got)
|
|
}
|
|
continue
|
|
}
|
|
if err != nil {
|
|
t.Errorf("ParsePRRef(%q): unexpected error: %v", tt.in, err)
|
|
continue
|
|
}
|
|
if got.Owner != tt.owner || got.Repo != tt.repo || got.Number != tt.num {
|
|
t.Errorf("ParsePRRef(%q) = %+v, want %s/%s#%d", tt.in, got, tt.owner, tt.repo, tt.num)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPRRefString(t *testing.T) {
|
|
r := PRRef{Owner: "unkin", Repo: "repo", Number: 7}
|
|
if got := r.String(); got != "unkin/repo#7" {
|
|
t.Errorf("String() = %q, want unkin/repo#7", got)
|
|
}
|
|
if got := r.RepoPath(); got != "unkin/repo" {
|
|
t.Errorf("RepoPath() = %q, want unkin/repo", got)
|
|
}
|
|
}
|
|
|
|
func TestParseRepo(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
wantErr bool
|
|
owner string
|
|
repo string
|
|
}{
|
|
{"unkin/repo", false, "unkin", "repo"},
|
|
{" unkin/repo ", false, "unkin", "repo"},
|
|
{"repo", true, "", ""},
|
|
{"unkin/a/b", true, "", ""},
|
|
{"/repo", true, "", ""},
|
|
{"unkin/", true, "", ""},
|
|
}
|
|
for _, tt := range tests {
|
|
o, r, err := ParseRepo(tt.in)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Errorf("ParseRepo(%q): expected error", tt.in)
|
|
}
|
|
continue
|
|
}
|
|
if err != nil || o != tt.owner || r != tt.repo {
|
|
t.Errorf("ParseRepo(%q) = (%q,%q,%v), want (%q,%q,nil)", tt.in, o, r, err, tt.owner, tt.repo)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseDurationFlag(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
want time.Duration
|
|
wantErr bool
|
|
}{
|
|
{"15", 15 * time.Second, false},
|
|
{"15s", 15 * time.Second, false},
|
|
{"2m", 2 * time.Minute, false},
|
|
{"1h30m", 90 * time.Minute, false},
|
|
{"500ms", 500 * time.Millisecond, false},
|
|
{" 45 ", 45 * time.Second, false},
|
|
{"0", 0, true},
|
|
{"0s", 0, true},
|
|
{"-5", 0, true},
|
|
{"-5s", 0, true},
|
|
{"15x", 0, true},
|
|
{"", 0, true},
|
|
}
|
|
for _, tt := range tests {
|
|
got, err := ParseDurationFlag("interval", tt.in)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Errorf("ParseDurationFlag(%q): expected error, got %v", tt.in, got)
|
|
}
|
|
continue
|
|
}
|
|
if err != nil {
|
|
t.Errorf("ParseDurationFlag(%q): unexpected error: %v", tt.in, err)
|
|
continue
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("ParseDurationFlag(%q) = %v, want %v", tt.in, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The error must name the flag and show valid forms instead of surfacing
|
|
// time.ParseDuration's "missing unit" wording.
|
|
func TestParseDurationFlagErrorMessage(t *testing.T) {
|
|
_, err := ParseDurationFlag("interval", "soon")
|
|
if err == nil {
|
|
t.Fatal("ParseDurationFlag(\"soon\"): expected error")
|
|
}
|
|
for _, want := range []string{"--interval", `"soon"`, "30s", "seconds"} {
|
|
if !strings.Contains(err.Error(), want) {
|
|
t.Errorf("error %q does not mention %q", err, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Not every managed repo lives under the default owner, so the API path comes
|
|
// from origin's URL rather than the directory name.
|
|
func TestRepoPathFromRemoteURL(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
want string
|
|
}{
|
|
{"https://git.unkin.net/unkin/agent-tools.git", "unkin/agent-tools"},
|
|
{"https://git.unkin.net/unkinben/dotfiles.git", "unkinben/dotfiles"},
|
|
{"https://git.unkin.net/unkin/agent-tools", "unkin/agent-tools"},
|
|
{"https://user@git.unkin.net/unkin/agent-tools.git", "unkin/agent-tools"},
|
|
{"ssh://git@git.unkin.net:2222/unkin/agent-tools.git", "unkin/agent-tools"},
|
|
{"git@git.unkin.net:unkin/agent-tools.git", "unkin/agent-tools"},
|
|
}
|
|
for _, tt := range tests {
|
|
got, err := RepoPathFromRemoteURL(tt.in)
|
|
if err != nil {
|
|
t.Errorf("RepoPathFromRemoteURL(%q): %v", tt.in, err)
|
|
continue
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("RepoPathFromRemoteURL(%q) = %q, want %q", tt.in, got, tt.want)
|
|
}
|
|
}
|
|
for _, bad := range []string{"", "https://git.unkin.net", "agent-tools"} {
|
|
if got, err := RepoPathFromRemoteURL(bad); err == nil {
|
|
t.Errorf("RepoPathFromRemoteURL(%q) = %q, want error", bad, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRemoteHost(t *testing.T) {
|
|
tests := []struct {
|
|
in, want string
|
|
}{
|
|
{"https://git.unkin.net/unkin/repo.git", "git.unkin.net"},
|
|
{"https://user@git.unkin.net/unkin/repo.git", "git.unkin.net"},
|
|
{"ssh://git@git.unkin.net:2222/unkin/repo.git", "git.unkin.net:2222"},
|
|
{"git@git.unkin.net:unkin/repo.git", "git.unkin.net"},
|
|
{"/tmp/fixture/origin.git", ""},
|
|
{"../other/origin.git", ""},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := RemoteHost(tt.in); got != tt.want {
|
|
t.Errorf("RemoteHost(%q) = %q, want %q", tt.in, got, tt.want)
|
|
}
|
|
}
|
|
}
|