github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/test/integration/revert_int_test.go (about) 1 package integration 2 3 import ( 4 "fmt" 5 "path/filepath" 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 ) 13 14 type RevertIntegrationTestSuite struct { 15 tagsuite.Suite 16 } 17 18 func (suite *RevertIntegrationTestSuite) TestRevert() { 19 suite.OnlyRunForTags(tagsuite.Revert) 20 ts := e2e.New(suite.T(), false) 21 defer ts.Close() 22 ts.LoginAsPersistentUser() 23 24 namespace := "ActiveState-CLI/Revert" 25 cp := ts.SpawnWithOpts(e2e.OptArgs("checkout", namespace)) 26 cp.Expect("Skipping runtime setup") 27 cp.Expect("Checked out project") 28 cp.ExpectExitCode(0) 29 wd := filepath.Join(ts.Dirs.Work, "Revert") 30 31 // Revert the commit that added urllib3. 32 commitID := "1f4f4f7d-7883-400e-b2ad-a5803c018ecd" 33 cp = ts.SpawnWithOpts(e2e.OptArgs("revert", commitID), e2e.OptWD(wd)) 34 cp.Expect(fmt.Sprintf("Operating on project %s", namespace)) 35 cp.Expect("You are about to revert the following commit:") 36 cp.Expect(commitID) 37 cp.SendLine("y") 38 cp.Expect("Successfully reverted commit:") 39 cp.ExpectExitCode(0) 40 41 // Verify the commit history has both the new revert commit and all prior history. 42 cp = ts.SpawnWithOpts( 43 e2e.OptArgs("history"), 44 e2e.OptWD(wd), 45 ) 46 cp.Expect("Reverted commit for commit " + commitID) 47 cp.Expect("- urllib3") 48 cp.Expect("+ argparse") // parent commit 49 cp.Expect("+ urllib3") // commit whose changes were just reverted 50 cp.Expect("+ python") // initial commit 51 52 // Verify that argparse still exists (it was not reverted along with urllib3). 53 cp = ts.SpawnWithOpts( 54 e2e.OptArgs("shell", "Revert"), 55 e2e.OptAppendEnv(constants.DisableRuntime+"=false"), 56 ) 57 cp.ExpectInput(e2e.RuntimeSourcingTimeoutOpt) 58 cp.SendLine("python3") 59 cp.Expect("3.9.15") 60 cp.SendLine("import urllib3") 61 cp.Expect("No module named 'urllib3'") 62 cp.SendLine("import argparse") 63 suite.Assert().NotContains(cp.Output(), "No module named 'argparse'") 64 cp.SendLine("exit()") // exit python3 65 cp.SendLine("exit") // exit state shell 66 cp.ExpectExitCode(0) 67 } 68 69 func (suite *RevertIntegrationTestSuite) TestRevertRemote() { 70 suite.OnlyRunForTags(tagsuite.Revert) 71 ts := e2e.New(suite.T(), false) 72 defer ts.Close() 73 74 cp := ts.Spawn("checkout", "ActiveState-CLI/Revert", ".") 75 cp.Expect("Skipping runtime setup") 76 cp.Expect("Checked out project") 77 cp.ExpectExitCode(0) 78 79 cp = ts.Spawn("install", "requests") 80 cp.Expect("Package added") 81 cp.ExpectExitCode(0) 82 83 cp = ts.Spawn("revert", "REMOTE", "--non-interactive") 84 cp.Expect("Successfully reverted") 85 cp.ExpectExitCode(0) 86 87 cp = ts.Spawn("history") 88 cp.Expect("- requests") 89 cp.Expect("+ requests") 90 cp.ExpectExitCode(0) 91 } 92 93 func (suite *RevertIntegrationTestSuite) TestRevert_failsOnCommitNotInHistory() { 94 suite.OnlyRunForTags(tagsuite.Revert) 95 ts := e2e.New(suite.T(), false) 96 defer ts.Close() 97 98 namespace := "ActiveState-CLI/small-python" 99 cp := ts.SpawnWithOpts(e2e.OptArgs("checkout", namespace)) 100 cp.Expect("Skipping runtime setup") 101 cp.Expect("Checked out project") 102 cp.ExpectExitCode(0) 103 wd := filepath.Join(ts.Dirs.Work, "small-python") 104 105 // valid commit id not from project 106 commitID := "cb9b1aab-8e40-4a1d-8ad6-5ea112da40f1" // from Perl-5.32 107 cp = ts.SpawnWithOpts(e2e.OptArgs("revert", commitID), e2e.OptWD(wd)) 108 cp.Expect(fmt.Sprintf("Operating on project %s", namespace)) 109 cp.SendLine("Y") 110 cp.Expect(commitID) 111 cp.Expect("not found") 112 cp.ExpectNotExitCode(0) 113 ts.IgnoreLogErrors() 114 } 115 116 func (suite *RevertIntegrationTestSuite) TestRevertTo() { 117 suite.OnlyRunForTags(tagsuite.Revert) 118 ts := e2e.New(suite.T(), false) 119 defer ts.Close() 120 ts.LoginAsPersistentUser() 121 122 namespace := "ActiveState-CLI/Revert" 123 cp := ts.SpawnWithOpts(e2e.OptArgs("checkout", namespace)) 124 cp.Expect("Skipping runtime setup") 125 cp.Expect("Checked out project") 126 cp.ExpectExitCode(0) 127 wd := filepath.Join(ts.Dirs.Work, "Revert") 128 129 // Revert the commit that added urllib3. 130 commitID := "1f4f4f7d-7883-400e-b2ad-a5803c018ecd" 131 cp = ts.SpawnWithOpts(e2e.OptArgs("revert", "--to", commitID), e2e.OptWD(wd)) 132 cp.Expect(fmt.Sprintf("Operating on project %s", namespace)) 133 cp.SendLine("Y") 134 cp.Expect("You are about to revert to the following commit:") 135 cp.Expect(commitID) 136 cp.Expect("Successfully reverted to commit:") 137 cp.ExpectExitCode(0) 138 139 // Verify the commit history has both the new revert commit and all prior history. 140 cp = ts.SpawnWithOpts( 141 e2e.OptArgs("history"), 142 e2e.OptWD(wd), 143 ) 144 cp.Expect("Revert to commit " + commitID) 145 cp.Expect("- argparse") // effectively reverting previous commit 146 cp.Expect("+ argparse") // commit being effectively reverted 147 cp.Expect("+ urllib3") // commit reverted to 148 cp.Expect("+ python") // initial commit 149 } 150 151 func (suite *RevertIntegrationTestSuite) TestRevertTo_failsOnCommitNotInHistory() { 152 suite.OnlyRunForTags(tagsuite.Revert) 153 ts := e2e.New(suite.T(), false) 154 defer ts.Close() 155 156 namespace := "ActiveState-CLI/small-python" 157 cp := ts.SpawnWithOpts(e2e.OptArgs("checkout", namespace)) 158 cp.Expect("Skipping runtime setup") 159 cp.Expect("Checked out project") 160 cp.ExpectExitCode(0) 161 wd := filepath.Join(ts.Dirs.Work, "small-python") 162 163 // valid commit id not from project 164 commitID := "cb9b1aab-8e40-4a1d-8ad6-5ea112da40f1" // from Perl-5.32 165 cp = ts.SpawnWithOpts(e2e.OptArgs("revert", "--to", commitID), e2e.OptWD(wd)) 166 cp.Expect(fmt.Sprintf("Operating on project %s", namespace)) 167 cp.SendLine("Y") 168 cp.Expect(commitID) 169 cp.Expect("not found") 170 cp.ExpectNotExitCode(0) 171 ts.IgnoreLogErrors() 172 } 173 174 func (suite *RevertIntegrationTestSuite) TestJSON() { 175 suite.OnlyRunForTags(tagsuite.Revert, tagsuite.JSON) 176 ts := e2e.New(suite.T(), false) 177 defer ts.Close() 178 179 cp := ts.Spawn("checkout", "ActiveState-CLI/Revert", ".") 180 cp.Expect("Skipping runtime setup") 181 cp.Expect("Checked out project") 182 cp.ExpectExitCode(0) 183 184 cp = ts.Spawn("revert", "--to", "1f4f4f7d-7883-400e-b2ad-a5803c018ecd", "-o", "json") 185 cp.Expect(`{"current_commit_id":`) 186 cp.ExpectExitCode(0) 187 // AssertValidJSON(suite.T(), cp) // cannot assert here due to "Skipping runtime setup" notice 188 } 189 190 func TestRevertIntegrationTestSuite(t *testing.T) { 191 suite.Run(t, new(RevertIntegrationTestSuite)) 192 }