...

Text file src/github.com/bazelbuild/buildtools/buildozer/buildozer_test.sh

Documentation: github.com/bazelbuild/buildtools/buildozer

     1#!/bin/bash
     2
     3# --- begin runfiles.bash initialization ---
     4# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
     5set -euo pipefail
     6if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
     7  if [[ -f "$0.runfiles_manifest" ]]; then
     8    export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
     9  elif [[ -f "$0.runfiles/MANIFEST" ]]; then
    10    export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
    11  elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
    12    export RUNFILES_DIR="$0.runfiles"
    13  fi
    14fi
    15if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
    16  source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
    17elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
    18  source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
    19            "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
    20else
    21  echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
    22  exit 1
    23fi
    24# --- end runfiles.bash initialization ---
    25
    26die () {
    27  echo "$1" 1>&2
    28  exit 1
    29}
    30
    31[[ "$1" =~ external/* ]] && buildozer="${{1#external/}}" || buildozer="$TEST_WORKSPACE/$1"
    32buildozer="$(rlocation "$buildozer")"
    33
    34source $TEST_SRCDIR/buildtools/buildozer/test_common.sh
    35
    36## TEST INPUTS
    37
    38no_deps='go_library(
    39    name = "edit",
    40)'
    41
    42empty_deps='go_library(
    43    name = "edit",
    44    deps = [],
    45)'
    46
    47one_dep='go_library(
    48    name = "edit",
    49    deps = ["//buildifier:build"],
    50)'
    51
    52two_deps='go_library(
    53    name = "edit",
    54    deps = [
    55        ":local",
    56        "//buildifier:build",
    57    ],
    58)'
    59
    60two_deps_with_select='go_library(
    61    name = "edit",
    62    deps = [
    63        ":local",
    64        "//buildifier:build",
    65    ] + select({
    66        "//tools/some:condition": [
    67            "//some:value",
    68            "//some/other:value",
    69        ],
    70        "//tools/other:condition": [
    71            "//yet/another:value",
    72        ],
    73    }),
    74)'
    75
    76quoted_deps='go_library(
    77    name = "edit",
    78    deps = [
    79        "//buildifier:build",
    80        "//foo",
    81        "//bar",
    82        "\"//baz\"",
    83    ],
    84)'
    85
    86## TESTS
    87
    88function test_add_one_dep() {
    89  run "$one_dep" 'add deps //foo' '//pkg:edit'
    90  assert_equals 'go_library(
    91    name = "edit",
    92    deps = [
    93        "//buildifier:build",
    94        "//foo",
    95    ],
    96)'
    97}
    98
    99function test_add_dep_no_deps() {
   100  run "$no_deps" 'add deps //foo' '//pkg:edit'
   101  assert_equals 'go_library(
   102    name = "edit",
   103    deps = ["//foo"],
   104)'
   105}
   106
   107function test_add_dep_quotes() {
   108  run "$no_deps" 'add deps "//foo"' '//pkg:edit'
   109  assert_equals 'go_library(
   110    name = "edit",
   111    deps = ["//foo"],
   112)'
   113}
   114
   115function test_add_dep_empty_deps() {
   116  run "$empty_deps" 'add deps //foo' '//pkg:edit'
   117  assert_equals 'go_library(
   118    name = "edit",
   119    deps = ["//foo"],
   120)'
   121}
   122
   123function test_add_dep_two_deps() {
   124  run "$two_deps" 'add deps :local2' '//pkg:edit'
   125  assert_equals 'go_library(
   126    name = "edit",
   127    deps = [
   128        ":local",
   129        ":local2",
   130        "//buildifier:build",
   131    ],
   132)'
   133}
   134
   135function test_add_existing_dep() {
   136  in='go_library(
   137    name = "edit",
   138    deps = [":local"],
   139)'
   140  ERROR=3 run "$in" 'add deps //pkg:local' '//pkg:edit'
   141  assert_equals "$in"
   142}
   143
   144function test_add_existing_dep2() {
   145  in='go_library(
   146    name = "edit",
   147    deps = ["//pkg:local"],
   148)'
   149  ERROR=3 run "$in" 'add deps //pkg:local' '//pkg:edit'
   150  assert_equals "$in"
   151}
   152
   153function test_add_shortened_dep() {
   154  in='go_library(
   155    name = "edit",
   156)'
   157  run "$in" 'add deps //pkg:local' '//pkg:edit'
   158  assert_equals 'go_library(
   159    name = "edit",
   160    deps = [":local"],
   161)'
   162}
   163
   164function test_sorted_deps() {
   165  in='go_library(
   166    name = "edit",
   167    deps = [
   168      ":c",
   169      # comment that prevents buildifier reordering
   170      ":x",
   171      "//foo",
   172    ],
   173)'
   174  run "$in" 'add deps :z :a :e //last' '//pkg:edit'
   175  assert_equals 'go_library(
   176    name = "edit",
   177    deps = [
   178        ":a",
   179        ":c",
   180        ":e",
   181        # comment that prevents buildifier reordering
   182        ":x",
   183        ":z",
   184        "//foo",
   185        "//last",
   186    ],
   187)'
   188}
   189
   190function test_noshorten_labels_flag() {
   191  in='go_library(
   192    name = "edit",
   193)'
   194  run "$in" --shorten_labels=false 'add deps //pkg:local' '//pkg:edit'
   195  assert_equals 'go_library(
   196    name = "edit",
   197    deps = ["//pkg:local"],
   198)'
   199}
   200
   201function test_add_duplicate_label() {
   202  # "build" and ":build" labels are equivalent
   203  in='go_library(
   204    name = "edit",
   205    deps = ["build"],
   206)'
   207  ERROR=3 run "$in" 'add deps :build' '//pkg:edit'
   208  assert_equals 'go_library(
   209    name = "edit",
   210    deps = ["build"],
   211)'
   212}
   213
   214function test_add_duplicate_label2() {
   215  # "build" and ":build" labels are equivalent
   216  in='go_library(
   217    name = "edit",
   218    deps = [":build"],
   219)'
   220  ERROR=3 run "$in" 'add deps build' '//pkg:edit'
   221  assert_equals 'go_library(
   222    name = "edit",
   223    deps = [":build"],
   224)'
   225}
   226
   227function test_remove_last_dep() {
   228  run "$one_dep" 'remove deps //buildifier:build' '//pkg:edit'
   229  assert_equals 'go_library(name = "edit")'
   230}
   231
   232function test_remove_all_attrs() {
   233  run "$one_dep" 'remove *' '//pkg:edit'
   234  assert_equals 'go_library(name = "edit")'
   235}
   236
   237function test_remove_all_attrs_none() {
   238  ERROR=3 run "$no_deps" 'remove *' '//pkg:edit'
   239  assert_equals 'go_library(
   240    name = "edit",
   241)'
   242}
   243
   244function test_remove_dep() {
   245  run "$two_deps" 'remove deps //buildifier:build' '//pkg:edit'
   246  assert_equals 'go_library(
   247    name = "edit",
   248    deps = [":local"],
   249)'
   250}
   251
   252function test_remove_dep_outside_of_select() {
   253  run "$two_deps_with_select" 'remove deps //buildifier:build' '//pkg:edit'
   254  assert_equals 'go_library(
   255    name = "edit",
   256    deps = [":local"] + select({
   257        "//tools/some:condition": [
   258            "//some:value",
   259            "//some/other:value",
   260        ],
   261        "//tools/other:condition": [
   262            "//yet/another:value",
   263        ],
   264    }),
   265)'
   266}
   267
   268function test_remove_all_deps_outside_of_select() {
   269  run "$two_deps_with_select" 'remove deps //buildifier:build :local' '//pkg:edit'
   270  assert_equals 'go_library(
   271    name = "edit",
   272    deps = select({
   273        "//tools/some:condition": [
   274            "//some:value",
   275            "//some/other:value",
   276        ],
   277        "//tools/other:condition": [
   278            "//yet/another:value",
   279        ],
   280    }),
   281)'
   282}
   283
   284function test_remove_dep_in_select() {
   285  run "$two_deps_with_select" 'remove deps //some:value' '//pkg:edit'
   286  assert_equals 'go_library(
   287    name = "edit",
   288    deps = [
   289        ":local",
   290        "//buildifier:build",
   291    ] + select({
   292        "//tools/some:condition": ["//some/other:value"],
   293        "//tools/other:condition": [
   294            "//yet/another:value",
   295        ],
   296    }),
   297)'
   298}
   299
   300function test_remove_deps_in_select() {
   301  run "$two_deps_with_select" 'remove deps //some:value //some/other:value' '//pkg:edit'
   302  assert_equals 'go_library(
   303    name = "edit",
   304    deps = [
   305        ":local",
   306        "//buildifier:build",
   307    ] + select({
   308        "//tools/some:condition": [],
   309        "//tools/other:condition": [
   310            "//yet/another:value",
   311        ],
   312    }),
   313)'
   314}
   315
   316function test_remove_all_deps_in_select() {
   317  run "$two_deps_with_select" 'remove deps //some:value //some/other:value //yet/another:value' '//pkg:edit'
   318  assert_equals 'go_library(
   319    name = "edit",
   320    deps = [
   321        ":local",
   322        "//buildifier:build",
   323    ],
   324)'
   325}
   326
   327function test_remove_all_deps() {
   328  run "$two_deps_with_select" 'remove deps //some:value //some/other:value //yet/another:value :local //buildifier:build' '//pkg:edit'
   329  assert_equals 'go_library(name = "edit")'
   330}
   331
   332function test_remove_dep_quotes() {
   333  run "$two_deps" 'remove deps "//buildifier:build"' '//pkg:edit'
   334  assert_equals 'go_library(
   335    name = "edit",
   336    deps = [":local"],
   337)'
   338}
   339
   340function test_remove_local_dep() {
   341  run "$two_deps" 'remove deps local' '//pkg:edit'
   342  assert_equals 'go_library(
   343    name = "edit",
   344    deps = ["//buildifier:build"],
   345)'
   346}
   347
   348function test_remove_two_deps() {
   349  run "$two_deps" 'remove deps //buildifier:build :local' '//pkg:edit'
   350  assert_equals 'go_library(name = "edit")'
   351}
   352
   353function test_remove_dep_using_long_label() {
   354  run "$two_deps" 'remove deps //pkg:local' '//pkg:edit'
   355  assert_equals 'go_library(
   356    name = "edit",
   357    deps = ["//buildifier:build"],
   358)'
   359}
   360
   361function test_remove_nonexistent_item() {
   362  run "$two_deps" 'remove deps //foo:bar :local' '//pkg:edit'
   363  assert_equals 'go_library(
   364    name = "edit",
   365    deps = ["//buildifier:build"],
   366)'
   367}
   368
   369function test_remove_item_with_comment() {
   370  commented_deps='go_library(
   371    name = "edit",
   372    deps = [
   373        # Do not remove!!!!11111!!!
   374        ":local",
   375        "//buildifier:build",  #fixdeps: keep
   376        "//remove:me",
   377    ],
   378)'
   379
   380  run "$commented_deps" 'remove deps //buildifier:build //remove:me :local' '//pkg:edit'
   381  assert_equals 'go_library(name = "edit")'
   382}
   383
   384function test_remove_src() {
   385  in='go_library(
   386    name = "edit",
   387    srcs = ["file" + ".go", "other.go"],
   388)'
   389  run "$in" 'remove srcs other.go' '//pkg:edit'
   390  assert_equals 'go_library(
   391    name = "edit",
   392    srcs = ["file" + ".go"],
   393)'
   394}
   395
   396function test_remove_dep_without_colon() {
   397  in='go_library(
   398    name = "edit",
   399    deps = ["local", "//base"],
   400)'
   401  run "$in" 'remove deps //pkg:local' '//pkg:edit'
   402  assert_equals 'go_library(
   403    name = "edit",
   404    deps = ["//base"],
   405)'
   406}
   407
   408function test_remove_attribute() {
   409  run "$two_deps" 'remove deps' '//pkg:edit'
   410  assert_equals 'go_library(name = "edit")'
   411}
   412
   413function test_remove_concatenated_attribute() {
   414  in='cc_library(name = "a", deps = ["//my/"] + ["foo"])'
   415  run "$in" 'remove deps :foo' '//pkg:a'
   416  assert_equals 'cc_library(
   417    name = "a",
   418    deps = ["//my/"],
   419)'
   420}
   421
   422function test_remove_from_all_attributes() {
   423  in='java_library(name = "a", data = ["b","c"], exports = ["c"], deps = ["c","d"])'
   424  run "$in" 'remove * c' '//pkg:a'
   425  assert_equals 'java_library(
   426    name = "a",
   427    data = ["b"],
   428    deps = ["d"],
   429)'
   430}
   431
   432function test_remove_from_all_rules() {
   433  in='cc_library(name = "a", visibility = ["//visibility:private"])
   434cc_library(name = "b")
   435exports_files(["a.cc"], visibility = ["//visibility:private"])'
   436  run "$in" 'remove visibility' '//pkg:all'
   437  assert_equals 'cc_library(name = "a")
   438
   439cc_library(name = "b")
   440
   441exports_files(["a.cc"])'
   442}
   443
   444function test_remove_package_attribute() {
   445  in='package(default_visibility = ["//visibility:public"])'
   446  run "$in" 'remove default_visibility' '//pkg:__pkg__'
   447  [ $(wc -c < "$root/pkg/BUILD") -eq 0 ] || fail "Expected empty file"
   448}
   449
   450function test_remove_if_equal_label() {
   451  in='go_library(
   452    name = "edit",
   453    shared_library = ":local",  # Suffix comment.
   454)'
   455  run "$in" 'remove_if_equal shared_library :local' '//pkg:edit'
   456  assert_equals 'go_library(name = "edit")'
   457}
   458
   459function test_remove_if_equal_label_does_not_match() {
   460  in='go_library(
   461    name = "edit",
   462    shared_library = ":local",  # Suffix comment.
   463)'
   464  ERROR=3 run "$in" 'remove_if_equal shared_library :global' '//pkg:edit'
   465  assert_equals 'go_library(
   466    name = "edit",
   467    shared_library = ":local",  # Suffix comment.
   468)'
   469}
   470
   471function test_remove_if_equal_label_full_path() {
   472  in='go_library(
   473    name = "edit",
   474    shared_library = ":local",  # Suffix comment.
   475)'
   476  run "$in" 'remove_if_equal shared_library //pkg:local' '//pkg:edit'
   477  assert_equals 'go_library(name = "edit")'
   478}
   479
   480function test_remove_if_equal_ident() {
   481  in='go_library(
   482    name = "edit",
   483    flag = True,
   484)'
   485  run "$in" 'remove_if_equal flag True' '//pkg:edit'
   486  assert_equals 'go_library(name = "edit")'
   487}
   488
   489function test_remove_if_equal_ident_does_not_match() {
   490  in='go_library(
   491    name = "edit",
   492    flag = True,
   493)'
   494  ERROR=3 run "$in" 'remove_if_equal flag False' '//pkg:edit'
   495  assert_equals 'go_library(
   496    name = "edit",
   497    flag = True,
   498)'
   499}
   500
   501function test_remove_if_equal_string() {
   502  in='go_library(
   503    name = "edit",
   504    flag = "True",
   505)'
   506  run "$in" 'remove_if_equal flag True' '//pkg:edit'
   507  assert_equals 'go_library(name = "edit")'
   508}
   509
   510function test_remove_if_equal_string_does_not_match() {
   511  in='go_library(
   512    name = "edit",
   513    flag = "False",
   514)'
   515  ERROR=3 run "$in" 'remove_if_equal flag True' '//pkg:edit'
   516  assert_equals 'go_library(
   517    name = "edit",
   518    flag = "False",
   519)'
   520}
   521
   522function test_remove_if_equal_string_attr_string() {
   523  in='go_library(
   524    name = "edit",
   525    toolchain = "something",
   526)'
   527  run "$in" 'remove_if_equal toolchain something' '//pkg:edit'
   528  assert_equals 'go_library(name = "edit")'
   529}
   530
   531function test_move_last_dep() {
   532  run "$one_dep" 'move deps runtime_deps //buildifier:build' '//pkg:edit'
   533  assert_equals 'go_library(
   534    name = "edit",
   535    runtime_deps = ["//buildifier:build"],
   536)'
   537}
   538
   539function test_move_dep() {
   540  run "$two_deps" 'move deps runtime_deps //buildifier:build' '//pkg:edit'
   541  assert_equals 'go_library(
   542    name = "edit",
   543    runtime_deps = ["//buildifier:build"],
   544    deps = [":local"],
   545)'
   546}
   547
   548function test_move_two_deps() {
   549  run "$two_deps" 'move deps runtime_deps //buildifier:build :local' '//pkg:edit'
   550  assert_equals 'go_library(
   551    name = "edit",
   552    runtime_deps = [
   553        ":local",
   554        "//buildifier:build",
   555    ],
   556)'
   557}
   558
   559function test_move_dep_using_long_label() {
   560  run "$two_deps" 'move deps runtime_deps //pkg:local' '//pkg:edit'
   561  assert_equals 'go_library(
   562    name = "edit",
   563    runtime_deps = [":local"],
   564    deps = ["//buildifier:build"],
   565)'
   566}
   567
   568function test_move_dep_with_comment() {
   569  local input='go_library(
   570    name = "edit",
   571    deps = [
   572        ":local",  # needed at runtime for some obscure reason
   573        "//buildifier:build",
   574    ],
   575)'
   576  run "$input" 'move deps runtime_deps :local' '//pkg:edit'
   577  assert_equals 'go_library(
   578    name = "edit",
   579    runtime_deps = [
   580        ":local",  # needed at runtime for some obscure reason
   581    ],
   582    deps = ["//buildifier:build"],
   583)'
   584}
   585
   586function test_move_nonexistent_item() {
   587  run "$two_deps" 'move deps runtime_deps //foo:bar :local' '//pkg:edit'
   588  assert_equals 'go_library(
   589    name = "edit",
   590    runtime_deps = [":local"],
   591    deps = ["//buildifier:build"],
   592)'
   593}
   594
   595function test_move_all_deps_to_new_attribute() {
   596  run "$two_deps" 'move deps runtime_deps *' '//pkg:edit'
   597  assert_equals 'go_library(
   598    name = "edit",
   599    runtime_deps = [
   600        ":local",
   601        "//buildifier:build",
   602    ],
   603)'
   604}
   605
   606function test_move_all_deps_to_existing_attribute() {
   607  local input='go_library(
   608    name = "edit",
   609    runtime_deps = [":remote"],
   610    deps = [
   611        ":local",
   612        "//buildifier:build",
   613    ],
   614)'
   615  run "$input" 'move deps runtime_deps *' '//pkg:edit'
   616  assert_equals 'go_library(
   617    name = "edit",
   618    runtime_deps = [
   619        ":local",
   620        ":remote",
   621        "//buildifier:build",
   622    ],
   623)'
   624}
   625
   626function test_rename_attribute() {
   627  run "$two_deps" 'rename deps data' '//pkg:edit'
   628  assert_equals 'go_library(
   629    name = "edit",
   630    data = [
   631        ":local",
   632        "//buildifier:build",
   633    ],
   634)'
   635}
   636
   637function test_rename_attribute_already_exist() {
   638  in='cc_library(
   639    name = "edit",
   640    srcs = ["b.h"],
   641    hdrs = ["a.h"],
   642)'
   643  ERROR=2 run "$in" 'rename srcs hdrs' '//pkg:edit'
   644  assert_equals "$in"
   645  assert_err "attribute hdrs already exists in rule edit"
   646}
   647
   648function test_rename_attribute_does_not_exist() {
   649  ERROR=2 run "$two_deps" 'rename data resources' '//pkg:edit'
   650  assert_equals "$two_deps"
   651  assert_err "no attribute data found in rule edit"
   652}
   653
   654function test_with_python_code() {
   655  in='# test that Python code is preserved and does not crash
   656top_files = [
   657    "lgpl.txt",
   658    "README",
   659]
   660
   661boom_files = top_files
   662
   663boom_files.extend(glob(["**/*.hpp"]))
   664
   665boom_files.remove("src/math/special_functions.hpp")
   666
   667# comment for foo
   668def foo(x):
   669    x += [1, 2]
   670
   671foo(boom_files)'
   672
   673  run "$in" 'add default_visibility //visibility:public' '//pkg:__pkg__'
   674  assert_equals "package(default_visibility = [\"//visibility:public\"])
   675
   676$in"
   677}
   678
   679function test_replace_string_attr() {
   680  in='go_library(
   681    name = "edit",
   682    shared_library = ":local",  # Suffix comment.
   683)'
   684  run "$in" 'replace shared_library :local :new' '//pkg:edit'
   685  assert_equals 'go_library(
   686    name = "edit",
   687    shared_library = ":new",  # Suffix comment.
   688)'
   689}
   690
   691function test_replace_string_attr_quotes() {
   692  in='go_library(
   693    name = "edit",
   694    shared_library = ":local",  # Suffix comment.
   695)'
   696  run "$in" 'replace shared_library ":local" ":new"' '//pkg:edit'
   697  assert_equals 'go_library(
   698    name = "edit",
   699    shared_library = ":new",  # Suffix comment.
   700)'
   701}
   702
   703function test_replace_string_attr_no_match() {
   704  in='go_library(
   705    name = "edit",
   706    library = ":no_match",  # Suffix comment.
   707)'
   708  ERROR=3 run "$in" 'replace library :local :new' '//pkg:edit'
   709  assert_equals 'go_library(
   710    name = "edit",
   711    library = ":no_match",  # Suffix comment.
   712)'
   713}
   714
   715function test_replace_concatenated_lists() {
   716  in='go_library(
   717    name = "edit",
   718    deps = [":local"] + CONSTANT,
   719)'
   720  run "$in" 'replace deps :local :new' '//pkg:edit'
   721  assert_equals 'go_library(
   722    name = "edit",
   723    deps = [":new"] + CONSTANT,
   724)'
   725}
   726
   727function test_replace_dep() {
   728  in='go_library(
   729    name = "edit",
   730    deps = [
   731        # Before-comment.
   732        ":local",  # Suffix comment.
   733        "//buildifier:build",
   734    ] + select({
   735        "//tools/some:condition": [
   736            "//some:value",
   737        ],
   738    }),
   739)'
   740  run "$in" 'replace deps :local :new' '//pkg:edit'
   741  assert_equals 'go_library(
   742    name = "edit",
   743    deps = [
   744        # Before-comment.
   745        ":new",  # Suffix comment.
   746        "//buildifier:build",
   747    ] + select({
   748        "//tools/some:condition": [
   749            "//some:value",
   750        ],
   751    }),
   752)'
   753}
   754
   755function test_replace_dep_select() {
   756  # Replace a dep inside a select statement
   757  in='go_library(
   758    name = "edit",
   759    deps = [":dep"] + select({
   760        "//tools/some:condition": [
   761            "//some/other:value",
   762        ],
   763        "//tools/other:condition": [
   764            "//yet/another:value",
   765        ],
   766        "//conditions:default": SOME_CONSTANT,
   767    }),
   768)'
   769  run "$in" 'replace deps //some/other:value :new' '//pkg:edit'
   770  assert_equals 'go_library(
   771    name = "edit",
   772    deps = [":dep"] + select({
   773        "//tools/some:condition": [
   774            ":new",
   775        ],
   776        "//tools/other:condition": [
   777            "//yet/another:value",
   778        ],
   779        "//conditions:default": SOME_CONSTANT,
   780    }),
   781)'
   782}
   783
   784function test_replace_dep_using_long_label() {
   785  run "$two_deps" 'replace deps //pkg:local //pkg:new' '//pkg:edit'
   786  assert_equals 'go_library(
   787    name = "edit",
   788    deps = [
   789        ":new",
   790        "//buildifier:build",
   791    ],
   792)'
   793}
   794
   795function test_replace_in_all_attributes() {
   796  in='java_library(name = "a", data = ["b","c"], exports = ["c"], deps = ["c","d"])'
   797  run "$in" 'replace * c e' '//pkg:a'
   798  assert_equals 'java_library(
   799    name = "a",
   800    data = [
   801        "b",
   802        "e",
   803    ],
   804    exports = ["e"],
   805    deps = [
   806        "d",
   807        "e",
   808    ],
   809)'
   810}
   811
   812function test_delete_rule_all() {
   813  in='cc_library(name = "all")
   814cc_library(name = "b")'
   815  run "$in" 'delete' '//pkg:all'
   816  assert_equals 'cc_library(name = "b")'
   817}
   818
   819function test_delete_rule_star() {
   820  in='cc_library(name = "all")
   821cc_library(name = "b")'
   822  run "$in" 'delete' '//pkg:*'
   823  [ $(wc -c < "$root/pkg/BUILD") -eq 0 ] || fail "Expected empty file"
   824}
   825
   826function test_delete_rule() {
   827  in='cc_library(name = "a")
   828
   829cc_library(name = "b")
   830
   831# Comment to make sure it is preserved
   832
   833a = 42
   834
   835cc_library(name = "c")'
   836
   837  run "$in" 'delete' '//pkg:b'
   838  assert_equals 'cc_library(name = "a")
   839
   840# Comment to make sure it is preserved
   841
   842a = 42
   843
   844cc_library(name = "c")'
   845}
   846
   847function test_delete_package() {
   848  in='package(default_visibility = ["//visibility:public"])
   849
   850cc_library(name = "a")'
   851
   852  run "$in" 'delete' '//pkg:__pkg__'
   853  assert_equals 'cc_library(name = "a")'
   854}
   855
   856function test_delete_missing_package() {
   857  in='cc_library(name = "a")'
   858  ERROR=3 run "$in" 'delete' '//pkg:__pkg__'
   859  assert_equals "$in"
   860}
   861
   862function test_delete_target_without_name() {
   863  in='load("/path/f", "a")
   864
   865a(arg1 = "foo")'
   866
   867  run "$in" 'delete' '//pkg:%a'
   868  assert_equals 'load("/path/f", "a")'
   869}
   870
   871function test_delete_using_line_number() {
   872  in='load("/path/f", "a")
   873
   874a(arg1 = "foo")'
   875
   876    run "$in" 'delete' '//pkg:%3'
   877  assert_equals 'load("/path/f", "a")'
   878}
   879
   880function test_copy() {
   881  in='proto_library(name = "from", visibility = ["://foo"] + CONST)
   882
   883cc_binary(name = "to")'
   884
   885  run "$in" 'copy visibility from' '//pkg:to'
   886  assert_equals 'proto_library(
   887    name = "from",
   888    visibility = ["://foo"] + CONST,
   889)
   890
   891cc_binary(
   892    name = "to",
   893    visibility = ["://foo"] + CONST,
   894)'
   895}
   896
   897function test_copy_overwrite() {
   898  in='proto_library(name = "from", testonly = 1)
   899
   900cc_binary(name = "to", testonly = 2)'
   901
   902  run "$in" 'copy testonly from' '//pkg:to'
   903  assert_equals 'proto_library(
   904    name = "from",
   905    testonly = 1,
   906)
   907
   908cc_binary(
   909    name = "to",
   910    testonly = 1,
   911)'
   912}
   913
   914function test_copy_no_overwrite() {
   915  in='proto_library(name = "from", visibility = ["://foo"] + CONST)
   916
   917cc_binary(name = "to")'
   918
   919  run "$in" 'copy_no_overwrite visibility from' '//pkg:to'
   920  assert_equals 'proto_library(
   921    name = "from",
   922    visibility = ["://foo"] + CONST,
   923)
   924
   925cc_binary(
   926    name = "to",
   927    visibility = ["://foo"] + CONST,
   928)'
   929}
   930
   931function test_copy_no_overwrite_no_overwrite() {
   932  in='proto_library(name = "from", testonly = 1)
   933
   934cc_binary(name = "to", testonly = 2)'
   935
   936  run "$in" 'copy_no_overwrite testonly from' '//pkg:to'
   937  assert_equals 'proto_library(
   938    name = "from",
   939    testonly = 1,
   940)
   941
   942cc_binary(
   943    name = "to",
   944    testonly = 2,
   945)'
   946}
   947
   948function test_copy_no_from_rule() {
   949  in='go_binary(name = "to")'
   950  ERROR=2 run "$in" 'copy visibility from' '//pkg:to'
   951  assert_err "could not find rule 'from'"
   952}
   953
   954function test_copy_no_attribute() {
   955  in='go_binary(name = "from")
   956
   957go_binary(name = "to")'
   958  ERROR=2 run "$in" 'copy visibility from' '//pkg:to'
   959  assert_err "rule 'from' does not have attribute 'visibility'"
   960}
   961
   962function test_set_kind() {
   963  in='cc_library(name = "a")'
   964
   965  run "$in" 'set kind java_library' '//pkg:a'
   966  assert_equals 'java_library(name = "a")'
   967}
   968
   969function test_set_list() {
   970  in='cc_library(name = "a")'
   971
   972  run "$in" 'set copts foo' '//pkg:a'
   973  assert_equals 'cc_library(
   974    name = "a",
   975    copts = ["foo"],
   976)'
   977}
   978
   979function test_set_list() {
   980  in='cc_library(name = "a")'
   981
   982  run "$in" 'set copts foo bar baz' '//pkg:a'
   983  assert_equals 'cc_library(
   984    name = "a",
   985    copts = [
   986        "foo",
   987        "bar",
   988        "baz",
   989    ],
   990)'
   991}
   992
   993function test_set_string() {
   994  in='cc_library(name = "a")'
   995
   996  run "$in" 'set name b' '//pkg:a'
   997  assert_equals 'cc_library(name = "b")'
   998}
   999
  1000function test_set_int() {
  1001  in='cc_test(name = "a")'
  1002
  1003  run "$in" 'set shard_count 8' '//pkg:a'
  1004  assert_equals 'cc_test(
  1005    name = "a",
  1006    shard_count = 8,
  1007)'
  1008}
  1009
  1010function test_set_licenses() {
  1011  in='cc_test(name = "a")'
  1012
  1013  run "$in" 'set licenses foo' '//pkg:a'
  1014  assert_equals 'cc_test(
  1015    name = "a",
  1016    licenses = ["foo"],
  1017)'
  1018}
  1019
  1020function test_set_distribs() {
  1021  in='cc_test(name = "a")'
  1022
  1023  run "$in" 'set distribs foo' '//pkg:a'
  1024  assert_equals 'cc_test(
  1025    name = "a",
  1026    distribs = ["foo"],
  1027)'
  1028}
  1029
  1030function test_set_if_absent_absent() {
  1031  in='soy_js(name = "a")'
  1032
  1033  run "$in" 'set_if_absent allowv1syntax 1' '//pkg:a'
  1034  assert_equals 'soy_js(
  1035    name = "a",
  1036    allowv1syntax = 1,
  1037)'
  1038}
  1039
  1040function test_set_if_absent_present() {
  1041  in='soy_js(
  1042  name = "a",
  1043  allowv1syntax = 0
  1044  )'
  1045
  1046  run "$in" 'set_if_absent allowv1syntax 1' '//pkg:a'
  1047  assert_equals 'soy_js(
  1048    name = "a",
  1049    allowv1syntax = 0,
  1050)'
  1051}
  1052
  1053function test_set_custom_code() {
  1054  in='cc_test(name = "a")'
  1055
  1056  run "$in" 'set attr foo(a=1,b=2)' '//pkg:a'
  1057  assert_equals 'cc_test(
  1058    name = "a",
  1059    attr = foo(
  1060        a = 1,
  1061        b = 2,
  1062    ),
  1063)'
  1064}
  1065
  1066function assert_output() {
  1067  echo "$1" > "expected"
  1068  diff -u "expected" "$log" || fail "Output didn't match"
  1069}
  1070
  1071function assert_output_any_order() {
  1072  echo "$1" | sort > "expected"
  1073  sort < "$log" > "log_sorted"
  1074  diff -u "expected" "log_sorted" || fail "Output didn't match"
  1075}
  1076
  1077function test_print_all_functions() {
  1078  in='package(default_visibility = ["//visibility:public"])
  1079cc_test(name = "a")
  1080java_binary(name = "b")
  1081exports_files(["a.cc"])'
  1082  run "$in" 'print kind' '//pkg:all'
  1083  assert_output 'package
  1084cc_test
  1085java_binary
  1086exports_files'
  1087}
  1088
  1089function test_print_java_libraries() {
  1090  in='cc_test(name = "a")
  1091java_library(name = "b")
  1092java_library(name = "c")'
  1093  run "$in" 'print' '//pkg:%java_library'
  1094  assert_output 'b java_library
  1095c java_library'
  1096}
  1097
  1098function test_refer_to_rule_by_location() {
  1099  in='cc_test(name = "a")
  1100java_library(name = "b")
  1101java_library(name = "c")'
  1102  run "$in" 'print label' '//pkg:%2'
  1103  assert_output '//pkg:b'
  1104}
  1105
  1106function test_refer_to_rule_by_location_no_such_rule() {
  1107  in='cc_test(name = "a")
  1108java_library(name = "b")
  1109java_library(name = "c")'
  1110  ERROR=2 run "$in" 'print label' '//pkg:%999'
  1111  assert_err "rule '%999' not found"
  1112}
  1113
  1114function test_visibility_exported_file() {
  1115  in='exports_files(["a.txt", "b.txt"])'
  1116  run "$in" 'set visibility //foo:__pkg__' '//pkg:b.txt'
  1117  assert_equals 'exports_files(
  1118    [
  1119        "a.txt",
  1120        "b.txt",
  1121    ],
  1122    visibility = ["//foo:__pkg__"],
  1123)'
  1124}
  1125
  1126function test_print_srcs() {
  1127  in='cc_test(name = "a", srcs = ["foo.cc"])
  1128java_library(name = "b")
  1129java_library(name = "c", srcs = ["foo.java", "bar.java"])'
  1130  run "$in" 'print name kind srcs' '//pkg:*'
  1131  assert_output 'a cc_test [foo.cc]
  1132b java_library (missing)
  1133c java_library [foo.java bar.java]'
  1134  assert_err 'rule "//pkg:b" has no attribute "srcs"'
  1135}
  1136
  1137function test_print_empty_list() {
  1138  in='package()
  1139java_library(name = "b", deps = [])'
  1140  run "$in" 'print deps' '//pkg:b'
  1141  assert_output '[]'
  1142}
  1143
  1144function test_print_label() {
  1145  in='package()
  1146java_library(name = "b")'
  1147  run "$in" 'print label kind' '//pkg:*'
  1148  assert_output '//pkg:b java_library'
  1149}
  1150
  1151function test_print_label_json() {
  1152  in='package()
  1153java_library(name = "b")'
  1154  run --output_json "$in" 'print label kind' '//pkg:*'
  1155  assert_output '{"records":[{"fields":[{"text":"//pkg:b"},{"text":"java_library"}]}]}'
  1156}
  1157
  1158function test_print_label_ellipsis() {
  1159  mkdir -p "ellipsis_test/foo/bar"
  1160  echo 'java_library(name = "test")' > "ellipsis_test/BUILD"
  1161  echo 'java_library(name = "foo")' > "ellipsis_test/foo/BUILD"
  1162  echo 'java_library(name = "foobar"); java_library(name = "bar");' > "ellipsis_test/foo/bar/BUILD"
  1163
  1164  in='package()
  1165java_library(name = "b")'
  1166  run "$in" 'print label' '//ellipsis_test/...:*'
  1167  assert_output_any_order '//ellipsis_test:test
  1168//ellipsis_test/foo
  1169//ellipsis_test/foo/bar:foobar
  1170//ellipsis_test/foo/bar'
  1171}
  1172
  1173function test_print_startline() {
  1174  in='package()
  1175java_library(name = "b")'
  1176  run "$in" 'print startline label' '//pkg:*'
  1177  assert_output '2 //pkg:b'
  1178}
  1179
  1180function test_print_endline() {
  1181  in='package()
  1182java_library(
  1183    name = "b"
  1184)'
  1185  run "$in" 'print endline label' '//pkg:*'
  1186  assert_output '4 //pkg:b'
  1187}
  1188
  1189function test_print_rule() {
  1190  in='cc_library(name = "a")
  1191
  1192# Comment before
  1193cc_test(
  1194    name = "b",
  1195    copts = [
  1196        # comment before
  1197        "foo",  # comment after
  1198    ],
  1199)
  1200
  1201cc_library(name = "c")'
  1202  run "$in" 'print rule' '//pkg:b'
  1203  assert_output '# Comment before
  1204cc_test(
  1205    name = "b",
  1206    copts = [
  1207        # comment before
  1208        "foo",  # comment after
  1209    ],
  1210)'
  1211}
  1212
  1213function test_print_version() {
  1214  in='gendeb(name = "foobar", version = "12345")'
  1215  run "$in" 'print version' '//pkg:*'
  1216  assert_output '12345'
  1217}
  1218
  1219function test_new_cc_library() {
  1220  in='cc_test(name = "a")
  1221
  1222# end of file comment'
  1223  run "$in" 'new cc_library foo' '//pkg:__pkg__'
  1224  assert_equals 'cc_test(name = "a")
  1225
  1226cc_library(name = "foo")
  1227
  1228# end of file comment'
  1229}
  1230
  1231function test_new_cc_library_after_other_libraries() {
  1232  in='cc_library(name = "l")
  1233cc_test(name = "a")'
  1234  run "$in" 'new cc_library foo' '//pkg:__pkg__'
  1235  assert_equals 'cc_library(name = "l")
  1236
  1237cc_library(name = "foo")
  1238
  1239cc_test(name = "a")'
  1240}
  1241
  1242function test_new_cc_library_empty_file() {
  1243  in=''
  1244  run "$in" 'new cc_library foo' '//pkg:__pkg__'
  1245  assert_equals 'cc_library(name = "foo")'
  1246}
  1247
  1248function test_new_java_library() {
  1249  in='cc_test(name = "a")'
  1250  run "$in" 'new java_library foo' 'pkg/BUILD'
  1251  assert_equals 'cc_test(name = "a")
  1252
  1253java_library(name = "foo")'
  1254}
  1255
  1256function test_new_already_exists() {
  1257  in='cc_test(name = "a")'
  1258  ERROR=2 run "$in" 'new cc_library a' '//pkg:__pkg__'
  1259  assert_err "rule 'a' already exists"
  1260}
  1261
  1262function test_new_before_first() {
  1263  in='cc_test(name = "a")'
  1264  run "$in" 'new java_library foo before a' 'pkg/BUILD'
  1265  assert_equals 'java_library(name = "foo")
  1266
  1267cc_test(name = "a")'
  1268}
  1269
  1270function test_new_before_last() {
  1271  in='cc_test(name = "a")
  1272
  1273cc_test(name = "b")'
  1274  run "$in" 'new java_library foo before b' 'pkg/BUILD'
  1275  assert_equals 'cc_test(name = "a")
  1276
  1277java_library(name = "foo")
  1278
  1279cc_test(name = "b")'
  1280}
  1281
  1282function test_new_before_nonexistent_rule() {
  1283  in='cc_test(name = "a")
  1284
  1285cc_test(name = "b")'
  1286  run "$in" 'new java_library foo before bar' 'pkg/BUILD'
  1287  assert_equals 'cc_test(name = "a")
  1288
  1289cc_test(name = "b")
  1290
  1291java_library(name = "foo")'
  1292}
  1293
  1294function test_new_before_already_exists() {
  1295  in='cc_test(name = "foo")
  1296cc_test(name = "new_rule")'
  1297  ERROR=2 run "$in" 'new java_library new_rule before foo' 'pkg/BUILD'
  1298  assert_err "rule 'new_rule' already exists"
  1299}
  1300
  1301function test_new_after_first() {
  1302  in='cc_test(name = "a")'
  1303  run "$in" 'new java_library foo after a' 'pkg/BUILD'
  1304  assert_equals 'cc_test(name = "a")
  1305
  1306java_library(name = "foo")'
  1307}
  1308
  1309function test_new_after_last() {
  1310  in='cc_test(name = "a")
  1311
  1312cc_test(name = "b")'
  1313  run "$in" 'new java_library foo after b' 'pkg/BUILD'
  1314  assert_equals 'cc_test(name = "a")
  1315
  1316cc_test(name = "b")
  1317
  1318java_library(name = "foo")'
  1319}
  1320
  1321function test_new_after_by_location() {
  1322  in='
  1323cc_test(name = "a")
  1324
  1325cc_test(name = "b")'
  1326  run "$in" 'new java_library foo after %2' 'pkg/BUILD'
  1327  assert_equals 'cc_test(name = "a")
  1328
  1329java_library(name = "foo")
  1330
  1331cc_test(name = "b")'
  1332}
  1333
  1334function test_new_after_package() {
  1335  in='
  1336load("/foo/bar", "x", "y", "z")
  1337package(default_visibility = "//visibility:public")
  1338
  1339cc_test(name = "a")
  1340
  1341cc_test(name = "b")'
  1342  run "$in" 'new java_library foo after __pkg__' 'pkg/BUILD'
  1343  assert_equals 'load("/foo/bar", "x", "y", "z")
  1344
  1345package(default_visibility = "//visibility:public")
  1346
  1347java_library(name = "foo")
  1348
  1349cc_test(name = "a")
  1350
  1351cc_test(name = "b")'
  1352}
  1353
  1354function test_not_enough_arguments() {
  1355  ERROR=1 run "$one_dep" 'add foo' '//pkg:edit'
  1356  assert_err "Too few arguments for command 'add', expected at least 2."
  1357}
  1358
  1359function test_too_many_arguments() {
  1360  ERROR=1 run "$one_dep" 'delete foo' '//pkg:edit'
  1361  assert_err "Too many arguments for command 'delete', expected at most 0."
  1362}
  1363
  1364function test_package_name_missing() {
  1365  ERROR=2 run "$one_dep" 'add deps //dep' ':edit'
  1366  assert_err "file not found"
  1367}
  1368
  1369function test_nonexistent_package_name() {
  1370  ERROR=2 run "$one_dep" 'add deps //dep' '//doesnt_exist:edit'
  1371  assert_err "file not found"
  1372}
  1373
  1374function test_nonexistent_rule_name() {
  1375  ERROR=2 run "$one_dep" 'add deps //dep' '//pkg:doesnt_exist'
  1376  assert_err "rule 'doesnt_exist' not found"
  1377}
  1378
  1379function test_keep_going() {
  1380  ERROR=2 run "$one_dep" -k 'add deps //dep' 'new cc_library edit' '//pkg:doesnt_exist' '//pkg:edit'
  1381  assert_err "rule 'doesnt_exist' not found"
  1382  assert_err "rule 'edit' already exists"
  1383  # Make sure the commands are in the error message.
  1384  assert_err "add deps //dep"
  1385  assert_err "new cc_library edit"
  1386  # Make sure the full targets are in the error message.
  1387  assert_err "//pkg:doesnt_exist"
  1388  assert_equals 'go_library(
  1389    name = "edit",
  1390    deps = [
  1391        "//buildifier:build",
  1392        "//dep",
  1393    ],
  1394)'
  1395}
  1396
  1397function test_buildifier_missing() {
  1398  ERROR=2 run "$one_dep" '--buildifier=doesnt_exist' 'add deps //dep' '//pkg:edit'
  1399  assert_err "executable file not found in \$PATH"
  1400}
  1401
  1402function test_buildifier_return_error() {
  1403  ERROR=2 run "$one_dep" '--buildifier=false' 'add deps //dep' '//pkg:edit'
  1404  assert_err "running buildifier: exit status 1"
  1405}
  1406
  1407function test_add_deps_to_targets_in_same_package() {
  1408  in='cc_library(name = "smurf")
  1409cc_library(name = "smorf")'
  1410  run "$in" 'add deps //foo:bar' //pkg:smurf //pkg:smorf
  1411  assert_equals 'cc_library(
  1412    name = "smurf",
  1413    deps = ["//foo:bar"],
  1414)
  1415
  1416cc_library(
  1417    name = "smorf",
  1418    deps = ["//foo:bar"],
  1419)'
  1420}
  1421
  1422function test_rule_comment() {
  1423  in='cc_library(name = "a")'
  1424  run "$in" 'comment Hello' //pkg:a
  1425  assert_equals '# Hello
  1426cc_library(name = "a")'
  1427}
  1428
  1429function test_attribute_comment() {
  1430  in='cc_library(
  1431    name = "a",
  1432    srcs = ["a.cc"],
  1433)'
  1434  run "$in" 'comment srcs Hello\ World' //pkg:a
  1435  assert_equals 'cc_library(
  1436    name = "a",
  1437    srcs = ["a.cc"],  # Hello World
  1438)'
  1439}
  1440
  1441function test_attribute_comment_no_eol() {
  1442  in='cc_library(
  1443    name = "a",
  1444    srcs = ["a.cc"],
  1445)'
  1446  run "$in" --eol-comments=false 'comment srcs Hello\ World' //pkg:a
  1447  assert_equals 'cc_library(
  1448    name = "a",
  1449    # Hello World
  1450    srcs = ["a.cc"],
  1451)'
  1452}
  1453
  1454function test_value_comment() {
  1455  in='cc_library(
  1456    name = "a",
  1457    srcs = [
  1458        "a.cc",
  1459        "b.cc",  # Old
  1460    ],
  1461)'
  1462  run "$in" 'comment srcs a.cc Hello' 'comment srcs b.cc New' //pkg:a
  1463  assert_equals 'cc_library(
  1464    name = "a",
  1465    srcs = [
  1466        "a.cc",  # Hello
  1467        "b.cc",  # New
  1468    ],
  1469)'
  1470}
  1471
  1472function test_value_multiline_comment() {
  1473  in='cc_library(
  1474    name = "a",
  1475    srcs = [
  1476        "a.cc",
  1477        "b.cc",
  1478    ],
  1479)'
  1480  run "$in" 'comment srcs b.cc Just\ a\
  1481multiline\ comment' //pkg:a
  1482  assert_equals 'cc_library(
  1483    name = "a",
  1484    srcs = [
  1485        "a.cc",
  1486        # Just a
  1487        # multiline comment
  1488        "b.cc",
  1489    ],
  1490)'
  1491  run "$in" 'comment srcs b.cc Another\nmultiline\ comment' //pkg:a
  1492  assert_equals 'cc_library(
  1493    name = "a",
  1494    srcs = [
  1495        "a.cc",
  1496        # Another
  1497        # multiline comment
  1498        "b.cc",
  1499    ],
  1500)'
  1501}
  1502
  1503function test_rule_print_comment() {
  1504  in='# Hello
  1505cc_library(name = "a")'
  1506  run "$in" 'print_comment' //pkg:a
  1507  assert_output 'Hello'
  1508}
  1509
  1510function test_rule_print_comment_with_suffix_and_after() {
  1511  in='# Hello Before
  1512cc_library(name = "a") # Hello Suffix
  1513# Hello After'
  1514  run "$in" 'print_comment' //pkg:a
  1515  assert_output 'Hello Before Hello Suffix Hello After'
  1516}
  1517
  1518function test_attribute_print_comment() {
  1519  in='cc_library(
  1520    name = "a",
  1521    srcs = ["a.cc"],  # Hello World
  1522)'
  1523  run "$in" 'print_comment srcs' //pkg:a
  1524  assert_output 'Hello World'
  1525}
  1526
  1527function test_attribute_print_comment_no_eol() {
  1528  in='cc_library(
  1529    name = "a",
  1530    # Hello World
  1531    srcs = ["a.cc"],
  1532)'
  1533  run "$in" --eol-comments=false 'print_comment srcs' //pkg:a
  1534  assert_output 'Hello World'
  1535}
  1536
  1537function test_value_print_comment() {
  1538  in='cc_library(
  1539    name = "a",
  1540    srcs = [
  1541        "a.cc",  # World
  1542        "b.cc",  # Hello
  1543    ],
  1544)'
  1545  run "$in" 'print_comment srcs b.cc' 'print_comment srcs a.cc' //pkg:a
  1546  assert_output 'Hello
  1547World'
  1548}
  1549
  1550function test_value_multiline_print_comment() {
  1551  in='cc_library(
  1552    name = "a",
  1553    srcs = [
  1554        "a.cc",
  1555        # Just a
  1556        # multiline comment
  1557        "b.cc",
  1558    ],
  1559)'
  1560  run "$in" 'print_comment srcs b.cc' //pkg:a
  1561  assert_output 'Just a multiline comment'
  1562}
  1563
  1564function test_value_inside_select_print_comment() {
  1565  in='cc_library(
  1566    name = "a",
  1567    srcs = [
  1568        "a.cc",  # World
  1569        "b.cc",  # Hello
  1570    ] + select({
  1571        "foo": [
  1572            "c.cc",  # hello
  1573            "d.cc",  # world
  1574        ],
  1575    }),
  1576)'
  1577  run "$in" 'print_comment srcs c.cc' 'print_comment srcs d.cc' //pkg:a
  1578  assert_output 'hello
  1579world'
  1580}
  1581
  1582# Test both absolute and relative package names
  1583function test_path() {
  1584  mkdir -p "java/com/foo/myproject"
  1585  echo 'java_library(name = "foo")' > "java/com/foo/myproject/BUILD"
  1586
  1587  $buildozer --buildifier= 'add deps a' "java/com/foo/myproject:foo"
  1588  cd java
  1589  $buildozer --buildifier= 'add deps b' "com/foo/myproject:foo"
  1590  cd com
  1591  $buildozer --buildifier= 'add deps c' "//java/com/foo/myproject:foo"
  1592  cd foo
  1593  $buildozer --buildifier= 'add deps d' "myproject:foo"
  1594  cd myproject
  1595  $buildozer --buildifier= 'add deps e' ":foo"
  1596
  1597  # Check that all dependencies have been added
  1598  echo -n 'java_library(name="foo",deps=["a","b","c","d","e",],)' > expected
  1599  tr -d ' \n' < "BUILD" > result
  1600  diff -u "expected" "result" || fail "Output didn't match"
  1601}
  1602
  1603function setup_file_test() {
  1604  mkdir -p "a/pkg1"
  1605  mkdir -p "a/pkg2"
  1606  cat > a/pkg1/BUILD <<EOF
  1607cc_library(name = "foo")
  1608cc_library(name = "bar")
  1609EOF
  1610  cat > a/pkg2/BUILD << EOF
  1611cc_library(name = "foo")
  1612cc_library(name = "bar")
  1613EOF
  1614
  1615  echo -n "
  1616new cc_library baz|//a/pkg1:__pkg__
  1617add deps a|//a/pkg1:baz
  1618add deps a|a/pkg1:foo
  1619add deps x#|a/pkg2:foo
  1620# add deps y|a/pkg1:foo
  1621  # add deps y|a/pkg2:foo
  1622
  1623add deps y|a/pkg2:bar|add deps c|a/pkg1:foo
  1624add deps z|a/pkg2:bar
  1625add deps a|//a/pkg1:bar
  1626add deps b|a/pkg1:foo" > commands
  1627}
  1628
  1629function check_file_test() {
  1630  # Check that all dependencies have been added
  1631  cat > expected_pkg_1 <<EOF
  1632cc_library(
  1633    name = "foo",
  1634    deps = [
  1635        "a",
  1636        "b",
  1637        "c",
  1638        "y",
  1639    ],
  1640)
  1641
  1642cc_library(
  1643    name = "bar",
  1644    deps = ["a"],
  1645)
  1646
  1647cc_library(
  1648    name = "baz",
  1649    deps = ["a"],
  1650)
  1651EOF
  1652  diff -u expected_pkg_1 a/pkg1/BUILD || fail "Output didn't match"
  1653
  1654  cat > expected_pkg_2 <<EOF
  1655cc_library(
  1656    name = "foo",
  1657    deps = ["x#"],
  1658)
  1659
  1660cc_library(
  1661    name = "bar",
  1662    deps = [
  1663        "c",
  1664        "y",
  1665        "z",
  1666    ],
  1667)
  1668EOF
  1669  diff -u expected_pkg_2 a/pkg2/BUILD || fail "Output didn't match"
  1670}
  1671
  1672# Test reading commands from stdin
  1673function test_file() {
  1674  setup_file_test
  1675  $buildozer --buildifier= -f - < commands
  1676  check_file_test
  1677
  1678  setup_file_test
  1679  $buildozer --buildifier= -f commands
  1680  check_file_test
  1681}
  1682
  1683# Test for selectively adding to specific rule types
  1684function test_add_dep_filtered() {
  1685  in='go_library(
  1686    name = "edit",
  1687    deps = ["//pkg:a"],
  1688)'
  1689  ERROR=3 run "$in" -types='cc_library' 'add deps //pkg:b' '//pkg:edit'
  1690  assert_equals "$in"
  1691}
  1692
  1693function test_add_dep_unfiltered() {
  1694  in='cc_library(
  1695    name = "edit",
  1696    deps = ["//pkg:a"],
  1697)'
  1698  run "$in" -types='cc_library' 'add deps //pkg:b' '//pkg:edit'
  1699  assert_equals 'cc_library(
  1700    name = "edit",
  1701    deps = [
  1702        ":b",
  1703        "//pkg:a",
  1704    ],
  1705)'
  1706}
  1707
  1708function test_add_library_always_unfiltered() {
  1709  in='cc_library(
  1710    name = "edit",
  1711    deps = ["//pkg:a"],
  1712)'
  1713  run "$in" -types='go_library' 'new cc_library a' '//pkg:__pkg__'
  1714  assert_equals 'cc_library(
  1715    name = "edit",
  1716    deps = ["//pkg:a"],
  1717)
  1718
  1719cc_library(name = "a")'
  1720}
  1721
  1722function test_new_load_after_package() {
  1723in='# Comment
  1724
  1725package(default_visibility = ["//visibility:public"])
  1726
  1727x(
  1728    name="x",
  1729    srcs=["x.cc"],
  1730)'
  1731
  1732  run "$in" 'new_load /foo/bar x y z' '//pkg:x'
  1733  assert_equals '# Comment
  1734
  1735load("/foo/bar", "x", "y", "z")
  1736
  1737package(default_visibility = ["//visibility:public"])
  1738
  1739x(
  1740    name = "x",
  1741    srcs = ["x.cc"],
  1742)'
  1743}
  1744
  1745
  1746function test_new_load_no_package() {
  1747in='x(
  1748    name="x",
  1749    srcs=["x.cc"],
  1750)'
  1751
  1752  run "$in" 'new_load /foo/bar x y z' '//pkg:x'
  1753  assert_equals 'load("/foo/bar", "x", "y", "z")
  1754
  1755x(
  1756    name = "x",
  1757    srcs = ["x.cc"],
  1758)'
  1759}
  1760
  1761
  1762function test_new_load_empty_file() {
  1763  run '' 'new_load /foo/bar x y z' pkg/BUILD
  1764  assert_equals 'load("/foo/bar", "x", "y", "z")'
  1765}
  1766
  1767function test_new_load_only_comments() {
  1768in='# Just comments
  1769# And more comments'
  1770
  1771  run "$in" 'new_load /foo/bar x y z' pkg/BUILD
  1772  assert_equals '# Just comments
  1773# And more comments
  1774
  1775load("/foo/bar", "x", "y", "z")'
  1776}
  1777
  1778function test_new_load_after_workspace() {
  1779in='# A comment
  1780
  1781workspace(name = "blah")'
  1782
  1783  run "$in" 'new_load /foo/bar x y z' pkg/BUILD
  1784  assert_equals '# A comment
  1785
  1786workspace(name = "blah")
  1787
  1788load("/foo/bar", "x", "y", "z")'
  1789}
  1790
  1791function test_new_load_existing() {
  1792in='load("/foo/bar", "y")
  1793'
  1794
  1795  run "$in" 'new_load /foo/bar z x y' pkg/BUILD
  1796  assert_equals 'load("/foo/bar", "x", "y", "z")'
  1797}
  1798
  1799function test_new_load_existing_multiple() {
  1800in='load("/foo/bar", "x")
  1801load("/foo/bar", "y")
  1802'
  1803
  1804  run "$in" 'new_load /foo/bar x y z' pkg/BUILD
  1805  assert_equals 'load("/foo/bar", "x", "y", "z")'
  1806}
  1807
  1808function test_new_load_wrong_location() {
  1809in='load("/foo/bar", "x")
  1810'
  1811
  1812  run "$in" 'new_load /baz/bam y z' pkg/BUILD
  1813  assert_equals 'load("/baz/bam", "y", "z")
  1814load("/foo/bar", "x")'
  1815}
  1816
  1817function test_print_attribute_value_with_spaces() {
  1818  in='cc_test(name = "a", deprecation = "one two three")'
  1819  run "$in" 'print deprecation' '//pkg:a'
  1820  assert_output '"one two three"'
  1821}
  1822
  1823function test_insert_into_variable() {
  1824in='some_var = ["a"]
  1825some_rule(name="r", deps=some_var)'
  1826
  1827  run "$in" --edit-variables 'add deps b' '//pkg:r'
  1828  assert_equals 'some_var = [
  1829    "a",
  1830    "b",
  1831]
  1832
  1833some_rule(
  1834    name = "r",
  1835    deps = some_var,
  1836)'
  1837}
  1838
  1839function test_insert_into_variable_by_adding() {
  1840in='some_var = glob(["x"])
  1841some_rule(name="r", deps=some_var)'
  1842
  1843  run "$in" --edit-variables "add deps b" '//pkg:r'
  1844  assert_equals 'some_var = glob(["x"]) + ["b"]
  1845
  1846some_rule(
  1847    name = "r",
  1848    deps = some_var,
  1849)'
  1850}
  1851
  1852function test_insert_into_same_variable_twice() {
  1853in='some_var = ["a"]
  1854some_rule(name="r1", deps=some_var)
  1855some_rule(name="r2", deps=some_var)'
  1856
  1857  run "$in" --edit-variables "add deps b" '//pkg:r1' '//pkg:r2'
  1858  assert_equals 'some_var = [
  1859    "a",
  1860    "b",
  1861]
  1862
  1863some_rule(
  1864    name = "r1",
  1865    deps = some_var,
  1866)
  1867
  1868some_rule(
  1869    name = "r2",
  1870    deps = some_var,
  1871)'
  1872}
  1873
  1874function test_pkg_recursive_wildcard() {
  1875mkdir -p foo/bar/baz
  1876mkdir -p foo/abc
  1877
  1878cat > foo/BUILD <<EOF
  1879matching_rule(name = "r1", deps = [":bar"])
  1880EOF
  1881cat > foo/bar/BUILD <<EOF
  1882non_matching_rule(name = "r2", deps = [":bar"])
  1883EOF
  1884cat > foo/bar/baz/BUILD <<EOF
  1885matching_rule(name = "r3", deps = [":bar"])
  1886EOF
  1887cat > foo/abc/BUILD <<EOF
  1888matching_rule(name = "r4", deps = [":bar"])
  1889EOF
  1890
  1891run_with_current_workspace "$buildozer --buildifier=" "add deps new_dep" "//foo/...:%matching_rule"
  1892assert_no_err "rule '%matching_rule' not found"
  1893
  1894assert_equals 'matching_rule(
  1895    name = "r1",
  1896    deps = [
  1897        "new_dep",
  1898        ":bar",
  1899    ],
  1900)' foo
  1901
  1902assert_equals 'non_matching_rule(name = "r2", deps = [":bar"])' foo/bar
  1903
  1904assert_equals 'matching_rule(
  1905    name = "r3",
  1906    deps = [
  1907        "new_dep",
  1908        ":bar",
  1909    ],
  1910)' foo/bar/baz
  1911
  1912assert_equals 'matching_rule(
  1913    name = "r4",
  1914    deps = [
  1915        "new_dep",
  1916        ":bar",
  1917    ],
  1918)' foo/abc
  1919}
  1920
  1921function test_add_dep_string() {
  1922  # Add a string without quotes, a quoted string (should be unquoted automatically)
  1923  # and a quoted string with quotes inside (should be unquoted just once).
  1924  run "$one_dep" 'add deps //foo "//bar" "\"//baz\""' '//pkg:edit'
  1925  assert_equals 'go_library(
  1926    name = "edit",
  1927    deps = [
  1928        "\"//baz\"",
  1929        "//bar",
  1930        "//buildifier:build",
  1931        "//foo",
  1932    ],
  1933)'
  1934}
  1935
  1936function test_remove_dep_string() {
  1937  # Remove quoted and unquoted strings to make sure that `add` and `remove`
  1938  # unquote them equally
  1939  run "$quoted_deps" 'remove deps //foo "//bar" "\"//baz\""' '//pkg:edit'
  1940  assert_equals 'go_library(
  1941    name = "edit",
  1942    deps = ["//buildifier:build"],
  1943)'
  1944}
  1945
  1946function test_set_config_string() {
  1947  run "$one_dep" 'set config "foo"' '//pkg:edit'
  1948  assert_equals 'go_library(
  1949    name = "edit",
  1950    config = "foo",
  1951    deps = ["//buildifier:build"],
  1952)'
  1953}
  1954
  1955function test_fix_unused_load() {
  1956  run 'load(":a.bzl", "a")
  1957# TODO: refactor
  1958
  1959# begin loads
  1960
  1961load(":foo.bzl", "foo")  # foo
  1962load(":foobar.bzl", "foobar")  # this one is actually used
  1963load(":baz.bzl", "baz")  # this is @unused
  1964load(":bar.bzl", "bar")  # bar
  1965
  1966# end loads
  1967
  1968# before
  1969load(":qux.bzl", "qux")
  1970# after
  1971
  1972foobar()
  1973
  1974load(":somewhere_else.bzl", "foobar")
  1975
  1976foobar()
  1977
  1978load(":somewhere_else.bzl", "foobar")
  1979
  1980foobar()' 'fix unusedLoads' 'pkg/BUILD'
  1981  assert_equals '# TODO: refactor
  1982
  1983# begin loads
  1984
  1985load(":baz.bzl", "baz")  # this is @unused
  1986load(":foobar.bzl", "foobar")  # this one is actually used
  1987
  1988# end loads
  1989
  1990# before
  1991# after
  1992
  1993load(":somewhere_else.bzl", "foobar")
  1994
  1995foobar()
  1996
  1997foobar()
  1998
  1999foobar()'
  2000}
  2001
  2002function test_commands_with_targets() {
  2003  mkdir -p pkg1
  2004  mkdir -p pkg2
  2005
  2006  cat > pkg1/BUILD <<EOF
  2007rule(name = "r1", deps = [":bar"], compatible_with=["//env:a"])
  2008EOF
  2009  cat > pkg2/BUILD <<EOF
  2010rule(name = "r2", compatible_with=["//env:a"])
  2011EOF
  2012
  2013  cat > commands <<EOF
  2014remove compatible_with //env:a|*
  2015add deps :baz|*
  2016EOF
  2017  $buildozer --buildifier= -f commands pkg1:* pkg2:*
  2018  assert_equals 'rule(
  2019    name = "r1",
  2020    deps = [
  2021        ":bar",
  2022        ":baz",
  2023    ],
  2024)' pkg1
  2025  assert_equals 'rule(
  2026    name = "r2",
  2027    deps = [":baz"],
  2028)' pkg2
  2029}
  2030
  2031function test_module_bazel() {
  2032  cat > MODULE.bazel <<EOF
  2033module(
  2034    name = "foo",
  2035    version = "0.27.0",
  2036)
  2037
  2038bazel_dep(name = "gazelle", version = "0.30.0")
  2039
  2040go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
  2041go_deps.from_file(go_mod = "//:go.mod")
  2042use_repo(go_deps, "com_example_foo")
  2043EOF
  2044
  2045  cat > MODULE.bazel.expected <<EOF
  2046module(
  2047    name = "foo",
  2048    version = "0.27.0",
  2049)
  2050
  2051bazel_dep(name = "gazelle", version = "0.30.0")
  2052
  2053go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
  2054go_deps.from_file(go_mod = "//:go.mod")
  2055EOF
  2056
  2057  $buildozer 'delete' //MODULE.bazel:%10
  2058  diff -u MODULE.bazel.expected MODULE.bazel || fail "Output didn't match"
  2059}
  2060
  2061function test_module_bazel_new() {
  2062  cat > MODULE.bazel <<EOF
  2063bazel_dep(name = "gazelle", version = "0.30.0")
  2064EOF
  2065
  2066  cat > MODULE.bazel.expected <<EOF
  2067bazel_dep(name = "gazelle", version = "0.30.0")
  2068bazel_dep(name = "rules_go")
  2069EOF
  2070
  2071  $buildozer 'new bazel_dep rules_go after gazelle' //MODULE.bazel:__pkg__
  2072  diff -u MODULE.bazel.expected MODULE.bazel || fail "Output didn't match"
  2073}
  2074
  2075function test_use_repo_add() {
  2076  cat > MODULE.bazel <<EOF
  2077module(
  2078    name = "foo",
  2079    version = "0.27.0",
  2080)
  2081
  2082bazel_dep(name = "gazelle", version = "0.30.0")
  2083
  2084go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
  2085go_deps.from_file(go_mod = "//:go.mod")
  2086use_repo(go_deps, "com_example_foo")
  2087
  2088go_dev_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", dev_dependency = True)
  2089go_dev_deps.from_file(go_mod = "//:go_dev.mod")
  2090EOF
  2091
  2092  cat > MODULE.bazel.expected <<EOF
  2093module(
  2094    name = "foo",
  2095    version = "0.27.0",
  2096)
  2097
  2098bazel_dep(name = "gazelle", version = "0.30.0")
  2099
  2100go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
  2101go_deps.from_file(go_mod = "//:go.mod")
  2102use_repo(go_deps, "com_example_foo", "org_example_bar")
  2103
  2104go_dev_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", dev_dependency = True)
  2105go_dev_deps.from_file(go_mod = "//:go_dev.mod")
  2106EOF
  2107
  2108  $buildozer 'use_repo_add @gazelle//:extensions.bzl go_deps org_example_bar com_example_foo' //MODULE.bazel:all
  2109  diff -u MODULE.bazel.expected MODULE.bazel || fail "Output didn't match"
  2110}
  2111
  2112function test_use_repo_add_dev() {
  2113  cat > MODULE.bazel <<EOF
  2114module(
  2115    name = "foo",
  2116    version = "0.27.0",
  2117)
  2118
  2119bazel_dep(name = "gazelle", version = "0.30.0")
  2120
  2121go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
  2122go_deps.from_file(go_mod = "//:go.mod")
  2123use_repo(go_deps, "com_example_foo")
  2124
  2125go_dev_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", dev_dependency = True)
  2126go_dev_deps.from_file(go_mod = "//:go_dev.mod")
  2127EOF
  2128
  2129  cat > MODULE.bazel.expected <<EOF
  2130module(
  2131    name = "foo",
  2132    version = "0.27.0",
  2133)
  2134
  2135bazel_dep(name = "gazelle", version = "0.30.0")
  2136
  2137go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
  2138go_deps.from_file(go_mod = "//:go.mod")
  2139use_repo(go_deps, "com_example_foo")
  2140
  2141go_dev_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", dev_dependency = True)
  2142go_dev_deps.from_file(go_mod = "//:go_dev.mod")
  2143use_repo(go_dev_deps, "org_example_bar", "org_example_foo")
  2144EOF
  2145
  2146  $buildozer 'use_repo_add dev @gazelle//:extensions.bzl go_deps org_example_foo org_example_bar' //MODULE.bazel:all
  2147  diff -u MODULE.bazel.expected MODULE.bazel || fail "Output didn't match"
  2148}
  2149
  2150function test_use_repo_remove() {
  2151  cat > MODULE.bazel <<EOF
  2152module(
  2153    name = "foo",
  2154    version = "0.27.0",
  2155)
  2156
  2157bazel_dep(name = "gazelle", version = "0.30.0")
  2158
  2159go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
  2160go_deps.from_file(go_mod = "//:go.mod")
  2161use_repo(go_deps, "com_example_foo")
  2162
  2163go_dev_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", dev_dependency = True)
  2164go_dev_deps.from_file(go_mod = "//:go_dev.mod")
  2165EOF
  2166
  2167  cat > MODULE.bazel.expected <<EOF
  2168module(
  2169    name = "foo",
  2170    version = "0.27.0",
  2171)
  2172
  2173bazel_dep(name = "gazelle", version = "0.30.0")
  2174
  2175go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
  2176go_deps.from_file(go_mod = "//:go.mod")
  2177
  2178go_dev_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", dev_dependency = True)
  2179go_dev_deps.from_file(go_mod = "//:go_dev.mod")
  2180EOF
  2181
  2182  $buildozer 'use_repo_remove @gazelle//:extensions.bzl go_deps bar_example com_example_foo' //MODULE.bazel:all
  2183  diff -u MODULE.bazel.expected MODULE.bazel || fail "Output didn't match"
  2184}
  2185
  2186function test_use_repo_remove_dev() {
  2187  cat > MODULE.bazel <<EOF
  2188module(
  2189    name = "foo",
  2190    version = "0.27.0",
  2191)
  2192
  2193bazel_dep(name = "gazelle", version = "0.30.0")
  2194
  2195go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
  2196go_deps.from_file(go_mod = "//:go.mod")
  2197use_repo(go_deps, "com_example_foo")
  2198
  2199go_dev_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", dev_dependency = True)
  2200go_dev_deps.from_file(go_mod = "//:go_dev.mod")
  2201use_repo(go_dev_deps, "invalid_foo")
  2202use_repo(
  2203    go_dev_deps,
  2204    # group these
  2205    "org_example_quz",
  2206    "org_example_foo",
  2207    "org_example_bar",
  2208
  2209    # and these
  2210    "com_example_quz",
  2211    my_com_example_baz = "com_example_baz",
  2212    "com_example_bar",
  2213)
  2214EOF
  2215
  2216  cat > MODULE.bazel.expected <<EOF
  2217module(
  2218    name = "foo",
  2219    version = "0.27.0",
  2220)
  2221
  2222bazel_dep(name = "gazelle", version = "0.30.0")
  2223
  2224go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
  2225go_deps.from_file(go_mod = "//:go.mod")
  2226use_repo(go_deps, "com_example_foo")
  2227
  2228go_dev_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", dev_dependency = True)
  2229go_dev_deps.from_file(go_mod = "//:go_dev.mod")
  2230use_repo(
  2231    go_dev_deps,
  2232    # group these
  2233    "org_example_foo",
  2234    "org_example_quz",
  2235
  2236    # and these
  2237    "com_example_bar",
  2238    "com_example_quz",
  2239)
  2240EOF
  2241
  2242  $buildozer 'use_repo_remove dev @gazelle//:extensions.bzl go_deps com_example_baz org_example_bar invalid_foo' //MODULE.bazel:all
  2243  diff -u MODULE.bazel.expected MODULE.bazel || fail "Output didn't match"
  2244}
  2245
  2246function test_use_repo_add_isolated() {
  2247  cat > MODULE.bazel <<EOF
  2248module(
  2249    name = "foo",
  2250    version = "0.27.0",
  2251)
  2252
  2253bazel_dep(name = "gazelle", version = "0.30.0")
  2254
  2255go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
  2256go_deps.from_file(go_mod = "//:go.mod")
  2257use_repo(go_deps, "com_example_foo")
  2258
  2259go_isolated_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", isolate = True)
  2260go_isolated_deps.from_file(go_mod = "//:go_tool_deps.mod")
  2261use_repo(go_isolated_deps, "com_example_foo")
  2262
  2263go_isolated_dev_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", dev_dependency = True, isolate = True)
  2264go_isolated_dev_deps.from_file(go_mod = "//:go_more_tool_deps.mod")
  2265use_repo(go_isolated_dev_deps, "com_example_foo")
  2266
  2267go_more_isolated_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", isolate = True)
  2268go_more_isolated_deps.from_file(go_mod = "//:go_more_tool_deps.mod")
  2269use_repo(go_more_isolated_deps, "com_example_foo")
  2270EOF
  2271
  2272  cat > MODULE.bazel.expected <<EOF
  2273module(
  2274    name = "foo",
  2275    version = "0.27.0",
  2276)
  2277
  2278bazel_dep(name = "gazelle", version = "0.30.0")
  2279
  2280go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
  2281go_deps.from_file(go_mod = "//:go.mod")
  2282use_repo(go_deps, "com_example_foo")
  2283
  2284go_isolated_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", isolate = True)
  2285go_isolated_deps.from_file(go_mod = "//:go_tool_deps.mod")
  2286use_repo(go_isolated_deps, "com_example_foo")
  2287
  2288go_isolated_dev_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", dev_dependency = True, isolate = True)
  2289go_isolated_dev_deps.from_file(go_mod = "//:go_more_tool_deps.mod")
  2290use_repo(go_isolated_dev_deps, "com_example_foo", "org_example_bar")
  2291
  2292go_more_isolated_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", isolate = True)
  2293go_more_isolated_deps.from_file(go_mod = "//:go_more_tool_deps.mod")
  2294use_repo(go_more_isolated_deps, "com_example_foo")
  2295EOF
  2296
  2297  $buildozer 'use_repo_add go_isolated_dev_deps org_example_bar com_example_foo' //MODULE.bazel:all
  2298  diff -u MODULE.bazel.expected MODULE.bazel || fail "Output didn't match"
  2299}
  2300
  2301function test_format() {
  2302  cat > MODULE.bazel <<EOF
  2303module(
  2304  name = "foo", version = "0.27.0",
  2305)
  2306EOF
  2307
  2308  cat > MODULE.bazel.expected <<EOF
  2309module(
  2310    name = "foo",
  2311    version = "0.27.0",
  2312)
  2313EOF
  2314
  2315  $buildozer 'format' //MODULE.bazel:all
  2316  diff -u MODULE.bazel.expected MODULE.bazel || fail "Output didn't match"
  2317}
  2318
  2319run_suite "buildozer tests"

View as plain text