From ec6f108a17f86bbf3c79e15ef24415a63db96210 Mon Sep 17 00:00:00 2001 From: Mathias Hansen Date: Sat, 15 Jan 2022 23:50:06 +0100 Subject: [PATCH] fix(git): Removed errornous gitignore entry --- .gitignore | 3 +-- app/cli.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ app/cli_test.go | 21 +++++++++++++++++++++ app/status_test.go | 28 ++++++++++++++++++++++++++++ app/testutils.go | 24 ++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 app/cli.go create mode 100644 app/cli_test.go create mode 100644 app/status_test.go create mode 100644 app/testutils.go diff --git a/.gitignore b/.gitignore index 91a8ec9..346c013 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ -app .idea dist/ -geocodio \ No newline at end of file +geocodio diff --git a/app/cli.go b/app/cli.go new file mode 100644 index 0000000..9a9ed37 --- /dev/null +++ b/app/cli.go @@ -0,0 +1,45 @@ +package app + +import ( + "github.com/geocodio/geocodio-cli/create" + "github.com/geocodio/geocodio-cli/download" + "github.com/geocodio/geocodio-cli/list" + "github.com/geocodio/geocodio-cli/release" + "github.com/geocodio/geocodio-cli/remove" + "github.com/geocodio/geocodio-cli/status" + "github.com/urfave/cli/v2" +) + +func BuildApp() *cli.App { + app := cli.NewApp() + app.Name = "Geocodio" + app.Usage = "Geocode lists using the Geocodio API" + app.Version = release.Version() + app.EnableBashCompletion = true + app.Flags = []cli.Flag{ + &cli.StringFlag{ + Name: "hostname", + Aliases: []string{"n"}, + Value: "api.geocod.io", + Usage: "Geocodio hostname to use, change this for Geocodio+HIPAA or on-premise environments", + EnvVars: []string{"GEOCODIO_HOSTNAME"}, + }, + &cli.StringFlag{ + Name: "apikey", + Aliases: []string{"k"}, + Value: "", + Usage: "Geocodio API Key to use. Generate a new one in the Geocodio Dashboard", + EnvVars: []string{"GEOCODIO_API_KEY"}, + Required: true, + }, + } + app.Commands = []*cli.Command{ + create.RegisterCommand(), + list.RegisterCommand(), + status.RegisterCommand(), + remove.RegisterCommand(), + download.RegisterCommand(), + } + + return app +} diff --git a/app/cli_test.go b/app/cli_test.go new file mode 100644 index 0000000..172205d --- /dev/null +++ b/app/cli_test.go @@ -0,0 +1,21 @@ +package app + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNoArgs(t *testing.T) { + err, output := RunAppForTesting([]string{}) + + assert.Contains(t, err.Error(), "\"apikey\" not set", "apikey flag should be required") + assert.Contains(t, output, "Geocodio - Geocode lists using the Geocodio API", "Output should contain expected string") +} + +func TestWithDummyAPIKey(t *testing.T) { + err, output := RunAppForTesting([]string{"--apikey=DEMO"}) + + assert.Nil(t, err) + assert.Contains(t, output, "Geocodio - Geocode lists using the Geocodio API", "Output should contain expected string") +} + diff --git a/app/status_test.go b/app/status_test.go new file mode 100644 index 0000000..729ddb2 --- /dev/null +++ b/app/status_test.go @@ -0,0 +1,28 @@ +package app + +import ( + "github.com/stretchr/testify/assert" + "os" + "testing" +) + +func TestStatusWithoutArgument(t *testing.T) { + err, output := RunAppForTesting([]string{"--apikey=" + os.Getenv("GEOCODIO_TEST_API_KEY"), "status"}) + + assert.Contains(t, err.Error(), "Invalid spreadsheet job id specified") + assert.Equal(t, "", output, "No standard output should be present") +} + +func TestStatusWithInvalidSpreadsheetId(t *testing.T) { + err, output := RunAppForTesting([]string{"--apikey=" + os.Getenv("GEOCODIO_TEST_API_KEY"), "status", "1"}) + + assert.Contains(t, err.Error(), " No spreadsheet job with that id found") + assert.Equal(t, "", output, "No standard output should be present") +} + +func TestStatusWithValidSpreadsheetId(t *testing.T) { + err, output := RunAppForTesting([]string{"--apikey=" + os.Getenv("GEOCODIO_TEST_API_KEY"), "status", "11471563"}) + + assert.Nil(t, err) + assert.Contains(t, output, "State: COMPLETED") +} \ No newline at end of file diff --git a/app/testutils.go b/app/testutils.go new file mode 100644 index 0000000..309f514 --- /dev/null +++ b/app/testutils.go @@ -0,0 +1,24 @@ +package app + +import ( + "bytes" + "github.com/urfave/cli/v2" + "os" +) + +func RunAppForTesting(args []string) (error, string) { + w := new(bytes.Buffer) + + program := os.Args[0:1] + args = append(program, args...) + + app := BuildApp() + app.ExitErrHandler = func(context *cli.Context, err error) { + // Do nothing + } + app.Writer = w + + err := app.Run(args) + + return err, w.String() +}