Skip to content

Commit

Permalink
Merge pull request #237 from transifex/devel
Browse files Browse the repository at this point in the history
Release v1.6.15
  • Loading branch information
foteinigk authored Aug 12, 2024
2 parents 1ef9d1b + ca77c86 commit 1641901
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
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,
)
}

0 comments on commit 1641901

Please sign in to comment.