github.com/woremacx/kocha@v0.7.1-0.20150731103243-a5889322afc9/cmd/kocha-new/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"go/build"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"reflect"
    10  	"sort"
    11  	"testing"
    12  )
    13  
    14  func Test_newCommand_Run_withNoAPPPATHGiven(t *testing.T) {
    15  	c := &newCommand{}
    16  	args := []string{}
    17  	err := c.Run(args)
    18  	var actual interface{} = err
    19  	var expect interface{} = fmt.Errorf("no APP_PATH given")
    20  	if !reflect.DeepEqual(actual, expect) {
    21  		t.Errorf(`run(%#v) => %#v; want %#v`, args, actual, expect)
    22  	}
    23  }
    24  
    25  func Test_newCommand_Run_withAlreadyExists(t *testing.T) {
    26  	tempdir, err := ioutil.TempDir("", "TestRunWithAlreadyExists")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	defer os.RemoveAll(tempdir)
    31  	appPath := filepath.Base(tempdir)
    32  	dstPath := filepath.Join(tempdir, "src", appPath)
    33  	configDir := filepath.Join(dstPath, "config")
    34  	if err := os.MkdirAll(configDir, 0755); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	if err := ioutil.WriteFile(filepath.Join(configDir, "app.go"), nil, 0644); err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	origGOPATH := build.Default.GOPATH
    41  	defer func() {
    42  		build.Default.GOPATH = origGOPATH
    43  	}()
    44  	build.Default.GOPATH = tempdir + string(filepath.ListSeparator) + build.Default.GOPATH
    45  	c := &newCommand{}
    46  	args := []string{appPath}
    47  	err = c.Run(args)
    48  	var actual interface{} = err
    49  	var expect interface{} = fmt.Errorf("Kocha application is already exists")
    50  	if !reflect.DeepEqual(actual, expect) {
    51  		t.Errorf(`run(%#v) => %#v; want %#v`, args, actual, expect)
    52  	}
    53  }
    54  
    55  func Test_newCommand_Run(t *testing.T) {
    56  	tempdir, err := ioutil.TempDir("", "Test_newRun")
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	defer os.RemoveAll(tempdir)
    61  	appPath := filepath.Base(tempdir)
    62  	dstPath := filepath.Join(tempdir, "src", appPath)
    63  	f, err := os.OpenFile(os.DevNull, os.O_WRONLY, os.ModePerm)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	defer f.Close()
    68  	oldStdout, oldStderr := os.Stdout, os.Stderr
    69  	os.Stdout, os.Stderr = f, f
    70  	defer func() {
    71  		os.Stdout, os.Stderr = oldStdout, oldStderr
    72  	}()
    73  	origGOPATH := build.Default.GOPATH
    74  	defer func() {
    75  		build.Default.GOPATH = origGOPATH
    76  	}()
    77  	build.Default.GOPATH = tempdir + string(filepath.ListSeparator) + build.Default.GOPATH
    78  	c := &newCommand{}
    79  	args := []string{appPath}
    80  	err = c.Run(args)
    81  	var actual interface{} = err
    82  	var expect interface{} = nil
    83  	if !reflect.DeepEqual(actual, expect) {
    84  		t.Errorf(`run(%#v) => %#v; want %#v`, args, actual, expect)
    85  	}
    86  
    87  	var actuals []string
    88  	filepath.Walk(dstPath, func(path string, info os.FileInfo, err error) error {
    89  		if err != nil {
    90  			panic(err)
    91  		}
    92  		if info.IsDir() {
    93  			return nil
    94  		}
    95  		actuals = append(actuals, path)
    96  		return nil
    97  	})
    98  	expects := []string{
    99  		filepath.Join("main.go"),
   100  		filepath.Join("app", "controller", "root.go"),
   101  		filepath.Join("app", "view", "error", "404.html.tmpl"),
   102  		filepath.Join("app", "view", "error", "500.html.tmpl"),
   103  		filepath.Join("app", "view", "layout", "app.html.tmpl"),
   104  		filepath.Join("app", "view", "root.html.tmpl"),
   105  		filepath.Join("config", "app.go"),
   106  		filepath.Join("config", "routes.go"),
   107  		filepath.Join("public", "robots.txt"),
   108  	}
   109  	sort.Strings(actuals)
   110  	sort.Strings(expects)
   111  	for i, _ := range actuals {
   112  		actual := actuals[i]
   113  		expected := filepath.Join(dstPath, expects[i])
   114  		if !reflect.DeepEqual(actual, expected) {
   115  			t.Errorf(`run(%#v); generated file => %#v; want %#v`, args, actual, expected)
   116  		}
   117  	}
   118  }