github.com/maheshbr/terraform@v0.3.1-0.20141020033300-deec7194a3ea/command/init_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/mitchellh/cli"
     9  )
    10  
    11  func TestInit(t *testing.T) {
    12  	dir := tempDir(t)
    13  
    14  	ui := new(cli.MockUi)
    15  	c := &InitCommand{
    16  		Meta: Meta{
    17  			ContextOpts: testCtxConfig(testProvider()),
    18  			Ui:          ui,
    19  		},
    20  	}
    21  
    22  	args := []string{
    23  		testFixturePath("init"),
    24  		dir,
    25  	}
    26  	if code := c.Run(args); code != 0 {
    27  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    28  	}
    29  
    30  	if _, err := os.Stat(filepath.Join(dir, "hello.tf")); err != nil {
    31  		t.Fatalf("err: %s", err)
    32  	}
    33  }
    34  
    35  func TestInit_cwd(t *testing.T) {
    36  	dir := tempDir(t)
    37  	if err := os.MkdirAll(dir, 0755); err != nil {
    38  		t.Fatalf("err: %s", err)
    39  	}
    40  
    41  	// Change to the temporary directory
    42  	cwd, err := os.Getwd()
    43  	if err != nil {
    44  		t.Fatalf("err: %s", err)
    45  	}
    46  	if err := os.Chdir(dir); err != nil {
    47  		t.Fatalf("err: %s", err)
    48  	}
    49  	defer os.Chdir(cwd)
    50  
    51  	ui := new(cli.MockUi)
    52  	c := &InitCommand{
    53  		Meta: Meta{
    54  			ContextOpts: testCtxConfig(testProvider()),
    55  			Ui:          ui,
    56  		},
    57  	}
    58  
    59  	args := []string{
    60  		testFixturePath("init"),
    61  	}
    62  	if code := c.Run(args); code != 0 {
    63  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    64  	}
    65  
    66  	if _, err := os.Stat("hello.tf"); err != nil {
    67  		t.Fatalf("err: %s", err)
    68  	}
    69  }
    70  
    71  func TestInit_multipleArgs(t *testing.T) {
    72  	ui := new(cli.MockUi)
    73  	c := &InitCommand{
    74  		Meta: Meta{
    75  			ContextOpts: testCtxConfig(testProvider()),
    76  			Ui:          ui,
    77  		},
    78  	}
    79  
    80  	args := []string{
    81  		"bad",
    82  		"bad",
    83  	}
    84  	if code := c.Run(args); code != 1 {
    85  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
    86  	}
    87  }
    88  
    89  func TestInit_noArgs(t *testing.T) {
    90  	ui := new(cli.MockUi)
    91  	c := &InitCommand{
    92  		Meta: Meta{
    93  			ContextOpts: testCtxConfig(testProvider()),
    94  			Ui:          ui,
    95  		},
    96  	}
    97  
    98  	args := []string{}
    99  	if code := c.Run(args); code != 1 {
   100  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
   101  	}
   102  }