github.com/alvr/restinmock@v0.1.0/cmd/app_test.go (about) 1 package cmd 2 3 import ( 4 "os" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func Test_ConfigName_NoArgs(t *testing.T) { 12 var args []string 13 14 configName := configName(args) 15 16 assert.Equal(t, defaultConfigFile, configName) 17 } 18 19 func Test_ConfigName_OneArgs(t *testing.T) { 20 args := []string{"config.yml"} 21 22 configName := configName(args) 23 24 assert.Equal(t, "config.yml", configName) 25 assert.NotEqual(t, defaultConfigFile, configName) 26 } 27 28 func Test_ConfigName_MultipleArgs(t *testing.T) { 29 args := []string{"config2.yml", "another", "argument"} 30 31 configName := configName(args) 32 33 assert.Equal(t, "config2.yml", configName) 34 assert.NotEqual(t, defaultConfigFile, configName) 35 } 36 37 func Test_Execute_NoConfigSpecified_FileDoesNotExists(t *testing.T) { 38 args := []string{"restinmock"} 39 40 assert.EqualError(t, Execute(args), "configuration file rim.yml could not be found") 41 } 42 43 func Test_Execute_NoConfigSpecified_FileExists(t *testing.T) { 44 args := []string{"restinmock"} 45 46 _ = os.Chdir("..") 47 48 stopServer(t) 49 assert.NoError(t, Execute(args)) 50 } 51 52 func Test_Execute_OneArgs(t *testing.T) { 53 args := []string{"restinmock", "./testdata/default.yml"} 54 55 stopServer(t) 56 assert.NoError(t, Execute(args)) 57 } 58 59 func Test_Execute_MultipleArgs(t *testing.T) { 60 args := []string{"restinmock", "./testdata/empty.yml", "another", "argument"} 61 62 stopServer(t) 63 assert.NoError(t, Execute(args)) 64 } 65 66 func Test_Execute_InvalidConfiguration(t *testing.T) { 67 args := []string{"restinmock", "./testdata/invalid_data.yml", "another", "argument"} 68 69 assert.NotNil(t, Execute(args)) 70 assert.Regexp(t, "error parsing configuration file*", Execute(args)) 71 } 72 73 func Test_Execute_InvalidMocksDir(t *testing.T) { 74 args := []string{"restinmock", "./testdata/invalid_mocksdir.yml"} 75 76 assert.EqualError(t, Execute(args), "directory what does not exists. Create the folder first") 77 } 78 79 func stopServer(t *testing.T) { 80 time.AfterFunc(100*time.Millisecond, func() { 81 process, err := os.FindProcess(os.Getpid()) 82 assert.NoError(t, err) 83 assert.NoError(t, process.Signal(os.Interrupt)) 84 }) 85 }