• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2020 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cmdline_test
16
17import (
18	"bytes"
19	"testing"
20
21	"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
22)
23
24func TestMain(m *testing.M) {
25	bazel_testing.TestMain(m, bazel_testing.Args{
26		Main: `
27-- BUILD.bazel --
28load("@io_bazel_rules_go//go:def.bzl", "go_binary")
29
30go_binary(
31    name = "maybe_pure",
32    srcs = [
33        "not_pure.go",
34        "pure.go",
35    ],
36)
37
38-- not_pure.go --
39// +build cgo
40
41package main
42
43import "fmt"
44
45func main() {
46	fmt.Println("not pure")
47}
48
49-- pure.go --
50// +build !cgo
51
52package main
53
54import "fmt"
55
56func main() {
57	fmt.Println("pure")
58}
59`,
60	})
61}
62
63// TestPure checks that the --@io_bazel_rules_go//go/config:pure flag controls
64// whether a target is built in pure mode. It doesn't actually require cgo,
65// since that doesn't work within go_bazel_test on Windows.
66func TestPure(t *testing.T) {
67	out, err := bazel_testing.BazelOutput("run", "//:maybe_pure")
68	if err != nil {
69		t.Fatalf("running //:maybe_pure without flag: %v", err)
70	}
71	got := string(bytes.TrimSpace(out))
72	if want := "not pure"; got != want {
73		t.Fatalf("got %q; want %q", got, want)
74	}
75
76	out, err = bazel_testing.BazelOutput("run", "--@io_bazel_rules_go//go/config:pure", "//:maybe_pure")
77	if err != nil {
78		t.Fatalf("running //:maybe_pure with flag: %v", err)
79	}
80	got = string(bytes.TrimSpace(out))
81	if want := "pure"; got != want {
82		t.Fatalf("got %q; want %q", got, want)
83	}
84}
85