Fix "PR c++/92804 ICE trying to use concept as a nested-name-specifier"
[official-gcc.git] / libgo / go / os / executable_test.go
blobf25ee0c95a1cdf11202b25557a72216c054abedc
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package os_test
7 import (
8 "fmt"
9 "internal/testenv"
10 "os"
11 osexec "os/exec"
12 "path/filepath"
13 "runtime"
14 "testing"
17 const executable_EnvVar = "OSTEST_OUTPUT_EXECPATH"
19 func TestExecutable(t *testing.T) {
20 testenv.MustHaveExec(t)
21 ep, err := os.Executable()
22 if err != nil {
23 t.Fatalf("Executable failed: %v", err)
25 // we want fn to be of the form "dir/prog"
26 dir := filepath.Dir(filepath.Dir(ep))
27 fn, err := filepath.Rel(dir, ep)
28 if err != nil {
29 t.Fatalf("filepath.Rel: %v", err)
32 cmd := &osexec.Cmd{}
33 // make child start with a relative program path
34 cmd.Dir = dir
35 cmd.Path = fn
36 // forge argv[0] for child, so that we can verify we could correctly
37 // get real path of the executable without influenced by argv[0].
38 cmd.Args = []string{"-", "-test.run=XXXX"}
39 if runtime.GOOS == "openbsd" || runtime.GOOS == "aix" {
40 // OpenBSD and AIX rely on argv[0]
41 cmd.Args[0] = fn
43 cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", executable_EnvVar))
44 out, err := cmd.CombinedOutput()
45 if err != nil {
46 t.Fatalf("exec(self) failed: %v", err)
48 outs := string(out)
49 if !filepath.IsAbs(outs) {
50 t.Fatalf("Child returned %q, want an absolute path", out)
52 if !sameFile(outs, ep) {
53 t.Fatalf("Child returned %q, not the same file as %q", out, ep)
57 func sameFile(fn1, fn2 string) bool {
58 fi1, err := os.Stat(fn1)
59 if err != nil {
60 return false
62 fi2, err := os.Stat(fn2)
63 if err != nil {
64 return false
66 return os.SameFile(fi1, fi2)
69 func init() {
70 if e := os.Getenv(executable_EnvVar); e != "" {
71 // first chdir to another path
72 dir := "/"
73 if runtime.GOOS == "windows" {
74 cwd, err := os.Getwd()
75 if err != nil {
76 panic(err)
78 dir = filepath.VolumeName(cwd)
80 os.Chdir(dir)
81 if ep, err := os.Executable(); err != nil {
82 fmt.Fprint(os.Stderr, "ERROR: ", err)
83 } else {
84 fmt.Fprint(os.Stderr, ep)
86 os.Exit(0)