2014-09-27 01:03:39 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-09-27 01:30:49 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-09-27 01:03:39 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2014-09-27 01:30:49 +02:00
|
|
|
func TestInit(t *testing.T) {
|
|
|
|
dir := tempDir(t)
|
|
|
|
|
2014-09-27 01:03:39 +02:00
|
|
|
ui := new(cli.MockUi)
|
2014-09-27 01:30:49 +02:00
|
|
|
c := &InitCommand{
|
2014-09-27 01:03:39 +02:00
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2014-09-27 01:30:49 +02:00
|
|
|
testFixturePath("init"),
|
|
|
|
dir,
|
2014-09-27 01:03:39 +02:00
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2014-09-27 01:30:49 +02:00
|
|
|
if _, err := os.Stat(filepath.Join(dir, "hello.tf")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
2014-09-27 01:03:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 18:23:02 +02:00
|
|
|
func TestInit_cwd(t *testing.T) {
|
|
|
|
dir := tempDir(t)
|
|
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change to the temporary directory
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(dir); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
testFixturePath("init"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat("hello.tf"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 01:03:39 +02:00
|
|
|
func TestInit_multipleArgs(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"bad",
|
|
|
|
"bad",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInit_noArgs(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 21:19:39 +01:00
|
|
|
|
|
|
|
// https://github.com/hashicorp/terraform/issues/518
|
2014-11-20 19:17:04 +01:00
|
|
|
func TestInit_dstInSrc(t *testing.T) {
|
2014-11-04 21:19:39 +01:00
|
|
|
dir := tempDir(t)
|
|
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change to the temporary directory
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(dir); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
if _, err := os.Create("issue518.tf"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &InitCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
".",
|
|
|
|
"foo",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(dir, "foo", "issue518.tf")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|