github.imxd.top/gopinath-langote/1build@v1.2.0/testing/fixtures/execute_cmd_fixtures.go (about)

     1  package fixtures
     2  
     3  import (
     4  	"github.com/gopinath-langote/1build/testing/utils"
     5  	"github.com/stretchr/testify/assert"
     6  	"testing"
     7  )
     8  
     9  var Execute = "execute"
    10  
    11  func FeatureExecuteCmdTestData() []Test {
    12  	feature := "exec"
    13  
    14  	return []Test{
    15  		shouldExecuteAvailableCommand(feature),
    16  		shouldShowErrorIfCommandNotFound(feature),
    17  		shouldExecuteBeforeCommand(feature),
    18  		shouldExecuteAfterCommand(feature),
    19  		shouldExecuteBeforeAndAfterCommand(feature),
    20  		shouldStopExecutionIfBeforeCommandFailed(feature),
    21  		shouldStopExecutionIfCommandFailed(feature),
    22  	}
    23  }
    24  
    25  func shouldExecuteAvailableCommand(feature string) Test {
    26  	fileContent := `
    27  project: Sample Project
    28  commands:
    29    - build: echo building project
    30  `
    31  	expectedOutput := `--------------------------------------------------
    32  build : echo building project
    33  --------------------------------------------------
    34  building project
    35  `
    36  	return Test{
    37  		Feature: feature,
    38  		Name:    "shouldExecuteAvailableCommand",
    39  		CmdArgs: []string{"build"},
    40  		Setup: func(dir string) error {
    41  			return utils.CreateConfigFile(dir, fileContent)
    42  		},
    43  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    44  			return assert.Contains(t, actualOutput, expectedOutput)
    45  		},
    46  	}
    47  }
    48  
    49  func shouldShowErrorIfCommandNotFound(feature string) Test {
    50  	fileContent := `
    51  project: Sample Project
    52  commands:
    53    - build: echo building project
    54  `
    55  
    56  	expectedOutput := `No command 'random' found in config file
    57  
    58  --------------------------------------------------
    59  project: Sample Project
    60  commands:
    61  build | echo building project
    62  --------------------------------------------------
    63  `
    64  	return Test{
    65  		Feature: feature,
    66  		Name:    "shouldShowErrorIfCommandNotFound",
    67  		CmdArgs: []string{"random"},
    68  		Setup: func(dir string) error {
    69  			return utils.CreateConfigFile(dir, fileContent)
    70  		},
    71  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    72  			return assert.Contains(t, actualOutput, expectedOutput)
    73  		},
    74  	}
    75  }
    76  
    77  func shouldExecuteBeforeCommand(feature string) Test {
    78  	fileContent := `
    79  project: Sample Project
    80  before: echo running pre-command
    81  commands:
    82    - build: echo building project
    83  `
    84  	expectedOutput := `--------------------------------------------------
    85  Before: echo running pre-command
    86  
    87  build : echo building project
    88  --------------------------------------------------
    89  running pre-command
    90  building project
    91  `
    92  	return Test{
    93  		Feature: feature,
    94  		Name:    "shouldExecuteBeforeCommand",
    95  		CmdArgs: []string{"build"},
    96  		Setup: func(dir string) error {
    97  			return utils.CreateConfigFile(dir, fileContent)
    98  		},
    99  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   100  			return assert.Contains(t, actualOutput, expectedOutput)
   101  		},
   102  	}
   103  }
   104  
   105  func shouldExecuteAfterCommand(feature string) Test {
   106  	fileContent := `
   107  project: Sample Project
   108  after: echo running post-command
   109  commands:
   110    - build: echo building project
   111  `
   112  	expectedOutput := `--------------------------------------------------
   113  build : echo building project
   114  
   115  After: echo running post-command
   116  --------------------------------------------------
   117  building project
   118  running post-command
   119  `
   120  	return Test{
   121  		Feature: feature,
   122  		Name:    "shouldExecuteAfterCommand",
   123  		CmdArgs: []string{"build"},
   124  		Setup: func(dir string) error {
   125  			return utils.CreateConfigFile(dir, fileContent)
   126  		},
   127  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   128  			return assert.Contains(t, actualOutput, expectedOutput)
   129  		},
   130  	}
   131  }
   132  
   133  func shouldExecuteBeforeAndAfterCommand(feature string) Test {
   134  	fileContent := `
   135  project: Sample Project
   136  before: echo running pre-command
   137  after: echo running post-command
   138  commands:
   139    - build: echo building project
   140  `
   141  	expectedOutput := `--------------------------------------------------
   142  Before: echo running pre-command
   143  
   144  build : echo building project
   145  
   146  After: echo running post-command
   147  --------------------------------------------------
   148  running pre-command
   149  building project
   150  running post-command
   151  `
   152  	return Test{
   153  		Feature: feature,
   154  		Name:    "shouldExecuteBeforeAndAfterCommand",
   155  		CmdArgs: []string{"build"},
   156  		Setup: func(dir string) error {
   157  			return utils.CreateConfigFile(dir, fileContent)
   158  		},
   159  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   160  			return assert.Contains(t, actualOutput, expectedOutput)
   161  		},
   162  	}
   163  }
   164  
   165  func shouldStopExecutionIfBeforeCommandFailed(feature string) Test {
   166  	fileContent := `
   167  project: Sample Project
   168  before: invalid_command
   169  after: echo running post-command
   170  commands:
   171    - build: echo building project
   172  `
   173  	expectedOutput := `--------------------------------------------------
   174  Before: invalid_command
   175  
   176  build : echo building project
   177  
   178  After: echo running post-command
   179  --------------------------------------------------
   180  
   181  Failed to execute 'invalid_command'
   182  `
   183  	return Test{
   184  		Feature: feature,
   185  		Name:    "shouldStopExecutionIfBeforeCommandFailed",
   186  		CmdArgs: []string{"build"},
   187  		Setup: func(dir string) error {
   188  			return utils.CreateConfigFile(dir, fileContent)
   189  		},
   190  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   191  			return assert.Contains(t, actualOutput, expectedOutput)
   192  		},
   193  	}
   194  }
   195  
   196  func shouldStopExecutionIfCommandFailed(feature string) Test {
   197  	fileContent := `
   198  project: Sample Project
   199  before: echo running pre-command
   200  after: echo running post-command
   201  commands:
   202    - build: invalid_command
   203  `
   204  	expectedOutput := `--------------------------------------------------
   205  Before: echo running pre-command
   206  
   207  build : invalid_command
   208  
   209  After: echo running post-command
   210  --------------------------------------------------
   211  running pre-command
   212  
   213  Failed to execute 'invalid_command'
   214  `
   215  	return Test{
   216  		Feature: feature,
   217  		Name:    "shouldStopExecutionIfCommandFailed",
   218  		CmdArgs: []string{"build"},
   219  		Setup: func(dir string) error {
   220  			return utils.CreateConfigFile(dir, fileContent)
   221  		},
   222  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   223  			return assert.Contains(t, actualOutput, expectedOutput)
   224  		},
   225  	}
   226  }