xref: /aosp_15_r20/external/swiftshader/tests/regres/shell/shell_windows.go (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1*03ce13f7SAndroid Build Coastguard Worker// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2*03ce13f7SAndroid Build Coastguard Worker//
3*03ce13f7SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*03ce13f7SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*03ce13f7SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*03ce13f7SAndroid Build Coastguard Worker//
7*03ce13f7SAndroid Build Coastguard Worker//    http://www.apache.org/licenses/LICENSE-2.0
8*03ce13f7SAndroid Build Coastguard Worker//
9*03ce13f7SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*03ce13f7SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*03ce13f7SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*03ce13f7SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*03ce13f7SAndroid Build Coastguard Worker// limitations under the License.
14*03ce13f7SAndroid Build Coastguard Worker
15*03ce13f7SAndroid Build Coastguard Workerpackage shell
16*03ce13f7SAndroid Build Coastguard Worker
17*03ce13f7SAndroid Build Coastguard Workerimport (
18*03ce13f7SAndroid Build Coastguard Worker	"bytes"
19*03ce13f7SAndroid Build Coastguard Worker	"os/exec"
20*03ce13f7SAndroid Build Coastguard Worker	"time"
21*03ce13f7SAndroid Build Coastguard Worker)
22*03ce13f7SAndroid Build Coastguard Worker
23*03ce13f7SAndroid Build Coastguard Worker// Exec runs the executable exe with the given arguments, in the working
24*03ce13f7SAndroid Build Coastguard Worker// directory wd, with the custom environment flags.
25*03ce13f7SAndroid Build Coastguard Worker// If the process does not finish within timeout a errTimeout will be returned.
26*03ce13f7SAndroid Build Coastguard Workerfunc Exec(timeout time.Duration, exe, wd string, env []string, toStdin string, args ...string) ([]byte, error) {
27*03ce13f7SAndroid Build Coastguard Worker	stdin := &bytes.Buffer{}
28*03ce13f7SAndroid Build Coastguard Worker	stdin.WriteString(toStdin)
29*03ce13f7SAndroid Build Coastguard Worker
30*03ce13f7SAndroid Build Coastguard Worker	b := bytes.Buffer{}
31*03ce13f7SAndroid Build Coastguard Worker	c := exec.Command(exe, args...)
32*03ce13f7SAndroid Build Coastguard Worker	c.Dir = wd
33*03ce13f7SAndroid Build Coastguard Worker	c.Env = env
34*03ce13f7SAndroid Build Coastguard Worker	c.Stdin = stdin
35*03ce13f7SAndroid Build Coastguard Worker	c.Stdout = &b
36*03ce13f7SAndroid Build Coastguard Worker	c.Stderr = &b
37*03ce13f7SAndroid Build Coastguard Worker
38*03ce13f7SAndroid Build Coastguard Worker	if err := c.Start(); err != nil {
39*03ce13f7SAndroid Build Coastguard Worker		return nil, err
40*03ce13f7SAndroid Build Coastguard Worker	}
41*03ce13f7SAndroid Build Coastguard Worker
42*03ce13f7SAndroid Build Coastguard Worker	res := make(chan error)
43*03ce13f7SAndroid Build Coastguard Worker	go func() { res <- c.Wait() }()
44*03ce13f7SAndroid Build Coastguard Worker
45*03ce13f7SAndroid Build Coastguard Worker	select {
46*03ce13f7SAndroid Build Coastguard Worker	case <-time.NewTimer(timeout).C:
47*03ce13f7SAndroid Build Coastguard Worker		c.Process.Kill()
48*03ce13f7SAndroid Build Coastguard Worker		return b.Bytes(), ErrTimeout{exe, timeout}
49*03ce13f7SAndroid Build Coastguard Worker	case err := <-res:
50*03ce13f7SAndroid Build Coastguard Worker		return b.Bytes(), err
51*03ce13f7SAndroid Build Coastguard Worker	}
52*03ce13f7SAndroid Build Coastguard Worker}
53