1
0
mirror of https://github.com/golang/go synced 2024-11-24 18:40:14 -07:00
go/misc/go_android_exec/exitcode_test.go
Austin Clements 27aa60f540 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 #15919.

This will help us in cleaning up "host tests" for #37486.

Change-Id: I9859f5b215e0ec4a7e33ada04a1857f3cfaf55ae
Reviewed-on: https://go-review.googlesource.com/c/go/+/488975
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
2023-05-03 14:54:58 +00:00

77 lines
2.1 KiB
Go

// 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")
}