github.com/bazelbuild/bazel-watcher@v0.25.2/internal/e2e/simple/simple_test.go (about)

     1  package simple
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/exec"
     8  	"syscall"
     9  	"testing"
    10  
    11  	"github.com/bazelbuild/bazel-watcher/internal/e2e"
    12  	"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
    13  )
    14  
    15  const mainFiles = `
    16  -- BUILD.bazel --
    17  # Base case test
    18  sh_binary(
    19    name = "simple",
    20    srcs = ["simple.sh"],
    21  )
    22  
    23  # Test and Coverage base case test
    24  sh_test(
    25    name = "test_simple_test_failing",
    26    srcs = ["test_simple_test_failing.sh"],
    27  )
    28  sh_test(
    29    name = "test_simple_test_passing",
    30    srcs = ["test_simple_test_passing.sh"],
    31  )
    32  
    33  # Environment variable tests
    34  sh_binary(
    35    name = "environment",
    36    srcs = ["environment.sh"],
    37  )
    38  
    39  # --define tests
    40  config_setting(
    41    name = "test_is_2",
    42    values = {"define": "test_number=2"},
    43  )
    44  
    45  sh_binary(
    46    name = "define",
    47    srcs = select({
    48          ":test_is_2": ["define_test_2.sh"],
    49          "//conditions:default": ["define_test_1.sh"],
    50      }),
    51  )
    52  -- simple.sh --
    53  printf "Started!"
    54  -- environment.sh --
    55  printf "Started and IBAZEL=${IBAZEL}!"
    56  -- define_test_1.sh --
    57  printf "define_test_1"
    58  -- define_test_2.sh --
    59  printf "define_test_2"
    60  -- subdir/BUILD.bazel --
    61  sh_binary(
    62    name = "subdir",
    63    srcs = ["subdir.sh"],
    64  )
    65  -- subdir/subdir.sh --
    66  printf "Hello subdir!"
    67  -- test_simple_test_failing.sh --
    68  echo "I'm a failing test."
    69  exit 1
    70  -- test_simple_test_passing.sh --
    71  echo "I'm a passing test."
    72  exit 0
    73  `
    74  
    75  func TestMain(m *testing.M) {
    76  	bazel_testing.TestMain(m, bazel_testing.Args{
    77  		Main: mainFiles,
    78  	})
    79  }
    80  
    81  func TestSimpleBuild(t *testing.T) {
    82  	ibazel := e2e.SetUp(t)
    83  	ibazel.Run([]string{}, "//:simple")
    84  	defer ibazel.Kill()
    85  
    86  	ibazel.ExpectOutput("Started!")
    87  }
    88  
    89  func TestSimpleTestFailing(t *testing.T) {
    90  	ibazel := e2e.SetUp(t)
    91  	ibazel.Test([]string{"--nocache_test_results", "--test_output=all"}, "//:test_simple_test_failing")
    92  
    93  	ibazel.ExpectOutput("I'm a failing test.")
    94  	ibazel.ExpectError("FAIL")
    95  }
    96  
    97  func TestSimpleTestPassing(t *testing.T) {
    98  	ibazel := e2e.SetUp(t)
    99  	ibazel.Test([]string{"--nocache_test_results", "--test_output=all"}, "//:test_simple_test_passing")
   100  	defer ibazel.Kill()
   101  
   102  	ibazel.ExpectOutput("I'm a passing test.")
   103  	ibazel.ExpectOutput("1 test passes")
   104  	ibazel.ExpectNoError()
   105  }
   106  
   107  func TestSimpleCoverageFailing(t *testing.T) {
   108  	ibazel := e2e.SetUp(t)
   109  	ibazel.Coverage([]string{"--nocache_test_results", "--test_output=all"}, "//:test_simple_test_failing")
   110  
   111  	ibazel.ExpectOutput("I'm a failing test.")
   112  	ibazel.ExpectOutput("FAIL")
   113  	ibazel.ExpectError("FAIL")
   114  }
   115  
   116  func TestSimpleCoveragePassing(t *testing.T) {
   117  	ibazel := e2e.SetUp(t)
   118  	ibazel.Coverage([]string{"--nocache_test_results", "--test_output=all"}, "//:test_simple_test_passing")
   119  	defer ibazel.Kill()
   120  
   121  	ibazel.ExpectOutput("I'm a passing test.")
   122  	ibazel.ExpectOutput("1 test passes")
   123  	ibazel.ExpectNoError()
   124  }
   125  
   126  func TestSimpleRunAfterShutdown(t *testing.T) {
   127  	cmd := bazel_testing.BazelCmd("shutdown")
   128  	if err := cmd.Run(); err != nil {
   129  		if exitErr, ok := err.(*exec.ExitError); ok {
   130  			status := exitErr.Sys().(syscall.WaitStatus)
   131  			if status.ExitStatus() != 0 {
   132  				t.Fatal(errors.New("bazel failed to shut down"))
   133  			}
   134  		}
   135  	}
   136  
   137  	ibazel := e2e.SetUp(t)
   138  	ibazel.Run([]string{}, "//:simple")
   139  	defer ibazel.Kill()
   140  
   141  	ibazel.ExpectOutput("Started!")
   142  }
   143  
   144  func TestSimpleRunConfirmEnvironment(t *testing.T) {
   145  	ibazel := e2e.SetUp(t)
   146  	ibazel.Run([]string{}, "//:environment")
   147  	defer ibazel.Kill()
   148  
   149  	ibazel.ExpectOutput("Started and IBAZEL=true!")
   150  }
   151  
   152  func TestSimpleRunUnderSubdir(t *testing.T) {
   153  	subdir := "subdir"
   154  
   155  	ibazel := e2e.SetUp(t)
   156  
   157  	err := os.Chdir(subdir)
   158  	if err != nil {
   159  		t.Fatalf("Error os.Chdir(%q): %v", subdir, err)
   160  	}
   161  	defer func() {
   162  		err := os.Chdir("..")
   163  		if err != nil {
   164  			t.Fatalf("Error os.Chdir(\"..\"): %v", err)
   165  		}
   166  	}()
   167  
   168  	ibazel.Run([]string{}, "//subdir")
   169  	defer ibazel.Kill()
   170  
   171  	ibazel.ExpectOutput("Hello subdir")
   172  }
   173  
   174  func TestSimpleRunWithModifiedFile(t *testing.T) {
   175  	ibazel := e2e.SetUp(t)
   176  	ibazel.Run([]string{}, "//:simple")
   177  	defer ibazel.Kill()
   178  
   179  	ibazel.ExpectOutput("Started!")
   180  
   181  	// Give it time to start up and query.
   182  	e2e.MustWriteFile(t, "simple.sh", `printf "Started2!"`)
   183  	ibazel.ExpectOutput("Started2!")
   184  
   185  	// Manipulate a source file and sleep past the debounce.
   186  	e2e.MustWriteFile(t, "simple.sh", `printf "Started3!"`)
   187  	ibazel.ExpectOutput("Started3!")
   188  
   189  	// TODO: put these in directories instead of storing the old value and rewriting it
   190  	oldValue, err := ioutil.ReadFile("BUILD.bazel")
   191  	if err != nil {
   192  		t.Errorf("Unable to Readfile(\"BUILD.bazel\"): %v", err)
   193  	}
   194  	defer e2e.MustWriteFile(t, "BUILD.bazel", string(oldValue))
   195  	// END TODO
   196  
   197  	// Now a BUILD.bazel file.
   198  	e2e.MustWriteFile(t, "BUILD.bazel", `
   199  sh_binary(
   200  	# New comment
   201  	name = "test",
   202  	srcs = ["test.sh"],
   203  )
   204  `)
   205  	ibazel.ExpectOutput("Started3!")
   206  }
   207  
   208  func TestSimpleRunWithFlag(t *testing.T) {
   209  	ibazel := e2e.SetUp(t)
   210  
   211  	ibazel.Run([]string{}, "//:define")
   212  	ibazel.ExpectOutput("define_test_1")
   213  	ibazel.Kill()
   214  
   215  	ibazel = e2e.NewIBazelTester(t)
   216  	ibazel.Run([]string{"--define=test_number=2"}, "//:define")
   217  	ibazel.ExpectOutput("define_test_2")
   218  	ibazel.Kill()
   219  
   220  	ibazel = e2e.NewIBazelTester(t)
   221  	ibazel.Run([]string{}, "//:define")
   222  	ibazel.ExpectOutput("define_test_1")
   223  	ibazel.Kill()
   224  }