github.com/gopinath-langote/1build@v1.7.0/testing/fixtures/command_set_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  	"os"
     9  	"testing"
    10  )
    11  
    12  func featureSetTestsData() []Test {
    13  	feature := "set"
    14  
    15  	return []Test{
    16  		shouldSetNewCommand(feature),
    17  		shouldSetNewCommandInSpecifiedFile(feature),
    18  		shouldUpdateExistingCommand(feature),
    19  		shouldFailWhenConfigurationFileIsNotFound(feature),
    20  		shouldFailWhenConfigurationFileIsInInvalidFormat(feature),
    21  		shouldSetBeforeCommand(feature),
    22  		shouldSetAfterCommand(feature),
    23  	}
    24  }
    25  
    26  func shouldSetNewCommand(feature string) Test {
    27  
    28  	defaultFileContent := `
    29  project: Sample Project
    30  commands:
    31    - build: go build
    32  `
    33  
    34  	expectedOutput := `project: Sample Project
    35  commands:
    36    - build: go build
    37    - Test: go Test
    38  `
    39  
    40  	return Test{
    41  		Feature: feature,
    42  		Name:    "shouldSetNewCommand",
    43  		CmdArgs: Args("set", "Test", "go Test"),
    44  		Setup: func(dir string) error {
    45  			return utils.CreateConfigFile(dir, defaultFileContent)
    46  		},
    47  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    48  			filePath := dir + "/" + def.ConfigFileName
    49  			assert.FileExists(t, dir+"/"+def.ConfigFileName)
    50  			content, _ := ioutil.ReadFile(filePath)
    51  			return assert.Contains(t, string(content), expectedOutput)
    52  		},
    53  	}
    54  }
    55  
    56  func shouldSetNewCommandInSpecifiedFile(feature string) Test {
    57  
    58  	defaultFileContent := `
    59  project: Sample Project
    60  commands:
    61    - build: go build
    62  `
    63  
    64  	expectedOutput := `project: Sample Project
    65  commands:
    66    - build: go build
    67    - Test: go Test
    68  `
    69  
    70  	return Test{
    71  		Feature: feature,
    72  		Name:    "shouldSetNewCommandInSpecifiedFile",
    73  		CmdArgs: func(dir string) []string {
    74  			return []string{"set", "Test", "go Test", "-f", dir + "/some-dir/some-config.yaml"}
    75  		},
    76  		Setup: func(dir string) error {
    77  			_ = os.MkdirAll(dir+"/some-dir", 0750)
    78  			return utils.CreateConfigFileWithName(dir+"/some-dir", "some-config.yaml", defaultFileContent)
    79  		},
    80  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    81  			filePath := dir + "/some-dir/some-config.yaml"
    82  			assert.FileExists(t, filePath)
    83  			content, _ := ioutil.ReadFile(filePath)
    84  			return assert.Contains(t, string(content), expectedOutput)
    85  		},
    86  	}
    87  }
    88  
    89  func shouldUpdateExistingCommand(feature string) Test {
    90  
    91  	defaultFileContent := `
    92  project: Sample Project
    93  commands:
    94    - build: go build
    95  `
    96  
    97  	expectedOutput := `project: Sample Project
    98  commands:
    99    - build: go build -o
   100  `
   101  
   102  	return Test{
   103  		Feature: feature,
   104  		Name:    "shouldUpdateExistingCommand",
   105  		CmdArgs: Args("set", "build", "go build -o"),
   106  		Setup: func(dir string) error {
   107  			return utils.CreateConfigFile(dir, defaultFileContent)
   108  		},
   109  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   110  			filePath := dir + "/" + def.ConfigFileName
   111  			assert.FileExists(t, dir+"/"+def.ConfigFileName)
   112  			content, _ := ioutil.ReadFile(filePath)
   113  			return assert.Contains(t, string(content), expectedOutput)
   114  		},
   115  	}
   116  }
   117  
   118  func shouldFailWhenConfigurationFileIsNotFound(feature string) Test {
   119  	return Test{
   120  		Feature: feature,
   121  		Name:    "shouldFailWhenConfigurationFileIsNotFound",
   122  		CmdArgs: Args("set", "build", "go build -o"),
   123  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   124  			return assert.Contains(t, actualOutput, "no '"+def.ConfigFileName+"' file found")
   125  		},
   126  	}
   127  }
   128  
   129  func shouldFailWhenConfigurationFileIsInInvalidFormat(feature string) Test {
   130  	return Test{
   131  		Feature: feature,
   132  		Name:    "shouldFailWhenConfigurationFileIsInInvalidFormat",
   133  		CmdArgs: Args("set", "build", "go build"),
   134  		Setup: func(dir string) error {
   135  			return utils.CreateConfigFile(dir, "invalid config content")
   136  		},
   137  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   138  			return assert.Contains(t, actualOutput, "Unable to parse '"+def.ConfigFileName+"' config file. Make sure file is in correct format.")
   139  		},
   140  	}
   141  }
   142  
   143  func shouldSetBeforeCommand(feature string) Test {
   144  
   145  	defaultFileContent := `
   146  project: Sample Project
   147  commands:
   148    - build: go build
   149  `
   150  
   151  	expectedOutput := `project: Sample Project
   152  after: yo
   153  commands:
   154    - build: go build
   155  `
   156  
   157  	return Test{
   158  		Feature: feature,
   159  		Name:    "shouldSetBeforeCommand",
   160  		CmdArgs: Args("set", "after", "yo"),
   161  		Setup: func(dir string) error {
   162  			return utils.CreateConfigFile(dir, defaultFileContent)
   163  		},
   164  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   165  			filePath := dir + "/" + def.ConfigFileName
   166  			assert.FileExists(t, dir+"/"+def.ConfigFileName)
   167  			content, _ := ioutil.ReadFile(filePath)
   168  			return assert.Contains(t, string(content), expectedOutput)
   169  		},
   170  	}
   171  }
   172  
   173  func shouldSetAfterCommand(feature string) Test {
   174  
   175  	defaultFileContent := `
   176  project: Sample Project
   177  commands:
   178    - build: go build
   179  `
   180  
   181  	expectedOutput := `project: Sample Project
   182  after: yo
   183  commands:
   184    - build: go build
   185  `
   186  
   187  	return Test{
   188  		Feature: feature,
   189  		Name:    "shouldSetBeforeCommand",
   190  		CmdArgs: Args("set", "after", "yo"),
   191  		Setup: func(dir string) error {
   192  			return utils.CreateConfigFile(dir, defaultFileContent)
   193  		},
   194  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   195  			filePath := dir + "/" + def.ConfigFileName
   196  			assert.FileExists(t, dir+"/"+def.ConfigFileName)
   197  			content, _ := ioutil.ReadFile(filePath)
   198  			return assert.Contains(t, string(content), expectedOutput)
   199  		},
   200  	}
   201  }