github.imxd.top/gopinath-langote/1build@v1.2.0/testing/fixtures/command_root_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  	"testing"
     8  )
     9  
    10  func FeatureRootTestData() []Test {
    11  	feature := "root"
    12  
    13  	return []Test{
    14  		shouldFailIfYamlFileIsNotPresent(feature),
    15  		shouldFailIfYamlFileIsNotInCorrectYamlFormat(feature),
    16  		shouldShowListOfCommandsIfNoCommandSpecified(feature),
    17  	}
    18  }
    19  
    20  func shouldFailIfYamlFileIsNotPresent(feature string) Test {
    21  	return Test{
    22  		Feature: feature,
    23  		Name:    "shouldFailIfYamlFileIsNotPresent",
    24  		CmdArgs: []string{},
    25  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    26  			return assert.Contains(t, actualOutput, "no '"+def.ConfigFileName+"' file found in current directory")
    27  		},
    28  	}
    29  }
    30  
    31  func shouldFailIfYamlFileIsNotInCorrectYamlFormat(feature string) Test {
    32  	erroredFileMessage :=
    33  		`Unable to parse '1build.yaml' config file. Make sure file is in correct format.
    34  Sample format is:
    35  
    36  --------------------------------------------------
    37  project: Sample Project
    38  commands:
    39    - build: npm run build
    40    - lint: eslint
    41  --------------------------------------------------
    42  `
    43  	return Test{
    44  		Feature: feature,
    45  		Name:    "shouldFailIfYamlFileIsNotInCorrectYamlFormat",
    46  		CmdArgs: []string{},
    47  		Setup: func(dir string) error {
    48  			return utils.CreateConfigFile(dir, "invalid yaml file")
    49  		},
    50  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    51  			return assert.Contains(t, actualOutput, erroredFileMessage)
    52  		},
    53  	}
    54  }
    55  
    56  func shouldShowListOfCommandsIfNoCommandSpecified(feature string) Test {
    57  	commandListMessage := `--------------------------------------------------
    58  project: Sample Project
    59  commands:
    60  build | npm run build
    61  lint | eslint
    62  --------------------------------------------------
    63  `
    64  	defaultFileContent := `
    65  project: Sample Project
    66  commands:
    67    - build: npm run build
    68    - lint: eslint
    69  `
    70  
    71  	return Test{
    72  		Feature: feature,
    73  		Name:    "shouldShowListOfCommandsIfNoCommandSpecified",
    74  		CmdArgs: []string{},
    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, commandListMessage)
    80  		},
    81  	}
    82  }