Files
gitea-tea/internal/app/labelfile_test.go
T
bircni 55574a9fd0 feat: fit list tables to the terminal and write TSV when piped
Lists had no header, wrapped every row once a title was longer than
the terminal, and were space-padded when piped, so cut -f and awk -F'\t'
could not split them and empty cells collapsed.

terminal.Table now takes column specs and a Display. On a terminal it
prints an uppercase header, aligns columns two spaces apart and shrinks
flexible columns, widest first, to Streams.Width (the terminal, then
$COLUMNS, then 80), cutting by display width with "…". States use the
terminal's ANSI 16 colors, hex colors fall back to the 256-color palette
unless COLORTERM advertises 24-bit, and OSC 8 links appear only on the
first column, independently of color (TEA_HYPERLINKS=auto|always|never).
Piped tables are tab-separated without header, color or links, and times
are RFC3339 UTC instead of "about 2 hours ago".

terminal.View lays out single resources (title, badges, metadata, fields,
body, footer URL) and renders markdown bodies with glamour on a terminal
only (TEA_MARKDOWN=raw keeps them raw). Shared formatters cover relative
times, timestamps, sizes, durations and status icons.

In the app, writeTable and writeView are the new list and view writers:
a "Showing N of M …" context line, an empty-state message on stderr and
the next-page hint. writeList, writeItem and batch keep working and now
render through the new table: tab-separated when piped, truncated at the
widest column on a terminal, with headers for issue list and pr list.

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

71 lines
2.6 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package app
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"gitea.dev/tea/internal/services"
"github.com/stretchr/testify/require"
)
func TestLabelFileRoundTrip(t *testing.T) {
for _, label := range []services.Label{
{Name: "bug", Color: "ff0000", Description: "Something is broken"},
{Name: "kind/feature request", Color: "#00FF00", Description: "Wanted; maybe"},
{Name: "#hash", Color: "0000ff"},
} {
row, err := formatLabel(label)
require.NoError(t, err, label.Name)
parsed, err := parseLabelFile(strings.NewReader(row))
require.NoError(t, err, row)
require.Len(t, parsed, 1)
require.Equal(t, label.Name, *parsed[0].Name)
require.Equal(t, strings.TrimPrefix(label.Color, "#"), *parsed[0].Color)
require.Equal(t, label.Description, *parsed[0].Description)
}
for _, label := range []services.Label{
{Name: "", Color: "ff0000"},
{Name: "two spaces", Color: "ff0000"},
{Name: "semi;colon", Color: "ff0000"},
{Name: "tab\tname", Color: "ff0000"},
{Name: "bug", Color: "red"},
{Name: "bug", Color: "ff0000", Description: "line\nbreak"},
{Name: "bug", Color: "ff0000", Description: " padded"},
} {
_, err := formatLabel(label)
require.ErrorContains(t, err, "cannot be represented", label)
}
}
func TestLabelImportAcceptsColorPrefixedRows(t *testing.T) {
var created []string
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "/api/v1/repos/alice/project/labels", r.URL.Path)
payload, err := io.ReadAll(r.Body)
require.NoError(t, err)
var label struct{ Name string }
require.NoError(t, json.Unmarshal(payload, &label))
created = append(created, string(payload))
_, _ = fmt.Fprintf(w, `{"id":%d,"name":%q}`, len(created), label.Name)
}))
defer server.Close()
deps, out, _ := serverDeps(t, server.URL)
file := filepath.Join(t.TempDir(), "labels.txt")
require.NoError(t, os.WriteFile(file, []byte("# Gitea default labels\n#ee0701 bug ; Something is not working\n84b6eb enhancement\n"), 0o600))
require.NoError(t, Execute(context.Background(), deps, []string{"label", "create", "-R", "alice/project", "--file", file, "--yes"}))
require.Len(t, created, 2)
requirePayload(t, `{"name":"bug","color":"ee0701","description":"Something is not working"}`, strings.NewReader(created[0]))
requirePayload(t, `{"name":"enhancement","color":"84b6eb"}`, strings.NewReader(created[1]))
require.Equal(t, "1\tbug\t\t\n2\tenhancement\t\t\n", out.String())
}