1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package no_prefix_test
import (
"testing"
"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
)
const mainFiles = `
-- BUILD.bazel --
load("@io_bazel_rules_go//go:def.bzl", "go_test", "go_library")
go_library(
name = "stamp",
srcs = ["stamp.go"],
importpath = "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp",
visibility = ["//visibility:public"],
x_defs = {
"XdefBuildTimestamp": "{BUILD_TIMESTAMP}",
},
)
go_test(
name = "stamp_with_x_defs",
size = "small",
srcs = ["stamped_bin_test.go"],
x_defs = {
"github.com/bazelbuild/rules_go/examples/stamped_bin/stamp.BUILD_TIMESTAMP": "{BUILD_TIMESTAMP}",
"github.com/bazelbuild/rules_go/examples/stamped_bin/stamp.PassIfEmpty": "",
"github.com/bazelbuild/rules_go/examples/stamped_bin/stamp.XdefInvalid": "{Undefined_Var}", # undefined should leave the var alone
"github.com/bazelbuild/rules_go/examples/stamped_bin/stamp.Multiple": "{BUILD_TIMESTAMP}{BUILD_TIMESTAMP}",
},
deps = [":stamp"],
)
-- stamp.go --
package stamp
var BUILD_TIMESTAMP = "fail"
// an xdef should set this to ""
var PassIfEmpty = "fail"
// an xdef should set this to nonempty
var XdefBuildTimestamp = ""
// an xdef with a missing key should leave this alone
var XdefInvalid = "pass"
// an xdef with multiple keys
var Multiple = "fail"
-- stamped_bin_test.go --
package stamped_bin_test
import (
"testing"
"github.com/bazelbuild/rules_go/examples/stamped_bin/stamp"
)
func TestStampedBin(t *testing.T) {
// If we use an x_def when linking to override BUILD_TIMESTAMP but fail to
// pass through the workspace status value, it'll be set to empty string -
// overridden but still wrong. Check for that case too.
if stamp.BUILD_TIMESTAMP == "fail" || stamp.BUILD_TIMESTAMP == "" {
t.Errorf("Expected timestamp to have been modified, got %s.", stamp.BUILD_TIMESTAMP)
}
if stamp.XdefBuildTimestamp == "" {
t.Errorf("Expected XdefBuildTimestamp to have been modified, got %s.", stamp.XdefBuildTimestamp)
}
if stamp.PassIfEmpty != "" {
t.Errorf("Expected PassIfEmpty to have been set to '', got %s.", stamp.PassIfEmpty)
}
if stamp.XdefInvalid != "pass" {
t.Errorf("Expected XdefInvalid to have been left alone, got %s.", stamp.XdefInvalid)
}
if stamp.Multiple != stamp.BUILD_TIMESTAMP + stamp.BUILD_TIMESTAMP {
t.Errorf("Expected Multiple to have two BUILD_TIMESTAMP, got %s.", stamp.Multiple)
}
}
`
func TestMain(m *testing.M) {
bazel_testing.TestMain(m, bazel_testing.Args{
Main: mainFiles,
})
}
func TestBuild(t *testing.T) {
if err := bazel_testing.RunBazel("test", "--stamp", ":all"); err != nil {
t.Fatal(err)
}
}
func TestBuildWithoutStamp(t *testing.T) {
if err := bazel_testing.RunBazel("test", "--nostamp", ":all"); err != nil {
if eErr, ok := err.(*bazel_testing.StderrExitError); ok {
if eErr.Err.ExitCode() == 3 { // 3 is TEST_FAILED bazel exit code
return
}
t.Fatalf("expected tests to have failed (instead got exit code %d)", eErr.Err.ExitCode())
}
t.Fatal("expected bazel_testing.StderrExitError")
}
t.Fatal("expected error")
}
|