xref: /aosp_15_r20/external/bazelbuild-rules_go/tests/legacy/examples/stamped_bin/stamped_test.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1// Copyright 2019 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 no_prefix_test
16
17import (
18	"testing"
19
20	"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
21)
22
23const mainFiles = `
24-- BUILD.bazel --
25load("@io_bazel_rules_go//go:def.bzl", "go_test", "go_library")
26
27go_library(
28    name = "stamp",
29    srcs = ["stamp.go"],
30    importpath = "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp",
31    visibility = ["//visibility:public"],
32    x_defs = {
33        "XdefBuildTimestamp": "{BUILD_TIMESTAMP}",
34    },
35)
36
37go_test(
38    name = "stamp_with_x_defs",
39    size = "small",
40    srcs = ["stamped_bin_test.go"],
41    x_defs = {
42        "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp.BUILD_TIMESTAMP": "{BUILD_TIMESTAMP}",
43        "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp.PassIfEmpty": "",
44        "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp.XdefInvalid": "{Undefined_Var}",  # undefined should leave the var alone
45        "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp.Multiple": "{BUILD_TIMESTAMP}{BUILD_TIMESTAMP}",
46    },
47    deps = [":stamp"],
48)
49
50-- stamp.go --
51package stamp
52
53var BUILD_TIMESTAMP = "fail"
54
55// an xdef should set this to ""
56var PassIfEmpty = "fail"
57
58// an xdef should set this to nonempty
59var XdefBuildTimestamp = ""
60
61// an xdef with a missing key should leave this alone
62var XdefInvalid = "pass"
63
64// an xdef with multiple keys
65var Multiple = "fail"
66
67-- stamped_bin_test.go --
68package stamped_bin_test
69
70import (
71	"testing"
72
73	"github.com/bazelbuild/rules_go/examples/stamped_bin/stamp"
74)
75
76func TestStampedBin(t *testing.T) {
77	// If we use an x_def when linking to override BUILD_TIMESTAMP but fail to
78	// pass through the workspace status value, it'll be set to empty string -
79	// overridden but still wrong. Check for that case too.
80	if stamp.BUILD_TIMESTAMP == "fail" || stamp.BUILD_TIMESTAMP == "" {
81		t.Errorf("Expected timestamp to have been modified, got %s.", stamp.BUILD_TIMESTAMP)
82	}
83	if stamp.XdefBuildTimestamp == "" {
84		t.Errorf("Expected XdefBuildTimestamp to have been modified, got %s.", stamp.XdefBuildTimestamp)
85	}
86	if stamp.PassIfEmpty != "" {
87		t.Errorf("Expected PassIfEmpty to have been set to '', got %s.", stamp.PassIfEmpty)
88	}
89	if stamp.XdefInvalid != "pass" {
90		t.Errorf("Expected XdefInvalid to have been left alone, got %s.", stamp.XdefInvalid)
91	}
92	if stamp.Multiple != stamp.BUILD_TIMESTAMP + stamp.BUILD_TIMESTAMP {
93		t.Errorf("Expected Multiple to have two BUILD_TIMESTAMP, got %s.", stamp.Multiple)
94	}
95}
96
97`
98
99func TestMain(m *testing.M) {
100	bazel_testing.TestMain(m, bazel_testing.Args{
101		Main: mainFiles,
102	})
103}
104
105func TestBuild(t *testing.T) {
106	if err := bazel_testing.RunBazel("test", "--stamp", ":all"); err != nil {
107		t.Fatal(err)
108	}
109}
110
111func TestBuildWithoutStamp(t *testing.T) {
112	if err := bazel_testing.RunBazel("test", "--nostamp", ":all"); err != nil {
113		if eErr, ok := err.(*bazel_testing.StderrExitError); ok {
114			if eErr.Err.ExitCode() == 3 { // 3 is TEST_FAILED bazel exit code
115				return
116			}
117			t.Fatalf("expected tests to have failed (instead got exit code %d)", eErr.Err.ExitCode())
118		}
119		t.Fatal("expected bazel_testing.StderrExitError")
120	}
121	t.Fatal("expected error")
122}
123