From 97359f001e8db30eb8656387bf51485512d8b11c Mon Sep 17 00:00:00 2001
From: mitchell <mitchellb@activestate.com>
Date: Mon, 21 Oct 2024 13:36:06 -0400
Subject: [PATCH 1/3] Ensure remote installer can run within restricted
 powershells.

---
 test/integration/remote_installer_int_test.go | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/test/integration/remote_installer_int_test.go b/test/integration/remote_installer_int_test.go
index 66891b6645..a0a63cfe2e 100644
--- a/test/integration/remote_installer_int_test.go
+++ b/test/integration/remote_installer_int_test.go
@@ -2,14 +2,19 @@ package integration
 
 import (
 	"fmt"
+	"os/exec"
 	"path/filepath"
+	"runtime"
+	"strings"
 	"testing"
 
+	svcAutostart "github.com/ActiveState/cli/cmd/state-svc/autostart"
 	anaConst "github.com/ActiveState/cli/internal/analytics/constants"
 	"github.com/ActiveState/cli/internal/constants"
 	"github.com/ActiveState/cli/internal/environment"
 	"github.com/ActiveState/cli/internal/fileutils"
 	"github.com/ActiveState/cli/internal/osutils"
+	"github.com/ActiveState/cli/internal/osutils/autostart"
 	"github.com/ActiveState/cli/internal/testhelpers/e2e"
 	"github.com/ActiveState/cli/internal/testhelpers/suite"
 	"github.com/ActiveState/cli/internal/testhelpers/tagsuite"
@@ -25,6 +30,25 @@ func (suite *RemoteInstallIntegrationTestSuite) TestInstall() {
 	ts := e2e.New(suite.T(), false)
 	defer ts.Close()
 
+	// Setup running the remote installer in restricted powershell mode.
+	if runtime.GOOS == "windows" {
+		getPolicy := func() string {
+			policy, err := exec.Command("powershell.exe", "Get-ExecutionPolicy").CombinedOutput()
+			suite.Require().NoError(err, "error getting policy: "+string(policy))
+			return strings.TrimSpace(string(policy))
+		}
+		setPolicy := func(policy string) {
+			output, err := exec.Command("powershell.exe", "Set-ExecutionPolicy", "-ExecutionPolicy", policy).CombinedOutput()
+			suite.Require().NoError(err, "error setting policy: "+string(output))
+		}
+
+		policy := getPolicy()
+		defer setPolicy(policy)
+
+		setPolicy("Restricted")
+		suite.Assert().Equal("Restricted", getPolicy(), "should have set powershell policy to 'Restricted'")
+	}
+
 	tests := []struct {
 		Name    string
 		Version string
@@ -103,6 +127,13 @@ func (suite *RemoteInstallIntegrationTestSuite) TestInstall() {
 				}
 			}
 			suite.Assert().True(sessionTokenFound, "sessionToken was not found in analytics")
+
+			// Verify a startup shortcut was created (we use powershell to create it).
+			if runtime.GOOS == "windows" {
+				shortcut, err := autostart.AutostartPath("", svcAutostart.Options)
+				suite.Require().NoError(err)
+				suite.Assert().FileExists(shortcut)
+			}
 		})
 	}
 }

From 3f5294cfd5de66729d798ddb35bd29889850e9dd Mon Sep 17 00:00:00 2001
From: mitchell <mitchellb@activestate.com>
Date: Mon, 21 Oct 2024 14:57:15 -0400
Subject: [PATCH 2/3] Fixed empty remote installer exe name.

---
 activestate.yaml                              | 4 ++--
 test/integration/remote_installer_int_test.go | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/activestate.yaml b/activestate.yaml
index 88dd7f8c29..0f18c9cfdb 100644
--- a/activestate.yaml
+++ b/activestate.yaml
@@ -181,9 +181,9 @@ scripts:
     value: |
       set -e
       $constants.SET_ENV
-      TARGET=$BUILD_REMOTE_INSTALLER_TARGET
+      TARGET=$constants.BUILD_REMOTE_INSTALLER_TARGET
       if [[ "$GOOS" == "windows" || "$OS" == "Windows_NT" ]]; then
-        TARGET="${BUILD_REMOTE_INSTALLER_TARGET}.exe"
+        TARGET="${constants.BUILD_REMOTE_INSTALLER_TARGET}.exe"
       fi
       GOFLAGS="" go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo@v1.4.0
       cd cmd/state-remote-installer
diff --git a/test/integration/remote_installer_int_test.go b/test/integration/remote_installer_int_test.go
index a0a63cfe2e..1b289788eb 100644
--- a/test/integration/remote_installer_int_test.go
+++ b/test/integration/remote_installer_int_test.go
@@ -143,7 +143,7 @@ func (s *RemoteInstallIntegrationTestSuite) setupTest(ts *e2e.Session) {
 	buildDir := fileutils.Join(root, "build")
 	installerExe := filepath.Join(buildDir, constants.StateRemoteInstallerCmd+osutils.ExeExtension)
 	if !fileutils.FileExists(installerExe) {
-		s.T().Fatal("E2E tests require a state-remote-installer binary. Run `state run build-installer`.")
+		s.T().Fatal("E2E tests require a state-remote-installer binary. Run `state run build-remote-installer`.")
 	}
 	s.remoteInstallerExe = ts.CopyExeToDir(installerExe, filepath.Join(ts.Dirs.Base, "installer"))
 }

From 0f874f8a36d9082b0e8709ff4dee44cc26c8381a Mon Sep 17 00:00:00 2001
From: mitchell <mitchellb@activestate.com>
Date: Wed, 23 Oct 2024 14:20:34 -0400
Subject: [PATCH 3/3] Verify execution policy was reset after the fact.

---
 test/integration/remote_installer_int_test.go | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/test/integration/remote_installer_int_test.go b/test/integration/remote_installer_int_test.go
index 1b289788eb..65949c3fb4 100644
--- a/test/integration/remote_installer_int_test.go
+++ b/test/integration/remote_installer_int_test.go
@@ -43,7 +43,10 @@ func (suite *RemoteInstallIntegrationTestSuite) TestInstall() {
 		}
 
 		policy := getPolicy()
-		defer setPolicy(policy)
+		defer func() {
+			setPolicy(policy)
+			suite.Assert().Equal(policy, getPolicy(), "execution policy was not reset to '"+policy+"'; subsequent test results may be invalid")
+		}()
 
 		setPolicy("Restricted")
 		suite.Assert().Equal("Restricted", getPolicy(), "should have set powershell policy to 'Restricted'")