modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/fixedbugs/issue22660.go (about)

     1  // run
     2  
     3  // Copyright 2017 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package main
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"log"
    14  	"os"
    15  	"os/exec"
    16  	"path/filepath"
    17  	"runtime"
    18  	"strings"
    19  )
    20  
    21  func main() {
    22  	if runtime.GOOS == "nacl" {
    23  		return // no file system available on builders
    24  	}
    25  
    26  	f, err := ioutil.TempFile("", "issue22660.go")
    27  	if err != nil {
    28  		log.Fatal(err)
    29  	}
    30  	f.Close()
    31  	defer os.Remove(f.Name())
    32  
    33  	// path must appear in error messages even if we strip them with -trimpath
    34  	path := filepath.Join("users", "xxx", "go")
    35  	var src bytes.Buffer
    36  	fmt.Fprintf(&src, "//line %s:1\n", filepath.Join(path, "foo.go"))
    37  
    38  	if err := ioutil.WriteFile(f.Name(), src.Bytes(), 0660); err != nil {
    39  		log.Fatal(err)
    40  	}
    41  
    42  	out, err := exec.Command("go", "tool", "compile", fmt.Sprintf("-trimpath=%s", path), f.Name()).CombinedOutput()
    43  	if err == nil {
    44  		log.Fatalf("expected compiling %s to fail", f.Name())
    45  	}
    46  
    47  	if !strings.HasPrefix(string(out), path) {
    48  		log.Fatalf("expected full path (%s) in error message, got:\n%s", path, out)
    49  	}
    50  }