github.imxd.top/gopinath-langote/1build@v1.2.0/testing/fixtures/command_unset_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 FeatureUnsetTestsData() []Test {
    12  	feature := "unset"
    13  
    14  	return []Test{
    15  		shouldUnsetTheExistingCommand(feature),
    16  		UnsetShouldFailWhenConfigurationFileIsNotFound(feature),
    17  		UnsetShouldFailWhenConfigurationFileIsInInvalidFormat(feature),
    18  		UnsetShouldFailWhenCommandIsNotFound(feature),
    19  	}
    20  }
    21  
    22  func shouldUnsetTheExistingCommand(feature string) Test {
    23  
    24  	defaultFileContent := `
    25  project: Sample Project
    26  commands:
    27    - build: go build
    28  `
    29  
    30  	expectedOutput := `project: Sample Project
    31  commands: []
    32  `
    33  
    34  	return Test{
    35  		Feature: feature,
    36  		Name:    "shouldUnsetTheExistingCommand",
    37  		CmdArgs: []string{"unset", "build"},
    38  		Setup: func(dir string) error {
    39  			return utils.CreateConfigFile(dir, defaultFileContent)
    40  		},
    41  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    42  			filePath := dir + "/" + def.ConfigFileName
    43  			assert.FileExists(t, dir+"/"+def.ConfigFileName)
    44  			content, _ := ioutil.ReadFile(filePath)
    45  			return assert.Exactly(t, expectedOutput, string(content))
    46  		},
    47  	}
    48  }
    49  
    50  func UnsetShouldFailWhenCommandIsNotFound(feature string) Test {
    51  
    52  	defaultFileContent := `
    53  project: Sample Project
    54  commands:
    55    - build: go build
    56  `
    57  
    58  	return Test{
    59  		Feature: feature,
    60  		Name:    "UnsetShouldFailWhenCommandIsNotFound",
    61  		CmdArgs: []string{"unset", "test"},
    62  		Setup: func(dir string) error {
    63  			return utils.CreateConfigFile(dir, defaultFileContent)
    64  		},
    65  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    66  			return assert.Contains(t, actualOutput, "Command 'test' not found")
    67  		},
    68  	}
    69  }
    70  
    71  func UnsetShouldFailWhenConfigurationFileIsNotFound(feature string) Test {
    72  	return Test{
    73  		Feature: feature,
    74  		Name:    "UnsetShouldFailWhenConfigurationFileIsNotFound",
    75  		CmdArgs: []string{"unset", "build"},
    76  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    77  			return assert.Contains(t, actualOutput, "no '"+def.ConfigFileName+"' file found in current directory")
    78  		},
    79  	}
    80  }
    81  
    82  func UnsetShouldFailWhenConfigurationFileIsInInvalidFormat(feature string) Test {
    83  	return Test{
    84  		Feature: feature,
    85  		Name:    "UnsetShouldFailWhenConfigurationFileIsInInvalidFormat",
    86  		CmdArgs: []string{"unset", "build"},
    87  		Setup: func(dir string) error {
    88  			return utils.CreateConfigFile(dir, "invalid config content")
    89  		},
    90  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    91  			return assert.Contains(t, actualOutput, "Unable to parse '"+def.ConfigFileName+"' config file. Make sure file is in correct format.")
    92  		},
    93  	}
    94  }