github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/test/integration/condition_int_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"runtime"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/ActiveState/cli/internal/testhelpers/suite"
     9  
    10  	"github.com/ActiveState/cli/internal/constants"
    11  	"github.com/ActiveState/cli/internal/testhelpers/e2e"
    12  	"github.com/ActiveState/cli/internal/testhelpers/tagsuite"
    13  )
    14  
    15  type ConditionIntegrationTestSuite struct {
    16  	tagsuite.Suite
    17  }
    18  
    19  func (suite *ConditionIntegrationTestSuite) TestCondition() {
    20  	suite.OnlyRunForTags(tagsuite.Condition)
    21  	ts := e2e.New(suite.T(), false)
    22  	defer ts.Close()
    23  
    24  	suite.PrepareActiveStateYAML(ts)
    25  
    26  	cp := ts.SpawnWithOpts(
    27  		e2e.OptArgs("run", "test"),
    28  	)
    29  	cp.Expect(`projectNameValue`)
    30  	cp.Expect(`projectOwnerValue`)
    31  	cp.Expect(`projectNamespaceValue`)
    32  	cp.Expect(`osNameValue`)
    33  	cp.Expect(`osVersionValue`)
    34  	cp.Expect(`osArchValue`)
    35  	cp.Expect(`shellValue`)
    36  	cp.ExpectExitCode(0)
    37  
    38  	cp = ts.SpawnWithOpts(
    39  		e2e.OptArgs("activate"),
    40  		e2e.OptAppendEnv(constants.DisableActivateEventsEnvVarName+"=false"),
    41  	)
    42  	cp.Expect(`Activation Event Ran`)
    43  	cp.ExpectInput()
    44  	cp.SendLine("exit")
    45  	cp.ExpectExitCode(0)
    46  
    47  	cp = ts.SpawnWithOpts(
    48  		e2e.OptArgs("run", "complex-true"),
    49  	)
    50  	cp.Expect(`I exist`)
    51  	cp.ExpectExitCode(0)
    52  
    53  	cp = ts.SpawnWithOpts(
    54  		e2e.OptArgs("run", "complex-false"),
    55  	)
    56  	cp.ExpectExitCode(1)
    57  }
    58  
    59  func (suite *ConditionIntegrationTestSuite) TestMixin() {
    60  	suite.OnlyRunForTags(tagsuite.Condition)
    61  	ts := e2e.New(suite.T(), false)
    62  	defer ts.Close()
    63  
    64  	suite.PrepareActiveStateYAML(ts)
    65  
    66  	cp := ts.SpawnWithOpts(
    67  		e2e.OptArgs("run", "MixinUser"),
    68  	)
    69  	cp.ExpectExitCode(0)
    70  	suite.Assert().NotContains(cp.Output(), "authenticated: yes", "expected not to be authenticated, output was:\n%s.", cp.Output())
    71  	suite.Assert().NotContains(cp.Output(), e2e.PersistentUsername, "expected not to be authenticated, output was:\n%s", cp.Output())
    72  
    73  	ts.LoginAsPersistentUser()
    74  	defer ts.LogoutUser()
    75  
    76  	cp = ts.SpawnWithOpts(
    77  		e2e.OptArgs("run", "MixinUser"),
    78  	)
    79  	cp.Expect("authenticated: yes")
    80  	cp.Expect(e2e.PersistentUsername)
    81  	cp.ExpectExitCode(0)
    82  }
    83  
    84  func (suite *ConditionIntegrationTestSuite) TestConditionOSName() {
    85  	suite.OnlyRunForTags(tagsuite.Condition)
    86  	ts := e2e.New(suite.T(), false)
    87  	defer ts.Close()
    88  
    89  	suite.PrepareActiveStateYAML(ts)
    90  
    91  	cp := ts.SpawnWithOpts(
    92  		e2e.OptArgs("run", "OSName"),
    93  	)
    94  	switch runtime.GOOS {
    95  	case "windows":
    96  		cp.Expect(`using-windows`)
    97  	case "darwin":
    98  		cp.Expect(`using-macos`)
    99  	default:
   100  		cp.Expect(`using-linux`)
   101  	}
   102  	cp.ExpectExitCode(0)
   103  }
   104  
   105  func (suite *ConditionIntegrationTestSuite) TestConditionSyntaxError() {
   106  	suite.OnlyRunForTags(tagsuite.Condition)
   107  	ts := e2e.New(suite.T(), false)
   108  	defer ts.Close()
   109  
   110  	suite.PrepareActiveStateYAMLWithSyntaxError(ts)
   111  
   112  	cp := ts.SpawnWithOpts(
   113  		e2e.OptArgs("run", "test"),
   114  	)
   115  	cp.Expect(`not defined`) // for now we aren't passing the error up the chain, so invalid syntax will lead to empty result
   116  	cp.ExpectExitCode(1)
   117  	ts.IgnoreLogErrors()
   118  }
   119  
   120  func (suite *ConditionIntegrationTestSuite) PrepareActiveStateYAML(ts *e2e.Session) {
   121  	asyData := strings.TrimSpace(`
   122  project: https://platform.activestate.com/ActiveState-CLI/test
   123  constants:
   124    - name: projectName
   125      value: invalidProjectName
   126      if: false
   127    - name: projectName
   128      value: projectNameValue
   129      if: ne .Project.Name ""
   130    - name: projectName
   131      value: invalidProjectName
   132      if: false
   133    - name: projectOwner
   134      value: projectOwnerValue
   135      if: ne .Project.Owner ""
   136    - name: projectNamespace
   137      value: projectNamespaceValue
   138      if: ne .Project.NamespacePrefix ""
   139    - name: osName
   140      value: osNameValue
   141      if: ne .OS.Name ""
   142    - name: osVersion
   143      value: osVersionValue
   144      if: ne .OS.Version.Name ""
   145    - name: osArch
   146      value: osArchValue
   147      if: ne .OS.Architecture ""
   148    - name: shell
   149      value: shellValue
   150      if: ne .Shell ""
   151    - name: mixinUser
   152      value: yes
   153      if: ne Mixin.User.Name ""
   154  scripts:
   155    - name: complex-true
   156      language: bash
   157      standalone: true
   158      value: echo "I exist"
   159      if: or (eq .OS.Architecture "") (Contains .OS.Architecture "64")
   160    - name: complex-false
   161      language: bash
   162      standalone: true
   163      value: echo "I exist"
   164      if: and (eq .OS.Architecture "") (Contains .OS.Architecture "64")
   165    - name: test
   166      language: bash
   167      standalone: true
   168      value: echo wrong script
   169      if: false
   170    - name: test
   171      standalone: true
   172      language: bash
   173      value: |
   174        echo ${constants.projectName}
   175        echo ${constants.projectOwner}
   176        echo ${constants.projectNamespace}
   177        echo ${constants.osName}
   178        echo ${constants.osVersion}
   179        echo ${constants.osArch}
   180        echo ${constants.shell}
   181      if: ne .Shell ""
   182    - name: test
   183      language: bash
   184      standalone: true
   185      value: echo wrong script
   186      if: false
   187    - name: OSName
   188      language: bash
   189      standalone: true
   190      value: echo using-windows
   191      if: eq .OS.Name "Windows"
   192    - name: OSName
   193      language: bash
   194      standalone: true
   195      value: echo using-macos
   196      if: eq .OS.Name "MacOS"
   197    - name: OSName
   198      language: bash
   199      standalone: true
   200      value: echo using-linux
   201      if: eq .OS.Name "Linux"
   202    - name: MixinUser
   203      language: bash
   204      standalone: true
   205      value: |
   206        echo "authenticated: ${constants.mixinUser}"
   207        echo "userName: ${mixin.user.name}"
   208  events:
   209    - name: ACTIVATE
   210      value: echo "Wrong event"
   211      if: false
   212    - name: ACTIVATE
   213      value: echo "Activation Event Ran"
   214      if: ne .Shell ""
   215    - name: ACTIVATE
   216      value: echo "Wrong event"
   217      if: false
   218  `)
   219  
   220  	ts.PrepareActiveStateYAML(asyData)
   221  	ts.PrepareCommitIdFile("9090c128-e948-4388-8f7f-96e2c1e00d98")
   222  }
   223  func (suite *ConditionIntegrationTestSuite) PrepareActiveStateYAMLWithSyntaxError(ts *e2e.Session) {
   224  	asyData := strings.TrimSpace(`
   225  project: https://platform.activestate.com/ActiveState-CLI/test
   226  scripts:
   227    - name: test
   228      language: bash
   229      standalone: true
   230      value: echo invalid value
   231      if: not a valid conditional
   232    - name: test
   233      language: bash
   234      standalone: true
   235      value: echo valid value
   236      if: true
   237  `)
   238  
   239  	ts.PrepareActiveStateYAML(asyData)
   240  	ts.PrepareCommitIdFile("9090c128-e948-4388-8f7f-96e2c1e00d98")
   241  }
   242  
   243  func TestConditionIntegrationTestSuite(t *testing.T) {
   244  	suite.Run(t, new(ConditionIntegrationTestSuite))
   245  }