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

     1  package integration
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/ActiveState/cli/internal/testhelpers/suite"
     8  
     9  	"github.com/ActiveState/cli/internal/constants"
    10  	"github.com/ActiveState/cli/internal/testhelpers/e2e"
    11  	"github.com/ActiveState/cli/internal/testhelpers/tagsuite"
    12  )
    13  
    14  type EventsIntegrationTestSuite struct {
    15  	tagsuite.Suite
    16  }
    17  
    18  func (suite *EventsIntegrationTestSuite) TestEvents() {
    19  	suite.OnlyRunForTags(tagsuite.Events)
    20  	ts := e2e.New(suite.T(), false)
    21  	defer ts.Close()
    22  
    23  	ts.PrepareActiveStateYAML(strings.TrimSpace(`
    24  project: https://platform.activestate.com/ActiveState-CLI/Python3
    25  scripts:
    26    - name: before
    27      language: bash
    28      value: echo before-script
    29    - name: after
    30      language: bash
    31      value: echo after-script
    32  events:
    33    - name: first-activate
    34      value: echo "First activate event"
    35    - name: activate
    36      value: echo "Activate event"
    37    - name: activate
    38      value: echo "Activate event duplicate"
    39    - name: before-command
    40      scope: ["activate"]
    41      value: before
    42    - name: after-command
    43      scope: ["activate"]
    44      value: after
    45  `))
    46  	ts.PrepareCommitIdFile("fbc613d6-b0b1-4f84-b26e-4aa5869c4e54")
    47  
    48  	cp := ts.SpawnWithOpts(
    49  		e2e.OptArgs("activate"),
    50  		e2e.OptAppendEnv(constants.DisableActivateEventsEnvVarName+"=false"),
    51  	)
    52  	cp.SendEnter()
    53  	cp.Expect("before-script")
    54  	cp.Expect("First activate event")
    55  	cp.Expect("Activate event")
    56  	cp.ExpectInput()
    57  	cp.SendLine("exit")
    58  	cp.Expect("after-script")
    59  	cp.ExpectExitCode(0)
    60  
    61  	cp = ts.SpawnWithOpts(
    62  		e2e.OptArgs("activate"),
    63  		e2e.OptAppendEnv(constants.DisableActivateEventsEnvVarName+"=false"),
    64  	)
    65  	cp.Expect("Activate event")
    66  	cp.ExpectInput()
    67  	cp.SendLine("exit")
    68  	cp.ExpectExitCode(0)
    69  	output := cp.Output()
    70  	if strings.Contains(output, "First activate event") {
    71  		suite.T().Fatal("Output from second activate event should not contain first-activate output")
    72  	}
    73  	if strings.Contains(output, "Activate event duplicate") {
    74  		suite.T().Fatal("Output should not contain output from duplicate activate event")
    75  	}
    76  }
    77  
    78  func (suite *EventsIntegrationTestSuite) TestJSON() {
    79  	suite.OnlyRunForTags(tagsuite.Events, tagsuite.JSON)
    80  	ts := e2e.New(suite.T(), false)
    81  	defer ts.Close()
    82  
    83  	cp := ts.Spawn("checkout", "ActiveState-CLI/Python3", ".")
    84  	cp.Expect("Skipping runtime setup")
    85  	cp.Expect("Checked out")
    86  	cp.ExpectExitCode(0)
    87  
    88  	cp = ts.Spawn("events", "-o", "json")
    89  	cp.Expect(`[{"event":`)
    90  	cp.ExpectExitCode(0)
    91  	AssertValidJSON(suite.T(), cp)
    92  }
    93  
    94  func TestEventsIntegrationTestSuite(t *testing.T) {
    95  	suite.Run(t, new(EventsIntegrationTestSuite))
    96  }