Skip to content

Commit

Permalink
Add check for input file in CLI Run method
Browse files Browse the repository at this point in the history
  • Loading branch information
catatsuy committed Mar 20, 2024
1 parent 6547b62 commit ed4e1fb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/catatsuy/notify_slack/config"
"github.com/catatsuy/notify_slack/slack"
"github.com/catatsuy/notify_slack/throttle"
"golang.org/x/term"
)

var (
Expand Down Expand Up @@ -106,6 +107,9 @@ func (c *CLI) Run(args []string) int {
fmt.Fprintln(c.errStream, "You cannot pass multiple files")
return ExitCodeParseFlagError
}
} else if term.IsTerminal(int(os.Stdin.Fd())) {
fmt.Fprintln(c.errStream, "No input file specified")
return ExitCodeFail
}

tomlFile = config.LoadTOMLFilename(tomlFile)
Expand Down
39 changes: 39 additions & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"bytes"
"context"
"fmt"
"io"
"os"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -42,6 +45,42 @@ func TestRun_versionFlg(t *testing.T) {
}
}

func TestRun_providedStdin(t *testing.T) {
errStream, inputStream := new(bytes.Buffer), new(bytes.Buffer)

// cf: https://cs.opensource.google/go/x/term/+/master:term_test.go
if runtime.GOOS != "linux" {
t.Skipf("unknown terminal path for GOOS %v", runtime.GOOS)
}
file, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0)
if err != nil {
t.Fatal(err)
}
defer file.Close()

var outStream io.Writer = file

cl := &CLI{
outStream: outStream,
errStream: errStream,
inputStream: inputStream,
sClient: &fakeSlackClient{},
conf: config.NewConfig(),
}

args := strings.Split("notify_slack", " ")
status := cl.Run(args)

if status != ExitCodeFail {
t.Errorf("ExitStatus=%d, want %d", status, ExitCodeOK)
}

expected := "No input file specified"
if !strings.Contains(errStream.String(), expected) {
t.Errorf("Output=%q, want %q", errStream.String(), expected)
}
}

func TestUploadSnippet(t *testing.T) {
cl := &CLI{
sClient: &fakeSlackClient{},
Expand Down
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module github.com/catatsuy/notify_slack

go 1.19
go 1.22.1

require github.com/pelletier/go-toml/v2 v2.2.0
require (
github.com/pelletier/go-toml/v2 v2.2.0
golang.org/x/term v0.18.0
)

require golang.org/x/sys v0.18.0 // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
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=
Expand Down

0 comments on commit ed4e1fb

Please sign in to comment.