Skip to content

Commit

Permalink
Attempt to work around pty output issues on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Naatan committed Aug 31, 2023
1 parent a260bf3 commit 8028459
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
1 change: 1 addition & 0 deletions internal/testhelpers/e2e/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions internal/testhelpers/e2e/spawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions test/integration/install_scripts_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/secrets_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions test/integration/shared_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions test/integration/update_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down
16 changes: 9 additions & 7 deletions test/integration/vscode_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
}
Expand Down

0 comments on commit 8028459

Please sign in to comment.