| 1 | package interp |
| 2 | |
| 3 | import ( |
| 4 | "go/build" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | type testBuild struct { |
| 9 | src string |
| 10 | res bool |
| 11 | } |
| 12 | |
| 13 | func TestBuildTag(t *testing.T) { |
| 14 | // Assume a specific OS, arch and go version no matter the real underlying system |
| 15 | ctx := build.Context{ |
| 16 | GOARCH: "amd64", |
| 17 | GOOS: "linux", |
| 18 | BuildTags: []string{"foo"}, |
| 19 | ReleaseTags: []string{"go1.11"}, |
| 20 | } |
| 21 | |
| 22 | tests := []testBuild{ |
| 23 | {"// +build linux", true}, |
| 24 | {"// +build windows", false}, |
| 25 | {"// +build go1.9", true}, |
| 26 | {"// +build go1.11", true}, |
| 27 | {"// +build go1.12", false}, |
| 28 | {"// +build !go1.10", false}, |
| 29 | {"// +build !go1.12", true}, |
| 30 | {"// +build ignore", false}, |
| 31 | {"// +build linux,amd64", true}, |
| 32 | {"// +build linux,i386", false}, |
| 33 | {"// +build linux,i386 go1.11", true}, |
| 34 | {"// +build linux\n// +build amd64", true}, |
| 35 | {"// +build linux\n\n\n// +build amd64", true}, |
| 36 | {"// +build linux\n// +build i386", false}, |
| 37 | {"// +build foo", true}, |
| 38 | {"// +build !foo", false}, |
| 39 | {"// +build bar", false}, |
| 40 | } |
| 41 | |
| 42 | i := New(Options{}) |
| 43 | for _, test := range tests { |
| 44 | test := test |
| 45 | src := test.src + "\npackage x" |
| 46 | t.Run(test.src, func(t *testing.T) { |
| 47 | if r, _ := i.buildOk(&ctx, "", src); r != test.res { |
| 48 | t.Errorf("got %v, want %v", r, test.res) |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestSkipFile(t *testing.T) { |
| 55 | // Assume a specific OS, arch and go pattern no matter the real underlying system |
| 56 | ctx := build.Context{ |
| 57 | GOARCH: "amd64", |
| 58 | GOOS: "linux", |
| 59 | } |
| 60 | |
| 61 | tests := []testBuild{ |
| 62 | {"foo/bar_linux_amd64.go", false}, |
| 63 | {"foo/bar.go", false}, |
| 64 | {"bar.go", false}, |
| 65 | {"bar_linux.go", false}, |
| 66 | {"bar_maix.go", false}, |
| 67 | {"bar_mlinux.go", false}, |
| 68 | |
| 69 | {"bar_aix_foo.go", false}, |
| 70 | {"bar_linux_foo.go", false}, |
| 71 | {"bar_foo_amd64.go", false}, |
| 72 | {"bar_foo_arm.go", true}, |
| 73 | |
| 74 | {"bar_aix_s390x.go", true}, |
| 75 | {"bar_aix_amd64.go", true}, |
| 76 | {"bar_linux_arm.go", true}, |
| 77 | |
| 78 | {"bar_amd64.go", false}, |
| 79 | {"bar_arm.go", true}, |
| 80 | } |
| 81 | |
| 82 | for _, test := range tests { |
| 83 | test := test |
| 84 | t.Run(test.src, func(t *testing.T) { |
| 85 | if r := skipFile(&ctx, test.src, NoTest); r != test.res { |
| 86 | t.Errorf("got %v, want %v", r, test.res) |
| 87 | } |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func Test_goMinorVersion(t *testing.T) { |
| 93 | tests := []struct { |
| 94 | desc string |
| 95 | context build.Context |
| 96 | expected int |
| 97 | }{ |
| 98 | { |
| 99 | desc: "stable", |
| 100 | context: build.Context{ReleaseTags: []string{ |
| 101 | "go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6", "go1.7", "go1.8", "go1.9", "go1.10", "go1.11", "go1.12", |
| 102 | }}, |
| 103 | expected: 12, |
| 104 | }, |
| 105 | { |
| 106 | desc: "devel/beta/rc", |
| 107 | context: build.Context{ReleaseTags: []string{ |
| 108 | "go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6", "go1.7", "go1.8", "go1.9", "go1.10", "go1.11", "go1.12", "go1.13", |
| 109 | }}, |
| 110 | expected: 13, |
| 111 | }, |
| 112 | } |
| 113 | |
| 114 | for _, test := range tests { |
| 115 | test := test |
| 116 | t.Run(test.desc, func(t *testing.T) { |
| 117 | minor := goMinorVersion(&test.context) |
| 118 | |
| 119 | if minor != test.expected { |
| 120 | t.Errorf("got %v, want %v", minor, test.expected) |
| 121 | } |
| 122 | }) |
| 123 | } |
| 124 | } |