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

feat: use output decorator, adding tests #2104

Merged
merged 12 commits into from
Jan 28, 2025
Merged
2 changes: 1 addition & 1 deletion cmd/conduit/root/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/matryer/is"
)

func TestConfig_WithFlags(t *testing.T) {
func TestConfigWithFlags(t *testing.T) {
testCases := []struct {
name string
args []string
Expand Down
11 changes: 8 additions & 3 deletions cmd/conduit/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package root
import (
"context"
"fmt"
"os"

"github.com/conduitio/conduit/cmd/conduit/root/config"
"github.com/conduitio/conduit/cmd/conduit/root/connectorplugins"
Expand All @@ -39,6 +38,7 @@ var (
_ ecdysis.CommandWithExecute = (*RootCommand)(nil)
_ ecdysis.CommandWithDocs = (*RootCommand)(nil)
_ ecdysis.CommandWithSubCommands = (*RootCommand)(nil)
_ ecdysis.CommandWithOutput = (*RootCommand)(nil)
)

type RootFlags struct {
Expand All @@ -50,12 +50,17 @@ type RootFlags struct {
}

type RootCommand struct {
flags RootFlags
flags RootFlags
output ecdysis.Output
}

func (c *RootCommand) Output(output ecdysis.Output) {
c.output = output
}

func (c *RootCommand) Execute(ctx context.Context) error {
if c.flags.Version {
_, _ = fmt.Fprintf(os.Stdout, "%s\n", conduit.Version(true))
c.output.Stdout(fmt.Sprintf("%s\n", conduit.Version(true)))
return nil
}

Expand Down
30 changes: 30 additions & 0 deletions cmd/conduit/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
package root

import (
"bytes"
"context"
"fmt"
"runtime"
"strings"
"testing"

"github.com/conduitio/ecdysis"
Expand Down Expand Up @@ -56,3 +61,28 @@ func TestRootCommandFlags(t *testing.T) {
is.Equal(cf.Usage, f.usage)
}
}

func TestRootCommandExecuteWithVersionFlag(t *testing.T) {
is := is.New(t)

buf := new(bytes.Buffer)

out := &ecdysis.DefaultOutput{}
out.Output(buf, nil)

cmd := &RootCommand{
flags: RootFlags{
Version: true,
},
}
cmd.Output(out)

expectedOutput := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)

err := cmd.Execute(context.Background())
is.NoErr(err)

actualOutput := strings.TrimSpace(buf.String())

is.Equal(actualOutput, expectedOutput)
}
12 changes: 9 additions & 3 deletions cmd/conduit/root/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package version
import (
"context"
"fmt"
"os"

"github.com/conduitio/conduit/pkg/conduit"
"github.com/conduitio/ecdysis"
Expand All @@ -26,14 +25,21 @@ import (
var (
_ ecdysis.CommandWithExecute = (*VersionCommand)(nil)
_ ecdysis.CommandWithDocs = (*VersionCommand)(nil)
_ ecdysis.CommandWithOutput = (*VersionCommand)(nil)
)

type VersionCommand struct{}
type VersionCommand struct {
output ecdysis.Output
}

func (c *VersionCommand) Output(output ecdysis.Output) {
c.output = output
}

func (c *VersionCommand) Usage() string { return "version" }

func (c *VersionCommand) Execute(_ context.Context) error {
_, _ = fmt.Fprintf(os.Stdout, "%s\n", conduit.Version(true))
c.output.Stdout(fmt.Sprintf("%s\n", conduit.Version(true)))
return nil
}

Expand Down
48 changes: 48 additions & 0 deletions cmd/conduit/root/version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © 2025 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package version

import (
"bytes"
"context"
"fmt"
"runtime"
"strings"
"testing"

"github.com/conduitio/ecdysis"
"github.com/matryer/is"
)

func TestVersionCommandExecute(t *testing.T) {
is := is.New(t)

buf := new(bytes.Buffer)

out := &ecdysis.DefaultOutput{}
out.Output(buf, nil)

cmd := &VersionCommand{}
cmd.Output(out)

expectedOutput := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
raulb marked this conversation as resolved.
Show resolved Hide resolved

err := cmd.Execute(context.Background())
is.NoErr(err)

actualOutput := strings.TrimSpace(buf.String())

is.Equal(actualOutput, expectedOutput)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/conduitio/conduit-connector-sdk v0.12.0
github.com/conduitio/conduit-processor-sdk v0.4.0
github.com/conduitio/conduit-schema-registry v0.2.2
github.com/conduitio/ecdysis v0.2.0
github.com/conduitio/ecdysis v0.3.0
github.com/conduitio/yaml/v3 v3.3.0
github.com/dop251/goja v0.0.0-20240806095544-3491d4a58fbe
github.com/dop251/goja_nodejs v0.0.0-20231122114759-e84d9a924c5c
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ github.com/conduitio/conduit-processor-sdk v0.4.0 h1:wF1Fj31aneNixNbW5rJ0/5Q3vwW
github.com/conduitio/conduit-processor-sdk v0.4.0/go.mod h1:Jj9ZBTee7nO0XeociDxe9gSvLFN1GbPWP1Aj04DPeZQ=
github.com/conduitio/conduit-schema-registry v0.2.2 h1:Q0uL8egRAzJlRV7Ed5nEcqZ1yE/UQeZJad3VmhgTSFE=
github.com/conduitio/conduit-schema-registry v0.2.2/go.mod h1:EmT4ylkz15LYddL6qU4wDX52n1Yp0aHvEDRIWOYYzFs=
github.com/conduitio/ecdysis v0.2.0 h1:sH1rc5icJBzwUU51yYMMFfcOez0bSeEpmJ+NrmYpU/8=
github.com/conduitio/ecdysis v0.2.0/go.mod h1:CRQ3QxTsu/hhFepToP8InsBltZMfGIncbWvUNnNJSC4=
github.com/conduitio/ecdysis v0.3.0 h1:vBoh60M1/d2CUnWG964OT6uhr3NSO90SFW9RRPCKt9A=
github.com/conduitio/ecdysis v0.3.0/go.mod h1:8ztJTkL5AfCm2qFMzaSUO/IItvJ3z7Jj1PWLcgqmT14=
github.com/conduitio/yaml/v3 v3.3.0 h1:kbbaOSHcuH39gP4+rgbJGl6DSbLZcJgEaBvkEXJlCsI=
github.com/conduitio/yaml/v3 v3.3.0/go.mod h1:JNgFMOX1t8W4YJuRZOh6GggVtSMsgP9XgTw+7dIenpc=
github.com/containerd/cgroups/v3 v3.0.5 h1:44na7Ud+VwyE7LIoJ8JTNQOa549a8543BmzaJHo6Bzo=
Expand Down
Loading