Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages length output in CLI #236

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion internal/txlib/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/mattn/go-isatty"
"github.com/transifex/cli/internal/txlib/config"
"github.com/transifex/cli/pkg/jsonapi"
"golang.org/x/term"
)

func figureOutBranch(branch string) string {
Expand Down Expand Up @@ -209,8 +210,21 @@ func isValidResolutionPolicy(policy string) (IsValid bool) {

}

type getSizeFuncType func(fd int) (int, int, error)

var getSizeFunc getSizeFuncType = term.GetSize

func truncateMessage(message string) string {
maxLength := 80
width, _, err := getSizeFunc(int(os.Stdout.Fd()))
if err != nil {
width = 80
}

maxLength := width - 2
if maxLength < 0 {
maxLength = 0
}

if len(message) > maxLength {
return message[:maxLength-2] + ".."
}
Expand Down
29 changes: 12 additions & 17 deletions internal/txlib/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,40 +67,35 @@ func TestFigureOutResources(t *testing.T) {
}
}

func TestConflictResolution(t *testing.T) {
ResultHead := isValidResolutionPolicy("USE_HEAD")
assert.Equal(t, ResultHead, true)

ResultBase := isValidResolutionPolicy("USE_BASE")
assert.Equal(t, ResultBase, true)

WrongResult := isValidResolutionPolicy("WRONG_BASE")
if WrongResult == true {
t.Error("Should be error")
}

func mockGetSize(fd int) (int, int, error) {
return 80, 0, nil
}

func TestTruncateMessage(t *testing.T) {
// Backup the original function
originalGetSizeFunc := getSizeFunc
defer func() { getSizeFunc = originalGetSizeFunc }()

// Test with 80 character terminal width
getSizeFunc = mockGetSize
result := truncateMessage("short message")
assert.Equal(t, result, "short message")
assert.Equal(t, "short message", result)

result = truncateMessage(
"this is a long message that needs to be truncated because it exceeds " +
"the maximum length of 75 characters",
"this is a long message that needs to be truncated because it exceeds the maximum length of 75 characters",
)
assert.Equal(
t,
"this is a long message that needs to be truncated because it exceeds the max..",
result,
"this is a long message that needs to be truncated because it exceeds the maxim..",
)

result = truncateMessage(
"a message with exactly 75 characters - this message should not be truncated",
)
assert.Equal(
t,
result,
"a message with exactly 75 characters - this message should not be truncated",
result,
)
}
Loading