Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. 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 bazel |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "reflect" |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 20 | "sort" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | func TestAqueryMultiArchGenrule(t *testing.T) { |
| 25 | // This input string is retrieved from a real build of bionic-related genrules. |
| 26 | const inputString = ` |
| 27 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 28 | "artifacts": [ |
| 29 | { "id": 1, "pathFragmentId": 1 }, |
| 30 | { "id": 2, "pathFragmentId": 6 }, |
| 31 | { "id": 3, "pathFragmentId": 8 }, |
| 32 | { "id": 4, "pathFragmentId": 12 }, |
| 33 | { "id": 5, "pathFragmentId": 19 }, |
| 34 | { "id": 6, "pathFragmentId": 20 }, |
| 35 | { "id": 7, "pathFragmentId": 21 }], |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 36 | "actions": [{ |
| 37 | "targetId": 1, |
| 38 | "actionKey": "ab53f6ecbdc2ee8cb8812613b63205464f1f5083f6dca87081a0a398c0f1ecf7", |
| 39 | "mnemonic": "Genrule", |
| 40 | "configurationId": 1, |
| 41 | "arguments": ["/bin/bash", "-c", "source ../bazel_tools/tools/genrule/genrule-setup.sh; ../sourceroot/bionic/libc/tools/gensyscalls.py arm ../sourceroot/bionic/libc/SYSCALLS.TXT \u003e bazel-out/sourceroot/k8-fastbuild/bin/bionic/libc/syscalls-arm.S"], |
| 42 | "environmentVariables": [{ |
| 43 | "key": "PATH", |
| 44 | "value": "/bin:/usr/bin:/usr/local/bin" |
| 45 | }], |
| 46 | "inputDepSetIds": [1], |
| 47 | "outputIds": [4], |
| 48 | "primaryOutputId": 4 |
| 49 | }, { |
| 50 | "targetId": 2, |
| 51 | "actionKey": "9f4309ce165dac458498cb92811c18b0b7919782cc37b82a42d2141b8cc90826", |
| 52 | "mnemonic": "Genrule", |
| 53 | "configurationId": 1, |
| 54 | "arguments": ["/bin/bash", "-c", "source ../bazel_tools/tools/genrule/genrule-setup.sh; ../sourceroot/bionic/libc/tools/gensyscalls.py x86 ../sourceroot/bionic/libc/SYSCALLS.TXT \u003e bazel-out/sourceroot/k8-fastbuild/bin/bionic/libc/syscalls-x86.S"], |
| 55 | "environmentVariables": [{ |
| 56 | "key": "PATH", |
| 57 | "value": "/bin:/usr/bin:/usr/local/bin" |
| 58 | }], |
| 59 | "inputDepSetIds": [2], |
| 60 | "outputIds": [5], |
| 61 | "primaryOutputId": 5 |
| 62 | }, { |
| 63 | "targetId": 3, |
| 64 | "actionKey": "50d6c586103ebeed3a218195540bcc30d329464eae36377eb82f8ce7c36ac342", |
| 65 | "mnemonic": "Genrule", |
| 66 | "configurationId": 1, |
| 67 | "arguments": ["/bin/bash", "-c", "source ../bazel_tools/tools/genrule/genrule-setup.sh; ../sourceroot/bionic/libc/tools/gensyscalls.py x86_64 ../sourceroot/bionic/libc/SYSCALLS.TXT \u003e bazel-out/sourceroot/k8-fastbuild/bin/bionic/libc/syscalls-x86_64.S"], |
| 68 | "environmentVariables": [{ |
| 69 | "key": "PATH", |
| 70 | "value": "/bin:/usr/bin:/usr/local/bin" |
| 71 | }], |
| 72 | "inputDepSetIds": [3], |
| 73 | "outputIds": [6], |
| 74 | "primaryOutputId": 6 |
| 75 | }, { |
| 76 | "targetId": 4, |
| 77 | "actionKey": "f30cbe442f5216f4223cf16a39112cad4ec56f31f49290d85cff587e48647ffa", |
| 78 | "mnemonic": "Genrule", |
| 79 | "configurationId": 1, |
| 80 | "arguments": ["/bin/bash", "-c", "source ../bazel_tools/tools/genrule/genrule-setup.sh; ../sourceroot/bionic/libc/tools/gensyscalls.py arm64 ../sourceroot/bionic/libc/SYSCALLS.TXT \u003e bazel-out/sourceroot/k8-fastbuild/bin/bionic/libc/syscalls-arm64.S"], |
| 81 | "environmentVariables": [{ |
| 82 | "key": "PATH", |
| 83 | "value": "/bin:/usr/bin:/usr/local/bin" |
| 84 | }], |
| 85 | "inputDepSetIds": [4], |
| 86 | "outputIds": [7], |
| 87 | "primaryOutputId": 7 |
| 88 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 89 | "targets": [ |
| 90 | { "id": 1, "label": "@sourceroot//bionic/libc:syscalls-arm", "ruleClassId": 1 }, |
| 91 | { "id": 2, "label": "@sourceroot//bionic/libc:syscalls-x86", "ruleClassId": 1 }, |
| 92 | { "id": 3, "label": "@sourceroot//bionic/libc:syscalls-x86_64", "ruleClassId": 1 }, |
| 93 | { "id": 4, "label": "@sourceroot//bionic/libc:syscalls-arm64", "ruleClassId": 1 }], |
| 94 | "depSetOfFiles": [ |
| 95 | { "id": 1, "directArtifactIds": [1, 2, 3] }, |
| 96 | { "id": 2, "directArtifactIds": [1, 2, 3] }, |
| 97 | { "id": 3, "directArtifactIds": [1, 2, 3] }, |
| 98 | { "id": 4, "directArtifactIds": [1, 2, 3] }], |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 99 | "configuration": [{ |
| 100 | "id": 1, |
| 101 | "mnemonic": "k8-fastbuild", |
| 102 | "platformName": "k8", |
| 103 | "checksum": "485c362832c178e367d972177f68e69e0981e51e67ef1c160944473db53fe046" |
| 104 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 105 | "ruleClasses": [{ "id": 1, "name": "genrule"}], |
| 106 | "pathFragments": [ |
| 107 | { "id": 5, "label": ".." }, |
| 108 | { "id": 4, "label": "sourceroot", "parentId": 5 }, |
| 109 | { "id": 3, "label": "bionic", "parentId": 4 }, |
| 110 | { "id": 2, "label": "libc", "parentId": 3 }, |
| 111 | { "id": 1, "label": "SYSCALLS.TXT", "parentId": 2 }, |
| 112 | { "id": 7, "label": "tools", "parentId": 2 }, |
| 113 | { "id": 6, "label": "gensyscalls.py", "parentId": 7 }, |
| 114 | { "id": 11, "label": "bazel_tools", "parentId": 5 }, |
| 115 | { "id": 10, "label": "tools", "parentId": 11 }, |
| 116 | { "id": 9, "label": "genrule", "parentId": 10 }, |
| 117 | { "id": 8, "label": "genrule-setup.sh", "parentId": 9 }, |
| 118 | { "id": 18, "label": "bazel-out" }, |
| 119 | { "id": 17, "label": "sourceroot", "parentId": 18 }, |
| 120 | { "id": 16, "label": "k8-fastbuild", "parentId": 17 }, |
| 121 | { "id": 15, "label": "bin", "parentId": 16 }, |
| 122 | { "id": 14, "label": "bionic", "parentId": 15 }, |
| 123 | { "id": 13, "label": "libc", "parentId": 14 }, |
| 124 | { "id": 12, "label": "syscalls-arm.S", "parentId": 13 }, |
| 125 | { "id": 19, "label": "syscalls-x86.S", "parentId": 13 }, |
| 126 | { "id": 20, "label": "syscalls-x86_64.S", "parentId": 13 }, |
| 127 | { "id": 21, "label": "syscalls-arm64.S", "parentId": 13 }] |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 128 | }` |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 129 | actualbuildStatements, actualDepsets, _ := AqueryBuildStatements([]byte(inputString)) |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 130 | var expectedBuildStatements []BuildStatement |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 131 | for _, arch := range []string{"arm", "arm64", "x86", "x86_64"} { |
| 132 | expectedBuildStatements = append(expectedBuildStatements, |
| 133 | BuildStatement{ |
| 134 | Command: fmt.Sprintf( |
| 135 | "/bin/bash -c 'source ../bazel_tools/tools/genrule/genrule-setup.sh; ../sourceroot/bionic/libc/tools/gensyscalls.py %s ../sourceroot/bionic/libc/SYSCALLS.TXT > bazel-out/sourceroot/k8-fastbuild/bin/bionic/libc/syscalls-%s.S'", |
| 136 | arch, arch), |
| 137 | OutputPaths: []string{ |
| 138 | fmt.Sprintf("bazel-out/sourceroot/k8-fastbuild/bin/bionic/libc/syscalls-%s.S", arch), |
| 139 | }, |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 140 | Env: []KeyValuePair{ |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 141 | {Key: "PATH", Value: "/bin:/usr/bin:/usr/local/bin"}, |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 142 | }, |
| 143 | Mnemonic: "Genrule", |
| 144 | }) |
| 145 | } |
| 146 | assertBuildStatements(t, expectedBuildStatements, actualbuildStatements) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 147 | |
| 148 | expectedFlattenedInputs := []string{ |
| 149 | "../sourceroot/bionic/libc/SYSCALLS.TXT", |
| 150 | "../sourceroot/bionic/libc/tools/gensyscalls.py", |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 151 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 152 | // In this example, each depset should have the same expected inputs. |
| 153 | for _, actualDepset := range actualDepsets { |
| 154 | actualFlattenedInputs := flattenDepsets([]string{actualDepset.ContentHash}, actualDepsets) |
| 155 | if !reflect.DeepEqual(actualFlattenedInputs, expectedFlattenedInputs) { |
| 156 | t.Errorf("Expected flattened inputs %v, but got %v", expectedFlattenedInputs, actualFlattenedInputs) |
| 157 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 158 | } |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | func TestInvalidOutputId(t *testing.T) { |
| 162 | const inputString = ` |
| 163 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 164 | "artifacts": [ |
| 165 | { "id": 1, "pathFragmentId": 1 }, |
| 166 | { "id": 2, "pathFragmentId": 2 }], |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 167 | "actions": [{ |
| 168 | "targetId": 1, |
| 169 | "actionKey": "x", |
| 170 | "mnemonic": "x", |
| 171 | "arguments": ["touch", "foo"], |
| 172 | "inputDepSetIds": [1], |
| 173 | "outputIds": [3], |
| 174 | "primaryOutputId": 3 |
| 175 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 176 | "depSetOfFiles": [ |
| 177 | { "id": 1, "directArtifactIds": [1, 2] }], |
| 178 | "pathFragments": [ |
| 179 | { "id": 1, "label": "one" }, |
| 180 | { "id": 2, "label": "two" }] |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 181 | }` |
| 182 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 183 | _, _, err := AqueryBuildStatements([]byte(inputString)) |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 184 | assertError(t, err, "undefined outputId 3") |
| 185 | } |
| 186 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 187 | func TestInvalidInputDepsetIdFromAction(t *testing.T) { |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 188 | const inputString = ` |
| 189 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 190 | "artifacts": [ |
| 191 | { "id": 1, "pathFragmentId": 1 }, |
| 192 | { "id": 2, "pathFragmentId": 2 }], |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 193 | "actions": [{ |
| 194 | "targetId": 1, |
| 195 | "actionKey": "x", |
| 196 | "mnemonic": "x", |
| 197 | "arguments": ["touch", "foo"], |
| 198 | "inputDepSetIds": [2], |
| 199 | "outputIds": [1], |
| 200 | "primaryOutputId": 1 |
| 201 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 202 | "depSetOfFiles": [ |
| 203 | { "id": 1, "directArtifactIds": [1, 2] }], |
| 204 | "pathFragments": [ |
| 205 | { "id": 1, "label": "one" }, |
| 206 | { "id": 2, "label": "two" }] |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 207 | }` |
| 208 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 209 | _, _, err := AqueryBuildStatements([]byte(inputString)) |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 210 | assertError(t, err, "undefined input depsetId 2") |
| 211 | } |
| 212 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 213 | func TestInvalidInputDepsetIdFromDepset(t *testing.T) { |
| 214 | const inputString = ` |
| 215 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 216 | "artifacts": [ |
| 217 | { "id": 1, "pathFragmentId": 1 }, |
| 218 | { "id": 2, "pathFragmentId": 2 }], |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 219 | "actions": [{ |
| 220 | "targetId": 1, |
| 221 | "actionKey": "x", |
| 222 | "mnemonic": "x", |
| 223 | "arguments": ["touch", "foo"], |
| 224 | "inputDepSetIds": [1], |
| 225 | "outputIds": [1], |
| 226 | "primaryOutputId": 1 |
| 227 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 228 | "depSetOfFiles": [ |
| 229 | { "id": 1, "directArtifactIds": [1, 2], "transitiveDepSetIds": [42] }], |
| 230 | "pathFragments": [ |
| 231 | { "id": 1, "label": "one"}, |
| 232 | { "id": 2, "label": "two" }] |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 233 | }` |
| 234 | |
| 235 | _, _, err := AqueryBuildStatements([]byte(inputString)) |
| 236 | assertError(t, err, "undefined input depsetId 42 (referenced by depsetId 1)") |
| 237 | } |
| 238 | |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 239 | func TestInvalidInputArtifactId(t *testing.T) { |
| 240 | const inputString = ` |
| 241 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 242 | "artifacts": [ |
| 243 | { "id": 1, "pathFragmentId": 1 }, |
| 244 | { "id": 2, "pathFragmentId": 2 }], |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 245 | "actions": [{ |
| 246 | "targetId": 1, |
| 247 | "actionKey": "x", |
| 248 | "mnemonic": "x", |
| 249 | "arguments": ["touch", "foo"], |
| 250 | "inputDepSetIds": [1], |
| 251 | "outputIds": [1], |
| 252 | "primaryOutputId": 1 |
| 253 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 254 | "depSetOfFiles": [ |
| 255 | { "id": 1, "directArtifactIds": [1, 3] }], |
| 256 | "pathFragments": [ |
| 257 | { "id": 1, "label": "one" }, |
| 258 | { "id": 2, "label": "two" }] |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 259 | }` |
| 260 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 261 | _, _, err := AqueryBuildStatements([]byte(inputString)) |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 262 | assertError(t, err, "undefined input artifactId 3") |
| 263 | } |
| 264 | |
| 265 | func TestInvalidPathFragmentId(t *testing.T) { |
| 266 | const inputString = ` |
| 267 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 268 | "artifacts": [ |
| 269 | { "id": 1, "pathFragmentId": 1 }, |
| 270 | { "id": 2, "pathFragmentId": 2 }], |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 271 | "actions": [{ |
| 272 | "targetId": 1, |
| 273 | "actionKey": "x", |
| 274 | "mnemonic": "x", |
| 275 | "arguments": ["touch", "foo"], |
| 276 | "inputDepSetIds": [1], |
| 277 | "outputIds": [1], |
| 278 | "primaryOutputId": 1 |
| 279 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 280 | "depSetOfFiles": [ |
| 281 | { "id": 1, "directArtifactIds": [1, 2] }], |
| 282 | "pathFragments": [ |
| 283 | { "id": 1, "label": "one" }, |
| 284 | { "id": 2, "label": "two", "parentId": 3 }] |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 285 | }` |
| 286 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 287 | _, _, err := AqueryBuildStatements([]byte(inputString)) |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 288 | assertError(t, err, "undefined path fragment id 3") |
| 289 | } |
| 290 | |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 291 | func TestDepfiles(t *testing.T) { |
| 292 | const inputString = ` |
| 293 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 294 | "artifacts": [ |
| 295 | { "id": 1, "pathFragmentId": 1 }, |
| 296 | { "id": 2, "pathFragmentId": 2 }, |
| 297 | { "id": 3, "pathFragmentId": 3 }], |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 298 | "actions": [{ |
| 299 | "targetId": 1, |
| 300 | "actionKey": "x", |
| 301 | "mnemonic": "x", |
| 302 | "arguments": ["touch", "foo"], |
| 303 | "inputDepSetIds": [1], |
| 304 | "outputIds": [2, 3], |
| 305 | "primaryOutputId": 2 |
| 306 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 307 | "depSetOfFiles": [ |
| 308 | { "id": 1, "directArtifactIds": [1, 2, 3] }], |
| 309 | "pathFragments": [ |
| 310 | { "id": 1, "label": "one" }, |
| 311 | { "id": 2, "label": "two" }, |
| 312 | { "id": 3, "label": "two.d" }] |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 313 | }` |
| 314 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 315 | actual, _, err := AqueryBuildStatements([]byte(inputString)) |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 316 | if err != nil { |
| 317 | t.Errorf("Unexpected error %q", err) |
| 318 | } |
| 319 | if expected := 1; len(actual) != expected { |
| 320 | t.Fatalf("Expected %d build statements, got %d", expected, len(actual)) |
| 321 | } |
| 322 | |
| 323 | bs := actual[0] |
| 324 | expectedDepfile := "two.d" |
| 325 | if bs.Depfile == nil { |
| 326 | t.Errorf("Expected depfile %q, but there was none found", expectedDepfile) |
| 327 | } else if *bs.Depfile != expectedDepfile { |
| 328 | t.Errorf("Expected depfile %q, but got %q", expectedDepfile, *bs.Depfile) |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | func TestMultipleDepfiles(t *testing.T) { |
| 333 | const inputString = ` |
| 334 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 335 | "artifacts": [ |
| 336 | { "id": 1, "pathFragmentId": 1 }, |
| 337 | { "id": 2, "pathFragmentId": 2 }, |
| 338 | { "id": 3, "pathFragmentId": 3 }, |
| 339 | { "id": 4, "pathFragmentId": 4 }], |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 340 | "actions": [{ |
| 341 | "targetId": 1, |
| 342 | "actionKey": "x", |
| 343 | "mnemonic": "x", |
| 344 | "arguments": ["touch", "foo"], |
| 345 | "inputDepSetIds": [1], |
| 346 | "outputIds": [2,3,4], |
| 347 | "primaryOutputId": 2 |
| 348 | }], |
| 349 | "depSetOfFiles": [{ |
| 350 | "id": 1, |
| 351 | "directArtifactIds": [1, 2, 3, 4] |
| 352 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 353 | "pathFragments": [ |
| 354 | { "id": 1, "label": "one" }, |
| 355 | { "id": 2, "label": "two" }, |
| 356 | { "id": 3, "label": "two.d" }, |
| 357 | { "id": 4, "label": "other.d" }] |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 358 | }` |
| 359 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 360 | _, _, err := AqueryBuildStatements([]byte(inputString)) |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 361 | assertError(t, err, `found multiple potential depfiles "two.d", "other.d"`) |
| 362 | } |
| 363 | |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 364 | func TestTransitiveInputDepsets(t *testing.T) { |
| 365 | // The input aquery for this test comes from a proof-of-concept starlark rule which registers |
| 366 | // a single action with many inputs given via a deep depset. |
| 367 | const inputString = ` |
| 368 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 369 | "artifacts": [ |
| 370 | { "id": 1, "pathFragmentId": 1 }, |
| 371 | { "id": 2, "pathFragmentId": 7 }, |
| 372 | { "id": 3, "pathFragmentId": 8 }, |
| 373 | { "id": 4, "pathFragmentId": 9 }, |
| 374 | { "id": 5, "pathFragmentId": 10 }, |
| 375 | { "id": 6, "pathFragmentId": 11 }, |
| 376 | { "id": 7, "pathFragmentId": 12 }, |
| 377 | { "id": 8, "pathFragmentId": 13 }, |
| 378 | { "id": 9, "pathFragmentId": 14 }, |
| 379 | { "id": 10, "pathFragmentId": 15 }, |
| 380 | { "id": 11, "pathFragmentId": 16 }, |
| 381 | { "id": 12, "pathFragmentId": 17 }, |
| 382 | { "id": 13, "pathFragmentId": 18 }, |
| 383 | { "id": 14, "pathFragmentId": 19 }, |
| 384 | { "id": 15, "pathFragmentId": 20 }, |
| 385 | { "id": 16, "pathFragmentId": 21 }, |
| 386 | { "id": 17, "pathFragmentId": 22 }, |
| 387 | { "id": 18, "pathFragmentId": 23 }, |
| 388 | { "id": 19, "pathFragmentId": 24 }, |
| 389 | { "id": 20, "pathFragmentId": 25 }, |
| 390 | { "id": 21, "pathFragmentId": 26 }], |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 391 | "actions": [{ |
| 392 | "targetId": 1, |
| 393 | "actionKey": "3b826d17fadbbbcd8313e456b90ec47c078c438088891dd45b4adbcd8889dc50", |
| 394 | "mnemonic": "Action", |
| 395 | "configurationId": 1, |
| 396 | "arguments": ["/bin/bash", "-c", "touch bazel-out/sourceroot/k8-fastbuild/bin/testpkg/test_out"], |
| 397 | "inputDepSetIds": [1], |
| 398 | "outputIds": [21], |
| 399 | "primaryOutputId": 21 |
| 400 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 401 | "depSetOfFiles": [ |
| 402 | { "id": 3, "directArtifactIds": [1, 2, 3, 4, 5] }, |
| 403 | { "id": 4, "directArtifactIds": [6, 7, 8, 9, 10] }, |
| 404 | { "id": 2, "transitiveDepSetIds": [3, 4], "directArtifactIds": [11, 12, 13, 14, 15] }, |
| 405 | { "id": 5, "directArtifactIds": [16, 17, 18, 19] }, |
| 406 | { "id": 1, "transitiveDepSetIds": [2, 5], "directArtifactIds": [20] }], |
| 407 | "pathFragments": [ |
| 408 | { "id": 6, "label": "bazel-out" }, |
| 409 | { "id": 5, "label": "sourceroot", "parentId": 6 }, |
| 410 | { "id": 4, "label": "k8-fastbuild", "parentId": 5 }, |
| 411 | { "id": 3, "label": "bin", "parentId": 4 }, |
| 412 | { "id": 2, "label": "testpkg", "parentId": 3 }, |
| 413 | { "id": 1, "label": "test_1", "parentId": 2 }, |
| 414 | { "id": 7, "label": "test_2", "parentId": 2 }, |
| 415 | { "id": 8, "label": "test_3", "parentId": 2 }, |
| 416 | { "id": 9, "label": "test_4", "parentId": 2 }, |
| 417 | { "id": 10, "label": "test_5", "parentId": 2 }, |
| 418 | { "id": 11, "label": "test_6", "parentId": 2 }, |
| 419 | { "id": 12, "label": "test_7", "parentId": 2 }, |
| 420 | { "id": 13, "label": "test_8", "parentId": 2 }, |
| 421 | { "id": 14, "label": "test_9", "parentId": 2 }, |
| 422 | { "id": 15, "label": "test_10", "parentId": 2 }, |
| 423 | { "id": 16, "label": "test_11", "parentId": 2 }, |
| 424 | { "id": 17, "label": "test_12", "parentId": 2 }, |
| 425 | { "id": 18, "label": "test_13", "parentId": 2 }, |
| 426 | { "id": 19, "label": "test_14", "parentId": 2 }, |
| 427 | { "id": 20, "label": "test_15", "parentId": 2 }, |
| 428 | { "id": 21, "label": "test_16", "parentId": 2 }, |
| 429 | { "id": 22, "label": "test_17", "parentId": 2 }, |
| 430 | { "id": 23, "label": "test_18", "parentId": 2 }, |
| 431 | { "id": 24, "label": "test_19", "parentId": 2 }, |
| 432 | { "id": 25, "label": "test_root", "parentId": 2 }, |
| 433 | { "id": 26,"label": "test_out", "parentId": 2 }] |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 434 | }` |
| 435 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 436 | actualbuildStatements, actualDepsets, _ := AqueryBuildStatements([]byte(inputString)) |
| 437 | |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 438 | expectedBuildStatements := []BuildStatement{ |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 439 | { |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 440 | Command: "/bin/bash -c 'touch bazel-out/sourceroot/k8-fastbuild/bin/testpkg/test_out'", |
| 441 | OutputPaths: []string{"bazel-out/sourceroot/k8-fastbuild/bin/testpkg/test_out"}, |
| 442 | Mnemonic: "Action", |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 443 | }, |
| 444 | } |
| 445 | assertBuildStatements(t, expectedBuildStatements, actualbuildStatements) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 446 | |
| 447 | // Inputs for the action are test_{i} from 1 to 20, and test_root. These inputs |
| 448 | // are given via a deep depset, but the depset is flattened when returned as a |
| 449 | // BuildStatement slice. |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 450 | var expectedFlattenedInputs []string |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 451 | for i := 1; i < 20; i++ { |
| 452 | expectedFlattenedInputs = append(expectedFlattenedInputs, fmt.Sprintf("bazel-out/sourceroot/k8-fastbuild/bin/testpkg/test_%d", i)) |
| 453 | } |
| 454 | expectedFlattenedInputs = append(expectedFlattenedInputs, "bazel-out/sourceroot/k8-fastbuild/bin/testpkg/test_root") |
| 455 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 456 | actualDepsetHashes := actualbuildStatements[0].InputDepsetHashes |
| 457 | actualFlattenedInputs := flattenDepsets(actualDepsetHashes, actualDepsets) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 458 | if !reflect.DeepEqual(actualFlattenedInputs, expectedFlattenedInputs) { |
| 459 | t.Errorf("Expected flattened inputs %v, but got %v", expectedFlattenedInputs, actualFlattenedInputs) |
| 460 | } |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 461 | } |
| 462 | |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 463 | func TestBazelOutRemovalFromInputDepsets(t *testing.T) { |
| 464 | const inputString = `{ |
| 465 | "artifacts": [{ |
| 466 | "id": 1, |
| 467 | "pathFragmentId": 10 |
| 468 | }, { |
| 469 | "id": 2, |
| 470 | "pathFragmentId": 20 |
| 471 | }, { |
| 472 | "id": 3, |
| 473 | "pathFragmentId": 30 |
| 474 | }, { |
| 475 | "id": 4, |
| 476 | "pathFragmentId": 40 |
| 477 | }], |
| 478 | "depSetOfFiles": [{ |
| 479 | "id": 1111, |
| 480 | "directArtifactIds": [3 , 4] |
| 481 | }], |
| 482 | "actions": [{ |
| 483 | "targetId": 100, |
| 484 | "actionKey": "x", |
| 485 | "inputDepSetIds": [1111], |
| 486 | "mnemonic": "x", |
| 487 | "arguments": ["bogus", "command"], |
| 488 | "outputIds": [2], |
| 489 | "primaryOutputId": 1 |
| 490 | }], |
| 491 | "pathFragments": [{ |
| 492 | "id": 10, |
| 493 | "label": "input" |
| 494 | }, { |
| 495 | "id": 20, |
| 496 | "label": "output" |
| 497 | }, { |
| 498 | "id": 30, |
| 499 | "label": "dep1", |
| 500 | "parentId": 50 |
| 501 | }, { |
| 502 | "id": 40, |
| 503 | "label": "dep2", |
| 504 | "parentId": 60 |
| 505 | }, { |
| 506 | "id": 50, |
| 507 | "label": "bazel_tools", |
| 508 | "parentId": 60 |
| 509 | }, { |
| 510 | "id": 60, |
| 511 | "label": ".." |
| 512 | }] |
| 513 | }` |
| 514 | actualBuildStatements, actualDepsets, _ := AqueryBuildStatements([]byte(inputString)) |
| 515 | if len(actualDepsets) != 1 { |
| 516 | t.Errorf("expected 1 depset but found %#v", actualDepsets) |
| 517 | return |
| 518 | } |
| 519 | dep2Found := false |
| 520 | for _, dep := range flattenDepsets([]string{actualDepsets[0].ContentHash}, actualDepsets) { |
| 521 | if dep == "../bazel_tools/dep1" { |
| 522 | t.Errorf("dependency %s expected to be removed but still exists", dep) |
| 523 | } else if dep == "../dep2" { |
| 524 | dep2Found = true |
| 525 | } |
| 526 | } |
| 527 | if !dep2Found { |
| 528 | t.Errorf("dependency ../dep2 expected but not found") |
| 529 | } |
| 530 | |
| 531 | expectedBuildStatement := BuildStatement{ |
| 532 | Command: "bogus command", |
| 533 | OutputPaths: []string{"output"}, |
| 534 | Mnemonic: "x", |
| 535 | } |
| 536 | buildStatementFound := false |
| 537 | for _, actualBuildStatement := range actualBuildStatements { |
| 538 | if buildStatementEquals(actualBuildStatement, expectedBuildStatement) == "" { |
| 539 | buildStatementFound = true |
| 540 | break |
| 541 | } |
| 542 | } |
| 543 | if !buildStatementFound { |
| 544 | t.Errorf("expected but missing %#v in %#v", expectedBuildStatement, actualBuildStatements) |
| 545 | return |
| 546 | } |
| 547 | } |
| 548 | |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 549 | func TestMiddlemenAction(t *testing.T) { |
| 550 | const inputString = ` |
| 551 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 552 | "artifacts": [ |
| 553 | { "id": 1, "pathFragmentId": 1 }, |
| 554 | { "id": 2, "pathFragmentId": 2 }, |
| 555 | { "id": 3, "pathFragmentId": 3 }, |
| 556 | { "id": 4, "pathFragmentId": 4 }, |
| 557 | { "id": 5, "pathFragmentId": 5 }, |
| 558 | { "id": 6, "pathFragmentId": 6 }], |
| 559 | "pathFragments": [ |
| 560 | { "id": 1, "label": "middleinput_one" }, |
| 561 | { "id": 2, "label": "middleinput_two" }, |
| 562 | { "id": 3, "label": "middleman_artifact" }, |
| 563 | { "id": 4, "label": "maininput_one" }, |
| 564 | { "id": 5, "label": "maininput_two" }, |
| 565 | { "id": 6, "label": "output" }], |
| 566 | "depSetOfFiles": [ |
| 567 | { "id": 1, "directArtifactIds": [1, 2] }, |
| 568 | { "id": 2, "directArtifactIds": [3, 4, 5] }], |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 569 | "actions": [{ |
| 570 | "targetId": 1, |
| 571 | "actionKey": "x", |
| 572 | "mnemonic": "Middleman", |
| 573 | "arguments": ["touch", "foo"], |
| 574 | "inputDepSetIds": [1], |
| 575 | "outputIds": [3], |
| 576 | "primaryOutputId": 3 |
| 577 | }, { |
| 578 | "targetId": 2, |
| 579 | "actionKey": "y", |
| 580 | "mnemonic": "Main action", |
| 581 | "arguments": ["touch", "foo"], |
| 582 | "inputDepSetIds": [2], |
| 583 | "outputIds": [6], |
| 584 | "primaryOutputId": 6 |
| 585 | }] |
| 586 | }` |
| 587 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 588 | actualBuildStatements, actualDepsets, err := AqueryBuildStatements([]byte(inputString)) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 589 | if err != nil { |
| 590 | t.Errorf("Unexpected error %q", err) |
| 591 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 592 | if expected := 1; len(actualBuildStatements) != expected { |
| 593 | t.Fatalf("Expected %d build statements, got %d", expected, len(actualBuildStatements)) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 594 | } |
| 595 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 596 | expectedDepsetFiles := [][]string{ |
Usta Shrestha | 2ccdb42 | 2022-06-02 10:19:13 -0400 | [diff] [blame] | 597 | {"middleinput_one", "middleinput_two", "maininput_one", "maininput_two"}, |
| 598 | {"middleinput_one", "middleinput_two"}, |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 599 | } |
| 600 | assertFlattenedDepsets(t, actualDepsets, expectedDepsetFiles) |
| 601 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 602 | bs := actualBuildStatements[0] |
| 603 | if len(bs.InputPaths) > 0 { |
| 604 | t.Errorf("Expected main action raw inputs to be empty, but got %q", bs.InputPaths) |
| 605 | } |
| 606 | |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 607 | expectedOutputs := []string{"output"} |
| 608 | if !reflect.DeepEqual(bs.OutputPaths, expectedOutputs) { |
| 609 | t.Errorf("Expected main action outputs %q, but got %q", expectedOutputs, bs.OutputPaths) |
| 610 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 611 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 612 | expectedFlattenedInputs := []string{"middleinput_one", "middleinput_two", "maininput_one", "maininput_two"} |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 613 | actualFlattenedInputs := flattenDepsets(bs.InputDepsetHashes, actualDepsets) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 614 | |
| 615 | if !reflect.DeepEqual(actualFlattenedInputs, expectedFlattenedInputs) { |
| 616 | t.Errorf("Expected flattened inputs %v, but got %v", expectedFlattenedInputs, actualFlattenedInputs) |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | // Returns the contents of given depsets in concatenated post order. |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 621 | func flattenDepsets(depsetHashesToFlatten []string, allDepsets []AqueryDepset) []string { |
| 622 | depsetsByHash := map[string]AqueryDepset{} |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 623 | for _, depset := range allDepsets { |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 624 | depsetsByHash[depset.ContentHash] = depset |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 625 | } |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 626 | var result []string |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 627 | for _, depsetId := range depsetHashesToFlatten { |
| 628 | result = append(result, flattenDepset(depsetId, depsetsByHash)...) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 629 | } |
| 630 | return result |
| 631 | } |
| 632 | |
| 633 | // Returns the contents of a given depset in post order. |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 634 | func flattenDepset(depsetHashToFlatten string, allDepsets map[string]AqueryDepset) []string { |
| 635 | depset := allDepsets[depsetHashToFlatten] |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 636 | var result []string |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 637 | for _, depsetId := range depset.TransitiveDepSetHashes { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 638 | result = append(result, flattenDepset(depsetId, allDepsets)...) |
| 639 | } |
| 640 | result = append(result, depset.DirectArtifacts...) |
| 641 | return result |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 642 | } |
| 643 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 644 | func assertFlattenedDepsets(t *testing.T, actualDepsets []AqueryDepset, expectedDepsetFiles [][]string) { |
| 645 | t.Helper() |
| 646 | if len(actualDepsets) != len(expectedDepsetFiles) { |
Sasha Smundak | f10c3ac | 2022-06-08 11:46:31 -0700 | [diff] [blame] | 647 | t.Errorf("Expected %d depsets, but got %d depsets", len(expectedDepsetFiles), len(actualDepsets)) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 648 | } |
| 649 | for i, actualDepset := range actualDepsets { |
| 650 | actualFlattenedInputs := flattenDepsets([]string{actualDepset.ContentHash}, actualDepsets) |
| 651 | if !reflect.DeepEqual(actualFlattenedInputs, expectedDepsetFiles[i]) { |
| 652 | t.Errorf("Expected depset files: %v, but got %v", expectedDepsetFiles[i], actualFlattenedInputs) |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 657 | func TestSimpleSymlink(t *testing.T) { |
| 658 | const inputString = ` |
| 659 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 660 | "artifacts": [ |
| 661 | { "id": 1, "pathFragmentId": 3 }, |
| 662 | { "id": 2, "pathFragmentId": 5 }], |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 663 | "actions": [{ |
| 664 | "targetId": 1, |
| 665 | "actionKey": "x", |
| 666 | "mnemonic": "Symlink", |
| 667 | "inputDepSetIds": [1], |
| 668 | "outputIds": [2], |
| 669 | "primaryOutputId": 2 |
| 670 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 671 | "depSetOfFiles": [ |
| 672 | { "id": 1, "directArtifactIds": [1] }], |
| 673 | "pathFragments": [ |
| 674 | { "id": 1, "label": "one" }, |
| 675 | { "id": 2, "label": "file_subdir", "parentId": 1 }, |
| 676 | { "id": 3, "label": "file", "parentId": 2 }, |
| 677 | { "id": 4, "label": "symlink_subdir", "parentId": 1 }, |
| 678 | { "id": 5, "label": "symlink", "parentId": 4 }] |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 679 | }` |
| 680 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 681 | actual, _, err := AqueryBuildStatements([]byte(inputString)) |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 682 | |
| 683 | if err != nil { |
| 684 | t.Errorf("Unexpected error %q", err) |
| 685 | } |
| 686 | |
| 687 | expectedBuildStatements := []BuildStatement{ |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 688 | { |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 689 | Command: "mkdir -p one/symlink_subdir && " + |
| 690 | "rm -f one/symlink_subdir/symlink && " + |
Liz Kammer | c773778 | 2021-11-04 10:56:13 -0400 | [diff] [blame] | 691 | "ln -sf $PWD/one/file_subdir/file one/symlink_subdir/symlink", |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 692 | InputPaths: []string{"one/file_subdir/file"}, |
| 693 | OutputPaths: []string{"one/symlink_subdir/symlink"}, |
| 694 | SymlinkPaths: []string{"one/symlink_subdir/symlink"}, |
| 695 | Mnemonic: "Symlink", |
| 696 | }, |
| 697 | } |
| 698 | assertBuildStatements(t, actual, expectedBuildStatements) |
| 699 | } |
| 700 | |
| 701 | func TestSymlinkQuotesPaths(t *testing.T) { |
| 702 | const inputString = ` |
| 703 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 704 | "artifacts": [ |
| 705 | { "id": 1, "pathFragmentId": 3 }, |
| 706 | { "id": 2, "pathFragmentId": 5 }], |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 707 | "actions": [{ |
| 708 | "targetId": 1, |
| 709 | "actionKey": "x", |
| 710 | "mnemonic": "SolibSymlink", |
| 711 | "inputDepSetIds": [1], |
| 712 | "outputIds": [2], |
| 713 | "primaryOutputId": 2 |
| 714 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 715 | "depSetOfFiles": [ |
| 716 | { "id": 1, "directArtifactIds": [1] }], |
| 717 | "pathFragments": [ |
| 718 | { "id": 1, "label": "one" }, |
| 719 | { "id": 2, "label": "file subdir", "parentId": 1 }, |
| 720 | { "id": 3, "label": "file", "parentId": 2 }, |
| 721 | { "id": 4, "label": "symlink subdir", "parentId": 1 }, |
| 722 | { "id": 5, "label": "symlink", "parentId": 4 }] |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 723 | }` |
| 724 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 725 | actual, _, err := AqueryBuildStatements([]byte(inputString)) |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 726 | |
| 727 | if err != nil { |
| 728 | t.Errorf("Unexpected error %q", err) |
| 729 | } |
| 730 | |
| 731 | expectedBuildStatements := []BuildStatement{ |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 732 | { |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 733 | Command: "mkdir -p 'one/symlink subdir' && " + |
| 734 | "rm -f 'one/symlink subdir/symlink' && " + |
Liz Kammer | c773778 | 2021-11-04 10:56:13 -0400 | [diff] [blame] | 735 | "ln -sf $PWD/'one/file subdir/file' 'one/symlink subdir/symlink'", |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 736 | InputPaths: []string{"one/file subdir/file"}, |
| 737 | OutputPaths: []string{"one/symlink subdir/symlink"}, |
| 738 | SymlinkPaths: []string{"one/symlink subdir/symlink"}, |
| 739 | Mnemonic: "SolibSymlink", |
| 740 | }, |
| 741 | } |
Liz Kammer | c773778 | 2021-11-04 10:56:13 -0400 | [diff] [blame] | 742 | assertBuildStatements(t, expectedBuildStatements, actual) |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | func TestSymlinkMultipleInputs(t *testing.T) { |
| 746 | const inputString = ` |
| 747 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 748 | "artifacts": [ |
| 749 | { "id": 1, "pathFragmentId": 1 }, |
| 750 | { "id": 2, "pathFragmentId": 2 }, |
| 751 | { "id": 3, "pathFragmentId": 3 }], |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 752 | "actions": [{ |
| 753 | "targetId": 1, |
| 754 | "actionKey": "x", |
| 755 | "mnemonic": "Symlink", |
| 756 | "inputDepSetIds": [1], |
| 757 | "outputIds": [3], |
| 758 | "primaryOutputId": 3 |
| 759 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 760 | "depSetOfFiles": [{ "id": 1, "directArtifactIds": [1,2] }], |
| 761 | "pathFragments": [ |
| 762 | { "id": 1, "label": "file" }, |
| 763 | { "id": 2, "label": "other_file" }, |
| 764 | { "id": 3, "label": "symlink" }] |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 765 | }` |
| 766 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 767 | _, _, err := AqueryBuildStatements([]byte(inputString)) |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 768 | assertError(t, err, `Expect 1 input and 1 output to symlink action, got: input ["file" "other_file"], output ["symlink"]`) |
| 769 | } |
| 770 | |
| 771 | func TestSymlinkMultipleOutputs(t *testing.T) { |
| 772 | const inputString = ` |
| 773 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 774 | "artifacts": [ |
| 775 | { "id": 1, "pathFragmentId": 1 }, |
| 776 | { "id": 2, "pathFragmentId": 2 }, |
| 777 | { "id": 3, "pathFragmentId": 3 }], |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 778 | "actions": [{ |
| 779 | "targetId": 1, |
| 780 | "actionKey": "x", |
| 781 | "mnemonic": "Symlink", |
| 782 | "inputDepSetIds": [1], |
| 783 | "outputIds": [2,3], |
| 784 | "primaryOutputId": 2 |
| 785 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 786 | "depSetOfFiles": [ |
| 787 | { "id": 1, "directArtifactIds": [1] }], |
| 788 | "pathFragments": [ |
| 789 | { "id": 1, "label": "file" }, |
| 790 | { "id": 2, "label": "symlink" }, |
| 791 | { "id": 3, "label": "other_symlink" }] |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 792 | }` |
| 793 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 794 | _, _, err := AqueryBuildStatements([]byte(inputString)) |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 795 | assertError(t, err, `Expect 1 input and 1 output to symlink action, got: input ["file"], output ["symlink" "other_symlink"]`) |
| 796 | } |
| 797 | |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 798 | func TestTemplateExpandActionSubstitutions(t *testing.T) { |
| 799 | const inputString = ` |
| 800 | { |
| 801 | "artifacts": [{ |
| 802 | "id": 1, |
| 803 | "pathFragmentId": 1 |
| 804 | }], |
| 805 | "actions": [{ |
| 806 | "targetId": 1, |
| 807 | "actionKey": "x", |
| 808 | "mnemonic": "TemplateExpand", |
| 809 | "configurationId": 1, |
| 810 | "outputIds": [1], |
| 811 | "primaryOutputId": 1, |
| 812 | "executionPlatform": "//build/bazel/platforms:linux_x86_64", |
| 813 | "templateContent": "Test template substitutions: %token1%, %python_binary%", |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 814 | "substitutions": [ |
| 815 | { "key": "%token1%", "value": "abcd" }, |
| 816 | { "key": "%python_binary%", "value": "python3" }] |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 817 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 818 | "pathFragments": [ |
| 819 | { "id": 1, "label": "template_file" }] |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 820 | }` |
| 821 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 822 | actual, _, err := AqueryBuildStatements([]byte(inputString)) |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 823 | |
| 824 | if err != nil { |
| 825 | t.Errorf("Unexpected error %q", err) |
| 826 | } |
| 827 | |
| 828 | expectedBuildStatements := []BuildStatement{ |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 829 | { |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 830 | Command: "/bin/bash -c 'echo \"Test template substitutions: abcd, python3\" | sed \"s/\\\\\\\\n/\\\\n/g\" > template_file && " + |
| 831 | "chmod a+x template_file'", |
| 832 | OutputPaths: []string{"template_file"}, |
| 833 | Mnemonic: "TemplateExpand", |
| 834 | }, |
| 835 | } |
| 836 | assertBuildStatements(t, expectedBuildStatements, actual) |
| 837 | } |
| 838 | |
| 839 | func TestTemplateExpandActionNoOutput(t *testing.T) { |
| 840 | const inputString = ` |
| 841 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 842 | "artifacts": [ |
| 843 | { "id": 1, "pathFragmentId": 1 }], |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 844 | "actions": [{ |
| 845 | "targetId": 1, |
| 846 | "actionKey": "x", |
| 847 | "mnemonic": "TemplateExpand", |
| 848 | "configurationId": 1, |
| 849 | "primaryOutputId": 1, |
| 850 | "executionPlatform": "//build/bazel/platforms:linux_x86_64", |
| 851 | "templateContent": "Test template substitutions: %token1%, %python_binary%", |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 852 | "substitutions": [ |
| 853 | { "key": "%token1%", "value": "abcd" }, |
| 854 | { "key": "%python_binary%", "value": "python3" }] |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 855 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 856 | "pathFragments": [ |
| 857 | { "id": 1, "label": "template_file" }] |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 858 | }` |
| 859 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 860 | _, _, err := AqueryBuildStatements([]byte(inputString)) |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 861 | assertError(t, err, `Expect 1 output to template expand action, got: output []`) |
| 862 | } |
| 863 | |
| 864 | func TestPythonZipperActionSuccess(t *testing.T) { |
| 865 | const inputString = ` |
| 866 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 867 | "artifacts": [ |
| 868 | { "id": 1, "pathFragmentId": 1 }, |
| 869 | { "id": 2, "pathFragmentId": 2 }, |
| 870 | { "id": 3, "pathFragmentId": 3 }, |
| 871 | { "id": 4, "pathFragmentId": 4 }, |
| 872 | { "id": 5, "pathFragmentId": 10 }, |
| 873 | { "id": 10, "pathFragmentId": 20 }], |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 874 | "actions": [{ |
| 875 | "targetId": 1, |
| 876 | "actionKey": "x", |
| 877 | "mnemonic": "TemplateExpand", |
| 878 | "configurationId": 1, |
| 879 | "outputIds": [1], |
| 880 | "primaryOutputId": 1, |
| 881 | "executionPlatform": "//build/bazel/platforms:linux_x86_64", |
| 882 | "templateContent": "Test template substitutions: %token1%, %python_binary%", |
| 883 | "substitutions": [{ |
| 884 | "key": "%token1%", |
| 885 | "value": "abcd" |
| 886 | },{ |
| 887 | "key": "%python_binary%", |
| 888 | "value": "python3" |
| 889 | }] |
| 890 | },{ |
| 891 | "targetId": 1, |
| 892 | "actionKey": "x", |
| 893 | "mnemonic": "PythonZipper", |
| 894 | "configurationId": 1, |
| 895 | "arguments": ["../bazel_tools/tools/zip/zipper/zipper", "cC", "python_binary.zip", "__main__.py\u003dbazel-out/k8-fastbuild/bin/python_binary.temp", "__init__.py\u003d", "runfiles/__main__/__init__.py\u003d", "runfiles/__main__/python_binary.py\u003dpython_binary.py", "runfiles/bazel_tools/tools/python/py3wrapper.sh\u003dbazel-out/bazel_tools/k8-fastbuild/bin/tools/python/py3wrapper.sh"], |
| 896 | "outputIds": [2], |
| 897 | "inputDepSetIds": [1], |
| 898 | "primaryOutputId": 2 |
| 899 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 900 | "depSetOfFiles": [ |
| 901 | { "id": 1, "directArtifactIds": [4, 3, 5] }], |
| 902 | "pathFragments": [ |
| 903 | { "id": 1, "label": "python_binary" }, |
| 904 | { "id": 2, "label": "python_binary.zip" }, |
| 905 | { "id": 3, "label": "python_binary.py" }, |
| 906 | { "id": 9, "label": ".." }, |
| 907 | { "id": 8, "label": "bazel_tools", "parentId": 9 }, |
| 908 | { "id": 7, "label": "tools", "parentId": 8 }, |
| 909 | { "id": 6, "label": "zip", "parentId": 7 }, |
| 910 | { "id": 5, "label": "zipper", "parentId": 6 }, |
| 911 | { "id": 4, "label": "zipper", "parentId": 5 }, |
| 912 | { "id": 16, "label": "bazel-out" }, |
| 913 | { "id": 15, "label": "bazel_tools", "parentId": 16 }, |
| 914 | { "id": 14, "label": "k8-fastbuild", "parentId": 15 }, |
| 915 | { "id": 13, "label": "bin", "parentId": 14 }, |
| 916 | { "id": 12, "label": "tools", "parentId": 13 }, |
| 917 | { "id": 11, "label": "python", "parentId": 12 }, |
| 918 | { "id": 10, "label": "py3wrapper.sh", "parentId": 11 }, |
| 919 | { "id": 20, "label": "python_binary" }] |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 920 | }` |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 921 | actual, _, err := AqueryBuildStatements([]byte(inputString)) |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 922 | |
| 923 | if err != nil { |
| 924 | t.Errorf("Unexpected error %q", err) |
| 925 | } |
| 926 | |
| 927 | expectedBuildStatements := []BuildStatement{ |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 928 | { |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 929 | Command: "/bin/bash -c 'echo \"Test template substitutions: abcd, python3\" | sed \"s/\\\\\\\\n/\\\\n/g\" > python_binary && " + |
| 930 | "chmod a+x python_binary'", |
| 931 | InputPaths: []string{"python_binary.zip"}, |
| 932 | OutputPaths: []string{"python_binary"}, |
| 933 | Mnemonic: "TemplateExpand", |
| 934 | }, |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 935 | { |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 936 | Command: "../bazel_tools/tools/zip/zipper/zipper cC python_binary.zip __main__.py=bazel-out/k8-fastbuild/bin/python_binary.temp " + |
| 937 | "__init__.py= runfiles/__main__/__init__.py= runfiles/__main__/python_binary.py=python_binary.py && " + |
| 938 | "../bazel_tools/tools/zip/zipper/zipper x python_binary.zip -d python_binary.runfiles && ln -sf runfiles/__main__ python_binary.runfiles", |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 939 | InputPaths: []string{"python_binary.py"}, |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 940 | OutputPaths: []string{"python_binary.zip"}, |
| 941 | Mnemonic: "PythonZipper", |
| 942 | }, |
| 943 | } |
| 944 | assertBuildStatements(t, expectedBuildStatements, actual) |
| 945 | } |
| 946 | |
| 947 | func TestPythonZipperActionNoInput(t *testing.T) { |
| 948 | const inputString = ` |
| 949 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 950 | "artifacts": [ |
| 951 | { "id": 1, "pathFragmentId": 1 }, |
| 952 | { "id": 2, "pathFragmentId": 2 }], |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 953 | "actions": [{ |
| 954 | "targetId": 1, |
| 955 | "actionKey": "x", |
| 956 | "mnemonic": "PythonZipper", |
| 957 | "configurationId": 1, |
| 958 | "arguments": ["../bazel_tools/tools/zip/zipper/zipper", "cC", "python_binary.zip", "__main__.py\u003dbazel-out/k8-fastbuild/bin/python_binary.temp", "__init__.py\u003d", "runfiles/__main__/__init__.py\u003d", "runfiles/__main__/python_binary.py\u003dpython_binary.py", "runfiles/bazel_tools/tools/python/py3wrapper.sh\u003dbazel-out/bazel_tools/k8-fastbuild/bin/tools/python/py3wrapper.sh"], |
| 959 | "outputIds": [2], |
| 960 | "primaryOutputId": 2 |
| 961 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 962 | "pathFragments": [ |
| 963 | { "id": 1, "label": "python_binary" }, |
| 964 | { "id": 2, "label": "python_binary.zip" }] |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 965 | }` |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 966 | _, _, err := AqueryBuildStatements([]byte(inputString)) |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 967 | assertError(t, err, `Expect 1+ input and 1 output to python zipper action, got: input [], output ["python_binary.zip"]`) |
| 968 | } |
| 969 | |
| 970 | func TestPythonZipperActionNoOutput(t *testing.T) { |
| 971 | const inputString = ` |
| 972 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 973 | "artifacts": [ |
| 974 | { "id": 1, "pathFragmentId": 1 }, |
| 975 | { "id": 2, "pathFragmentId": 2 }, |
| 976 | { "id": 3, "pathFragmentId": 3 }, |
| 977 | { "id": 4, "pathFragmentId": 4 }, |
| 978 | { "id": 5, "pathFragmentId": 10 }], |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 979 | "actions": [{ |
| 980 | "targetId": 1, |
| 981 | "actionKey": "x", |
| 982 | "mnemonic": "PythonZipper", |
| 983 | "configurationId": 1, |
| 984 | "arguments": ["../bazel_tools/tools/zip/zipper/zipper", "cC", "python_binary.zip", "__main__.py\u003dbazel-out/k8-fastbuild/bin/python_binary.temp", "__init__.py\u003d", "runfiles/__main__/__init__.py\u003d", "runfiles/__main__/python_binary.py\u003dpython_binary.py", "runfiles/bazel_tools/tools/python/py3wrapper.sh\u003dbazel-out/bazel_tools/k8-fastbuild/bin/tools/python/py3wrapper.sh"], |
| 985 | "inputDepSetIds": [1] |
| 986 | }], |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 987 | "depSetOfFiles": [ |
| 988 | { "id": 1, "directArtifactIds": [4, 3, 5]}], |
| 989 | "pathFragments": [ |
| 990 | { "id": 1, "label": "python_binary" }, |
| 991 | { "id": 2, "label": "python_binary.zip" }, |
| 992 | { "id": 3, "label": "python_binary.py" }, |
| 993 | { "id": 9, "label": ".." }, |
| 994 | { "id": 8, "label": "bazel_tools", "parentId": 9 }, |
| 995 | { "id": 7, "label": "tools", "parentId": 8 }, |
| 996 | { "id": 6, "label": "zip", "parentId": 7 }, |
| 997 | { "id": 5, "label": "zipper", "parentId": 6 }, |
| 998 | { "id": 4, "label": "zipper", "parentId": 5 }, |
| 999 | { "id": 16, "label": "bazel-out" }, |
| 1000 | { "id": 15, "label": "bazel_tools", "parentId": 16 }, |
| 1001 | { "id": 14, "label": "k8-fastbuild", "parentId": 15 }, |
| 1002 | { "id": 13, "label": "bin", "parentId": 14 }, |
| 1003 | { "id": 12, "label": "tools", "parentId": 13 }, |
| 1004 | { "id": 11, "label": "python", "parentId": 12 }, |
| 1005 | { "id": 10, "label": "py3wrapper.sh", "parentId": 11 }] |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 1006 | }` |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1007 | _, _, err := AqueryBuildStatements([]byte(inputString)) |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 1008 | assertError(t, err, `Expect 1+ input and 1 output to python zipper action, got: input ["python_binary.py"], output []`) |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1011 | func assertError(t *testing.T, err error, expected string) { |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 1012 | t.Helper() |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 1013 | if err == nil { |
| 1014 | t.Errorf("expected error '%s', but got no error", expected) |
| 1015 | } else if err.Error() != expected { |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 1016 | t.Errorf("expected error:\n\t'%s', but got:\n\t'%s'", expected, err.Error()) |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | // Asserts that the given actual build statements match the given expected build statements. |
| 1021 | // Build statement equivalence is determined using buildStatementEquals. |
| 1022 | func assertBuildStatements(t *testing.T, expected []BuildStatement, actual []BuildStatement) { |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 1023 | t.Helper() |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1024 | if len(expected) != len(actual) { |
Liz Kammer | c319299 | 2021-11-16 17:01:11 -0500 | [diff] [blame] | 1025 | t.Errorf("expected %d build statements, but got %d,\n expected: %#v,\n actual: %#v", |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1026 | len(expected), len(actual), expected, actual) |
| 1027 | return |
| 1028 | } |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1029 | type compareFn = func(i int, j int) bool |
| 1030 | byCommand := func(slice []BuildStatement) compareFn { |
| 1031 | return func(i int, j int) bool { |
| 1032 | return slice[i].Command < slice[j].Command |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1033 | } |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1034 | } |
| 1035 | sort.SliceStable(expected, byCommand(expected)) |
| 1036 | sort.SliceStable(actual, byCommand(actual)) |
| 1037 | for i, actualStatement := range actual { |
| 1038 | expectedStatement := expected[i] |
| 1039 | if differingField := buildStatementEquals(actualStatement, expectedStatement); differingField != "" { |
| 1040 | t.Errorf("%s differs\nunexpected build statement %#v.\nexpected: %#v", |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 1041 | differingField, actualStatement, expectedStatement) |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1042 | return |
| 1043 | } |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1047 | func buildStatementEquals(first BuildStatement, second BuildStatement) string { |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1048 | if first.Mnemonic != second.Mnemonic { |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1049 | return "Mnemonic" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1050 | } |
| 1051 | if first.Command != second.Command { |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1052 | return "Command" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1053 | } |
| 1054 | // Ordering is significant for environment variables. |
| 1055 | if !reflect.DeepEqual(first.Env, second.Env) { |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1056 | return "Env" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1057 | } |
| 1058 | // Ordering is irrelevant for input and output paths, so compare sets. |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1059 | if !reflect.DeepEqual(sortedStrings(first.InputPaths), sortedStrings(second.InputPaths)) { |
| 1060 | return "InputPaths" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1061 | } |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1062 | if !reflect.DeepEqual(sortedStrings(first.OutputPaths), sortedStrings(second.OutputPaths)) { |
| 1063 | return "OutputPaths" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1064 | } |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1065 | if !reflect.DeepEqual(sortedStrings(first.SymlinkPaths), sortedStrings(second.SymlinkPaths)) { |
| 1066 | return "SymlinkPaths" |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 1067 | } |
| 1068 | if first.Depfile != second.Depfile { |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1069 | return "Depfile" |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 1070 | } |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1071 | return "" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1072 | } |
| 1073 | |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1074 | func sortedStrings(stringSlice []string) []string { |
| 1075 | sorted := make([]string, len(stringSlice)) |
| 1076 | copy(sorted, stringSlice) |
| 1077 | sort.Strings(sorted) |
| 1078 | return sorted |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1079 | } |