aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/shell/bazel/external_patching_test.sh
blob: ee3c938fe8f8eb3a7d3c4fb641f11c6dae45910e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#!/bin/bash
#
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Tests the patching functionality of external repositories.

# Load the test setup defined in the parent directory
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${CURRENT_DIR}/../integration_test_setup.sh" \
  || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
source "${CURRENT_DIR}/remote_helpers.sh" \
  || { echo "remote_helpers.sh not found!" >&2; exit 1; }


set_up() {
  WRKDIR=$(mktemp -d "${TEST_TMPDIR}/testXXXXXX")
  cd "${WRKDIR}"
  # create an archive file with files interesting for patching
  mkdir ext-0.1.2
  cat > ext-0.1.2/foo.sh <<'EOF'
#!/usr/bin/env sh

echo Here be dragons...
EOF
  zip ext.zip ext-0.1.2/*
  rm -rf ext-0.1.2
}


test_patch_file() {
  EXTREPODIR=`pwd`

  # Test that the patches attribute of http_archive is honored
  mkdir main
  cd main
  cat > patch_foo.sh <<'EOF'
--- foo.sh.orig	2018-01-15 10:39:20.183909147 +0100
+++ foo.sh	2018-01-15 10:43:35.331566052 +0100
@@ -1,3 +1,3 @@
 #!/usr/bin/env sh

-echo Here be dragons...
+echo There are dragons...
EOF
  cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
  name="ext",
  strip_prefix="ext-0.1.2",
  urls=["file://${EXTREPODIR}/ext.zip"],
  build_file_content="exports_files([\"foo.sh\"])",
  patches = ["//:patch_foo.sh"],
  patch_cmds = ["find . -name '*.sh' -exec sed -i.orig '1s|#!/usr/bin/env sh\$|/bin/sh\$|' {} +"],
)
EOF
  cat > BUILD <<'EOF'
genrule(
  name = "foo",
  outs = ["foo.sh"],
  srcs = ["@ext//:foo.sh"],
  cmd = "cp $< $@; chmod u+x $@",
  executable = True,
)
EOF
  bazel build :foo.sh
  foopath=`bazel info bazel-genfiles`/foo.sh
  grep -q 'There are' $foopath || fail "expected patch to be applied"
  grep env $foopath && fail "expected patch commands to be executed" || :

  # Verify that changes to the patch files trigger enough rebuilding
  cat > patch_foo.sh <<'EOF'
--- foo.sh.orig	2018-01-15 10:39:20.183909147 +0100
+++ foo.sh	2018-01-15 10:43:35.331566052 +0100
@@ -1,3 +1,3 @@
 #!/usr/bin/env sh

-echo Here be dragons...
+echo completely differently patched
EOF
  bazel build :foo.sh
  foopath=`bazel info bazel-genfiles`/foo.sh
  grep -q 'differently patched' $foopath \
      || fail "expected the new patch to be applied"

  # Verify that changes to the patches attribute trigger enough rebuilding
  cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
  name="ext",
  strip_prefix="ext-0.1.2",
  urls=["file://${EXTREPODIR}/ext.zip"],
  build_file_content="exports_files([\"foo.sh\"])",
)
EOF
  bazel build :foo.sh
  foopath=`bazel info bazel-genfiles`/foo.sh
  grep -q 'Here be' $foopath || fail "expected unpatched file"
  grep -q 'env' $foopath || fail "expected unpatched file"
}

test_patch_failed() {
  EXTREPODIR=`pwd`

  cat > my_patch_tool <<'EOF'
#!/bin/sh

echo Helpful message
exit 1
EOF
  chmod u+x my_patch_tool

  # Test that the patches attribute of http_archive is honored
  mkdir main
  cd main
  echo "ignored anyway" > patch_foo.sh
  cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
  name="ext",
  strip_prefix="ext-0.1.2",
  urls=["file://${EXTREPODIR}/ext.zip"],
  build_file_content="exports_files([\"foo.sh\"])",
  patches = ["//:patch_foo.sh"],
  patch_tool = "${EXTREPODIR}/my_patch_tool",
)
EOF
  touch BUILD

  bazel build @ext//... >"${TEST_log}" 2>&1 && fail "expected failure" || :
  expect_log 'Helpful message'
}

test_patch_git() {
  EXTREPODIR=`pwd`
  export GIT_CONFIG_NOSYSTEM=YES

  mkdir extgit
  (cd extgit && git init \
       && git config user.email 'me@example.com' \
       && git config user.name 'E X Ample' )
  cat > extgit/foo.sh <<'EOF'
#!/usr/bin/env sh

echo Here be dragons...
EOF
  (cd extgit
   git add .
   git commit --author="A U Thor <author@example.com>" -m 'initial commit'
   git tag mytag)

  # Test that the patches attribute of git_repository is honored
  mkdir main
  cd main
  cat > patch_foo.sh <<'EOF'
--- foo.sh.orig	2018-01-15 10:39:20.183909147 +0100
+++ foo.sh	2018-01-15 10:43:35.331566052 +0100
@@ -1,3 +1,3 @@
 #!/usr/bin/env sh

-echo Here be dragons...
+echo There are dragons...
EOF
  cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
new_git_repository(
  name="ext",
  remote="file://${EXTREPODIR}/extgit/.git",
  tag="mytag",
  build_file_content="exports_files([\"foo.sh\"])",
  patches = ["//:patch_foo.sh"],
  patch_cmds = ["find . -name '*.sh' -exec sed -i.orig '1s|#!/usr/bin/env sh\$|/bin/sh\$|' {} +"],
)
EOF
  cat > BUILD <<'EOF'
genrule(
  name = "foo",
  outs = ["foo.sh"],
  srcs = ["@ext//:foo.sh"],
  cmd = "cp $< $@; chmod u+x $@",
  executable = True,
)
EOF
  bazel build :foo.sh
  foopath=`bazel info bazel-genfiles`/foo.sh
  grep -q 'There are' $foopath || fail "expected patch to be applied"
  grep env $foopath && fail "expected patch commands to be executed" || :

  # Verify that changes to the patch files trigger enough rebuilding
  cat > patch_foo.sh <<'EOF'
--- foo.sh.orig	2018-01-15 10:39:20.183909147 +0100
+++ foo.sh	2018-01-15 10:43:35.331566052 +0100
@@ -1,3 +1,3 @@
 #!/usr/bin/env sh

-echo Here be dragons...
+echo completely differently patched
EOF
  bazel build :foo.sh
  foopath=`bazel info bazel-genfiles`/foo.sh
  grep -q 'differently patched' $foopath \
      || fail "expected the new patch to be applied"

  # Verify that changes to the patches attribute trigger enough rebuilding
  cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
new_git_repository(
  name="ext",
  remote="file://${EXTREPODIR}/extgit/.git",
  tag="mytag",
  build_file_content="exports_files([\"foo.sh\"])",
)
EOF
  bazel build :foo.sh
  foopath=`bazel info bazel-genfiles`/foo.sh
  grep -q 'Here be' $foopath || fail "expected unpatched file"
  grep -q 'env' $foopath || fail "expected unpatched file"
}

test_override_buildfile() {
  ## Verify that the BUILD file of an external repository can be overriden
  ## via the http_archive rule.
  EXTREPODIR=`pwd`

  mkdir withbuild
  cat > withbuild/BUILD <<'EOF'
genrule(
  name="target",
  srcs=["file.txt"],
  outs=["target.txt"],
  cmd="cp $< $@ && echo BAD >> $@",
)
EOF
  cat > withbuild/file.txt <<'EOF'
from external repo
EOF
  zip withbuild.zip withbuild/*
  rm -rf withbuild

  mkdir main
  cd main
  cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
  name="withbuild",
  strip_prefix="withbuild",
  urls=["file://${EXTREPODIR}/withbuild.zip"],
  build_file="@//:ext.BUILD",
)
EOF
  cat > BUILD <<'EOF'
genrule(
  name = "local",
  outs = ["local.txt"],
  srcs = ["@withbuild//:target"],
  cmd = "cp $< $@",
)
EOF
  cat > ext.BUILD <<'EOF'
genrule(
  name="target",
  srcs=["file.txt"],
  outs=["target.txt"],
  cmd="cp $< $@ && echo GOOD >> $@",
  visibility=["//visibility:public"],
)
EOF

  bazel build //:local || fail "Expected success"

  cat `bazel info bazel-genfiles`/local.txt > "${TEST_log}"
  expect_log "from external repo"
  expect_log "GOOD"
  expect_not_log "BAD"
}

test_override_buildfile_content() {
  ## Verify that the BUILD file of an external repository can be overriden
  ## via specified content in the http_archive rule.
  EXTREPODIR=`pwd`

  mkdir withbuild
  cat > withbuild/BUILD <<'EOF'
genrule(
  name="target",
  srcs=["file.txt"],
  outs=["target.txt"],
  cmd="cp $< $@ && echo BAD >> $@",
)
EOF
  cat > withbuild/file.txt <<'EOF'
from external repo
EOF
  zip withbuild.zip withbuild/*
  rm -rf withbuild

  mkdir main
  cd main
  cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
  name="withbuild",
  strip_prefix="withbuild",
  urls=["file://${EXTREPODIR}/withbuild.zip"],
  build_file_content="""
genrule(
  name="target",
  srcs=["file.txt"],
  outs=["target.txt"],
  cmd="cp \$< \$@ && echo GOOD >> \$@",
  visibility=["//visibility:public"],
)
  """,
)
EOF
  cat > BUILD <<'EOF'
genrule(
  name = "local",
  outs = ["local.txt"],
  srcs = ["@withbuild//:target"],
  cmd = "cp $< $@",
)
EOF

  bazel build //:local || fail "Expected success"

  cat `bazel info bazel-genfiles`/local.txt > "${TEST_log}"
  expect_log "from external repo"
  expect_log "GOOD"
  expect_not_log "BAD"
}

test_override_buildfile_git() {
  ## Verify that the BUILD file of an external repository can be overriden
  ## via the git_repository rule.
  EXTREPODIR=`pwd`
  export GIT_CONFIG_NOSYSTEM=YES

  mkdir withbuild
  (cd withbuild && git init \
       && git config user.email 'me@example.com' \
       && git config user.name 'E X Ample' )
  cat > withbuild/BUILD.bazel <<'EOF'
genrule(
  name="target",
  srcs=["file.txt"],
  outs=["target.txt"],
  cmd="cp $< $@ && echo BAD >> $@",
  visibility=["//visibility:public"],
)
EOF
  cat > withbuild/file.txt <<'EOF'
from external repo
EOF
  (cd withbuild
   git add .
   git commit --author="A U Thor <author@example.com>" -m 'initial commit'
   git tag mytag)

  mkdir main
  cd main
  cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
new_git_repository(
  name="withbuild",
  remote="file://${EXTREPODIR}/withbuild/.git",
  tag="mytag",
  build_file="@//:ext.BUILD",
)
EOF
  cat > BUILD <<'EOF'
genrule(
  name = "local",
  outs = ["local.txt"],
  srcs = ["@withbuild//:target"],
  cmd = "cp $< $@",
)
EOF
  cat > ext.BUILD <<'EOF'
genrule(
  name="target",
  srcs=["file.txt"],
  outs=["target.txt"],
  cmd="cp $< $@ && echo GOOD >> $@",
  visibility=["//visibility:public"],
)
EOF

  bazel build //:local || fail "Expected success"

  cat `bazel info bazel-genfiles`/local.txt > "${TEST_log}"
  expect_log "from external repo"
  expect_log "GOOD"
  expect_not_log "BAD"
}

test_override_buildfilecontents_git() {
  ## Verify that the BUILD file of an external repository can be overriden
  ## via specified content in the git_repository rule.
  EXTREPODIR=`pwd`
  export GIT_CONFIG_NOSYSTEM=YES

  mkdir withbuild
  (cd withbuild && git init \
       && git config user.email 'me@example.com' \
       && git config user.name 'E X Ample' )
  cat > withbuild/BUILD.bazel <<'EOF'
genrule(
  name="target",
  srcs=["file.txt"],
  outs=["target.txt"],
  cmd="cp $< $@ && echo BAD >> $@",
  visibility=["//visibility:public"],
)
EOF
  cat > withbuild/file.txt <<'EOF'
from external repo
EOF
  (cd withbuild
   git add .
   git commit --author="A U Thor <author@example.com>" -m 'initial commit'
   git tag mytag)

  mkdir main
  cd main
  cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
new_git_repository(
  name="withbuild",
  remote="file://${EXTREPODIR}/withbuild/.git",
  tag="mytag",
  build_file_content="""
genrule(
  name="target",
  srcs=["file.txt"],
  outs=["target.txt"],
  cmd="cp \$< \$@ && echo GOOD >> \$@",
  visibility=["//visibility:public"],
)
  """,
)
EOF
  cat > BUILD <<'EOF'
genrule(
  name = "local",
  outs = ["local.txt"],
  srcs = ["@withbuild//:target"],
  cmd = "cp $< $@",
)
EOF

  bazel build //:local || fail "Expected success"

  cat `bazel info bazel-genfiles`/local.txt > "${TEST_log}"
  expect_log "from external repo"
  expect_log "GOOD"
  expect_not_log "BAD"
}

test_build_file_build_bazel() {
  ## Verify that the BUILD file of an external repository can be overriden
  ## via the http_archive rule.
  EXTREPODIR=`pwd`

  mkdir withbuild
  cat > withbuild/BUILD.bazel <<'EOF'
genrule(
  name="target",
  srcs=["file.txt"],
  outs=["target.txt"],
  cmd="cp $< $@ && echo BAD >> $@",
)
EOF
  cat > withbuild/file.txt <<'EOF'
from external repo
EOF
  zip withbuild.zip withbuild/*
  rm -rf withbuild

  mkdir main
  cd main
  cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
  name="withbuild",
  strip_prefix="withbuild",
  urls=["file://${EXTREPODIR}/withbuild.zip"],
  build_file="@//:ext.BUILD",
)
EOF
  cat > BUILD <<'EOF'
genrule(
  name = "local",
  outs = ["local.txt"],
  srcs = ["@withbuild//:target"],
  cmd = "cp $< $@",
)
EOF
  cat > ext.BUILD <<'EOF'
genrule(
  name="target",
  srcs=["file.txt"],
  outs=["target.txt"],
  cmd="cp $< $@ && echo GOOD >> $@",
  visibility=["//visibility:public"],
)
EOF

  bazel build //:local || fail "Expected success"

  cat `bazel info bazel-genfiles`/local.txt > "${TEST_log}"
  expect_log "from external repo"
  expect_log "GOOD"
  expect_not_log "BAD"
}

test_git_format_patch() {
  EXTREPODIR=`pwd`

  # Verify that a patch in the style of git-format-patch(1) can be handled.
  mkdir main
  cd main
  ls -al
  cat > 0001-foo.sh-remove-dragons.patch <<'EOF'
From a8c0f9248dd85feac9d08b017776b5aedd1e7be8 Mon Sep 17 00:00:00 2001
From: Klaus Aehlig <aehlig@google.com>
Date: Wed, 13 Jun 2018 11:32:39 +0200
Subject: [PATCH] foo.sh: remove dragons

---
 foo.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/foo.sh b/foo.sh
index 1f4c41e..9d548ff 100644
--- a/foo.sh
+++ b/foo.sh
@@ -1,3 +1,3 @@
 #!/usr/bin/env sh

-echo Here be dragons...
+echo New version of foo.sh, no more dangerous animals...
--
2.18.0.rc1.244.gcf134e6275-goog

EOF
  cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
  name="ext",
  strip_prefix="ext-0.1.2",
  urls=["file://${EXTREPODIR}/ext.zip"],
  build_file_content="exports_files([\"foo.sh\"])",
  patches = ["//:0001-foo.sh-remove-dragons.patch"],
  patch_args = ["-p1"],
  patch_cmds = ["find . -name '*.sh' -exec sed -i.orig '1s|#!/usr/bin/env sh\$|/bin/sh\$|' {} +"],
)
EOF
  cat > BUILD <<'EOF'
genrule(
  name = "foo",
  outs = ["foo.sh"],
  srcs = ["@ext//:foo.sh"],
  cmd = "cp $< $@; chmod u+x $@",
  executable = True,
)
EOF
  bazel build :foo.sh
  foopath=`bazel info bazel-genfiles`/foo.sh
  grep -q 'New version' $foopath || fail "expected patch to be applied"
}

run_suite "external patching tests"