Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaLanziani committed Oct 16, 2023
1 parent a90465c commit 03264b5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
},
Expand Down
18 changes: 18 additions & 0 deletions src/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
31 changes: 22 additions & 9 deletions src/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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-")
Expand All @@ -102,22 +115,22 @@ 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)
}
compareConfig(t, "FromParam", "ghcr.io/nearform", cli.Writer)
}

func TestAppName(t *testing.T) {
cli := GeticliForTesting(os.DirFS("../.."))
cli := geticliForTesting(os.DirFS("../.."))

err := cli.Run([]string{"initium", "build"})
if err == nil {
Expand Down
9 changes: 5 additions & 4 deletions src/cli/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "../../"
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 03264b5

Please sign in to comment.