1// run 2 3//go:build !js && !wasip1 && gc 4 5// Copyright 2018 The Go Authors. All rights reserved. 6// Use of this source code is governed by a BSD-style 7// license that can be found in the LICENSE file. 8 9// Verify the impact of line directives on error positions and position formatting. 10 11package main 12 13import ( 14 "io/ioutil" 15 "log" 16 "os" 17 "os/exec" 18 "strings" 19) 20 21// Each of these tests is expected to fail (missing package clause) 22// at the position determined by the preceding line directive. 23var tests = []struct { 24 src, pos string 25}{ 26 {"//line :10\n", ":10:"}, // no filename means no filename 27 {"//line :10:4\n", "filename:10:4"}, // no filename means use existing filename 28 {"//line foo.go:10\n", "foo.go:10:"}, // no column means don't print a column 29 {"//line foo.go:10:4\n", "foo.go:10:4:"}, // column means print a column 30 {"//line foo.go:10:4\n\n", "foo.go:11:1:"}, // relative columns start at 1 after newline 31 32 {"/*line :10*/", ":10:"}, 33 {"/*line :10:4*/", "filename:10:4"}, 34 {"/*line foo.go:10*/", "foo.go:10:"}, 35 {"/*line foo.go:10:4*/", "foo.go:10:4:"}, 36 {"/*line foo.go:10:4*/\n", "foo.go:11:1:"}, 37} 38 39func main() { 40 f, err := ioutil.TempFile("", "issue22662b.go") 41 if err != nil { 42 log.Fatal(err) 43 } 44 f.Close() 45 defer os.Remove(f.Name()) 46 47 for _, test := range tests { 48 if err := ioutil.WriteFile(f.Name(), []byte(test.src), 0660); err != nil { 49 log.Fatal(err) 50 } 51 52 out, err := exec.Command("go", "tool", "compile", "-p=p", f.Name()).CombinedOutput() 53 if err == nil { 54 log.Fatalf("expected compiling\n---\n%s\n---\nto fail", test.src) 55 } 56 57 errmsg := strings.Replace(string(out), f.Name(), "filename", -1) // use "filename" instead of actual (long) filename 58 if !strings.HasPrefix(errmsg, test.pos) { 59 log.Fatalf("%q: got %q; want position %q", test.src, errmsg, test.pos) 60 } 61 } 62} 63