github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/cmd/compile/internal/gc/lang_test.go (about) 1 // Copyright 2018 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 gc 6 7 import ( 8 "internal/testenv" 9 "io/ioutil" 10 "os" 11 "os/exec" 12 "path/filepath" 13 "testing" 14 ) 15 16 const aliasSrc = ` 17 package x 18 19 type T = int 20 ` 21 22 func TestInvalidLang(t *testing.T) { 23 t.Parallel() 24 25 testenv.MustHaveGoBuild(t) 26 27 dir, err := ioutil.TempDir("", "TestInvalidLang") 28 if err != nil { 29 t.Fatal(err) 30 } 31 defer os.RemoveAll(dir) 32 33 src := filepath.Join(dir, "alias.go") 34 if err := ioutil.WriteFile(src, []byte(aliasSrc), 0644); err != nil { 35 t.Fatal(err) 36 } 37 38 outfile := filepath.Join(dir, "alias.o") 39 40 if testLang(t, "go9.99", src, outfile) == nil { 41 t.Error("compilation with -lang=go9.99 succeeded unexpectedly") 42 } 43 44 // This test will have to be adjusted if we ever reach 1.99 or 2.0. 45 if testLang(t, "go1.99", src, outfile) == nil { 46 t.Error("compilation with -lang=go1.99 succeeded unexpectedly") 47 } 48 49 if testLang(t, "go1.8", src, outfile) == nil { 50 t.Error("compilation with -lang=go1.8 succeeded unexpectedly") 51 } 52 53 if err := testLang(t, "go1.9", src, outfile); err != nil { 54 t.Errorf("compilation with -lang=go1.9 failed unexpectedly: %v", err) 55 } 56 } 57 58 func testLang(t *testing.T, lang, src, outfile string) error { 59 run := []string{testenv.GoToolPath(t), "tool", "compile", "-lang", lang, "-o", outfile, src} 60 t.Log(run) 61 out, err := exec.Command(run[0], run[1:]...).CombinedOutput() 62 t.Logf("%s", out) 63 return err 64 }