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

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  	"strconv"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestNew_command_frameworks(t *testing.T) {
    15  
    16  	tests := []struct {
    17  		framework string
    18  		expectOut string
    19  	}{
    20  		{
    21  			framework: "codegangsta_cli",
    22  			expectOut: "[global options] command [command options] [arguments...]",
    23  		},
    24  		{
    25  			framework: "mitchellh_cli",
    26  			expectOut: "[--version] [--help] <command> [<args>]",
    27  		},
    28  		{
    29  			framework: "go_cmd",
    30  			expectOut: "help [command]\" for more information about a command.",
    31  		},
    32  	}
    33  
    34  	owner := "awesome_user_" + strconv.Itoa(int(time.Now().Unix()))
    35  	cleanFunc, err := chdirSrcPath(owner)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	defer cleanFunc()
    40  
    41  	for _, tt := range tests {
    42  
    43  		artifactBin := fmt.Sprintf("%s_todo", tt.framework)
    44  		args := []string{
    45  			"new",
    46  			"-framework", tt.framework,
    47  			"-owner", owner,
    48  			"-flag=verbose:bool:'Run verbose mode'",
    49  			"-flag=username:string:'Username'",
    50  			"-command=add:'Add new task'",
    51  			"-command=list:'List tasks'",
    52  			"-command=delete:'Delete specified task'",
    53  			artifactBin,
    54  		}
    55  
    56  		output, err := runGcli(args)
    57  		if err != nil {
    58  			t.Fatal(err)
    59  		}
    60  
    61  		expect := "Successfully generated"
    62  		if !strings.Contains(output, expect) {
    63  			t.Fatalf("[%s] expect %q to contain %q", tt.framework, output, expect)
    64  		}
    65  
    66  		if err := goTests(artifactBin); err != nil {
    67  			t.Fatal(err)
    68  		}
    69  
    70  		if err := os.Chdir(artifactBin); err != nil {
    71  			t.Fatal(err)
    72  		}
    73  
    74  		var stdout, stderr bytes.Buffer
    75  		cmd := exec.Command("./" + artifactBin)
    76  		cmd.Stderr = &stderr
    77  		cmd.Stdout = &stdout
    78  
    79  		// cmd.Wait() returns error
    80  		_ = cmd.Run()
    81  
    82  		output = stdout.String() + stderr.String()
    83  		// t.Logf("%s \n\n%s", tt.framework, output)
    84  		if !strings.Contains(output, tt.expectOut) {
    85  			t.Errorf("[%s] expects %q to contain %q", tt.framework, output, tt.expectOut)
    86  		}
    87  
    88  		// Back to src directory
    89  		if err := os.Chdir(".."); err != nil {
    90  			t.Fatal(err)
    91  		}
    92  	}
    93  }