Skip to content

Commit

Permalink
init and interpolate working
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhuber committed Aug 4, 2023
1 parent c7d4996 commit c27e306
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 101 deletions.
1 change: 1 addition & 0 deletions internal/commands/apply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package commands_test
82 changes: 82 additions & 0 deletions internal/commands/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package commands_test

import (
"testing"

"github.com/patrickhuber/caster/internal/commands"
"github.com/patrickhuber/caster/internal/global"
"github.com/patrickhuber/caster/internal/setup"
"github.com/patrickhuber/go-di"
"github.com/patrickhuber/go-xplat/console"
"github.com/patrickhuber/go-xplat/env"
"github.com/patrickhuber/go-xplat/filepath"
"github.com/patrickhuber/go-xplat/fs"
"github.com/patrickhuber/go-xplat/os"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
)

type TestContext struct {
app *cli.App
container di.Container
console console.Console
fs fs.FS
env env.Environment
os os.OS
path *filepath.Processor
}

func SetupTestContext(t *testing.T) *TestContext {
var err error
s := setup.NewTest()
container := s.Container()

o, err := di.Resolve[os.OS](container)
require.NoError(t, err)

wd, err := o.WorkingDirectory()
require.NoError(t, err)

con, err := di.Resolve[console.Console](container)
require.NoError(t, err)

f, err := di.Resolve[fs.FS](container)
require.NoError(t, err)

paths := []string{"/", "/template", "/data", wd, o.Home()}
for _, p := range paths {
err = f.MkdirAll(p, 0666)
require.NoError(t, err)
}

e, err := di.Resolve[env.Environment](container)
require.NoError(t, err)

p, err := di.Resolve[*filepath.Processor](container)
require.NoError(t, err)

app := &cli.App{
Version: "1.0.0",
Metadata: map[string]interface{}{
global.DependencyInjectionContainer: container,
},
Commands: []*cli.Command{
commands.Interpolate,
commands.Apply,
commands.Initialize,
},
Reader: con.In(),
ErrWriter: con.Error(),
Writer: con.Out(),
}

return &TestContext{
app: app,
container: container,
console: con,
fs: f,
env: e,
os: o,
path: p,
}
}
54 changes: 51 additions & 3 deletions internal/commands/initialize_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
package commands
package commands_test

import "testing"
import (
"testing"

"github.com/patrickhuber/caster/internal/global"
"github.com/stretchr/testify/require"
)

func TestInitialize(t *testing.T) {

t.Run("default", func(t *testing.T) {
cx := SetupTestContext(t)

args := []string{"caster", "init"}
cx.app.Metadata[global.OSArgs] = args

err := cx.app.Run(args)
require.NoError(t, err)

wd, err := cx.os.WorkingDirectory()
require.NoError(t, err)

ok, err := cx.fs.Exists(cx.path.Join(wd, ".caster.yml"))
require.NoError(t, err)
require.True(t, ok)
})

t.Run("template_dir", func(t *testing.T) {
cx := SetupTestContext(t)

args := []string{"caster", "init", "-t", "/template"}
cx.app.Metadata[global.OSArgs] = args

err := cx.app.Run(args)
require.NoError(t, err)

ok, err := cx.fs.Exists(cx.path.Join("/template", ".caster.yml"))
require.NoError(t, err)
require.True(t, ok)
})

t.Run("template_file", func(t *testing.T) {
cx := SetupTestContext(t)

args := []string{"caster", "init", "-t", "/template/test.yml"}
cx.app.Metadata[global.OSArgs] = args

err := cx.app.Run(args)
require.NoError(t, err)

ok, err := cx.fs.Exists(cx.path.Join("/template", "test.yml"))
require.NoError(t, err)
require.True(t, ok)
})
}
118 changes: 27 additions & 91 deletions internal/commands/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,15 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/patrickhuber/caster/internal/commands"
"github.com/patrickhuber/caster/internal/global"
"github.com/patrickhuber/caster/internal/setup"
"github.com/patrickhuber/go-di"
"github.com/patrickhuber/go-xplat/console"
"github.com/patrickhuber/go-xplat/env"
"github.com/patrickhuber/go-xplat/fs"
"github.com/patrickhuber/go-xplat/os"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
)

type InterpolateTestContext struct {
app *cli.App
container di.Container
con console.Console
f fs.FS
e env.Environment
}

func TestInterpolate(t *testing.T) {

t.Run("basic", func(t *testing.T) {
cx := SetupInterpolateTestContext(t)
err := cx.f.WriteFile("/template/.caster.yml", []byte("files:\n- name: test.txt\n"), 0600)
cx := SetupTestContext(t)
err := cx.fs.WriteFile("/template/.caster.yml", []byte("files:\n- name: test.txt\n"), 0600)
require.NoError(t, err)

args := []string{"caster", "interpolate", "-t", "/template"}
Expand All @@ -38,7 +22,7 @@ func TestInterpolate(t *testing.T) {
err = cx.app.Run(args)
require.NoError(t, err)

buf, ok := cx.con.Out().(*bytes.Buffer)
buf, ok := cx.console.Out().(*bytes.Buffer)
require.True(t, ok)
require.Equal(t, "files:\n - name: test.txt\n", buf.String())
})
Expand All @@ -47,19 +31,19 @@ func TestInterpolate(t *testing.T) {
- name: test.txt
content: {{ .key }}
`
cx := SetupInterpolateTestContext(t)
err := cx.f.WriteFile("/template/.caster.yml", []byte(template), 0600)
cx := SetupTestContext(t)
err := cx.fs.WriteFile("/template/.caster.yml", []byte(template), 0600)
require.NoError(t, err)

cx.e.Set("CASTER_VAR_key", "value")
cx.env.Set("CASTER_VAR_key", "value")

args := []string{"caster", "interpolate", "-t", "/template"}
cx.app.Metadata[global.OSArgs] = args

err = cx.app.Run(args)
require.NoError(t, err)

buf, ok := cx.con.Out().(*bytes.Buffer)
buf, ok := cx.console.Out().(*bytes.Buffer)
require.True(t, ok)
want := `files:
- name: test.txt
Expand All @@ -69,17 +53,17 @@ func TestInterpolate(t *testing.T) {
require.Equal(t, want, have, cmp.Diff(have, want))
})
t.Run("multi_data", func(t *testing.T) {
cx := SetupInterpolateTestContext(t)
cx.f.WriteFile("/template/.caster.yml", []byte("files:\n- name: test.txt\n content: {{.first}}{{.second}}"), 0600)
cx.f.WriteFile("/data/1.yml", []byte("first: first"), 0600)
cx.f.WriteFile("/data/2.yml", []byte("second: second"), 0600)
cx := SetupTestContext(t)
cx.fs.WriteFile("/template/.caster.yml", []byte("files:\n- name: test.txt\n content: {{.first}}{{.second}}"), 0600)
cx.fs.WriteFile("/data/1.yml", []byte("first: first"), 0600)
cx.fs.WriteFile("/data/2.yml", []byte("second: second"), 0600)

args := []string{"caster", "interpolate", "--var-file", "/data/1.yml", "--var-file", "/data/2.yml", "-t", "/template"}
cx.app.Metadata[global.OSArgs] = args
err := cx.app.Run(args)
require.NoError(t, err)

buf, ok := cx.con.Out().(*bytes.Buffer)
buf, ok := cx.console.Out().(*bytes.Buffer)
require.True(t, ok)
want := `files:
- name: test.txt
Expand All @@ -89,15 +73,15 @@ func TestInterpolate(t *testing.T) {
require.Equal(t, want, have, cmp.Diff(have, want))
})
t.Run("multi_arg", func(t *testing.T) {
cx := SetupInterpolateTestContext(t)
cx.f.WriteFile("/template/.caster.yml", []byte("files:\n- name: test.txt\n content: {{.first}}{{.second}}"), 0600)
cx := SetupTestContext(t)
cx.fs.WriteFile("/template/.caster.yml", []byte("files:\n- name: test.txt\n content: {{.first}}{{.second}}"), 0600)

args := []string{"caster", "interpolate", "--var", "first=first", "--var", "second=second", "-t", "/template"}
cx.app.Metadata[global.OSArgs] = args
err := cx.app.Run(args)
require.NoError(t, err)

buf, ok := cx.con.Out().(*bytes.Buffer)
buf, ok := cx.console.Out().(*bytes.Buffer)
require.True(t, ok)
want := `files:
- name: test.txt
Expand All @@ -107,16 +91,16 @@ func TestInterpolate(t *testing.T) {
require.Equal(t, want, have, cmp.Diff(have, want))
})
t.Run("mixed_arg", func(t *testing.T) {
cx := SetupInterpolateTestContext(t)
cx.f.WriteFile("/template/.caster.yml", []byte("files:\n- name: test.txt\n content: {{.key}}"), 0600)
cx.f.WriteFile("/data/1.yml", []byte("key: first"), 0600)
cx := SetupTestContext(t)
cx.fs.WriteFile("/template/.caster.yml", []byte("files:\n- name: test.txt\n content: {{.key}}"), 0600)
cx.fs.WriteFile("/data/1.yml", []byte("key: first"), 0600)

args := []string{"caster", "interpolate", "--var-file", "/data/1.yml", "--var", "key=second", "-t", "/template"}
cx.app.Metadata[global.OSArgs] = args
err := cx.app.Run(args)
require.NoError(t, err)

buf, ok := cx.con.Out().(*bytes.Buffer)
buf, ok := cx.console.Out().(*bytes.Buffer)
require.True(t, ok)
want := `files:
- name: test.txt
Expand All @@ -127,16 +111,16 @@ func TestInterpolate(t *testing.T) {
})
t.Run("override", func(t *testing.T) {

cx := SetupInterpolateTestContext(t)
cx.f.WriteFile("/template/.caster.yml", []byte("files:\n- name: test.txt\n content: {{.key}}"), 0600)
cx.f.WriteFile("/data/1.yml", []byte("key: second"), 0600)
cx := SetupTestContext(t)
cx.fs.WriteFile("/template/.caster.yml", []byte("files:\n- name: test.txt\n content: {{.key}}"), 0600)
cx.fs.WriteFile("/data/1.yml", []byte("key: second"), 0600)

args := []string{"caster", "interpolate", "--var", "key=first", "--var-file", "/data/1.yml", "-t", "/template"}
cx.app.Metadata[global.OSArgs] = args
err := cx.app.Run(args)
require.NoError(t, err)

buf, ok := cx.con.Out().(*bytes.Buffer)
buf, ok := cx.console.Out().(*bytes.Buffer)
require.True(t, ok)
want := `files:
- name: test.txt
Expand All @@ -147,16 +131,16 @@ func TestInterpolate(t *testing.T) {
})

t.Run("default", func(t *testing.T) {
cx := SetupInterpolateTestContext(t)
cx.f.WriteFile("/working/.caster.yml", []byte("files:\n- name: test.txt\n content: {{.key}}"), 0600)
cx.f.WriteFile("/data/1.yml", []byte("key: second"), 0600)
cx := SetupTestContext(t)
cx.fs.WriteFile("/working/.caster.yml", []byte("files:\n- name: test.txt\n content: {{.key}}"), 0600)
cx.fs.WriteFile("/data/1.yml", []byte("key: second"), 0600)

args := []string{"caster", "interpolate", "--var", "key=first", "--var-file", "/data/1.yml"}
cx.app.Metadata[global.OSArgs] = args
err := cx.app.Run(args)
require.NoError(t, err)

buf, ok := cx.con.Out().(*bytes.Buffer)
buf, ok := cx.console.Out().(*bytes.Buffer)
require.True(t, ok)
want := `files:
- name: test.txt
Expand All @@ -166,51 +150,3 @@ func TestInterpolate(t *testing.T) {
require.Equal(t, want, have, cmp.Diff(have, want))
})
}

func SetupInterpolateTestContext(t *testing.T) *InterpolateTestContext {
var err error
s := setup.NewTest()
container := s.Container()

o, err := di.Resolve[os.OS](container)
require.NoError(t, err)

wd, err := o.WorkingDirectory()
require.NoError(t, err)

con, err := di.Resolve[console.Console](container)
require.NoError(t, err)

f, err := di.Resolve[fs.FS](container)
require.NoError(t, err)

paths := []string{"/", "/template", "/data", wd, o.Home()}
for _, p := range paths {
err = f.MkdirAll(p, 0666)
require.NoError(t, err)
}

e, err := di.Resolve[env.Environment](container)
require.NoError(t, err)

app := &cli.App{
Version: "1.0.0",
Metadata: map[string]interface{}{
global.DependencyInjectionContainer: container,
},
Commands: []*cli.Command{
commands.Interpolate,
},
Reader: con.In(),
ErrWriter: con.Error(),
Writer: con.Out(),
}

return &InterpolateTestContext{
app: app,
container: container,
con: con,
f: f,
e: e,
}
}
Loading

0 comments on commit c27e306

Please sign in to comment.