From 03264b572c6be6fbe776c7993390248a3d2f1a2a Mon Sep 17 00:00:00 2001 From: Luca Lanziani Date: Mon, 16 Oct 2023 17:22:15 +0200 Subject: [PATCH] chore: add test --- src/cli/cli.go | 2 +- src/cli/cli_test.go | 18 ++++++++++++++++++ src/cli/init_test.go | 31 ++++++++++++++++++++++--------- src/cli/template_test.go | 9 +++++---- 4 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 src/cli/cli_test.go diff --git a/src/cli/cli.go b/src/cli/cli.go index da0a875..85f3913 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -153,7 +153,7 @@ func (c icli) Run(args []string) error { Name: "version", Usage: "Return the version of the cli", Action: func(ctx *cli.Context) error { - _, err := fmt.Printf("version %s, commit %s, built at %s\n", c.release.Version, c.release.Commit, c.release.Date) + _, err := fmt.Fprintf(c.Writer, "version %s, commit %s, built at %s\n", c.release.Version, c.release.Commit, c.release.Date) return err }, }, diff --git a/src/cli/cli_test.go b/src/cli/cli_test.go new file mode 100644 index 0000000..ab2a614 --- /dev/null +++ b/src/cli/cli_test.go @@ -0,0 +1,18 @@ +package cli + +import ( + "os" + "testing" + + "gotest.tools/v3/assert" +) + +func TestVersion(t *testing.T) { + expected := "version 1, commit unknown, built at today\n" + icli := geticliForTesting(os.DirFS("../..")) + + reseticliBuffer(&icli) + icli.Run([]string{"initium", "version"}) + output := icliOutput(icli) + assert.Assert(t, output == expected, "Expected %s, got %s", expected, output) +} diff --git a/src/cli/init_test.go b/src/cli/init_test.go index ba513a0..58a4763 100644 --- a/src/cli/init_test.go +++ b/src/cli/init_test.go @@ -33,7 +33,7 @@ runtime-version: null } } -func GeticliForTesting(resources fs.FS) icli { +func geticliForTesting(resources fs.FS) icli { return NewWithOptions( resources, log.NewWithOptions(os.Stderr, log.Options{ @@ -42,11 +42,24 @@ func GeticliForTesting(resources fs.FS) icli { ReportTimestamp: true, }), new(bytes.Buffer), + Release{ + Version: "1", + Date: "today", + Commit: "unknown", + }, ) } +func reseticliBuffer(c *icli) { + c.Writer = new(bytes.Buffer) +} + +func icliOutput(c icli) string { + return fmt.Sprint(c.Writer.(*bytes.Buffer)) +} + func TestInitConfig(t *testing.T) { - icli := GeticliForTesting(os.DirFS("../..")) + icli := geticliForTesting(os.DirFS("../..")) // Config file is read correctly // Generate temporary file and add app-name parameter @@ -63,7 +76,7 @@ func TestInitConfig(t *testing.T) { t.Errorf("writing config content %v", err) } - icli.Writer = new(bytes.Buffer) + reseticliBuffer(&icli) if err = icli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "init", "config"}); err != nil { t.Error(err) } @@ -72,14 +85,14 @@ func TestInitConfig(t *testing.T) { // Environment Variable wins over config os.Setenv("INITIUM_APP_NAME", "FromEnv") defer os.Unsetenv("INITIUM_APP_NAME") // Unset the environment variable at the end - icli.Writer = new(bytes.Buffer) + reseticliBuffer(&icli) if err = icli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "init", "config"}); err != nil { t.Error(err) } compareConfig(t, "FromEnv", registry, icli.Writer) // Command line argument wins over config and Environment variable - icli.Writer = new(bytes.Buffer) + reseticliBuffer(&icli) if err = icli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "init", "config", "--app-name=FromParam"}); err != nil { t.Error(err) } @@ -88,7 +101,7 @@ func TestInitConfig(t *testing.T) { } func TestRepoNameRetrocompatibiliy(t *testing.T) { - cli := GeticliForTesting(os.DirFS("../..")) + cli := geticliForTesting(os.DirFS("../..")) // Generate temporary file and add repo-name parameter f, err := os.CreateTemp("", "tmpfile-") @@ -102,14 +115,14 @@ func TestRepoNameRetrocompatibiliy(t *testing.T) { t.Errorf("writing config content %v", err) } - cli.Writer = new(bytes.Buffer) + reseticliBuffer(&cli) if err = cli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "init", "config", "--app-name=FromParam"}); err != nil { t.Error(err) } compareConfig(t, "FromParam", "FromFile", cli.Writer) //Override from parameter - cli.Writer = new(bytes.Buffer) + reseticliBuffer(&cli) if err = cli.Run([]string{"initium", fmt.Sprintf("--config-file=%s", f.Name()), "init", "config", "--app-name=FromParam", "--container-registry=ghcr.io/nearform"}); err != nil { t.Error(err) } @@ -117,7 +130,7 @@ func TestRepoNameRetrocompatibiliy(t *testing.T) { } func TestAppName(t *testing.T) { - cli := GeticliForTesting(os.DirFS("../..")) + cli := geticliForTesting(os.DirFS("../..")) err := cli.Run([]string{"initium", "build"}) if err == nil { diff --git a/src/cli/template_test.go b/src/cli/template_test.go index 39e9e0a..7533112 100644 --- a/src/cli/template_test.go +++ b/src/cli/template_test.go @@ -3,12 +3,13 @@ package cli import ( "bytes" "fmt" - "github.com/nearform/initium-cli/src/services/project" - "github.com/urfave/cli/v2" - "gotest.tools/v3/assert" "os" "path" "testing" + + "github.com/nearform/initium-cli/src/services/project" + "github.com/urfave/cli/v2" + "gotest.tools/v3/assert" ) const root = "../../" @@ -61,7 +62,7 @@ func TestShouldRenderDockerTemplate(t *testing.T) { cCtx := cli.Context{} instance := icli{ - project: project.Project{ + project: &project.Project{ Name: string(projectType), Directory: path.Join(root, props["directory"]), Resources: os.DirFS(root),