Skip to content

Commit

Permalink
Prevent new line adding in SED. Upgrade go to 1.20. Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgerasimchuk committed Jun 6, 2023
1 parent 8b25181 commit 6d46d4b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 9 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: "Release a tag"
on: push

jobs:
test-unit:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Test Unit
run: go test -v ./...
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ These capabilities make Protty a useful tool for a variety of purposes, such as
The following command will start a proxy on port 8080, and after starting, all traffic from port 8080 will be redirected to a remote host located at https://example.com

```shell
docker run -p8080:80 -e REMOTE_URI=https://example.com:443 mgerasimchuk/protty:v0.4.0
docker run -p8080:80 -e REMOTE_URI=https://example.com:443 mgerasimchuk/protty:v0.4.1
```

## Running options and runtime configuration

```
» ~ docker run -p8080:80 -it mgerasimchuk/protty:v0.4.0 /bin/sh -c 'protty start --help'
» ~ docker run -p8080:80 -it mgerasimchuk/protty:v0.4.1 /bin/sh -c 'protty start --help'
Start the proxy
Usage:
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
module protty

go 1.18
go 1.20

require (
github.com/graze/go-throttled v0.3.1
github.com/itchyny/gojq v0.12.11
github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
github.com/thoas/go-funk v0.9.3
github.com/stretchr/testify v1.7.0
golang.org/x/time v0.3.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.2.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
5 changes: 1 addition & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,16 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw=
github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion pkg/util/component_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestGetCurrentFuncName(t *testing.T) {
actual string
want string
}{
{GetCurrentFuncName(), "szutil.TestGetCurrentFuncName"},
{GetCurrentFuncName(), "util.TestGetCurrentFuncName"},
}
for _, tt := range tests {
tt := tt
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/sed.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ func SED(sedExpr string, input []byte) ([]byte, []byte, error) {
if err != nil {
return input, input, fmt.Errorf("%s: %w", GetFuncName(engine.RunString), err)
}
// remove last \n cos sed adds this by default
if !strings.HasSuffix(string(input), "\n") && strings.HasSuffix(output, "\n") {
output = output[:len(output)-1]
}
return []byte(output), input, nil
}
41 changes: 41 additions & 0 deletions pkg/util/sed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package util

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestSED_Success(t *testing.T) {
type args struct {
msg string
input string
expr string
}
tests := []struct {
args args
want string
}{
{
args{"New line can be replaced", "first line\nsecond line", `:a;N;$!ba;s/\n/,/g`},
"first line,second line",
},
{
args{"New line doesn't add at the end", "Hello sed", `s/sed/world/`},
"Hello world",
},
{
args{"New line doesn't remove from the end", "Hello sed\n", `s/sed/world/`},
"Hello world\n",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.args.msg, func(t *testing.T) {
t.Parallel()
actual, _, err := SED(tt.args.expr, []byte(tt.args.input))
assert.NoError(t, err)
assert.Equal(t, tt.want, string(actual))
})
}
}

0 comments on commit 6d46d4b

Please sign in to comment.