Skip to content

Commit

Permalink
feat: check if command is available
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardobazico committed Oct 10, 2024
1 parent b490796 commit 287f9af
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 100 deletions.
126 changes: 126 additions & 0 deletions cmd/ynabconverter/cashapp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// this is a integration test
// which run the ynabconverter cli command and check the output

package main_test

import (
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCash2ynab(t *testing.T) {
t.Parallel()

if os.Getenv("SKIP_INTEGRATION") == "true" {
t.Skip("Skipping integration test")
}

projectFolderPath := getProjectFolderPath(t)
cli := buildCli(t, projectFolderPath)
t.Run("should run ynabconverter cashapp command and get the output", func(t *testing.T) {
// Given
cmd := exec.Command(cli, "cashapp", "-file", "tests/utils/examples/cash_app_report_sample.csv")
cmd.Env = append([]string{}, os.Environ()...)
cmd.Dir = projectFolderPath
// When
output, err := cmd.CombinedOutput()
// Then
require.NoError(t, err)
assert.Equal(t,
"Date,Payee,Memo,Amount\n"+
"10/06/2023,MTA*NYCT PAYGO,CARD CHARGED,-2.90\n"+
"01/10/2023,Some business name,PAYMENT SENT,-10.00\n",
string(output))
})

t.Run("should fail when file does not exist", func(t *testing.T) {
// Given
cmd := exec.Command(cli, "cashapp", "-file", "tests/utils/examples/does-not-exist.csv")
cmd.Env = append([]string{}, os.Environ()...)
cmd.Dir = projectFolderPath
// When
output, err := cmd.CombinedOutput()
// Then
require.Error(t, err)
assert.Contains(
t,
string(output),
"fail to open file: open tests/utils/examples/does-not-exist.csv: no such file or directory",
)
})

t.Run("should fail when file is not provided", func(t *testing.T) {
// Given
cmd := exec.Command(cli, "cashapp", "-file", "")
cmd.Env = append([]string{}, os.Environ()...)
cmd.Dir = projectFolderPath
// When
output, err := cmd.CombinedOutput()
// Then
require.Error(t, err)
assert.Contains(
t,
string(output),
"it is required to set a file to be converted",
)
})

t.Run("should handle absolute path", func(t *testing.T) {
// Given
cmd := exec.Command(cli, "cashapp", "-file", projectFolderPath+"/tests/utils/examples/cash_app_report_sample.csv")
cmd.Env = append([]string{}, os.Environ()...)
cmd.Dir = projectFolderPath
// When
output, err := cmd.CombinedOutput()
// Then
require.NoError(t, err)
assert.Equal(t,
"Date,Payee,Memo,Amount\n"+
"10/06/2023,MTA*NYCT PAYGO,CARD CHARGED,-2.90\n"+
"01/10/2023,Some business name,PAYMENT SENT,-10.00\n",
string(output))
})

t.Run("should handle relative path", func(t *testing.T) {
// Given
cmd := exec.Command(cli, "cashapp", "-file", "./tests/utils/examples/cash_app_report_sample.csv")
cmd.Env = append([]string{}, os.Environ()...)
cmd.Dir = projectFolderPath
// When
output, err := cmd.CombinedOutput()
// Then
require.NoError(t, err)
assert.Equal(t,
"Date,Payee,Memo,Amount\n"+
"10/06/2023,MTA*NYCT PAYGO,CARD CHARGED,-2.90\n"+
"01/10/2023,Some business name,PAYMENT SENT,-10.00\n",
string(output))
})

t.Run("should handle level up relative path", func(t *testing.T) {
// Given
cmd := exec.Command(
cli,
"cashapp",
"-file",
"../"+filepath.Base(projectFolderPath)+
"/tests/utils/examples/cash_app_report_sample.csv",
)
cmd.Env = append([]string{}, os.Environ()...)
cmd.Dir = projectFolderPath
// When
output, err := cmd.CombinedOutput()
// Then
require.NoError(t, err)
assert.Equal(t,
"Date,Payee,Memo,Amount\n"+
"10/06/2023,MTA*NYCT PAYGO,CARD CHARGED,-2.90\n"+
"01/10/2023,Some business name,PAYMENT SENT,-10.00\n",
string(output))
})
}
20 changes: 19 additions & 1 deletion cmd/ynabconverter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@ package main

import (
"flag"
"fmt"
"os"
)

func main() {
cashAppCmd := flag.NewFlagSet("cashapp", flag.ExitOnError)
cashAppFile := cashAppCmd.String("file", "", "(required) path to CashApp csv file")

cashApp(cashAppCmd, cashAppFile)
command := getCommandString()

switch command {
case "cashapp":
cashApp(cashAppCmd, cashAppFile)
default:
fmt.Fprintf(os.Stderr, "Command available: cashapp")
os.Exit(1)
}
}

func getCommandString() string {
if len(os.Args) > 1 {
return os.Args[1]
}

return ""
}
106 changes: 7 additions & 99 deletions cmd/ynabconverter/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// this is a integration test
// which run the ynabconverter cli command and check the output

package main_test

import (
Expand All @@ -13,7 +10,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestCash2ynab(t *testing.T) {
func TestMain(t *testing.T) {
t.Parallel()

if os.Getenv("SKIP_INTEGRATION") == "true" {
Expand All @@ -22,118 +19,29 @@ func TestCash2ynab(t *testing.T) {

projectFolderPath := getProjectFolderPath(t)
cli := buildCli(t, projectFolderPath)
t.Run("should run ynabconverter cashapp command and get the output", func(t *testing.T) {
t.Parallel()

// Given
cmd := exec.Command(cli, "cashapp", "-file", "tests/utils/examples/cash_app_report_sample.csv")
cmd.Env = append([]string{}, os.Environ()...)
cmd.Dir = projectFolderPath
// When
output, err := cmd.CombinedOutput()
// Then
require.NoError(t, err)
assert.Equal(t,
"Date,Payee,Memo,Amount\n"+
"10/06/2023,MTA*NYCT PAYGO,CARD CHARGED,-2.90\n"+
"01/10/2023,Some business name,PAYMENT SENT,-10.00\n",
string(output))
})

t.Run("should fail when file does not exist", func(t *testing.T) {
t.Parallel()

t.Run("should show available commands when nothings is passed", func(t *testing.T) {
// Given
cmd := exec.Command(cli, "cashapp", "-file", "tests/utils/examples/does-not-exist.csv")
cmd := exec.Command(cli, "")
cmd.Env = append([]string{}, os.Environ()...)
cmd.Dir = projectFolderPath
// When
output, err := cmd.CombinedOutput()
// Then
require.Error(t, err)
assert.Contains(
t,
string(output),
"fail to open file: open tests/utils/examples/does-not-exist.csv: no such file or directory",
)
assert.Equal(t, "Command available: cashapp", string(output))
})

t.Run("should fail when file is not provided", func(t *testing.T) {
t.Parallel()

t.Run("should show available commands when command is not recognize", func(t *testing.T) {
// Given
cmd := exec.Command(cli, "cashapp", "-file", "")
cmd := exec.Command(cli, "do-nothing")
cmd.Env = append([]string{}, os.Environ()...)
cmd.Dir = projectFolderPath
// When
output, err := cmd.CombinedOutput()
// Then
require.Error(t, err)
assert.Contains(
t,
string(output),
"it is required to set a file to be converted",
)
})

t.Run("should handle absolute path", func(t *testing.T) {
t.Parallel()

// Given
cmd := exec.Command(cli, "cashapp", "-file", projectFolderPath+"/tests/utils/examples/cash_app_report_sample.csv")
cmd.Env = append([]string{}, os.Environ()...)
cmd.Dir = projectFolderPath
// When
output, err := cmd.CombinedOutput()
// Then
require.NoError(t, err)
assert.Equal(t,
"Date,Payee,Memo,Amount\n"+
"10/06/2023,MTA*NYCT PAYGO,CARD CHARGED,-2.90\n"+
"01/10/2023,Some business name,PAYMENT SENT,-10.00\n",
string(output))
})

t.Run("should handle relative path", func(t *testing.T) {
t.Parallel()

// Given
cmd := exec.Command(cli, "cashapp", "-file", "./tests/utils/examples/cash_app_report_sample.csv")
cmd.Env = append([]string{}, os.Environ()...)
cmd.Dir = projectFolderPath
// When
output, err := cmd.CombinedOutput()
// Then
require.NoError(t, err)
assert.Equal(t,
"Date,Payee,Memo,Amount\n"+
"10/06/2023,MTA*NYCT PAYGO,CARD CHARGED,-2.90\n"+
"01/10/2023,Some business name,PAYMENT SENT,-10.00\n",
string(output))
})

t.Run("should handle level up relative path", func(t *testing.T) {
t.Parallel()

// Given
cmd := exec.Command(
cli,
"cashapp",
"-file",
"../"+filepath.Base(projectFolderPath)+
"/tests/utils/examples/cash_app_report_sample.csv",
)
cmd.Env = append([]string{}, os.Environ()...)
cmd.Dir = projectFolderPath
// When
output, err := cmd.CombinedOutput()
// Then
require.NoError(t, err)
assert.Equal(t,
"Date,Payee,Memo,Amount\n"+
"10/06/2023,MTA*NYCT PAYGO,CARD CHARGED,-2.90\n"+
"01/10/2023,Some business name,PAYMENT SENT,-10.00\n",
string(output))
assert.Equal(t, "Command available: cashapp", string(output))
})
}

Expand Down

0 comments on commit 287f9af

Please sign in to comment.