github.com/xgoffin/jenkins-library@v1.154.0/cmd/uiVeri5ExecuteTests_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/SAP/jenkins-library/pkg/mock"
     7  	"github.com/pkg/errors"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestRunUIVeri5(t *testing.T) {
    12  	t.Run("success case", func(t *testing.T) {
    13  		opts := &uiVeri5ExecuteTestsOptions{
    14  			InstallCommand: "npm install ui5/uiveri5",
    15  			RunCommand:     "uiveri5",
    16  			RunOptions:     []string{"conf.js"},
    17  			TestServerURL:  "http://path/to/deployment",
    18  		}
    19  
    20  		e := mock.ExecMockRunner{}
    21  		runUIVeri5(opts, &e)
    22  
    23  		assert.Equal(t, e.Env[0], "NPM_CONFIG_PREFIX=~/.npm-global", "NPM_CONFIG_PREFIX not set as expected")
    24  		assert.Contains(t, e.Env[1], "PATH", "PATH not in env list")
    25  		assert.Equal(t, e.Env[2], "TARGET_SERVER_URL=http://path/to/deployment", "TARGET_SERVER_URL not set as expected")
    26  
    27  		assert.Equal(t, e.Calls[0], mock.ExecCall{Exec: "npm", Params: []string{"install", "ui5/uiveri5"}}, "install command/params incorrect")
    28  
    29  		assert.Equal(t, e.Calls[1], mock.ExecCall{Exec: "uiveri5", Params: []string{"conf.js"}}, "run command/params incorrect")
    30  
    31  	})
    32  
    33  	t.Run("error case install command", func(t *testing.T) {
    34  		wantError := "failed to execute install command: fail install test: error case"
    35  
    36  		opts := &uiVeri5ExecuteTestsOptions{InstallCommand: "fail install test", RunCommand: "uiveri5"}
    37  
    38  		e := mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"fail install test": errors.New("error case")}}
    39  		err := runUIVeri5(opts, &e)
    40  		assert.EqualErrorf(t, err, wantError, "expected comman to exit with error")
    41  	})
    42  
    43  	t.Run("error case run command", func(t *testing.T) {
    44  		wantError := "failed to execute run command: fail uiveri5 testParam: error case"
    45  
    46  		opts := &uiVeri5ExecuteTestsOptions{InstallCommand: "npm install ui5/uiveri5", RunCommand: "fail uiveri5", RunOptions: []string{"testParam"}}
    47  
    48  		e := mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"fail uiveri5": errors.New("error case")}}
    49  		err := runUIVeri5(opts, &e)
    50  		assert.EqualErrorf(t, err, wantError, "expected comman to exit with error")
    51  	})
    52  }