From 4a85bb3d405eebe45bec563b664dbad5c9c1cc4d Mon Sep 17 00:00:00 2001 From: bircni Date: Mon, 12 Oct 2026 20:43:46 +0200 Subject: [PATCH] test(actions): drop duplicate cases and share the checks server Signed-off-by: bircni --- internal/app/actions_test.go | 4 ---- internal/app/artifacts_test.go | 8 ++------ internal/app/checks_test.go | 25 +++++++++++-------------- internal/app/download_test.go | 3 --- internal/app/webhooks_test.go | 2 -- 5 files changed, 13 insertions(+), 29 deletions(-) diff --git a/internal/app/actions_test.go b/internal/app/actions_test.go index 86eed165..01e97741 100644 --- a/internal/app/actions_test.go +++ b/internal/app/actions_test.go @@ -122,10 +122,6 @@ func (p *dispatchingPrompter) Ask(_ context.Context, title string, validate func return answer, validate(answer) } -func (p *dispatchingPrompter) AskText(context.Context, string, string) (string, error) { - return "", nil -} - func (p *dispatchingPrompter) ConfirmDefault(_ context.Context, title string, defaultYes bool) (bool, error) { p.asked = append(p.asked, title) return !defaultYes, nil diff --git a/internal/app/artifacts_test.go b/internal/app/artifacts_test.go index e6795d48..f0bc5c54 100644 --- a/internal/app/artifacts_test.go +++ b/internal/app/artifacts_test.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "testing" "gitea.dev/tea/internal/clierr" @@ -97,7 +98,7 @@ func TestRunDownloadExtractsArtifactsByName(t *testing.T) { return } require.NoError(t, err) - require.Equal(t, filepathReplace(tc.out, dir), diagnostics.String()) + require.Equal(t, strings.ReplaceAll(tc.out, "{dir}", dir), diagnostics.String()) for name, content := range tc.files { data, err := os.ReadFile(filepath.Join(dir, name)) require.NoError(t, err, name) @@ -153,11 +154,6 @@ func TestExtractZipKeepsPermissionBits(t *testing.T) { } } -// filepathReplace puts dir in place of {dir} in text. -func filepathReplace(text, dir string) string { - return string(bytes.ReplaceAll([]byte(text), []byte("{dir}"), []byte(dir))) -} - func TestArtifactListShowsSizesAndExpiry(t *testing.T) { server, _ := actionsAPI(t, map[string]string{ "GET actions/runs/29/artifacts": `{"total_count":2,"artifacts":[{"id":1,"name":"build","size_in_bytes":3145728,"expires_at":"2028-03-01T12:00:00Z"},{"id":3,"name":"old","size_in_bytes":13,"expired":true,"expires_at":"2027-12-01T12:00:00Z"}]}`, diff --git a/internal/app/checks_test.go b/internal/app/checks_test.go index 5caf4093..0162eb38 100644 --- a/internal/app/checks_test.go +++ b/internal/app/checks_test.go @@ -6,6 +6,7 @@ package app import ( "context" "net/http" + "net/http/httptest" "strings" "testing" @@ -109,17 +110,7 @@ func TestChecksSummaryAndExitCodes(t *testing.T) { {name: "none", statuses: `[]`}, } { t.Run(tc.name, func(t *testing.T) { - server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - switch { - case r.URL.Path == "/api/v1/repos/alice/x/pulls/8": - _, _ = w.Write([]byte(`{"number":8,"head":{"sha":"abc"}}`)) - case r.URL.Query().Get("page") == "1": - _, _ = w.Write([]byte(tc.statuses)) - default: - _, _ = w.Write([]byte(`[]`)) - } - })) - defer server.Close() + server := checksServer(t, tc.statuses) deps, out, diagnostics := serverDeps(t, server.URL) plainTTY(t, &deps, server.URL) err := Execute(context.Background(), deps, []string{"pr", "checks", "8", "-R", "alice/x"}) @@ -135,18 +126,24 @@ func TestChecksSummaryAndExitCodes(t *testing.T) { } } -func TestChecksWatchTimeoutCountsPendingChecks(t *testing.T) { +// checksServer serves pull request alice/x#8 whose head commit has statuses. +func checksServer(t *testing.T, statuses string) *httptest.Server { server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch { case r.URL.Path == "/api/v1/repos/alice/x/pulls/8": _, _ = w.Write([]byte(`{"number":8,"head":{"sha":"abc"}}`)) case r.URL.Query().Get("page") == "1": - _, _ = w.Write([]byte(`[{"id":1,"context":"a","status":"pending"},{"id":2,"context":"b","status":"pending"}]`)) + _, _ = w.Write([]byte(statuses)) default: _, _ = w.Write([]byte(`[]`)) } })) - defer server.Close() + t.Cleanup(server.Close) + return server +} + +func TestChecksWatchTimeoutCountsPendingChecks(t *testing.T) { + server := checksServer(t, `[{"id":1,"context":"a","status":"pending"},{"id":2,"context":"b","status":"pending"}]`) deps, _, _ := serverDeps(t, server.URL) err := Execute(context.Background(), deps, []string{"pr", "checks", "8", "-R", "alice/x", "--watch", "--interval=1ms", "--timeout=30ms"}) require.EqualError(t, err, "timed out after 30ms waiting for 2 pending checks") diff --git a/internal/app/download_test.go b/internal/app/download_test.go index 2168a27b..49c10163 100644 --- a/internal/app/download_test.go +++ b/internal/app/download_test.go @@ -96,9 +96,7 @@ func TestDownloadDestinations(t *testing.T) { {name: "existing in --dir", flags: downloadFlags{dir: "dl"}, file: "existing.txt", err: filepath.Join("dl", "existing.txt") + " already exists"}, {name: "--clobber", flags: downloadFlags{clobber: true}, file: "existing.txt", want: "existing.txt"}, {name: "--skip-existing", flags: downloadFlags{skipExisting: true}, file: "existing.txt", want: "existing.txt", skip: true}, - {name: "--skip-existing new file", flags: downloadFlags{skipExisting: true}, file: "notes.txt", want: "notes.txt"}, {name: "parent directory name", file: "../escape.txt", err: `cannot use "../escape.txt" as a file name; pass --output`}, - {name: "nested name", file: "a/b.txt", err: "cannot use"}, {name: "dot dot", file: "..", err: "cannot use"}, {name: "--output and --dir", flags: downloadFlags{output: "x", dir: "dl"}, file: "notes.txt", err: "--output and --dir cannot be combined"}, {name: "--clobber and --skip-existing", flags: downloadFlags{clobber: true, skipExisting: true}, file: "notes.txt", err: "--clobber and --skip-existing cannot be combined"}, @@ -146,7 +144,6 @@ func TestDownloadsToATerminalRefuseBinaryData(t *testing.T) { err string }{ {name: "piped binary stays byte exact", body: "PK\x03\x04\x00\x01", want: "PK\x03\x04\x00\x01"}, - {name: "terminal text", body: "hello\nworld\n", want: "hello\nworld\n", tty: true}, {name: "terminal text loses escapes", body: "title\x1b]0;PWNED\x07\n", want: "title]0;PWNED\n", tty: true}, {name: "terminal binary", body: "PK\x03\x04\x00\x01", tty: true, err: "refusing to write binary data to the terminal"}, {name: "terminal invalid UTF-8", body: "abc\xff\xfedef", tty: true, err: "refusing to write binary data"}, diff --git a/internal/app/webhooks_test.go b/internal/app/webhooks_test.go index 16eecbdd..305ba5c6 100644 --- a/internal/app/webhooks_test.go +++ b/internal/app/webhooks_test.go @@ -19,7 +19,6 @@ func TestWebhookCreateValidatesEventsAndTypes(t *testing.T) { want string }{ {[]string{"--events", "pushh"}, "--events must be create, delete, fork, push, issues"}, - {[]string{"--events", "push,pull_request_review_approved"}, "--events must be"}, {[]string{"--type", "slackk"}, "--type must be gitea, gogs, slack"}, {[]string{"--type", "slack"}, "--type slack needs a channel: --config channel=#name"}, } { @@ -50,7 +49,6 @@ func TestWebhookEventsCollapseCompleteGroups(t *testing.T) { events []string want string }{ - {[]string{"push"}, "push"}, {[]string{"issue_assign", "issue_comment", "issue_label", "issue_milestone", "issues", "push"}, "issues (5), push"}, {[]string{"issue_label", "issues"}, "issue_label, issues"}, {append([]string{"create"}, pulls...), "create, pull_request (8)"},