Skip to content

Commit

Permalink
Merge pull request #66 from catatsuy/feature-refactor-exit-code
Browse files Browse the repository at this point in the history
Update exit codes in CLI operations
  • Loading branch information
catatsuy authored Aug 11, 2024
2 parents 93703cf + c63b99e commit 923ac23
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
11 changes: 7 additions & 4 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

const (
ExitCodeOK = 0
ExitCodeParseFlagError = 1
ExitCodeFail = 1
ExitCodeParseFlagError = 2
ExitCodeFail = 2
)

var (
Expand Down Expand Up @@ -160,7 +160,8 @@ func (c *CLI) Run(args []string) int {
}

if len(c.replaceExpr) > 0 {
if err := c.replaceProcess(searchRe, replacement, file); err != nil {
err := c.replaceProcess(searchRe, replacement, file)
if err != nil {
fmt.Fprintf(c.errStream, "Failed to process files: %s\n", err)
return ExitCodeFail
}
Expand All @@ -183,7 +184,8 @@ func (c *CLI) Run(args []string) int {
}
} else {
if len(c.replaceExpr) > 0 {
if err := c.replaceProcess(searchRe, replacement, c.inputStream); err != nil {
err := c.replaceProcess(searchRe, replacement, c.inputStream)
if err != nil {
fmt.Fprintf(c.errStream, "Failed to process files: %s\n", err)
return ExitCodeFail
}
Expand Down Expand Up @@ -291,6 +293,7 @@ func (c *CLI) replaceProcess(searchRe *regexp.Regexp, replacement []byte, inputS
}
// Replace text in each line using the regex
modifiedLine := searchRe.ReplaceAll(line, replacement)

// Write the changed line to the output
if _, err := c.outStream.Write(modifiedLine); err != nil {
return fmt.Errorf("error writing to output: %w", err)
Expand Down
66 changes: 35 additions & 31 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,37 +168,44 @@ func TestRun_successProcessOnTerminal(t *testing.T) {

func TestRun_FailOnTerminal(t *testing.T) {
tests := map[string]struct {
args []string
input string
expected string
result int
args []string
input string
expected string
expectedCode int
result int
}{
"normal": {
args: []string{"purl", "-replace", "@search@replacement@"},
input: "searchb searchc",
args: []string{"purl", "-replace", "@search@replacement@"},
input: "searchb searchc",
expectedCode: 2,
},
"no match": {
args: []string{"purl", "-replace", "@search@replacement@"},
input: "no match",
args: []string{"purl", "-replace", "@search@replacement@"},
input: "no match",
expectedCode: 2,
},
"provide stdin for ignore case": {
args: []string{"purl", "-i", "-replace", "@search@replacement@"},
input: "searcha Search\nsearchc Searchd\n",
args: []string{"purl", "-i", "-replace", "@search@replacement@"},
input: "searcha Search\nsearchc Searchd\n",
expectedCode: 2,
},
"color text": {
args: []string{"purl", "-filter", "search", "-color"},
input: "searchb\nreplace\nsearchc",
expected: "\x1b[1m\x1b[91msearch\x1b[0mb\n\x1b[1m\x1b[91msearch\x1b[0mc\n",
args: []string{"purl", "-filter", "search", "-color"},
input: "searchb\nreplace\nsearchc",
expected: "\x1b[1m\x1b[91msearch\x1b[0mb\n\x1b[1m\x1b[91msearch\x1b[0mc\n",
expectedCode: 2,
},
"color text for multiple filter": {
args: []string{"purl", "-filter", "search", "-filter", "abcd", "-color"},
input: "searchb\nreplace\nsearchcabcdefg",
expected: "\x1b[1m\x1b[91msearch\x1b[0mb\n\x1b[1m\x1b[91msearch\x1b[0mc\x1b[1m\x1b[91mabcd\x1b[0mefg\n",
args: []string{"purl", "-filter", "search", "-filter", "abcd", "-color"},
input: "searchb\nreplace\nsearchcabcdefg",
expected: "\x1b[1m\x1b[91msearch\x1b[0mb\n\x1b[1m\x1b[91msearch\x1b[0mc\x1b[1m\x1b[91mabcd\x1b[0mefg\n",
expectedCode: 2,
},
"no color text": {
args: []string{"purl", "-filter", "search", "-no-color"},
input: "searchb\nreplace\nsearchc",
expected: "searchb\nsearchc\n",
args: []string{"purl", "-filter", "search", "-no-color"},
input: "searchb\nreplace\nsearchc",
expected: "searchb\nsearchc\n",
expectedCode: 2,
},
}

Expand All @@ -210,8 +217,7 @@ func TestRun_FailOnTerminal(t *testing.T) {
cl := cli.NewCLI(outStream, errStream, inputStream, true, true)
inputStream.WriteString(test.input)

expectedCode := 1
if got, expected := cl.Run(test.args), expectedCode; got != expected {
if got, expected := cl.Run(test.args), test.expectedCode; got != expected {
t.Fatalf("Expected exit code %d, but got %d; error: %q", expected, got, errStream.String())
}

Expand Down Expand Up @@ -360,22 +366,22 @@ func TestRun_failToProvideStdin(t *testing.T) {
{
desc: "fail to provide -replace",
args: []string{"purl", "-replace", "search@replacement"},
expectedCode: 1,
expectedCode: 2,
},
{
desc: "fail to provide -filter and -replace",
args: []string{"purl", "-filter", "aaa", "-replace", "@search@replacement@"},
expectedCode: 1,
expectedCode: 2,
},
{
desc: "non-existent file with -replace",
args: []string{"purl", "-replace", "@search@replacement@", "testdata/noexist.txt"},
expectedCode: 1,
expectedCode: 2,
},
{
desc: "non-existent file with -filter",
args: []string{"purl", "-filter", "aaaaa", "testdata/noexist.txt"},
expectedCode: 1,
expectedCode: 2,
},
}

Expand All @@ -401,7 +407,7 @@ func TestRun_failToProvideFiles(t *testing.T) {
{
desc: "fail to provide -replace",
args: []string{"purl", "-replace", "search@replacement", "testdata/test.txt"},
expectedCode: 1,
expectedCode: 2,
},
}

Expand All @@ -427,17 +433,17 @@ func TestRun_failToProvideOverwriteAndStdin(t *testing.T) {
{
desc: "fail to provide -replace",
args: []string{"purl", "-replace", "@search@replacement", "-overwrite"},
expectedCode: 1,
expectedCode: 2,
},
{
desc: "fail to provide -replace",
args: []string{"purl", "-filter", "search", "-overwrite"},
expectedCode: 1,
expectedCode: 2,
},
{
desc: "fail to provide -replace",
args: []string{"purl", "-exclude", "search", "-overwrite"},
expectedCode: 1,
expectedCode: 2,
},
}

Expand All @@ -461,7 +467,6 @@ func TestReplaceProcess_replace(t *testing.T) {
inputStream.WriteString("searchb searchc\n")

err := cl.ReplaceProcess(regexp.MustCompile("search"), []byte("replacement"), inputStream)

if err != nil {
t.Errorf("Error=%q", err)
}
Expand All @@ -479,7 +484,6 @@ func TestReplaceProcess_noMatch(t *testing.T) {
inputStream.WriteString("no match\n")

err := cl.ReplaceProcess(regexp.MustCompile("search"), []byte("replacement"), inputStream)

if err != nil {
t.Errorf("Error=%q", err)
}
Expand Down

0 comments on commit 923ac23

Please sign in to comment.