From 67f05a9275eee4136976c8bb5a68593e7a3da849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Noack?= Date: Mon, 16 Dec 2024 05:16:56 -0800 Subject: [PATCH] stdinservice_test: Simplify platform differences 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: 706670816 --- .../client/stdinservice/stdinservice_test.go | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/fleetspeak/src/client/stdinservice/stdinservice_test.go b/fleetspeak/src/client/stdinservice/stdinservice_test.go index 7d271b27a..f4b389ce3 100644 --- a/fleetspeak/src/client/stdinservice/stdinservice_test.go +++ b/fleetspeak/src/client/stdinservice/stdinservice_test.go @@ -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 { @@ -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) } } @@ -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"), }), @@ -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) } }