Skip to content

Commit

Permalink
[#809] Remove all code related to encoding in vcs package
Browse files Browse the repository at this point in the history
To support VCS that don't manage emojies
- don't call convertLine anymore in p4
- remove OS specific encoding
- remove code
- remove tests
  • Loading branch information
philou committed Oct 11, 2024
1 parent 2d751cc commit f2c8a8d
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 60 deletions.
16 changes: 1 addition & 15 deletions src/vcs/p4/p4_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
"github.com/murex/tcr/vcs"
"github.com/murex/tcr/vcs/shell"
"github.com/spf13/afero"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/transform"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -307,24 +305,12 @@ func buildDescriptionField(attr shell.Attributes, messages ...string) string {
var builder strings.Builder
_, _ = builder.WriteString("Description=")
for _, message := range messages {
_, _ = builder.WriteString(convertLine(attr.Encoding, message))
_, _ = builder.WriteString(message)
_, _ = builder.WriteString(attr.EOL)
}
return builder.String()
}

func convertLine(charMap *charmap.Charmap, message string) string {
if charMap == nil {
// By default, we use UTF-8, which is the default with Go strings
return message
}
var b bytes.Buffer
converter := transform.NewWriter(&b, charMap.NewDecoder())
_, _ = converter.Write([]byte(message))
_ = converter.Close()
return b.String()
}

func (p *p4Impl) submitChangeList(cl *changeList) error {
if cl == nil {
report.PostWarning("Empty changelist!")
Expand Down
22 changes: 0 additions & 22 deletions src/vcs/p4/p4_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ package p4

import (
"errors"
"fmt"
"github.com/murex/tcr/vcs"
"github.com/murex/tcr/vcs/shell"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"golang.org/x/text/encoding/charmap"
"path/filepath"
"testing"
)
Expand Down Expand Up @@ -99,26 +97,6 @@ func Test_p4_get_remote_name_is_always_empty(t *testing.T) {
assert.Equal(t, "", p.GetRemoteName())
}

func Test_convert_line(t *testing.T) {
tests := []struct {
desc string
input rune
encoding *charmap.Charmap
expected string
}{
{"UTC-8 smiley", '🙂', nil, "f09f9982"},
{"ISO8859-1 smiley", '🙂', charmap.ISO8859_1, "c3b0c29fc299c282"},
{"WINDOWS-1252 smiley", '🙂', charmap.Windows1252, "c3b0c5b8e284a2e2809a"},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
//t.Logf("%x - %x", test.input, convertLine(test.encoding, string(test.input)))
assert.Equal(t, test.expected, fmt.Sprintf("%x", convertLine(test.encoding, string(test.input))))
})
}
}

func Test_p4_diff(t *testing.T) {
testFlags := []struct {
desc string
Expand Down
5 changes: 1 addition & 4 deletions src/vcs/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ SOFTWARE.

package shell

import "golang.org/x/text/encoding/charmap"

// Attributes contain shell-specific attributes allowing to tune behavior
// when interacting with a shell command
type Attributes struct {
Encoding *charmap.Charmap
EOL string
EOL string
}

// GetAttributes Returns shell-specific attributes
Expand Down
3 changes: 1 addition & 2 deletions src/vcs/shell/shell_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ package shell
// osShellAttributes returns shell attributes associated to the underlying operating system
func osShellAttributes() Attributes {
return Attributes{
Encoding: nil,
EOL: "\n",
EOL: "\n",
}
}
6 changes: 0 additions & 6 deletions src/vcs/shell/shell_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ import (
"testing"
)

func Test_unix_shell_encoding(t *testing.T) {
// nil means that we use Go's default string encoding (UTF-8)
// e.g. no conversion is required
assert.Zero(t, GetAttributes().Encoding)
}

func Test_unix_shell_end_of_line(t *testing.T) {
assert.Equal(t, "\n", GetAttributes().EOL)
}
5 changes: 1 addition & 4 deletions src/vcs/shell/shell_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ SOFTWARE.

package shell

import "golang.org/x/text/encoding/charmap"

// osShellAttributes returns shell attributes associated to the underlying operating system
func osShellAttributes() Attributes {
return Attributes{
Encoding: charmap.Windows1252,
EOL: "\r\n",
EOL: "\r\n",
}
}
7 changes: 0 additions & 7 deletions src/vcs/shell/shell_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,9 @@ package shell

import (
"github.com/stretchr/testify/assert"
"golang.org/x/text/encoding/charmap"
"testing"
)

func Test_windows_shell_encoding(t *testing.T) {
// Windows1252 works for Western character set. We may need a more sophisticated
// approach if we want to cover users with other character sets.
assert.Equal(t, charmap.Windows1252, GetAttributes().Encoding)
}

func Test_windows_shell_end_of_line(t *testing.T) {
assert.Equal(t, "\r\n", GetAttributes().EOL)
}

0 comments on commit f2c8a8d

Please sign in to comment.