gitea.tea/cmd/issues/create_test.go
Danilo Sousa 83fb7c0d26
Make issues create honor --output json
Same hole and same shape as the merged pulls fix (#1111):
--output parses on issues create through the urfave/cli v3
ancestor-flag cascade but was never read, so the action always
printed glamour markdown plus a second bare URL line.

task.CreateIssue now returns the created issue and the cmd layer
switches on --output, emitting compact lean JSON (index, title,
url, state), matching the post-review shape of
writeCreatedPullAsJSON. Without the flag the output stays
byte-identical: the same print.IssueDetails and HTMLURL println
calls just moved to the callers.

Signed-off-by: Danilo Sousa <code@danilosousa.net>
2026-09-09 20:57:31 -03:00

42 lines
1,021 B
Go

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