github.com/bazelbuild/bazel-watcher@v0.25.2/internal/ibazel/command/default_command_test.go (about)

     1  // Copyright 2017 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 command
    16  
    17  import (
    18  	"os"
    19  	"runtime"
    20  	"testing"
    21  
    22  	mock_bazel "github.com/bazelbuild/bazel-watcher/internal/bazel/testing"
    23  	"github.com/bazelbuild/bazel-watcher/internal/ibazel/log"
    24  	"github.com/bazelbuild/bazel-watcher/internal/ibazel/process_group"
    25  )
    26  
    27  func TestDefaultCommand(t *testing.T) {
    28  	log.SetLogger(t)
    29  
    30  	var toKill process_group.ProcessGroup
    31  
    32  	if runtime.GOOS == "windows" {
    33  		// TODO(jchw): Remove hardcoded path.
    34  		toKill = process_group.Command("C:\\windows\\system32\\notepad")
    35  	} else {
    36  		toKill = process_group.Command("sleep", "10s")
    37  	}
    38  
    39  	execCommand = func(name string, args ...string) process_group.ProcessGroup {
    40  		if runtime.GOOS == "windows" {
    41  			// TODO(jchw): Remove hardcoded path.
    42  			return oldExecCommand("C:\\windows\\system32\\where")
    43  		}
    44  		return oldExecCommand("ls") // Every system has ls.
    45  	}
    46  	defer func() { execCommand = oldExecCommand }()
    47  
    48  	c := &defaultCommand{
    49  		args:      []string{"moo"},
    50  		bazelArgs: []string{},
    51  		pg:        toKill,
    52  		target:    "//path/to:target",
    53  	}
    54  
    55  	if c.IsSubprocessRunning() {
    56  		t.Errorf("New subprocess shouldn't have been started yet. State: %v", toKill.RootProcess().ProcessState)
    57  	}
    58  
    59  	toKill.Start()
    60  
    61  	if !c.IsSubprocessRunning() {
    62  		t.Errorf("New subprocess was never started. State: %v", toKill.RootProcess().ProcessState)
    63  	}
    64  
    65  	// This is synonymous with killing the job so use it to kill the job and test everything.
    66  	c.NotifyOfChanges()
    67  	assertKilled(t, toKill.RootProcess())
    68  }
    69  
    70  func TestDefaultCommand_Start(t *testing.T) {
    71  	log.SetLogger(t)
    72  
    73  	// Set up mock execCommand and prep it to be returned
    74  	execCommand = func(name string, args ...string) process_group.ProcessGroup {
    75  		if runtime.GOOS == "windows" {
    76  			// TODO(jchw): Remove hardcoded path.
    77  			return oldExecCommand("C:\\windows\\system32\\where")
    78  		}
    79  		return oldExecCommand("ls") // Every system has ls.
    80  	}
    81  	defer func() { execCommand = oldExecCommand }()
    82  
    83  	b := &mock_bazel.MockBazel{}
    84  
    85  	_, pg := start(b, "//path/to:target", []string{"moo"})
    86  	pg.Start()
    87  
    88  	if pg.RootProcess().Stdout != os.Stdout {
    89  		t.Errorf("Didn't set Stdout correctly")
    90  	}
    91  	if pg.RootProcess().Stderr != os.Stderr {
    92  		t.Errorf("Didn't set Stderr correctly")
    93  	}
    94  
    95  	b.AssertActions(t, [][]string{
    96  		[]string{"Run", "--script_path=.*", "//path/to:target"},
    97  	})
    98  }