github.com/gopinath-langote/1build@v1.7.0/testing/fixtures/command_init_fixtures.go (about)

     1  package fixtures
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/gopinath-langote/1build/testing/def"
     9  	"github.com/gopinath-langote/1build/testing/utils"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func featureInitTestsData() []Test {
    14  	feature := "init"
    15  	return []Test{
    16  		shouldInitialiseNewProject(feature),
    17  		shouldInitialiseNewProjectInSpecifiedFile(feature),
    18  		shouldFailIfFileAlreadyExists(feature),
    19  	}
    20  }
    21  
    22  func shouldInitialiseNewProject(feature string) Test {
    23  	expectedOutput := `project: trial
    24  commands:
    25    - build: echo 'Running build'
    26  `
    27  	return Test{
    28  		Feature: feature,
    29  		Name:    "shouldInitialiseNewProject",
    30  		CmdArgs: Args("init", "--name", "trial"),
    31  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    32  			filePath := dir + "/" + def.ConfigFileName
    33  			assert.FileExists(t, dir+"/"+def.ConfigFileName)
    34  			content, _ := ioutil.ReadFile(filePath)
    35  			return assert.Contains(t, string(content), expectedOutput)
    36  		},
    37  	}
    38  }
    39  
    40  func shouldInitialiseNewProjectInSpecifiedFile(feature string) Test {
    41  	expectedOutput := `project: trial
    42  commands:
    43    - build: echo 'Running build'
    44  `
    45  	return Test{
    46  		Feature: feature,
    47  		Name:    "shouldInitialiseNewProjectInSpecifiedFile",
    48  		CmdArgs: func(dir string) []string {
    49  			return []string{"init", "--name", "trial", "-f", dir + "/some-dir/some-config.yaml"}
    50  		},
    51  		Setup: func(dir string) error {
    52  			return os.MkdirAll(dir+"/some-dir/", 0750)
    53  		},
    54  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    55  			filePath := dir + "/some-dir/some-config.yaml"
    56  			assert.FileExists(t, filePath)
    57  			content, _ := ioutil.ReadFile(filePath)
    58  			return assert.Contains(t, string(content), expectedOutput)
    59  		},
    60  	}
    61  }
    62  
    63  func shouldFailIfFileAlreadyExists(feature string) Test {
    64  	defaultFileContent := `
    65  project: Sample Project
    66  commands:
    67    - build: npm run build
    68    - lint: eslint
    69  `
    70  	expectedOutput := "'" + def.ConfigFileName + "' configuration file already exists."
    71  	return Test{
    72  		Feature: feature,
    73  		Name:    "shouldFailIfFileAlreadyExists",
    74  		CmdArgs: Args("init", "--name", "trial"),
    75  		Setup: func(dir string) error {
    76  			return utils.CreateConfigFile(dir, defaultFileContent)
    77  		},
    78  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    79  			return assert.Contains(t, actualOutput, expectedOutput)
    80  		},
    81  	}
    82  }