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

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"runtime"
     7  	"testing"
     8  
     9  	"github.com/ActiveState/cli/internal/analytics/client/sync/reporters"
    10  	anaConst "github.com/ActiveState/cli/internal/analytics/constants"
    11  	"github.com/ActiveState/cli/internal/constants"
    12  	"github.com/ActiveState/cli/internal/fileutils"
    13  	"github.com/ActiveState/cli/internal/testhelpers/e2e"
    14  	"github.com/ActiveState/cli/internal/testhelpers/suite"
    15  	"github.com/ActiveState/cli/internal/testhelpers/tagsuite"
    16  	"github.com/ActiveState/cli/pkg/localcommit"
    17  	"github.com/ActiveState/cli/pkg/platform/api/buildplanner/types"
    18  	"github.com/ActiveState/cli/pkg/platform/runtime/buildscript"
    19  	"github.com/ActiveState/cli/pkg/project"
    20  )
    21  
    22  type PullIntegrationTestSuite struct {
    23  	tagsuite.Suite
    24  }
    25  
    26  func (suite *PullIntegrationTestSuite) TestPull() {
    27  	suite.OnlyRunForTags(tagsuite.Pull)
    28  	ts := e2e.New(suite.T(), false)
    29  	defer ts.Close()
    30  
    31  	ts.PrepareProject("ActiveState-CLI/Python3", "59404293-e5a9-4fd0-8843-77cd4761b5b5")
    32  
    33  	cp := ts.Spawn("pull")
    34  	cp.Expect("Operating on project")
    35  	cp.Expect("ActiveState-CLI/Python3")
    36  	cp.Expect("activestate.yaml has been updated")
    37  	cp.ExpectExitCode(0)
    38  
    39  	suite.assertMergeStrategyNotification(ts, string(types.MergeCommitStrategyFastForward))
    40  
    41  	cp = ts.Spawn("pull")
    42  	cp.Expect("already up to date")
    43  	cp.ExpectExitCode(0)
    44  }
    45  
    46  func (suite *PullIntegrationTestSuite) TestPull_Merge() {
    47  	suite.OnlyRunForTags(tagsuite.Pull)
    48  	unPulledCommit := "882ae76e-fbb7-4989-acc9-9a8b87d49388"
    49  
    50  	ts := e2e.New(suite.T(), false)
    51  	defer ts.Close()
    52  
    53  	ts.PrepareProject("ActiveState-CLI/cli", unPulledCommit)
    54  
    55  	ts.LoginAsPersistentUser()
    56  
    57  	cp := ts.SpawnWithOpts(e2e.OptArgs("push"))
    58  	cp.Expect("Your project has new changes available")
    59  	cp.ExpectExitCode(1)
    60  	ts.IgnoreLogErrors()
    61  
    62  	cp = ts.SpawnWithOpts(e2e.OptArgs("pull"))
    63  	cp.Expect("Merging history")
    64  	cp.ExpectExitCode(0)
    65  
    66  	exe := ts.ExecutablePath()
    67  	if runtime.GOOS == "windows" {
    68  		exe = filepath.ToSlash(exe)
    69  	}
    70  	cp = ts.SpawnCmd("bash", "-c", fmt.Sprintf("%s history | head -n 10", exe))
    71  	cp.Expect("Merged")
    72  	cp.ExpectExitCode(0)
    73  
    74  	suite.assertMergeStrategyNotification(ts, string(types.MergeCommitStrategyRecursiveKeepOnConflict))
    75  }
    76  
    77  func (suite *PullIntegrationTestSuite) TestMergeBuildScript() {
    78  	suite.OnlyRunForTags(tagsuite.Pull, tagsuite.BuildScripts)
    79  	ts := e2e.New(suite.T(), false)
    80  	defer ts.Close()
    81  
    82  	cp := ts.Spawn("config", "set", constants.OptinBuildscriptsConfig, "true")
    83  	cp.ExpectExitCode(0)
    84  
    85  	cp = ts.Spawn("checkout", "ActiveState-CLI/Merge#447b8363-024c-4143-bf4e-c96989314fdf", ".")
    86  	cp.Expect("Skipping runtime setup")
    87  	cp.Expect("Checked out")
    88  	cp.ExpectExitCode(0)
    89  
    90  	cp = ts.SpawnWithOpts(
    91  		e2e.OptArgs("install", "requests"),
    92  		e2e.OptAppendEnv(constants.DisableRuntime+"=false"),
    93  	)
    94  	cp.Expect("Package added", e2e.RuntimeSourcingTimeoutOpt)
    95  	cp.ExpectExitCode(0)
    96  
    97  	proj, err := project.FromPath(ts.Dirs.Work)
    98  	suite.NoError(err, "Error loading project")
    99  
   100  	_, err = buildscript.ScriptFromProject(proj)
   101  	suite.Require().NoError(err) // just verify it's a valid build script
   102  
   103  	cp = ts.Spawn("pull")
   104  	cp.Expect("The following changes will be merged")
   105  	cp.Expect("requests (2.30.0 → Auto)")
   106  	cp.Expect("Unable to automatically merge build scripts")
   107  	cp.ExpectNotExitCode(0)
   108  	ts.IgnoreLogErrors()
   109  
   110  	_, err = buildscript.ScriptFromProject(proj)
   111  	suite.Assert().Error(err)
   112  	bytes := fileutils.ReadFileUnsafe(filepath.Join(ts.Dirs.Work, constants.BuildScriptFileName))
   113  	suite.Assert().Contains(string(bytes), "<<<<<<<", "No merge conflict markers are in build script")
   114  	suite.Assert().Contains(string(bytes), "=======", "No merge conflict markers are in build script")
   115  	suite.Assert().Contains(string(bytes), ">>>>>>>", "No merge conflict markers are in build script")
   116  
   117  	// Verify the local commit was updated to the merge commit.
   118  	// Note: even though the buildscript merge failed, a merge commit was still created. After resolving
   119  	// buildscript conflicts, `state commit` should have something new to commit.
   120  	commit, err := localcommit.Get(ts.Dirs.Work)
   121  	suite.Require().NoError(err)
   122  	suite.Assert().NotEqual(commit.String(), "447b8363-024c-4143-bf4e-c96989314fdf", "localcommit not updated to merged commit")
   123  }
   124  
   125  func (suite *PullIntegrationTestSuite) assertMergeStrategyNotification(ts *e2e.Session, strategy string) {
   126  	conflictEvents := filterEvents(parseAnalyticsEvents(suite, ts), func(e reporters.TestLogEntry) bool {
   127  		return e.Category == anaConst.CatInteractions && e.Action == anaConst.ActVcsConflict
   128  	})
   129  	suite.Assert().Equal(1, len(conflictEvents), "Should have a single VCS Conflict event report")
   130  	suite.Assert().Equal(strategy, conflictEvents[0].Label)
   131  }
   132  
   133  func (suite *PullIntegrationTestSuite) TestPullNoCommonParent() {
   134  	suite.OnlyRunForTags(tagsuite.Pull)
   135  	ts := e2e.New(suite.T(), false)
   136  	defer ts.Close()
   137  
   138  	ts.PrepareProject("ActiveState-CLI/Python3", "19c5a165-167d-48f1-b5e0-826c2fed6ab7")
   139  
   140  	cp := ts.Spawn("pull")
   141  	cp.Expect("Operating on project")
   142  	cp.Expect("ActiveState-CLI/Python3")
   143  	cp.Expect("no common")
   144  	cp.Expect("To review your project history")
   145  	cp.ExpectExitCode(1)
   146  	ts.IgnoreLogErrors()
   147  }
   148  
   149  func TestPullIntegrationTestSuite(t *testing.T) {
   150  	suite.Run(t, new(PullIntegrationTestSuite))
   151  }