github.imxd.top/gopinath-langote/1build@v1.2.0/testing/fixtures/command_init_fixtures.go (about) 1 package fixtures 2 3 import ( 4 "github.com/gopinath-langote/1build/testing/def" 5 "github.com/gopinath-langote/1build/testing/utils" 6 "github.com/stretchr/testify/assert" 7 "io/ioutil" 8 "testing" 9 ) 10 11 func FeatureInitTestsData() []Test { 12 feature := "init" 13 return []Test{ 14 shouldInitialiseNewProject(feature), 15 shouldFailIfFileAlreadyExists(feature), 16 } 17 } 18 19 func shouldInitialiseNewProject(feature string) Test { 20 expectedOutput := `project: trial 21 commands: 22 - build: echo 'Running build' 23 ` 24 return Test{ 25 Feature: feature, 26 Name: "shouldInitialiseNewProject", 27 CmdArgs: []string{"init", "--name", "trial"}, 28 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 29 filePath := dir + "/" + def.ConfigFileName 30 assert.FileExists(t, dir+"/"+def.ConfigFileName) 31 content, _ := ioutil.ReadFile(filePath) 32 return assert.Contains(t, string(content), expectedOutput) 33 }, 34 } 35 } 36 37 func shouldFailIfFileAlreadyExists(feature string) Test { 38 defaultFileContent := ` 39 project: Sample Project 40 commands: 41 - build: npm run build 42 - lint: eslint 43 ` 44 expectedOutput := "'" + def.ConfigFileName + "' configuration file already exists." 45 return Test{ 46 Feature: feature, 47 Name: "shouldFailIfFileAlreadyExists", 48 CmdArgs: []string{"init", "--name", "trial"}, 49 Setup: func(dir string) error { 50 return utils.CreateConfigFile(dir, defaultFileContent) 51 }, 52 Assertion: func(dir string, actualOutput string, t *testing.T) bool { 53 return assert.Contains(t, actualOutput, expectedOutput) 54 }, 55 } 56 }