Skip to content

Commit

Permalink
stdinservice_test: Simplify platform differences
Browse files Browse the repository at this point in the history
Python auto-converts newlines into the platform-dependent form, which trips up
our equality check.  Make sure that we don't have newlines in the output.

Write through `sys.stdout` (it works in both Python 2 and Python 3)

PiperOrigin-RevId: 704729437
  • Loading branch information
gnoack authored and copybara-github committed Dec 16, 2024
1 parent ceb2de0 commit c87a46b
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions fleetspeak/src/client/stdinservice/stdinservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestStdinServiceWithEcho(t *testing.T) {
&fspb.Message{
MessageType: "StdinServiceInputMessage",
Data: anypbtest.New(t, &sspb.InputMessage{
Args: []string{"-c", `print("foo bar")`},
Args: []string{"-c", `import sys; sys.stdout.write("foo bar")`},
}),
})
if err != nil {
Expand All @@ -70,10 +70,8 @@ func TestStdinServiceWithEcho(t *testing.T) {
t.Fatal(err)
}

wantStdout := []byte("foo bar\n")
wantStdoutWin := []byte("foo bar\r\n")
if !bytes.Equal(om.Stdout, wantStdout) &&
!bytes.Equal(om.Stdout, wantStdoutWin) {
wantStdout := []byte("foo bar")
if !bytes.Equal(om.Stdout, wantStdout) {
t.Fatalf("unexpected output; got %q, want %q", om.Stdout, wantStdout)
}
}
Expand Down Expand Up @@ -102,16 +100,8 @@ func TestStdinServiceWithCat(t *testing.T) {
MessageType: "StdinServiceInputMessage",
Data: anypbtest.New(t, &sspb.InputMessage{
Args: []string{"-c", `
try:
my_input = raw_input # Python2 compat
except NameError:
my_input = input
try:
while True:
print(my_input())
except EOFError:
pass
import sys, shutil
shutil.copyfileobj(sys.stdin, sys.stdout)
`},
Input: []byte("foo bar"),
}),
Expand All @@ -132,10 +122,8 @@ except EOFError:
t.Fatal(err)
}

wantStdout := []byte("foo bar\n")
wantStdoutWin := []byte("foo bar\r\n")
if !bytes.Equal(om.Stdout, wantStdout) &&
!bytes.Equal(om.Stdout, wantStdoutWin) {
wantStdout := []byte("foo bar")
if !bytes.Equal(om.Stdout, wantStdout) {
t.Fatalf("unexpected output; got %q, want %q", om.Stdout, wantStdout)
}
}
Expand Down

0 comments on commit c87a46b

Please sign in to comment.