github.com/0xKiwi/rules_go@v0.24.3/tests/core/go_binary/package_conflict_test.go (about)

     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  
    15  package package_conflict_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
    21  )
    22  
    23  func TestMain(m *testing.M) {
    24  	bazel_testing.TestMain(m, bazel_testing.Args{
    25  		Main: `
    26  -- BUILD.bazel --
    27  load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
    28  
    29  go_library(
    30      name = "foo_de",
    31      importpath = "github.com/bazelbuild/rules_go/tests/core/package_conflict/foo",
    32      srcs = ["foo_de.go"],
    33  )
    34  
    35  go_library(
    36      name = "de",
    37      importpath = "github.com/bazelbuild/rules_go/tests/core/package_conflict/de",
    38      srcs = ["de.go"],
    39      deps = [":foo_de"],
    40  )
    41  
    42  go_library(
    43      name = "foo_en",
    44      importpath = "github.com/bazelbuild/rules_go/tests/core/package_conflict/foo",
    45      srcs = ["foo_en.go"],
    46  )
    47  
    48  go_library(
    49      name = "en",
    50      importpath = "github.com/bazelbuild/rules_go/tests/core/package_conflict/en",
    51      srcs = ["en.go"],
    52      deps = [":foo_en"],
    53  )
    54  
    55  go_binary(
    56      name = "main",
    57      srcs = ["main.go"],
    58      deps = [
    59          ":de",
    60          ":en",
    61      ],
    62  )
    63  
    64  -- foo_en.go --
    65  package foo
    66  
    67  import "fmt"
    68  
    69  func SayHello() {
    70    fmt.Println("Hello, World!")
    71  }
    72  
    73  -- en.go --
    74  package en
    75  
    76  import "github.com/bazelbuild/rules_go/tests/core/package_conflict/foo"
    77  
    78  func SayHello() {
    79    foo.SayHello()
    80  }
    81  
    82  -- foo_de.go --
    83  package foo
    84  
    85  import "fmt"
    86  
    87  func SayHello() {
    88    fmt.Println("Hallo, Welt!")
    89  }
    90  
    91  -- de.go --
    92  package de
    93  
    94  import "github.com/bazelbuild/rules_go/tests/core/package_conflict/foo"
    95  
    96  func SayHello() {
    97    foo.SayHello()
    98  }
    99  
   100  -- main.go --
   101  package main
   102  
   103  import (
   104    "github.com/bazelbuild/rules_go/tests/core/package_conflict/de"
   105    "github.com/bazelbuild/rules_go/tests/core/package_conflict/en"
   106  )
   107  
   108  func main() {
   109    de.SayHello()
   110    en.SayHello()
   111  }
   112  `,
   113  	})
   114  }
   115  
   116  func runTest(t *testing.T, expectError bool, extraArgs ...string) {
   117  	args := append([]string{"build", "//:main"}, extraArgs...)
   118  
   119  	err := bazel_testing.RunBazel(args...)
   120  	if expectError {
   121  		if err == nil {
   122  			t.Fatal("Expected error")
   123  		}
   124  	} else {
   125  		if err != nil {
   126  			t.Fatal(err)
   127  		}
   128  	}
   129  }
   130  
   131  func TestDefaultBehaviour(t *testing.T) {
   132  	// TODO(#1374): Default to `true`.
   133  	runTest(t, false)
   134  }
   135  
   136  func TestPackageConflictIsWarning(t *testing.T) {
   137  	runTest(t, false, "--@io_bazel_rules_go//go/config:incompatible_package_conflict_is_error=False")
   138  }
   139  
   140  func TestPackageConflictIsError(t *testing.T) {
   141  	runTest(t, true, "--@io_bazel_rules_go//go/config:incompatible_package_conflict_is_error=True")
   142  }