mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
feat: explain failed requests and exit silently on interrupt
Failures of tea's own API requests now name what was missing or refused instead of "resource or API endpoint unavailable": issue #999 not found in alice/project repository alice/nope not found on http://localhost:3001 (or you don't have access) issues are disabled for bob/project #1 in alice/project is an issue, not a pull request; view it with: tea issue view 1 alice/old-stuff is archived (read-only); unarchive it with: tea repo edit ... A 404 on a repository endpoint looks the repository up once to tell a missing or hidden repository from a disabled feature, an empty repository or an issue number given to a pull request command. Refusals get their next step, a rejected GITEA_TOKEN and unreachable servers are named, and timeoutError words an expired --timeout. Errors a command already wrapped with its own context, and raw tea api responses, keep their wording. Ctrl-C and SIGTERM exit with 130 without printing an error, and a declined confirmation prints "Canceled." with exit code 3. Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
4 files changed
+437
-9
No files matched your search
+49
-2
@@ -18,6 +18,11 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.dev/tea/internal/app"
|
||||
"gitea.dev/tea/internal/auth"
|
||||
"gitea.dev/tea/internal/settings"
|
||||
"gitea.dev/tea/internal/terminal"
|
||||
"gitea.dev/tea/internal/transport"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -71,6 +76,48 @@ func TestSignalInterruptReturns130(t *testing.T) {
|
||||
t.Fatal("CLI did not exit after interrupt")
|
||||
}
|
||||
require.Empty(t, stdout.String())
|
||||
require.Equal(t, 1, strings.Count(stderr.String(), "Error:"))
|
||||
require.NotContains(t, stderr.String(), "disposable-test-token")
|
||||
require.Empty(t, stderr.String(), "an interrupt exits silently")
|
||||
}
|
||||
|
||||
type decliningPrompter struct{}
|
||||
|
||||
func (decliningPrompter) Input(context.Context, string) (string, error) { return "", nil }
|
||||
func (decliningPrompter) SecretInput(context.Context, string) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
func (decliningPrompter) Confirm(context.Context, string) (bool, error) { return false, nil }
|
||||
|
||||
func TestRunReportsDeclinedConfirmationsAndExplainedFailures(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/api/v1/user":
|
||||
_, _ = w.Write([]byte(`{"login":"alice"}`))
|
||||
case "/api/v1/repos/alice/project":
|
||||
_, _ = w.Write([]byte(`{"full_name":"alice/project","has_issues":true}`))
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
_, _ = w.Write([]byte(`{"message":"not found"}`))
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
store := settings.New(t.TempDir())
|
||||
require.NoError(t, store.Update(settings.TargetHosts, func(s *settings.Snapshot) error {
|
||||
s.Hosts.Hosts[server.URL] = settings.Host{User: "alice", Users: map[string]settings.Account{"alice": {AuthMethod: settings.AuthToken, CredentialStore: settings.StoreFile}}}
|
||||
return nil
|
||||
}))
|
||||
var stderr bytes.Buffer
|
||||
deps := app.Dependencies{
|
||||
Settings: store, Credentials: &auth.Store{Dir: store.Dir}, Client: transport.New, Prompter: decliningPrompter{},
|
||||
Streams: terminal.Streams{In: &bytes.Buffer{}, Out: &bytes.Buffer{}, ErrOut: &stderr, InTTY: true, ErrTTY: true},
|
||||
Getenv: func(string) string { return "" },
|
||||
}
|
||||
require.Equal(t, 3, run(context.Background(), deps, []string{"auth", "logout"}))
|
||||
require.Equal(t, "Canceled.\n", stderr.String())
|
||||
|
||||
stderr.Reset()
|
||||
deps.Getenv = func(key string) string {
|
||||
return map[string]string{"GITEA_TOKEN": "token", "TEA_HOST": server.URL}[key]
|
||||
}
|
||||
require.Equal(t, 1, run(context.Background(), deps, []string{"issue", "view", "9", "-R", "alice/project", "--no-input"}))
|
||||
require.Equal(t, "Error: issue #9 not found in alice/project\n", stderr.String())
|
||||
}
|
||||
Reference in new issue
Block a user