Skip to content

Commit acad1a9

Browse files
authored
feat: use output decorator, adding tests (#2104)
* use display decorator * update method * add tests * go mod tidy * use latest commit * uses latest version of ecdysis * update test
1 parent f5dd0e2 commit acad1a9

File tree

5 files changed

+95
-7
lines changed

5 files changed

+95
-7
lines changed

Diff for: cmd/conduit/root/config/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"github.com/matryer/is"
2828
)
2929

30-
func TestConfig_WithFlags(t *testing.T) {
30+
func TestConfigWithFlags(t *testing.T) {
3131
testCases := []struct {
3232
name string
3333
args []string

Diff for: cmd/conduit/root/root.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package root
1717
import (
1818
"context"
1919
"fmt"
20-
"os"
2120

2221
"github.com/conduitio/conduit/cmd/conduit/root/config"
2322
"github.com/conduitio/conduit/cmd/conduit/root/connectorplugins"
@@ -39,6 +38,7 @@ var (
3938
_ ecdysis.CommandWithExecute = (*RootCommand)(nil)
4039
_ ecdysis.CommandWithDocs = (*RootCommand)(nil)
4140
_ ecdysis.CommandWithSubCommands = (*RootCommand)(nil)
41+
_ ecdysis.CommandWithOutput = (*RootCommand)(nil)
4242
)
4343

4444
type RootFlags struct {
@@ -50,12 +50,17 @@ type RootFlags struct {
5050
}
5151

5252
type RootCommand struct {
53-
flags RootFlags
53+
flags RootFlags
54+
output ecdysis.Output
55+
}
56+
57+
func (c *RootCommand) Output(output ecdysis.Output) {
58+
c.output = output
5459
}
5560

5661
func (c *RootCommand) Execute(ctx context.Context) error {
5762
if c.flags.Version {
58-
_, _ = fmt.Fprintf(os.Stdout, "%s\n", conduit.Version(true))
63+
c.output.Stdout(fmt.Sprintf("%s\n", conduit.Version(true)))
5964
return nil
6065
}
6166

Diff for: cmd/conduit/root/root_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
package root
1616

1717
import (
18+
"bytes"
19+
"context"
20+
"strings"
1821
"testing"
1922

23+
"github.com/conduitio/conduit/pkg/conduit"
2024
"github.com/conduitio/ecdysis"
2125
"github.com/matryer/is"
2226
"github.com/spf13/pflag"
@@ -56,3 +60,28 @@ func TestRootCommandFlags(t *testing.T) {
5660
is.Equal(cf.Usage, f.usage)
5761
}
5862
}
63+
64+
func TestRootCommandExecuteWithVersionFlag(t *testing.T) {
65+
is := is.New(t)
66+
67+
buf := new(bytes.Buffer)
68+
69+
out := &ecdysis.DefaultOutput{}
70+
out.Output(buf, nil)
71+
72+
cmd := &RootCommand{
73+
flags: RootFlags{
74+
Version: true,
75+
},
76+
}
77+
cmd.Output(out)
78+
79+
expectedOutput := strings.TrimSpace(conduit.Version(true))
80+
81+
err := cmd.Execute(context.Background())
82+
is.NoErr(err)
83+
84+
actualOutput := strings.TrimSpace(buf.String())
85+
86+
is.Equal(actualOutput, expectedOutput)
87+
}

Diff for: cmd/conduit/root/version/version.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package version
1717
import (
1818
"context"
1919
"fmt"
20-
"os"
2120

2221
"github.com/conduitio/conduit/pkg/conduit"
2322
"github.com/conduitio/ecdysis"
@@ -26,14 +25,21 @@ import (
2625
var (
2726
_ ecdysis.CommandWithExecute = (*VersionCommand)(nil)
2827
_ ecdysis.CommandWithDocs = (*VersionCommand)(nil)
28+
_ ecdysis.CommandWithOutput = (*VersionCommand)(nil)
2929
)
3030

31-
type VersionCommand struct{}
31+
type VersionCommand struct {
32+
output ecdysis.Output
33+
}
34+
35+
func (c *VersionCommand) Output(output ecdysis.Output) {
36+
c.output = output
37+
}
3238

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

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

Diff for: cmd/conduit/root/version/version_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright © 2025 Meroxa, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package version
16+
17+
import (
18+
"bytes"
19+
"context"
20+
"fmt"
21+
"runtime"
22+
"strings"
23+
"testing"
24+
25+
"github.com/conduitio/ecdysis"
26+
"github.com/matryer/is"
27+
)
28+
29+
func TestVersionCommandExecute(t *testing.T) {
30+
is := is.New(t)
31+
32+
buf := new(bytes.Buffer)
33+
34+
out := &ecdysis.DefaultOutput{}
35+
out.Output(buf, nil)
36+
37+
cmd := &VersionCommand{}
38+
cmd.Output(out)
39+
40+
expectedOutput := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
41+
42+
err := cmd.Execute(context.Background())
43+
is.NoErr(err)
44+
45+
actualOutput := strings.TrimSpace(buf.String())
46+
47+
is.Equal(actualOutput, expectedOutput)
48+
}

0 commit comments

Comments
 (0)