-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7d4996
commit c27e306
Showing
5 changed files
with
170 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package commands_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.