Skip to content

Commit

Permalink
feat: use output decorator, adding tests (#2104)
Browse files Browse the repository at this point in the history
* use display decorator

* update method

* add tests

* go mod tidy

* use latest commit

* uses latest version of ecdysis

* update test
  • Loading branch information
raulb authored Jan 28, 2025
1 parent f5dd0e2 commit acad1a9
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 7 deletions.
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
29 changes: 29 additions & 0 deletions cmd/conduit/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
package root

import (
"bytes"
"context"
"strings"
"testing"

"github.com/conduitio/conduit/pkg/conduit"
"github.com/conduitio/ecdysis"
"github.com/matryer/is"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -56,3 +60,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 := strings.TrimSpace(conduit.Version(true))

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)

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

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

is.Equal(actualOutput, expectedOutput)
}

0 comments on commit acad1a9

Please sign in to comment.