github.com/tcnksm/gcli@v0.2.4-0.20170129033839-7eb950507e5a/command/design_test.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/BurntSushi/toml"
    12  	"github.com/mitchellh/cli"
    13  	"github.com/tcnksm/gcli/skeleton"
    14  )
    15  
    16  func TestDesignCommand_implement(t *testing.T) {
    17  	var _ cli.Command = &DesignCommand{}
    18  }
    19  
    20  func TestDesignCommand(t *testing.T) {
    21  	ui := new(cli.MockUi)
    22  	c := &DesignCommand{
    23  		Meta: Meta{
    24  			UI: ui,
    25  		},
    26  	}
    27  
    28  	// Create temp directory to output file
    29  	tmpDir, err := ioutil.TempDir("", "design_command")
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	backFunc, err := TmpChdir(tmpDir)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	defer backFunc()
    39  
    40  	name := "todo"
    41  	if code := c.Run([]string{name}); code != 0 {
    42  		t.Fatalf("bad status code: %d\n\n%s", code, ui.ErrorWriter.String())
    43  	}
    44  
    45  	// Inspect generated file
    46  	outputFile := filepath.Join(tmpDir, fmt.Sprintf(defaultOutputFmt, name))
    47  	executable := skeleton.NewExecutable()
    48  
    49  	if _, err := toml.DecodeFile(outputFile, executable); err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	if executable.Name != name {
    54  		t.Errorf("expects %q to be eq %q", executable.Name, name)
    55  	}
    56  
    57  	if executable.Version != skeleton.DefaultVersion {
    58  		t.Errorf("expects %q to be eq %q", executable.Version, skeleton.DefaultVersion)
    59  	}
    60  
    61  	if executable.FrameworkStr != defaultFrameworkString {
    62  		t.Errorf("expects %q to be eq %q", executable.FrameworkStr, defaultFrameworkString)
    63  	}
    64  }
    65  
    66  func TestDesignCommand_fileExist(t *testing.T) {
    67  	ui := new(cli.MockUi)
    68  	c := &DesignCommand{
    69  		Meta: Meta{
    70  			UI: ui,
    71  		},
    72  	}
    73  
    74  	// Create temp directory to output file
    75  	tmpDir, err := ioutil.TempDir("", "design-command")
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	backFunc, err := TmpChdir(tmpDir)
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	defer backFunc()
    85  
    86  	name := fmt.Sprintf(defaultOutputFmt, "todo")
    87  	if _, err := os.Create(name); err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	if code := c.Run([]string{"todo"}); code != 1 {
    92  		t.Fatalf("bad status code: %d", code)
    93  	}
    94  
    95  	output := ui.ErrorWriter.String()
    96  	expect := fmt.Sprintf("Cannot create design file %s: file exists", name)
    97  	if !strings.Contains(output, expect) {
    98  		t.Errorf("expect %q to contain %q", output, expect)
    99  	}
   100  }