diff --git a/cmd/ynabconverter/cashapp_test.go b/cmd/ynabconverter/cashapp_test.go new file mode 100644 index 0000000..b71681b --- /dev/null +++ b/cmd/ynabconverter/cashapp_test.go @@ -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)) + }) +} diff --git a/cmd/ynabconverter/main.go b/cmd/ynabconverter/main.go index ae9040b..b3867b9 100644 --- a/cmd/ynabconverter/main.go +++ b/cmd/ynabconverter/main.go @@ -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 "" } diff --git a/cmd/ynabconverter/main_test.go b/cmd/ynabconverter/main_test.go index 40c1af1..8f6b6d2 100644 --- a/cmd/ynabconverter/main_test.go +++ b/cmd/ynabconverter/main_test.go @@ -1,6 +1,3 @@ -// this is a integration test -// which run the ynabconverter cli command and check the output - package main_test import ( @@ -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" { @@ -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)) }) }