-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc/android: improve exit code workaround
go_android_exec gets the exit status of the process run inside the Android emulator by sending a small shell script that runs the desired command and then prints "exitcode=" followed by the exit code. This is necessary because adb does not reliably pass through the exit status of the subprocess. An old bug about this (https://code.google.com/p/android/issues/detail?id=3254) was closed in 2016 as fixed in Android N (7.0), but it seems that the adb on the Android builder at least still sometimes fails to pass through the exit code. Unfortunately, this workaround has the effect of injecting "exitcode=N" into the output of the subprocess it runs, which messes up tests that are looking for golden output from a subprocess. Fix this by inserting a filter Writer that looks for the final "exitcode=N" and strips it from the exec wrapper's own stdout. For golang#15919. This will help us in cleaning up "host tests" for golang#37486. Change-Id: I9859f5b215e0ec4a7e33ada04a1857f3cfaf55ae Reviewed-on: https://go-review.googlesource.com/c/go/+/488975 TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Austin Clements <[email protected]> Run-TryBot: Austin Clements <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
- Loading branch information
Showing
2 changed files
with
171 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build !(windows || js || wasip1) | ||
|
||
package main | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestExitCodeFilter(t *testing.T) { | ||
// Write text to the filter one character at a time. | ||
var out strings.Builder | ||
f, exitStr := newExitCodeFilter(&out) | ||
// Embed a "fake" exit code in the middle to check that we don't get caught on it. | ||
pre := "abc" + exitStr + "123def" | ||
text := pre + exitStr + `1` | ||
for i := 0; i < len(text); i++ { | ||
_, err := f.Write([]byte{text[i]}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
// The "pre" output should all have been flushed already. | ||
if want, got := pre, out.String(); want != got { | ||
t.Errorf("filter should have already flushed %q, but flushed %q", want, got) | ||
} | ||
|
||
code, err := f.Finish() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Nothing more should have been written to out. | ||
if want, got := pre, out.String(); want != got { | ||
t.Errorf("want output %q, got %q", want, got) | ||
} | ||
if want := 1; want != code { | ||
t.Errorf("want exit code %d, got %d", want, code) | ||
} | ||
} | ||
|
||
func TestExitCodeMissing(t *testing.T) { | ||
var wantErr *regexp.Regexp | ||
check := func(text string) { | ||
t.Helper() | ||
var out strings.Builder | ||
f, exitStr := newExitCodeFilter(&out) | ||
if want := "exitcode="; want != exitStr { | ||
t.Fatalf("test assumes exitStr will be %q, but got %q", want, exitStr) | ||
} | ||
f.Write([]byte(text)) | ||
_, err := f.Finish() | ||
// We should get a no exit code error | ||
if err == nil || !wantErr.MatchString(err.Error()) { | ||
t.Errorf("want error matching %s, got %s", wantErr, err) | ||
} | ||
// And it should flush all output (even if it looks | ||
// like we may be getting an exit code) | ||
if got := out.String(); text != got { | ||
t.Errorf("want full output %q, got %q", text, got) | ||
} | ||
} | ||
wantErr = regexp.MustCompile("^no exit code") | ||
check("abc") | ||
check("exitcode") | ||
check("exitcode=") | ||
check("exitcode=123\n") | ||
wantErr = regexp.MustCompile("^bad exit code: .* value out of range") | ||
check("exitcode=999999999999999999999999") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters