Skip to content

Commit

Permalink
Refactored Output (#203)
Browse files Browse the repository at this point in the history
Refactored output
  • Loading branch information
yuce authored Apr 5, 2023
1 parent e223689 commit c45605a
Show file tree
Hide file tree
Showing 61 changed files with 1,985 additions and 1,509 deletions.
19 changes: 16 additions & 3 deletions .github/workflows/coverage_runner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,21 @@ jobs:
- name: Install JDK
uses: actions/setup-java@v2
with:
distribution: 'adopt'
distribution: 'zulu'
java-version: '8'

- name: "Download RCD (Linux/MacOS)"
if: "!contains(matrix.os, 'windows')"
run: |
wget https://rcd-download.s3.us-east-2.amazonaws.com/rcd-${{ matrix.os }}
- name: "Start Hazelcast Remote Controller (Linux/MacOS)"
if: "!contains(matrix.os, 'windows')"
run: |
chmod +x rcd-${{ matrix.os }}
./rcd-${{ matrix.os }} -version $HZ_VERSION -dir $HOME &
sleep 10
- name: Checkout code for PR
if: github.event_name == 'pull_request_target'
uses: actions/checkout@v2
Expand All @@ -65,10 +77,11 @@ jobs:

- name: Test
env:
HAZELCAST_ENTERPRISE_KEY: ${{ secrets.HAZELCAST_ENTERPRISE_KEY }}
# HAZELCAST_ENTERPRISE_KEY: ${{ secrets.HAZELCAST_ENTERPRISE_KEY }}
HZ_VERSION: 5.2.1
SSL_ENABLED: 1
# SSL_ENABLED: 1
MEMBER_COUNT: 3
DEFAULT_TIMEOUT: "30s"
run: |
./rc.sh start
go mod tidy
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/test-all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
env:
GOPATH: "${{ github.workspace }}"
HZ_VERSION: "5.2.2"
DEFAULT_TIMEOUT: "30s"
defaults:
run:
shell: "bash"
Expand All @@ -23,8 +24,8 @@ jobs:
- name: "Setup JRE"
uses: actions/setup-java@v2
with:
distribution: zulu
java-version: 8
distribution: "zulu"
java-version: "8"

- name: "Download RCD (Linux/MacOS)"
if: "!contains(matrix.os, 'windows')"
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ build:
CGO_ENABLED=0 go build -tags base,hazelcastinternal,hazelcastinternaltest -ldflags $(LDFLAGS) -o build/$(BINARY_NAME) ./cmd/clc

test:
go test -tags base,hazelcastinternal,hazelcastinternaltest $(TEST_FLAGS) ./...
go test -tags base,hazelcastinternal,hazelcastinternaltest -p 1 $(TEST_FLAGS) ./...

test-cover:
go test -tags base,hazelcastinternal,hazelcastinternaltest $(TEST_FLAGS) -coverprofile=coverage.out -coverpkg $(PACKAGES) -coverprofile=$(COVERAGE_OUT) ./...
go test -tags base,hazelcastinternal,hazelcastinternaltest -p 1 $(TEST_FLAGS) -coverprofile=coverage.out -coverpkg $(PACKAGES) -coverprofile=$(COVERAGE_OUT) ./...

view-cover:
go tool cover -func $(COVERAGE_OUT) | grep total:
Expand Down
67 changes: 67 additions & 0 deletions base/commands/config/config_it_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package config_test

import (
"os"
"regexp"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hazelcast/hazelcast-commandline-client/clc/paths"
"github.com/hazelcast/hazelcast-commandline-client/internal/check"
"github.com/hazelcast/hazelcast-commandline-client/internal/it"
)

func TestConfig(t *testing.T) {
testCases := []struct {
name string
f func(t *testing.T)
}{
{name: "Import", f: importTest},
{name: "Add", f: addTest},
}
for _, tc := range testCases {
t.Run(tc.name, tc.f)
}
}

func importTest(t *testing.T) {
tcx := it.TestContext{T: t}
const configURL = "https://rcd-download.s3.us-east-2.amazonaws.com/hazelcast-cloud-go-sample-client-pr-FOR_TESTING-default.zip"
tcx.Tester(func(tcx it.TestContext) {
name := it.NewUniqueObjectName("cfg")
tcx.WithReset(func() {
check.Must(tcx.CLC().Execute("config", "import", name, configURL))
tcx.AssertStdoutContains("OK\n")
path := paths.Join(paths.ResolveConfigPath(name))
tcx.T.Logf("config path: %s", path)
assert.True(tcx.T, paths.Exists(path))
})
tcx.WithReset(func() {
check.Must(tcx.CLC().Execute("config", "list"))
tcx.AssertStdoutContains(name)
})
})
}

func addTest(t *testing.T) {
tcx := it.TestContext{T: t}
tcx.Tester(func(tcx it.TestContext) {
name := it.NewUniqueObjectName("cfg")
tcx.WithReset(func() {
check.Must(tcx.CLC().Execute("config", "add", name, "cluster.address=foobar.com"))
tcx.AssertStdoutContains("OK\n")
})
tcx.WithReset(func() {
check.Must(tcx.CLC().Execute("config", "list"))
tcx.AssertStdoutContains(name)
})
path := paths.ResolveConfigPath(name)
require.True(tcx.T, paths.Exists(path))
r := check.MustValue(regexp.Compile(`cluster:\n\s+address: foobar.com`))
text := check.MustValue(os.ReadFile(path))
t.Logf(string(text))
require.True(tcx.T, r.Match(text))
})
}
4 changes: 2 additions & 2 deletions base/commands/map/map_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (mc *MapGetCommand) Exec(ctx context.Context, ec plug.ExecContext) error {
value, err := ci.DecodeData(raw)
if err != nil {
ec.Logger().Info("The value for %s was not decoded, due to error: %s", keyStr, err.Error())
value = serialization.NondecodedType(serialization.TypeToString(vt))
value = serialization.NondecodedType(serialization.TypeToLabel(vt))
}
row := output.Row{
output.Column{
Expand All @@ -65,7 +65,7 @@ func (mc *MapGetCommand) Exec(ctx context.Context, ec plug.ExecContext) error {
row = append(row, output.Column{
Name: output.NameValueType,
Type: serialization.TypeString,
Value: serialization.TypeToString(vt),
Value: serialization.TypeToLabel(vt),
})
}
return ec.AddOutputRows(ctx, row)
Expand Down
102 changes: 56 additions & 46 deletions base/commands/map/map_test.go → base/commands/map/map_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ func TestMap(t *testing.T) {
name string
f func(t *testing.T)
}{
{name: "Clear_NonInteractive", f: clear_NonInteractiveTest},
{name: "EntrySet_NonInteractive", f: entrySet_NonInteractiveTest},
{name: "Get_Noninteractive", f: get_NonInteractiveTest},
{name: "Remove_Noninteractive", f: remove_NonInteractiveTest},
{name: "Set_NonInteractive", f: set_NonInteractiveTest},
{name: "Size_Interactive", f: size_InteractiveTest},
{name: "Size_Noninteractive", f: size_NoninteractiveTest},
Expand All @@ -29,106 +31,114 @@ func TestMap(t *testing.T) {
}
}

func entrySet_NonInteractiveTest(t *testing.T) {
mapTester(t, func(tcx it.TestContext, m *hz.Map) {
func clear_NonInteractiveTest(t *testing.T) {
it.MapTester(t, func(tcx it.TestContext, m *hz.Map) {
t := tcx.T
ctx := context.Background()
tcx.WithReset(func() {
check.Must(m.Set(ctx, "foo", "bar"))
require.Equal(t, 1, check.MustValue(m.Size(ctx)))
check.Must(tcx.CLC().Execute("map", "-n", m.Name(), "clear", "--quite"))
require.Equal(t, 0, check.MustValue(m.Size(ctx)))
})
})

}

func entrySet_NonInteractiveTest(t *testing.T) {
it.MapTester(t, func(tcx it.TestContext, m *hz.Map) {
// no entry
tcx.WithReset(func() {
check.Must(tcx.CLC().Execute("map", "-n", m.Name(), "entry-set", "--quite"))
tcx.AssertStdoutEquals(t, "")
tcx.AssertStdoutEquals("")
})
// set an entry
tcx.WithReset(func() {
check.Must(m.Set(context.Background(), "foo", "bar"))
check.Must(tcx.CLC().Execute("map", "-n", m.Name(), "entry-set", "--quite"))
tcx.AssertStdoutContains(t, "foo\tbar\n")
tcx.AssertStdoutContains("foo\tbar\n")
})
// show type
tcx.WithReset(func() {
check.Must(tcx.CLC().Execute("map", "-n", m.Name(), "entry-set", "--show-type", "--quite"))
tcx.AssertStdoutContains(t, "foo\tSTRING\tbar\tSTRING\n")
tcx.AssertStdoutContains("foo\tSTRING\tbar\tSTRING\n")
})
})
}

func get_NonInteractiveTest(t *testing.T) {
mapTester(t, func(tcx it.TestContext, m *hz.Map) {
t := tcx.T
it.MapTester(t, func(tcx it.TestContext, m *hz.Map) {
// no entry
tcx.WithReset(func() {
check.Must(tcx.CLC().Execute("map", "-n", m.Name(), "get", "foo", "--quite"))
tcx.AssertStdoutEquals(t, "-\n")
tcx.AssertStdoutEquals("-\n")
})
// set an entry
tcx.WithReset(func() {
check.Must(m.Set(context.Background(), "foo", "bar"))
check.Must(tcx.CLC().Execute("map", "-n", m.Name(), "get", "foo", "--quite"))
tcx.AssertStdoutContains(t, "bar\n")
check.Must(tcx.CLC().Execute("map", "-n", m.Name(), "get", "foo", "--quite", "--show-type"))
tcx.AssertStdoutEquals("bar\tSTRING\n")
})
})
}

func remove_NonInteractiveTest(t *testing.T) {
it.MapTester(t, func(tcx it.TestContext, m *hz.Map) {
ctx := context.Background()
tcx.WithReset(func() {
check.Must(m.Set(ctx, "foo", "bar"))
size := check.MustValue(m.Size(ctx))
require.Equal(tcx.T, 1, size)
check.Must(tcx.CLC().Execute("map", "-n", m.Name(), "remove", "foo", "--quite", "--show-type"))
tcx.AssertStdoutEquals("bar\tSTRING\n")
size = check.MustValue(m.Size(ctx))
require.Equal(tcx.T, 0, size)
})
})
}

func set_NonInteractiveTest(t *testing.T) {
mapTester(t, func(tcx it.TestContext, m *hz.Map) {
it.MapTester(t, func(tcx it.TestContext, m *hz.Map) {
t := tcx.T
tcx.WithReset(func() {
tcx.CLCExecute("map", "-n", m.Name(), "set", "foo", "bar", "--quite")
tcx.AssertStderrEquals(t, "")
tcx.AssertStderrEquals("")
v := check.MustValue(m.Get(context.Background(), "foo"))
require.Equal(t, "bar", v)
})
})
}

func size_NoninteractiveTest(t *testing.T) {
mapTester(t, func(tcx it.TestContext, m *hz.Map) {
t := tcx.T
it.MapTester(t, func(tcx it.TestContext, m *hz.Map) {
// no entry
tcx.WithReset(func() {
check.Must(tcx.CLC().Execute("map", "-n", m.Name(), "size", "--quite"))
tcx.AssertStdoutEquals(t, "0\n")
tcx.AssertStdoutEquals("0\n")
})
// set an entry
tcx.WithReset(func() {
tcx.AssertStdoutEquals(t, "")
tcx.AssertStdoutEquals("")
check.Must(m.Set(context.Background(), "foo", "bar"))
check.Must(tcx.CLC().Execute("map", "-n", m.Name(), "size", "--quite"))
tcx.AssertStdoutEquals(t, "1\n")
tcx.AssertStdoutEquals("1\n")
})
})
}

func size_InteractiveTest(t *testing.T) {
mapTester(t, func(tcx it.TestContext, m *hz.Map) {
t := tcx.T
it.MapTester(t, func(tcx it.TestContext, m *hz.Map) {
ctx := context.Background()
go func(t *testing.T) {
check.Must(tcx.CLC().Execute())
}(t)
tcx.WithReset(func() {
tcx.WriteStdin([]byte(fmt.Sprintf("\\map -n %s size\n", m.Name())))
tcx.AssertStdoutDollarWithPath(t, "testdata/map_size_0.txt")
})
tcx.WithReset(func() {
check.Must(m.Set(ctx, "foo", "bar"))
tcx.WriteStdin([]byte(fmt.Sprintf("\\map -n %s size\n", m.Name())))
tcx.AssertStdoutDollarWithPath(t, "testdata/map_size_1.txt")
})
})
}

func withMap(tcx it.TestContext, fn func(m *hz.Map)) {
name := it.NewUniqueObjectName("map")
ctx := context.Background()
m := check.MustValue(tcx.Client.GetMap(ctx, name))
fn(m)
}

func mapTester(t *testing.T, fn func(tcx it.TestContext, m *hz.Map)) {
tcx := it.TestContext{T: t}
tcx.Tester(func(tcx it.TestContext) {
withMap(tcx, func(m *hz.Map) {
fn(tcx, m)
tcx.WithShell(func(tcx it.TestContext) {
tcx.WithReset(func() {
tcx.WriteStdin([]byte(fmt.Sprintf("\\map -n %s size\n", m.Name())))
tcx.AssertStdoutDollarWithPath("testdata/map_size_0.txt")
})
tcx.WithReset(func() {
check.Must(m.Set(ctx, "foo", "bar"))
tcx.WriteStdin([]byte(fmt.Sprintf("\\map -n %s size\n", m.Name())))
tcx.AssertStdoutDollarWithPath("testdata/map_size_1.txt")
})
})
})
}
4 changes: 2 additions & 2 deletions base/commands/map/map_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (mc *MapRemoveCommand) Exec(ctx context.Context, ec plug.ExecContext) error
value, err := ci.DecodeData(raw)
if err != nil {
ec.Logger().Info("The value for %s was not decoded, due to error: %s", keyStr, err.Error())
value = serialization.NondecodedType(serialization.TypeToString(vt))
value = serialization.NondecodedType(serialization.TypeToLabel(vt))
}
row := output.Row{
output.Column{
Expand All @@ -65,7 +65,7 @@ func (mc *MapRemoveCommand) Exec(ctx context.Context, ec plug.ExecContext) error
row = append(row, output.Column{
Name: output.NameValueType,
Type: serialization.TypeString,
Value: serialization.TypeToString(vt),
Value: serialization.TypeToLabel(vt),
})
}
return ec.AddOutputRows(ctx, row)
Expand Down
2 changes: 1 addition & 1 deletion base/commands/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (cm *ShellCommand) ExecInteractive(ctx context.Context, ec plug.ExecContext
Stderr: ec.Stderr(),
Stdout: ec.Stdout(),
}
sh := shell.NewOneshot(endLineFn, sio, textFn)
sh := shell.NewOneshotShell(endLineFn, sio, textFn)
sh.SetCommentPrefix("--")
return sh.Run(context.Background())
}
Expand Down
2 changes: 1 addition & 1 deletion base/commands/sql/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func ExecSQL(ctx context.Context, ec plug.ExecContext, query string) (sql.Result
// Once that is removed from the Go client, the code below may be removed.
err = AdaptSQLError(err)
if !as {
if serr.Suggestion != "" {
if serr.Suggestion != "" && !ec.Interactive() {
return nil, stop, fmt.Errorf("%w\n\nUse --%s to automatically apply the suggestion", err, propertyUseMappingSuggestion)
}
return nil, stop, err
Expand Down
Loading

0 comments on commit c45605a

Please sign in to comment.