github.com/wallyqs/gcli@v0.2.3-0.20151010121825-a114d5d1758d/tests/command_framework_test.go (about)

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