Files
gitea-tea/internal/app/repository_argument_test.go
T
bircni 2330d174ea fix(repo): reject --repo on commands that act on no repository
tea repo list -R acme/service listed the account's repositories and
tea repo create foo -R alice/project created alice/foo: the flag was
silently ignored. repo list, repo create, repo migrate and search repo
now fail with a usage error that suggests the intended command, such as
"--repo is not used by repo list; did you mean: tea repo list acme".

repo list takes the owner as an argument (--owner still works), and
repo create accepts OWNER/NAME, creating in the organization OWNER.

Signed-off-by: bircni <bircni@icloud.com>
2026-10-11 23:51:43 +02:00

89 lines
3.2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package app
import (
"context"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestRepositoryCommandsAcceptPositionalRepository(t *testing.T) {
var requests []string
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests = append(requests, r.Method+" "+r.URL.Path)
if r.Method == http.MethodPatch {
requirePayload(t, `{"archived":true}`, r.Body)
}
_, _ = w.Write([]byte(`{"full_name":"bob/tool","archived":true}`))
}))
defer server.Close()
for _, test := range []struct {
name string
args []string
want string
}{
{"view slug", []string{"repo", "view", "bob/tool"}, "GET /api/v1/repos/bob/tool"},
{"view URL", []string{"repo", "view", server.URL + "/bob/tool"}, "GET /api/v1/repos/bob/tool"},
{"archive", []string{"repo", "archive", "bob/tool", "--yes"}, "PATCH /api/v1/repos/bob/tool"},
} {
t.Run(test.name, func(t *testing.T) {
requests = nil
deps, out, _ := testDeps(t)
deps.Getenv = serverEnv(server.URL, map[string]string{"TEA_REPO": "alice/project"})
require.NoError(t, Execute(context.Background(), deps, append(test.args, "--no-input", "--json=full_name")))
require.Equal(t, []string{test.want}, requests)
require.JSONEq(t, `{"full_name":"bob/tool"}`, out.String())
})
}
}
func TestCommandsWithoutRepositoryRejectRepoFlag(t *testing.T) {
for _, test := range []struct {
args []string
want string
}{
{[]string{"repo", "list", "-R", "acme/service"}, "--repo is not used by repo list; did you mean: tea repo list acme"},
{[]string{"repo", "create", "foo", "-R", "alice/project", "--yes"}, "--repo is not used by repo create; did you mean: tea repo create alice/foo"},
{[]string{"repo", "migrate", "https://example.com/x.git", "--name", "x", "--repo", "acme/x"}, "--repo is not used by repo migrate; did you mean: tea repo migrate URL --owner acme"},
{[]string{"search", "repo", "tea", "-R", "acme/x"}, "--repo is not used by search repo; did you mean: tea search repo --owner acme"},
} {
t.Run(strings.Join(test.args[:2], " "), func(t *testing.T) {
requireUsageBeforeRequests(t, test.want, test.args...)
})
}
}
func TestRepositoryOwnerArguments(t *testing.T) {
var requests []string
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests = append(requests, r.Method+" "+r.URL.Path)
switch {
case r.URL.Path == "/api/v1/orgs/acme":
_, _ = w.Write([]byte(`{"id":3,"name":"acme"}`))
case r.Method == http.MethodGet:
_, _ = w.Write([]byte(`[]`))
default:
_, _ = w.Write([]byte(`{"full_name":"acme/tool"}`))
}
}))
defer server.Close()
for _, test := range []struct {
args []string
want string
}{
{[]string{"repo", "list", "acme"}, "GET /api/v1/orgs/acme/repos"},
{[]string{"repo", "create", "acme/tool"}, "POST /api/v1/orgs/acme/repos"},
{[]string{"repo", "create", "alice/tool"}, "POST /api/v1/user/repos"},
} {
requests = nil
deps, _, _ := serverDeps(t, server.URL)
require.NoError(t, Execute(context.Background(), deps, append(test.args, "--no-input", "--json")))
require.Equal(t, test.want, requests[len(requests)-1], test.args)
}
}