github.com/jduhamel/gcli@v0.2.4-0.20151019142748-0d5307cd7e21/tests/flag_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_flag_frameworks(t *testing.T) {
    18  
    19  	tests := []struct {
    20  		framework string
    21  		expectOut string
    22  	}{
    23  		{
    24  			framework: "flag",
    25  			expectOut: "Usage of ",
    26  		},
    27  	}
    28  
    29  	owner := "awesome_user_" + strconv.Itoa(int(time.Now().Unix()))
    30  	cleanFunc, err := chdirSrcPath(owner)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	defer cleanFunc()
    35  
    36  	for _, tt := range tests {
    37  
    38  		artifactBin := fmt.Sprintf("%s_grep", tt.framework)
    39  		args := []string{
    40  			"new",
    41  			"-framework", tt.framework,
    42  			"-owner", owner,
    43  			"-flag=ignore-case:Bool:'Perform case insensitive matching'",
    44  			"-flag=context:Int:'Print num lines of leading and trailing context'",
    45  			artifactBin,
    46  		}
    47  
    48  		output, err := runGcli(args)
    49  		if err != nil {
    50  			t.Fatal(err)
    51  		}
    52  
    53  		expect := "Successfully generated"
    54  		if !strings.Contains(output, expect) {
    55  			t.Fatalf("[%s] expect %q to contain %q", tt.framework, output, expect)
    56  		}
    57  
    58  		// Check common files are generated
    59  		for _, tmpl := range skeleton.CommonTemplates {
    60  			// NOTE: OutputPathTmpl of common template is same as final output name
    61  			// and not changed by templating
    62  			if _, err := os.Stat(filepath.Join(artifactBin, tmpl.OutputPathTmpl)); os.IsNotExist(err) {
    63  				t.Fatalf("file is not exist: %s", tmpl.OutputPathTmpl)
    64  			}
    65  		}
    66  
    67  		if err := goTests(artifactBin); err != nil {
    68  			t.Fatal(err)
    69  		}
    70  
    71  		if err := os.Chdir(artifactBin); err != nil {
    72  			t.Fatal(err)
    73  		}
    74  
    75  		var stdout, stderr bytes.Buffer
    76  		cmd := exec.Command("./"+artifactBin, "-help")
    77  		cmd.Stderr = &stderr
    78  		cmd.Stdout = &stdout
    79  
    80  		// cmd.Wait() returns error
    81  		_ = cmd.Run()
    82  
    83  		output = stdout.String() + stderr.String()
    84  		// t.Logf("%s \n\n%s", tt.framework, output)
    85  		if !strings.Contains(output, tt.expectOut) {
    86  			t.Errorf("[%s] expects %q to contain %q", tt.framework, output, tt.expectOut)
    87  		}
    88  
    89  		// Back to src directory
    90  		if err := os.Chdir(".."); err != nil {
    91  			t.Fatal(err)
    92  		}
    93  	}
    94  }