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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strconv"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  var flagTests = []struct {
    14  	framework  string
    15  	args       []string
    16  	expectHelp string
    17  }{
    18  	{
    19  		framework:  "flag",
    20  		args:       []string{"-h"},
    21  		expectHelp: "Usage of ",
    22  	},
    23  }
    24  
    25  func TestNew_flag(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	vcsHost := "github.com"
    29  	owner := "awesome_user_" + strconv.Itoa(int(time.Now().Unix()))
    30  
    31  	gopath, cleanFunc, err := tmpGopath()
    32  	if err != nil {
    33  		t.Fatalf("err: %s", err)
    34  	}
    35  	defer cleanFunc()
    36  
    37  	baseDir := filepath.Join(gopath, "src", vcsHost, owner)
    38  	if err := os.MkdirAll(baseDir, 0777); err != nil {
    39  		t.Fatalf("err: %s", err)
    40  	}
    41  
    42  	for _, tt := range flagTests {
    43  
    44  		name := fmt.Sprintf("%s_grep", tt.framework)
    45  		args := []string{
    46  			"new",
    47  			"-framework", tt.framework,
    48  			"-owner", owner,
    49  			"-flag=ignore-case:Bool:'Perform case insensitive matching'",
    50  			"-flag=context:Int:'Print num lines of leading and trailing context'",
    51  			name,
    52  		}
    53  
    54  		output, err := runGcli(baseDir, gopath, args)
    55  		if err != nil {
    56  			t.Fatalf("[%s] expects %s to be nil", tt.framework, err)
    57  		}
    58  
    59  		expectWarn := "WARNING: You are not in the directory gcli expects."
    60  		if strings.Contains(output, expectWarn) {
    61  			t.Fatalf("[%s] expects output not to contain %q", tt.framework, expectWarn)
    62  		}
    63  
    64  		expect := "Successfully generated"
    65  		if !strings.Contains(output, expect) {
    66  			t.Fatalf("[%s] expects output to contain %q", tt.framework, expect)
    67  		}
    68  	}
    69  }
    70  
    71  // TestNew_flag_goflag tests that the generated project
    72  // is go-buildable and it passes the go-test and go-vet.
    73  func TestNew_flag_gotests(t *testing.T) {
    74  	vcsHost := "github.com"
    75  	owner := "awesome_user_" + strconv.Itoa(int(time.Now().Unix()))
    76  
    77  	gopath, cleanFunc, err := tmpGopath()
    78  	if err != nil {
    79  		t.Fatalf("err: %s", err)
    80  	}
    81  	defer cleanFunc()
    82  
    83  	baseDir := filepath.Join(gopath, "src", vcsHost, owner)
    84  	if err := os.MkdirAll(baseDir, 0777); err != nil {
    85  		t.Fatalf("err: %s", err)
    86  	}
    87  
    88  	for _, tt := range flagTests {
    89  
    90  		name := fmt.Sprintf("%s_grep", tt.framework)
    91  		args := []string{
    92  			"new",
    93  			"-framework", tt.framework,
    94  			"-owner", owner,
    95  			"-flag=ignore-case:Bool:'Perform case insensitive matching'",
    96  			"-flag=context:Int:'Print num lines of leading and trailing context'",
    97  			name,
    98  		}
    99  
   100  		if _, err := runGcli(baseDir, gopath, args); err != nil {
   101  			t.Fatalf("err: %s", err)
   102  		}
   103  
   104  		if err := goTests(filepath.Join(baseDir, name), gopath); err != nil {
   105  			t.Fatalf("[%s] expects generated project to pass all go tests: \n\n %s", tt.framework, err)
   106  		}
   107  
   108  		// Also run executable and check its output.
   109  		// This test should be seaprated from this test.
   110  		// But it has costs to run go-get multiple times.
   111  		output := runExecutable(filepath.Join(baseDir, name, name), tt.args)
   112  		if !strings.Contains(output, tt.expectHelp) {
   113  			t.Errorf("[%s] expects %q to contain %q", tt.framework, output, tt.expectHelp)
   114  		}
   115  	}
   116  }