github.com/goonzoid/gcli@v0.2.3-0.20150926213610-155587606ea1/command/new_test.go (about)

     1  package command
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  func TestNewCommand_implement(t *testing.T) {
    13  	var _ cli.Command = &NewCommand{}
    14  }
    15  
    16  func TestNewCommand(t *testing.T) {
    17  	ui := new(cli.MockUi)
    18  	c := &NewCommand{
    19  		Meta: Meta{
    20  			UI: ui,
    21  		},
    22  	}
    23  
    24  	// Create temp directory to output file
    25  	tmpDir, err := ioutil.TempDir("", "new-command")
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	backFunc, err := TmpChdir(tmpDir)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	defer backFunc()
    35  
    36  	args := []string{"-F", "mitchellh_cli", "-owner", "deeeet", "todo"}
    37  	if code := c.Run(args); code != 0 {
    38  		t.Fatalf("bad status code: %d\n\n%s", code, ui.ErrorWriter.String())
    39  	}
    40  
    41  	// TODO, inspect generated files
    42  }
    43  
    44  func TestNewCommand_directoryExist(t *testing.T) {
    45  	ui := new(cli.MockUi)
    46  	c := &NewCommand{
    47  		Meta: Meta{
    48  			UI: ui,
    49  		},
    50  	}
    51  
    52  	// Create temp directory to output file
    53  	tmpDir, err := ioutil.TempDir("", "new-command")
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	backFunc, err := TmpChdir(tmpDir)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	defer backFunc()
    63  
    64  	// Create `todo` directory, same name as
    65  	// application which is generated later step
    66  	if err := os.Mkdir("todo", 0777); err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	args := []string{"todo"}
    71  	if code := c.Run(args); code != 1 {
    72  		t.Fatalf("bad status code: %d", code)
    73  	}
    74  
    75  	output := ui.ErrorWriter.String()
    76  	expect := "Cannot create directory todo: file exists"
    77  	if !strings.Contains(output, expect) {
    78  		t.Errorf("expect %q to contain %q", output, expect)
    79  	}
    80  }