Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(435)

Delta Between Two Patch Sets: src/pkg/os/executable_test.go

Issue 6736069: code review 6736069: runtime, syscall, os: add os.ExecPath() (string, error)
Left Patch Set: diff -r 8d919bfe75d3 https://code.google.com/p/go/ Created 11 years, 5 months ago
Right Patch Set: diff -r 617db9efbdf1 https://code.google.com/p/go/ Created 11 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/os/executable_procfs.go ('k') | src/pkg/os/executable_windows.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 // Copyright 2012 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.
4
5 // +build darwin linux freebsd netbsd windows
6
7 package os_test
8
9 import (
10 "fmt"
11 "os"
12 oexec "os/exec"
13 "path/filepath"
14 "runtime"
15 "testing"
16 )
17
18 const executable_EnvVar = "OSTEST_OUTPUT_EXECPATH"
19
20 func TestExecutable(t *testing.T) {
21 ep, err := os.Executable()
22 if err != nil {
23 t.Fatalf("Executable failed: %v", err)
24 }
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)
30 }
31 cmd := &oexec.Cmd{}
32 // make child start with a relative program path
33 cmd.Dir = dir
34 cmd.Path = fn
35 // forge argv[0] for child, so that we can verify we could correctly
36 // get real path of the executable without influenced by argv[0].
37 cmd.Args = []string{"-", "-test.run=XXXX"}
38 cmd.Env = []string{fmt.Sprintf("%s=1", executable_EnvVar)}
39 out, err := cmd.CombinedOutput()
40 if err != nil {
41 t.Fatalf("exec(self) failed: %v", err)
42 }
43 outs := string(out)
44 if !filepath.IsAbs(outs) {
45 t.Fatalf("Child returned %q, want an absolute path", out)
46 }
47 if !sameFile(outs, ep) {
48 t.Fatalf("Child returned %q, not the same file as %q", out, ep)
49 }
50 }
51
52 func sameFile(fn1, fn2 string) bool {
53 fi1, err := os.Stat(fn1)
54 if err != nil {
55 return false
56 }
57 fi2, err := os.Stat(fn2)
58 if err != nil {
59 return false
60 }
61 return os.SameFile(fi1, fi2)
62 }
63
64 func init() {
65 if e := os.Getenv(executable_EnvVar); e != "" {
66 // first chdir to another path
67 dir := "/"
68 if runtime.GOOS == "windows" {
69 dir = filepath.VolumeName(".")
70 }
71 os.Chdir(dir)
72 if ep, err := os.Executable(); err != nil {
73 fmt.Fprint(os.Stderr, "ERROR: ", err)
74 } else {
75 fmt.Fprint(os.Stderr, ep)
76 }
77 os.Exit(0)
78 }
79 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b