gitea.tea/cmd/pulls/create_test.go
Danilo Sousa f1cd86a8e2
Make pulls create honor --output json
--output parsed on create (urfave/cli v3 cascades ancestor flags
down to subcommands) but was never read: the action always printed
glamour markdown, embedding the PR URL as an OSC 8 hyperlink that
breaks consumers scraping piped stdout.

Mirror the detail-command precedent (cmd/pulls.go RunPullsDetails):
task.CreatePull now returns the created PR and the cmd layer switches
on --output, emitting lean JSON (index, title, url, state, base,
head). Without the flag the output stays byte-identical, as the same
print.PullDetails call just moved to its callers. The agit flow is
rejected explicitly when combined with --output, since it creates
the PR via git push and has no object to print.

Signed-off-by: Danilo Sousa <code@danilosousa.net>
2026-09-04 14:47:14 -03:00

46 lines
1.2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package pulls
import (
"bytes"
"encoding/json"
"testing"
gitea "gitea.dev/sdk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWriteCreatedPullAsJSON(t *testing.T) {
pr := &gitea.PullRequest{
Index: 33,
Title: "test title",
HTMLURL: "https://gitea.example.com/owner/repo/pulls/33",
State: gitea.StateOpen,
Base: &gitea.PRBranchInfo{Ref: "main"},
Head: &gitea.PRBranchInfo{Ref: "feature"},
}
var buf bytes.Buffer
require.NoError(t, writeCreatedPullAsJSON(&buf, pr))
var got map[string]any
require.NoError(t, json.Unmarshal(buf.Bytes(), &got))
assert.Equal(t, float64(33), got["index"])
assert.Equal(t, "test title", got["title"])
assert.Equal(t, "https://gitea.example.com/owner/repo/pulls/33", got["url"])
assert.Equal(t, "open", got["state"])
assert.Equal(t, "main", got["base"])
assert.Equal(t, "feature", got["head"])
// exactly the lean field set, nothing extra
assert.Len(t, got, 6)
// machine-readable output must not contain terminal escape sequences
assert.NotContains(t, buf.String(), "\x1b")
}