github.com/bazelbuild/bazel-watcher@v0.25.2/internal/ibazel/output_runner/output_runner_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 output_runner 16 17 import ( 18 "bytes" 19 "reflect" 20 "testing" 21 22 "github.com/bazelbuild/bazel-watcher/internal/ibazel/log" 23 "github.com/bazelbuild/bazel-watcher/internal/ibazel/workspace" 24 ) 25 26 func TestConvertArgs(t *testing.T) { 27 log.SetTesting(t) 28 29 matches := []string{"my_command my_arg1 my_arg2 my_arg3", "my_command", "my_arg1", "my_arg2", "my_arg3"} 30 // Command parsing tests 31 for _, c := range []struct { 32 cmd string 33 truth string 34 }{ 35 {"$1", "my_command"}, 36 {"warning", "warning"}, 37 {"keep_command", "keep_command"}, 38 } { 39 new_cmd := convertArg(matches, c.cmd) 40 if !reflect.DeepEqual(c.truth, new_cmd) { 41 t.Errorf("Command not equal: %v\nGot: %v\nWant: %v", 42 c.cmd, new_cmd, c.truth) 43 } 44 } 45 // Arguments parsing tests 46 for _, c := range []struct { 47 cmd []string 48 truth []string 49 }{ 50 {[]string{"$2", "$3"}, []string{"my_arg1", "my_arg2"}}, 51 {[]string{"$2", "$3", "$4"}, []string{"my_arg1", "my_arg2", "my_arg3"}}, 52 {[]string{"$2", "dont_change_arg"}, []string{"my_arg1", "dont_change_arg"}}, 53 {[]string{"keep_arg", "$3"}, []string{"keep_arg", "my_arg2"}}, 54 {[]string{"$2,$3"}, []string{"my_arg1,my_arg2"}}, 55 {[]string{"src/$2:1"}, []string{"src/my_arg1:1"}}, 56 {[]string{"literal$$char"}, []string{"literal$char"}}, 57 {[]string{"$99"}, []string{"$99"}}, 58 } { 59 new_cmd := convertArgs(matches, c.cmd) 60 if !reflect.DeepEqual(c.truth, new_cmd) { 61 t.Errorf("Command not equal: %v\nGot: %v\nWant: %v", 62 c.cmd, new_cmd, c.truth) 63 } 64 } 65 } 66 67 func TestReadConfigs(t *testing.T) { 68 log.SetTesting(t) 69 70 i := &OutputRunner{ 71 w: &workspace.FakeWorkspace{}, 72 } 73 optcmd := i.readConfigs("output_runner_test.json") 74 75 for idx, c := range []struct { 76 regex string 77 command string 78 args []string 79 }{ 80 {"^(buildozer) '(.*)'\\s+(.*)$", "$1", []string{"$2", "$3"}}, 81 {"WARNING", "warn", []string{"keep_calm", "dont_panic"}}, 82 {"DANGER", "danger", []string{"be_careful", "why_so_serious"}}, 83 } { 84 if !reflect.DeepEqual(c.regex, optcmd[idx].Regex) { 85 t.Errorf("Regex not equal: %v\nGot: %v\nWant: %v", 86 optcmd[idx], optcmd[idx].Regex, c.regex) 87 } 88 if !reflect.DeepEqual(c.command, optcmd[idx].Command) { 89 t.Errorf("Command not equal: %v\nGot: %v\nWant: %v", 90 optcmd[idx], optcmd[idx].Command, c.command) 91 } 92 if !reflect.DeepEqual(c.args, optcmd[idx].Args) { 93 t.Errorf("Args not equal: %v\nGot: %v\nWant: %v", 94 optcmd[idx], optcmd[idx].Args, c.args) 95 } 96 } 97 } 98 99 func TestMatchRegex(t *testing.T) { 100 log.SetTesting(t) 101 102 buf := bytes.Buffer{} 103 buf.WriteString("buildozer 'add deps test_dep1' //target1:target1\n") 104 buf.WriteString("buildozer 'add deps test_dep2' //target2:target2\n") 105 buf.WriteString("Check that imports in Go sources match importpath attributes in deps.\n") 106 buf.WriteString("Other build output which does not match things.\n") 107 // Duplicate matches, to be deduplicated. 108 buf.WriteString("Check that imports in Go sources match importpath attributes in deps.\n") 109 buf.WriteString("buildozer 'add deps test_dep2' //target2:target2\n") 110 111 optcmd := []Optcmd{ 112 {Regex: "^Check that imports in Go sources match importpath attributes in deps.$", Command: "bazel", Args: []string{"run", "//:gazelle"}}, 113 {Regex: "^(buildozer) '(.*)'\\s+(.*)$", Command: "$1", Args: []string{"$2", "$3"}}, 114 } 115 116 _, commands, args := matchRegex(optcmd, &buf) 117 118 expected := []struct { 119 cls string 120 cs string 121 as []string 122 }{ 123 {"buildozer 'add deps test_dep1' //target1:target1", "buildozer", []string{"add deps test_dep1", "//target1:target1"}}, 124 {"buildozer 'add deps test_dep2' //target2:target2", "buildozer", []string{"add deps test_dep2", "//target2:target2"}}, 125 {"Check that imports in Go sources match importpath attributes in deps.", "bazel", []string{"run", "//:gazelle"}}, 126 } 127 if len(expected) != len(commands) { 128 t.Errorf("Did not receive expected number of commands:\nGot: %d\nWant: %d", len(commands), len(expected)) 129 } 130 for idx, c := range expected { 131 if !reflect.DeepEqual(c.cs, commands[idx]) { 132 t.Errorf("Commands not equal: %v\nGot: %v\nWant: %v", 133 c.cls, commands[idx], c.cs) 134 } 135 if !reflect.DeepEqual(c.as, args[idx]) { 136 t.Errorf("Arguments not equal: %v\nGot: %v\nWant: %v", 137 c.cls, args[idx], c.as) 138 } 139 } 140 } 141 142 var cleanerTests = []struct { 143 in string 144 out []string 145 }{ 146 { 147 "buildozer 'add deps //wow' //fake:target0", 148 []string{"buildozer 'add deps //wow' //fake:target0"}, 149 }, 150 { 151 "[96mbuildozer [0m[93m[0m[93m[0m[91m'add deps [0m[90m//wow'[0m //fake:target1", 152 []string{"buildozer 'add deps //wow' //fake:target1"}, 153 }, 154 { 155 "build[96mozer 'add d[96meps //w[96mow' //fake:tar[96mget2", 156 []string{"buildozer 'add deps //wow' //fake:target2"}, 157 }, 158 { 159 "[0m[90mbuildozer 'a[0m[93mdd deps //wow' [0m[93m//fake:target3[91m", 160 []string{"buildozer 'add deps //wow' //fake:target3"}, 161 }, 162 { 163 "buildozer 'add deps //wow[0m[93m //fake:target4", 164 []string(nil), 165 }, 166 } 167 168 func TestMatchCleanRegex(t *testing.T) { 169 log.SetTesting(t) 170 171 optcmd := []Optcmd{ 172 {Regex: "^(buildozer) '(.*)'\\s+(.*)$", Command: "$1", Args: []string{"$2", "$3"}}, 173 } 174 175 for _, tt := range cleanerTests { 176 t.Run(tt.in, func(t *testing.T) { 177 buf := bytes.Buffer{} 178 buf.WriteString(tt.in) 179 cmdLines, _, _ := matchRegex(optcmd, &buf) 180 181 if !reflect.DeepEqual(cmdLines, tt.out) { 182 t.Errorf("Commands not equal!\nGot: %v\nWant: %v", cmdLines, tt.out) 183 } 184 }) 185 } 186 }