go/src/cmd/compile/internal/gc/lang_test.go
Ian Lance Taylor 2e9f0817f0 cmd/compile: add -lang flag to specify language version
The default language version is the current one.

For testing purposes, added a check that type aliases require version
go1.9. There is no consistent support for changes made before 1.12.

Updates #28221

Change-Id: Ia1ef63fff911d5fd29ef79d5fa4e20cfd945feb7
Reviewed-on: https://go-review.googlesource.com/c/144340
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2018-10-30 04:39:53 +00:00

59 lines
1.3 KiB
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gc
import (
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
)
const aliasSrc = `
package x
type T = int
`
func TestInvalidLang(t *testing.T) {
t.Parallel()
testenv.MustHaveGoBuild(t)
dir, err := ioutil.TempDir("", "TestInvalidLang")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
src := filepath.Join(dir, "alias.go")
if err := ioutil.WriteFile(src, []byte(aliasSrc), 0644); err != nil {
t.Fatal(err)
}
outfile := filepath.Join(dir, "alias.o")
if testLang(t, "go9.99", src, outfile) == nil {
t.Error("compilation with -lang=go9.99 succeeded unexpectedly")
}
if testLang(t, "go1.8", src, outfile) == nil {
t.Error("compilation with -lang=go1.8 succeeded unexpectedly")
}
if err := testLang(t, "go1.9", src, outfile); err != nil {
t.Errorf("compilation with -lang=go1.9 failed unexpectedly: %v", err)
}
}
func testLang(t *testing.T, lang, src, outfile string) error {
run := []string{testenv.GoToolPath(t), "tool", "compile", "-lang", lang, "-o", outfile, src}
t.Log(run)
out, err := exec.Command(run[0], run[1:]...).CombinedOutput()
t.Logf("%s", out)
return err
}