github.com/theliebeskind/genfig@v0.1.5-alpha/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  var (
    14  	fixturesDir, _ = filepath.Abs("./fixtures")
    15  	origArgs       = os.Args
    16  )
    17  
    18  func Test_run(t *testing.T) {
    19  	dir, _ := ioutil.TempDir("", "genfig")
    20  	out, _ := ioutil.TempDir("", "genfig")
    21  	defer os.RemoveAll(dir)
    22  	defer os.RemoveAll(out)
    23  
    24  	cwd, _ := os.Getwd()
    25  	defer os.Chdir(cwd)
    26  	err := os.Chdir(dir)
    27  	require.NoError(t, err)
    28  
    29  	configsDir := filepath.Join(fixturesDir, "configs")
    30  
    31  	tests := []struct {
    32  		name        string
    33  		args        []string
    34  		shouldPanic bool
    35  	}{
    36  		{"no args", []string{}, true},
    37  		{"version", []string{"--version"}, false},
    38  		{"help", []string{"--help"}, false},
    39  		{"without dir, no config files", []string{"*"}, true},
    40  		{"with dir, no config files", []string{"-dir", out, "*"}, true},
    41  		{"without dir, valid config files", []string{configsDir + "/default.yml", configsDir + "/development.yml"}, false},
    42  		{"with dir, valid config files", []string{"-dir", out, configsDir + "/default.yml", configsDir + "/development.yml"}, false},
    43  	}
    44  	for _, tt := range tests {
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			flag.CommandLine = flag.NewFlagSet(tt.name, flag.ContinueOnError)
    47  			args := append(origArgs[:1], tt.args...)
    48  			os.Args = args
    49  			if tt.shouldPanic {
    50  				require.Panics(t, run)
    51  			} else {
    52  				require.NotPanics(t, run)
    53  			}
    54  			os.Args = origArgs
    55  		})
    56  	}
    57  }