From 8028459efb3fb8f834d667ae172ed59cd3491efa Mon Sep 17 00:00:00 2001 From: Nathan Rijksen Date: Thu, 31 Aug 2023 14:28:14 -0700 Subject: [PATCH] Attempt to work around pty output issues on windows --- internal/testhelpers/e2e/session.go | 1 + internal/testhelpers/e2e/spawn.go | 6 ++++++ test/integration/install_scripts_int_test.go | 14 +++++++------- test/integration/secrets_int_test.go | 2 +- test/integration/shared_int_test.go | 5 +++-- test/integration/update_int_test.go | 9 +++++---- test/integration/vscode_int_test.go | 16 +++++++++------- 7 files changed, 32 insertions(+), 21 deletions(-) diff --git a/internal/testhelpers/e2e/session.go b/internal/testhelpers/e2e/session.go index 65f05ec54b..f2daf9a9d0 100644 --- a/internal/testhelpers/e2e/session.go +++ b/internal/testhelpers/e2e/session.go @@ -255,6 +255,7 @@ func (s *Session) SpawnCmdWithOpts(exe string, optSetters ...SpawnOptSetter) *Sp }), termtest.OptDefaultTimeout(defaultnTimeout), termtest.OptCols(140), + termtest.OptRows(30), // Needs to be able to accommodate JSON output ) // Work around issue where multiline values sometimes have the wrong line endings diff --git a/internal/testhelpers/e2e/spawn.go b/internal/testhelpers/e2e/spawn.go index 0c6aa596e0..fea98cbe3b 100644 --- a/internal/testhelpers/e2e/spawn.go +++ b/internal/testhelpers/e2e/spawn.go @@ -31,6 +31,12 @@ func (s *SpawnedCmd) Executable() string { return s.TermTest.Cmd().Path } +// StrippedSnapshot returns the snapshot with trimmed whitespace and stripped line endings +// Mainly intended for JSON parsing +func (s *SpawnedCmd) StrippedSnapshot() string { + return strings.Trim(strings.ReplaceAll(s.TermTest.Snapshot(), "\n", ""), "\x00\x20\x0a\x0d") +} + func (s *SpawnedCmd) ExpectRe(v string, opts ...termtest.SetExpectOpt) error { expectOpts, err := termtest.NewExpectOpts(opts...) if err != nil { diff --git a/test/integration/install_scripts_int_test.go b/test/integration/install_scripts_int_test.go index 504d7669cc..af77c5b35a 100644 --- a/test/integration/install_scripts_int_test.go +++ b/test/integration/install_scripts_int_test.go @@ -5,10 +5,14 @@ import ( "fmt" "path/filepath" "runtime" - "strings" "testing" "time" + "github.com/ActiveState/termtest" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "github.com/thoas/go-funk" + "github.com/ActiveState/cli/internal/constants" "github.com/ActiveState/cli/internal/environment" "github.com/ActiveState/cli/internal/fileutils" @@ -17,10 +21,6 @@ import ( "github.com/ActiveState/cli/internal/osutils" "github.com/ActiveState/cli/internal/testhelpers/e2e" "github.com/ActiveState/cli/internal/testhelpers/tagsuite" - "github.com/ActiveState/termtest" - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" - "github.com/thoas/go-funk" ) type InstallScriptsIntegrationTestSuite struct { @@ -242,8 +242,8 @@ func (suite *InstallScriptsIntegrationTestSuite) assertCorrectVersion(ts *e2e.Se cp := ts.SpawnCmd(stateExec, "--version", "--output=json") cp.ExpectExitCode(0) actual := versionData{} - out := strings.Trim(cp.Output(), "\x00") - json.Unmarshal([]byte(out), &actual) + out := cp.StrippedSnapshot() + suite.Require().NoError(json.Unmarshal([]byte(out), &actual)) if expectedVersion != "" { suite.Equal(expectedVersion, actual.Version) diff --git a/test/integration/secrets_int_test.go b/test/integration/secrets_int_test.go index e1dab54c26..f7fd6b3a45 100644 --- a/test/integration/secrets_int_test.go +++ b/test/integration/secrets_int_test.go @@ -44,7 +44,7 @@ func (suite *SecretsIntegrationTestSuite) TestSecrets_JSON() { cp = ts.Spawn("secrets", "get", "project.test-secret", "--output", "json") cp.ExpectExitCode(0) - suite.Equal(string(expected), cp.Output()) + suite.Equal(string(expected), cp.StrippedSnapshot()) cp = ts.Spawn("secrets", "sync") cp.Expect("Operating on project cli-integration-tests/Python3") diff --git a/test/integration/shared_int_test.go b/test/integration/shared_int_test.go index 3c7ccf765f..ba214d4ecb 100644 --- a/test/integration/shared_int_test.go +++ b/test/integration/shared_int_test.go @@ -7,9 +7,10 @@ import ( "strings" "testing" + "github.com/stretchr/testify/assert" + "github.com/ActiveState/cli/internal/logging" "github.com/ActiveState/cli/internal/testhelpers/e2e" - "github.com/stretchr/testify/assert" ) func init() { @@ -22,7 +23,7 @@ func init() { // any non-JSON/structured output. // This should only be called after a command has executed and all output is available. func AssertValidJSON(t *testing.T, cp *e2e.SpawnedCmd) { - output := cp.Output() + output := cp.StrippedSnapshot() if runtime.GOOS != "windows" { assert.True(t, json.Valid([]byte(output)), "The command produced invalid JSON/structured output:\n"+output) } else { diff --git a/test/integration/update_int_test.go b/test/integration/update_int_test.go index d92b154e69..f7e0a63846 100644 --- a/test/integration/update_int_test.go +++ b/test/integration/update_int_test.go @@ -11,6 +11,9 @@ import ( "testing" "time" + "github.com/ActiveState/termtest" + "github.com/stretchr/testify/suite" + "github.com/ActiveState/cli/internal/config" "github.com/ActiveState/cli/internal/constants" "github.com/ActiveState/cli/internal/exeutils" @@ -20,8 +23,6 @@ import ( "github.com/ActiveState/cli/internal/rtutils/singlethread" "github.com/ActiveState/cli/internal/testhelpers/e2e" "github.com/ActiveState/cli/internal/testhelpers/tagsuite" - "github.com/ActiveState/termtest" - "github.com/stretchr/testify/suite" ) type UpdateIntegrationTestSuite struct { @@ -73,7 +74,7 @@ func (suite *UpdateIntegrationTestSuite) versionCompare(ts *e2e.Session, expecte cp.ExpectExitCode(0) version := versionData{} - out := strings.Trim(cp.Output(), "\x00") + out := cp.StrippedSnapshot() json.Unmarshal([]byte(out), &version) matcher(expected, version.Version, fmt.Sprintf("Version could not be matched, output:\n\n%s", out)) @@ -88,7 +89,7 @@ func (suite *UpdateIntegrationTestSuite) branchCompare(ts *e2e.Session, expected cp.ExpectExitCode(0, termtest.OptExpectTimeout(30*time.Second)) branch := branchData{} - out := strings.Trim(cp.Output(), "\x00") + out := cp.StrippedSnapshot() json.Unmarshal([]byte(out), &branch) matcher(expected, branch.Branch, fmt.Sprintf("Branch could not be matched, output:\n\n%s", out)) diff --git a/test/integration/vscode_int_test.go b/test/integration/vscode_int_test.go index 1bbe6a63a1..3629be9db0 100644 --- a/test/integration/vscode_int_test.go +++ b/test/integration/vscode_int_test.go @@ -38,7 +38,7 @@ func (suite *PushIntegrationTestSuite) TestInitAndPush_VSCode() { e2e.OptWD(wd), ) cp.ExpectExitCode(0) - suite.Equal("", cp.Output()) + suite.Equal("", strings.TrimSpace(cp.Snapshot())) // check that pushed project exists cp = ts.Spawn("show", namespace) @@ -73,8 +73,9 @@ func (suite *ShowIntegrationTestSuite) TestShow_VSCode() { } var out ShowOutput - err := json.Unmarshal([]byte(cp.Output()), &out) - suite.Require().NoError(err, "Failed to parse JSON from: %s", cp.Output()) + snapshot := cp.StrippedSnapshot() + err := json.Unmarshal([]byte(snapshot), &out) + suite.Require().NoError(err, "Failed to parse JSON from: %s", snapshot) suite.Equal("Show", out.Name) suite.Equal(e2e.PersistentUsername, out.Organization) suite.Equal("Public", out.Visibility) @@ -136,11 +137,11 @@ func (suite *AuthIntegrationTestSuite) TestAuth_VSCode() { ) cp.Expect(`"privateProjects":false}`) cp.ExpectExitCode(0) - suite.Equal(string(expected), cp.Output()) + suite.Equal(string(expected), strings.TrimSpace(cp.Snapshot())) cp = ts.Spawn("export", "jwt", "--output", "editor") cp.ExpectExitCode(0) - suite.Assert().Greater(len(cp.Output()), 3, "expected jwt token to be non-empty") + suite.Assert().Greater(strings.TrimSpace(cp.Snapshot()), 3, "expected jwt token to be non-empty") } func (suite *PackageIntegrationTestSuite) TestPackages_VSCode() { @@ -165,8 +166,9 @@ func (suite *PackageIntegrationTestSuite) TestPackages_VSCode() { } var po []PackageOutput - err := json.Unmarshal([]byte(cp.Output()), &po) - suite.Require().NoError(err, "Could not parse JSON from: %s", cp.Output()) + out := cp.StrippedSnapshot() + err := json.Unmarshal([]byte(out), &po) + suite.Require().NoError(err, "Could not parse JSON from: %s", out) suite.Len(po, 2) }