Home | History | Annotate | Download | only in go
      1 // Copyright 2014 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 package main
      6 
      7 import (
      8 	"io/ioutil"
      9 	"os"
     10 	"os/exec"
     11 	"path/filepath"
     12 	"strings"
     13 	"testing"
     14 )
     15 
     16 func TestAbsolutePath(t *testing.T) {
     17 	tmp, err := ioutil.TempDir("", "TestAbsolutePath")
     18 	if err != nil {
     19 		t.Fatal(err)
     20 	}
     21 	defer os.RemoveAll(tmp)
     22 
     23 	file := filepath.Join(tmp, "a.go")
     24 	err = ioutil.WriteFile(file, []byte{}, 0644)
     25 	if err != nil {
     26 		t.Fatal(err)
     27 	}
     28 	dir := filepath.Join(tmp, "dir")
     29 	err = os.Mkdir(dir, 0777)
     30 	if err != nil {
     31 		t.Fatal(err)
     32 	}
     33 
     34 	wd, err := os.Getwd()
     35 	if err != nil {
     36 		t.Fatal(err)
     37 	}
     38 	defer os.Chdir(wd)
     39 
     40 	// Chdir so current directory and a.go reside on the same drive.
     41 	err = os.Chdir(dir)
     42 	if err != nil {
     43 		t.Fatal(err)
     44 	}
     45 
     46 	noVolume := file[len(filepath.VolumeName(file)):]
     47 	wrongPath := filepath.Join(dir, noVolume)
     48 	output, err := exec.Command("go", "build", noVolume).CombinedOutput()
     49 	if err == nil {
     50 		t.Fatal("build should fail")
     51 	}
     52 	if strings.Contains(string(output), wrongPath) {
     53 		t.Fatalf("wrong output found: %v %v", err, string(output))
     54 	}
     55 }
     56