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

     1  package fixtures
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/gopinath-langote/1build/testing/utils"
     8  )
     9  
    10  func featureExecuteCmdTestData() []Test {
    11  	feature := "exec"
    12  
    13  	return []Test{
    14  		shouldExecuteAvailableCommand(feature),
    15  		shouldExecuteAvailableCommandFromSpecifiedFile(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  -----    ---------------------
    33  Phase    Command
    34  -----    ---------------------
    35  build    echo building project
    36  
    37  
    38  -------------------------------[ ` + "build" + ` ]--------------------------------
    39  building project
    40  
    41  `
    42  	return Test{
    43  		Feature: feature,
    44  		Name:    "shouldExecuteAvailableCommand",
    45  		CmdArgs: Args("build"),
    46  		Setup: func(dir string) error {
    47  			return utils.CreateConfigFile(dir, fileContent)
    48  		},
    49  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    50  			return utils.AssertContains(t, actualOutput, expectedOutput) &&
    51  				assertSuccessBanner(t, actualOutput)
    52  		},
    53  	}
    54  }
    55  
    56  func shouldExecuteAvailableCommandFromSpecifiedFile(feature string) Test {
    57  	fileContent := `
    58  project: Sample Project
    59  commands:
    60    - build: echo building project
    61  `
    62  	expectedOutput := `
    63  -----    ---------------------
    64  Phase    Command
    65  -----    ---------------------
    66  build    echo building project
    67  
    68  
    69  -------------------------------[ ` + "build" + ` ]--------------------------------
    70  building project
    71  
    72  `
    73  	return Test{
    74  		Feature: feature,
    75  		Name:    "shouldExecuteAvailableCommandFromSpecifiedFile",
    76  		CmdArgs: func(dir string) []string {
    77  			return []string{"build", "-f", dir + "/some-dir/some-config.yaml"}
    78  		},
    79  		Setup: func(dir string) error {
    80  			_ = os.MkdirAll(dir+"/some-dir/", 0750)
    81  			return utils.CreateConfigFileWithName(dir+"/some-dir", "some-config.yaml", fileContent)
    82  		},
    83  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    84  			return utils.AssertContains(t, actualOutput, expectedOutput) &&
    85  				assertSuccessBanner(t, actualOutput)
    86  		},
    87  	}
    88  }
    89  
    90  func shouldShowErrorIfCommandNotFound(feature string) Test {
    91  	fileContent := `
    92  project: Sample Project
    93  commands:
    94    - build: echo building project
    95  `
    96  
    97  	expectedOutput := "\nError building execution plan. Command \"random\" not found." + `
    98  ------------------------------------------------------------------------
    99  project: Sample Project
   100  commands:
   101  build | echo building project
   102  ------------------------------------------------------------------------
   103  `
   104  	return Test{
   105  		Feature: feature,
   106  		Name:    "shouldShowErrorIfCommandNotFound",
   107  		CmdArgs: Args("random"),
   108  		Setup: func(dir string) error {
   109  			return utils.CreateConfigFile(dir, fileContent)
   110  		},
   111  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   112  			return utils.AssertContains(t, actualOutput, expectedOutput)
   113  		},
   114  	}
   115  }
   116  
   117  func shouldExecuteBeforeCommand(feature string) Test {
   118  	fileContent := `
   119  project: Sample Project
   120  before: echo running pre-command
   121  commands:
   122    - build: echo building project
   123  `
   124  	expectedOutput := `
   125  ------    ------------------------
   126  Phase     Command
   127  ------    ------------------------
   128  before    echo running pre-command
   129  build     echo building project
   130  
   131  
   132  -------------------------------[ ` + "before" + ` ]-------------------------------
   133  running pre-command
   134  -------------------------------[ ` + "build" + ` ]--------------------------------
   135  building project
   136  
   137  `
   138  	return Test{
   139  		Feature: feature,
   140  		Name:    "shouldExecuteBeforeCommand",
   141  		CmdArgs: Args("build"),
   142  		Setup: func(dir string) error {
   143  			return utils.CreateConfigFile(dir, fileContent)
   144  		},
   145  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   146  			return utils.AssertContains(t, actualOutput, expectedOutput) &&
   147  				assertSuccessBanner(t, actualOutput)
   148  		},
   149  	}
   150  }
   151  
   152  func shouldExecuteAfterCommand(feature string) Test {
   153  	fileContent := `
   154  project: Sample Project
   155  after: echo running post-command
   156  commands:
   157    - build: echo building project
   158  `
   159  	expectedOutput := `
   160  -----    -------------------------
   161  Phase    Command
   162  -----    -------------------------
   163  build    echo building project
   164  after    echo running post-command
   165  
   166  
   167  -------------------------------[ ` + "build" + ` ]--------------------------------
   168  building project
   169  -------------------------------[ ` + "after" + ` ]--------------------------------
   170  running post-command
   171  
   172  `
   173  	return Test{
   174  		Feature: feature,
   175  		Name:    "shouldExecuteAfterCommand",
   176  		CmdArgs: Args("build"),
   177  		Setup: func(dir string) error {
   178  			return utils.CreateConfigFile(dir, fileContent)
   179  		},
   180  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   181  			return utils.AssertContains(t, actualOutput, expectedOutput) &&
   182  				assertSuccessBanner(t, actualOutput)
   183  		},
   184  	}
   185  }
   186  
   187  func shouldExecuteBeforeAndAfterCommand(feature string) Test {
   188  	fileContent := `
   189  project: Sample Project
   190  before: echo running pre-command
   191  after: echo running post-command
   192  commands:
   193    - build: echo building project
   194  `
   195  	expectedOutput := `
   196  ------    -------------------------
   197  Phase     Command
   198  ------    -------------------------
   199  before    echo running pre-command
   200  build     echo building project
   201  after     echo running post-command
   202  
   203  
   204  -------------------------------[ ` + "before" + ` ]-------------------------------
   205  running pre-command
   206  -------------------------------[ ` + "build" + ` ]--------------------------------
   207  building project
   208  -------------------------------[ ` + "after" + ` ]--------------------------------
   209  running post-command
   210  
   211  `
   212  	return Test{
   213  		Feature: feature,
   214  		Name:    "shouldExecuteBeforeAndAfterCommand",
   215  		CmdArgs: Args("build"),
   216  		Setup: func(dir string) error {
   217  			return utils.CreateConfigFile(dir, fileContent)
   218  		},
   219  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   220  			return utils.AssertContains(t, actualOutput, expectedOutput) &&
   221  				assertSuccessBanner(t, actualOutput)
   222  		},
   223  	}
   224  }
   225  
   226  func shouldStopExecutionIfBeforeCommandFailed(feature string) Test {
   227  	fileContent := `
   228  project: Sample Project
   229  before: exit 10
   230  after: echo running post-command
   231  commands:
   232    - build: echo building project
   233  `
   234  	expectedOutput := `
   235  ------    -------------------------
   236  Phase     Command
   237  ------    -------------------------
   238  before    exit 10
   239  build     echo building project
   240  after     echo running post-command
   241  
   242  
   243  -------------------------------[ ` + "before" + ` ]-------------------------------
   244  `
   245  	return Test{
   246  		Feature: feature,
   247  		Name:    "shouldStopExecutionIfBeforeCommandFailed",
   248  		CmdArgs: Args("build"),
   249  		Setup: func(dir string) error {
   250  			return utils.CreateConfigFile(dir, fileContent)
   251  		},
   252  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   253  			return utils.AssertContains(t, actualOutput, expectedOutput) &&
   254  				assertFailureMessage(t, actualOutput, "before", "10") &&
   255  				assertFailureBanner(t, actualOutput)
   256  
   257  		},
   258  	}
   259  }
   260  
   261  func shouldStopExecutionIfCommandFailed(feature string) Test {
   262  	fileContent := `
   263  project: Sample Project
   264  before: echo running pre-command
   265  after: echo running post-command
   266  commands:
   267    - build: invalid_command
   268  `
   269  	expectedOutput := `
   270  ------    -------------------------
   271  Phase     Command
   272  ------    -------------------------
   273  before    echo running pre-command
   274  build     invalid_command
   275  after     echo running post-command
   276  
   277  
   278  -------------------------------[ ` + "before" + ` ]-------------------------------
   279  running pre-command
   280  -------------------------------[ ` + "build" + ` ]--------------------------------
   281  `
   282  	return Test{
   283  		Feature: feature,
   284  		Name:    "shouldStopExecutionIfCommandFailed",
   285  		CmdArgs: Args("build"),
   286  		Setup: func(dir string) error {
   287  			return utils.CreateConfigFile(dir, fileContent)
   288  		},
   289  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
   290  			return utils.AssertContains(t, actualOutput, expectedOutput) &&
   291  				assertFailureMessage(t, actualOutput, "build", "127") &&
   292  				assertFailureBanner(t, actualOutput)
   293  		},
   294  	}
   295  }
   296  
   297  func assertSuccessBanner(t *testing.T, actualOutput string) bool {
   298  	return utils.AssertContains(t, actualOutput, "SUCCESS - Total Time")
   299  }
   300  
   301  func assertFailureMessage(t *testing.T, actualOutput string, phase string, exitCode string) bool {
   302  	errorText := "\nExecution failed in phase '" + phase + "' – exit code: " + exitCode
   303  	return utils.AssertContains(t, actualOutput, errorText)
   304  }
   305  
   306  func assertFailureMessageNone(t *testing.T, actualOutput string, phase string, exitCode string) bool {
   307  	errorText := "\nExecution failed in phase '" + phase + "' – exit code: " + exitCode
   308  	return utils.AssertNotContains(t, actualOutput, errorText)
   309  }
   310  
   311  func assertFailureBanner(t *testing.T, actualOutput string) bool {
   312  	return utils.AssertContains(t, actualOutput, "FAILURE - Total Time")
   313  }