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

     1  package integration
     2  
     3  import (
     4  	"path/filepath"
     5  	"runtime"
     6  	"testing"
     7  
     8  	"github.com/ActiveState/cli/internal/constants"
     9  	"github.com/ActiveState/cli/internal/testhelpers/e2e"
    10  	"github.com/ActiveState/cli/internal/testhelpers/suite"
    11  	"github.com/ActiveState/cli/internal/testhelpers/tagsuite"
    12  	"github.com/ActiveState/cli/pkg/project"
    13  )
    14  
    15  type SwitchIntegrationTestSuite struct {
    16  	tagsuite.Suite
    17  }
    18  
    19  func (suite *SwitchIntegrationTestSuite) TestSwitch_Branch() {
    20  	suite.OnlyRunForTags(tagsuite.Switch)
    21  	ts := e2e.New(suite.T(), false)
    22  	defer ts.Close()
    23  
    24  	err := ts.ClearCache()
    25  	suite.Require().NoError(err)
    26  
    27  	ts.PrepareProject("ActiveState-CLI/Branches", "b5b327f8-468e-4999-a23e-8bee886e6b6d")
    28  	pjfilepath := filepath.Join(ts.Dirs.Work, constants.ConfigFileName)
    29  
    30  	pj, err := project.FromPath(pjfilepath)
    31  	suite.Require().NoError(err)
    32  	suite.Require().Equal("main", pj.BranchName(), "branch was not set to 'main' after pull")
    33  	mainBranchCommitID := ts.CommitID()
    34  	suite.Require().NoError(err)
    35  
    36  	cp := ts.SpawnWithOpts(e2e.OptArgs("switch", "secondbranch"))
    37  	cp.Expect("Operating on project")
    38  	cp.Expect("ActiveState-CLI/Branches")
    39  	cp.Expect("Successfully switched to branch:")
    40  	if runtime.GOOS != "windows" {
    41  		cp.ExpectExitCode(0)
    42  	}
    43  
    44  	// Check that branch and commitID were updated
    45  	pj, err = project.FromPath(pjfilepath)
    46  	suite.Require().NoError(err)
    47  	suite.Require().NoError(err)
    48  	suite.NotEqual(mainBranchCommitID, ts.CommitID(), "commitID was not updated after switching branches", pj.Dir())
    49  	suite.Equal("secondbranch", pj.BranchName(), "branch was not updated after switching branches")
    50  }
    51  
    52  func (suite *SwitchIntegrationTestSuite) TestSwitch_CommitID() {
    53  	suite.OnlyRunForTags(tagsuite.Switch)
    54  	ts := e2e.New(suite.T(), false)
    55  	defer ts.Close()
    56  
    57  	err := ts.ClearCache()
    58  	suite.Require().NoError(err)
    59  
    60  	ts.PrepareProject("ActiveState-CLI/History", "b5b327f8-468e-4999-a23e-8bee886e6b6d")
    61  	pjfilepath := filepath.Join(ts.Dirs.Work, constants.ConfigFileName)
    62  
    63  	pj, err := project.FromPath(pjfilepath)
    64  	suite.Require().NoError(err)
    65  	suite.Require().Equal("main", pj.BranchName(), "branch was not set to 'main' after pull")
    66  	originalCommitID := ts.CommitID()
    67  
    68  	cp := ts.SpawnWithOpts(e2e.OptArgs("switch", "efce7c7a-c61a-4b04-bb00-f8e7edfd247f"))
    69  	cp.Expect("Successfully switched to commit:")
    70  	if runtime.GOOS != "windows" {
    71  		cp.ExpectExitCode(0)
    72  	}
    73  
    74  	// Check that branch and commitID were updated
    75  	_, err = project.FromPath(pjfilepath)
    76  	suite.Require().NoError(err)
    77  	suite.Require().NotEqual(originalCommitID, ts.CommitID(), "commitID was not updated after switching branches")
    78  }
    79  
    80  func (suite *SwitchIntegrationTestSuite) TestSwitch_CommitID_NotInHistory() {
    81  	suite.OnlyRunForTags(tagsuite.Switch)
    82  	ts := e2e.New(suite.T(), false)
    83  	defer ts.Close()
    84  
    85  	err := ts.ClearCache()
    86  	suite.Require().NoError(err)
    87  
    88  	ts.PrepareProject("ActiveState-CLI/History", "b5b327f8-468e-4999-a23e-8bee886e6b6d")
    89  	pjfilepath := filepath.Join(ts.Dirs.Work, constants.ConfigFileName)
    90  
    91  	pj, err := project.FromPath(pjfilepath)
    92  	suite.Require().NoError(err)
    93  	suite.Require().Equal("main", pj.BranchName(), "branch was not set to 'main' after pull")
    94  	originalCommitID := ts.CommitID()
    95  
    96  	cp := ts.SpawnWithOpts(e2e.OptArgs("switch", "76dff77a-66b9-43e3-90be-dc75917dd661"))
    97  	cp.Expect("Commit does not belong")
    98  	if runtime.GOOS != "windows" {
    99  		cp.ExpectExitCode(1)
   100  		ts.IgnoreLogErrors()
   101  	}
   102  
   103  	// Check that branch and commitID were not updated
   104  	_, err = project.FromPath(pjfilepath)
   105  	suite.Require().NoError(err)
   106  	suite.Equal(originalCommitID, ts.CommitID(), "commitID was updated after switching branches")
   107  }
   108  
   109  func (suite *SwitchIntegrationTestSuite) TestJSON() {
   110  	suite.OnlyRunForTags(tagsuite.Switch, tagsuite.JSON)
   111  	ts := e2e.New(suite.T(), false)
   112  	defer ts.Close()
   113  
   114  	cp := ts.Spawn("checkout", "ActiveState-CLI/Branches", ".")
   115  	cp.Expect("Skipping runtime setup")
   116  	cp.Expect("Checked out")
   117  	cp.ExpectExitCode(0)
   118  
   119  	cp = ts.Spawn("switch", "firstbranch", "--output", "json")
   120  	cp.Expect(`"branch":`)
   121  	cp.ExpectExitCode(0)
   122  	// AssertValidJSON(suite.T(), cp) // cannot assert here due to "Skipping runtime setup" notice
   123  }
   124  
   125  func TestSwitchIntegrationTestSuite(t *testing.T) {
   126  	suite.Run(t, new(SwitchIntegrationTestSuite))
   127  }