github.com/cheshirekow/buildtools@v0.0.0-20200224190056-5d637702fe81/buildifier/integration_test.sh (about)

     1  #!/bin/bash
     2  
     3  # --- begin runfiles.bash initialization ---
     4  # Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
     5  set -euo pipefail
     6  if [[ ! -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
    14  fi
    15  if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
    16    source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
    17  elif [[ -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-)"
    20  else
    21    echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
    22    exit 1
    23  fi
    24  # --- end runfiles.bash initialization ---
    25  
    26  die () {
    27    echo "$1" 1>&2
    28    exit 1
    29  }
    30  
    31  [[ "$1" =~ external/* ]] && buildifier="${{1#external/}}" || buildifier="$TEST_WORKSPACE/$1"
    32  [[ "$2" =~ external/* ]] && buildifier2="${{2#external/}}" || buildifier2="$TEST_WORKSPACE/$2"
    33  buildifier="$(rlocation "$buildifier")"
    34  buildifier2="$(rlocation "$buildifier2")"
    35  
    36  mkdir -p test_dir/subdir
    37  mkdir -p golden
    38  INPUT="load(':foo.bzl', 'foo'); foo(tags=['b', 'a'],srcs=['d', 'c'])"  # formatted differently in build and bzl modes
    39  echo -e "$INPUT" > test_dir/BUILD
    40  echo -e "$INPUT" > test_dir/test.bzl
    41  echo -e "$INPUT" > test_dir/subdir/test.bzl
    42  echo -e "$INPUT" > test_dir/subdir/build  # lowercase, should be ignored by -r
    43  echo -e "$INPUT" > test.bzl  # outside the test_dir directory
    44  echo -e "$INPUT" > test2.bzl  # outside the test_dir directory
    45  echo -e "not valid +" > test_dir/foo.bar
    46  mkdir test_dir/workspace  # name of a starlark file, but a directory
    47  mkdir test_dir/.git  # contents should be ignored
    48  echo -e "a+b" > test_dir/.git/git.bzl
    49  
    50  cp test_dir/foo.bar golden/foo.bar
    51  cp test_dir/subdir/build golden/build
    52  cp test_dir/.git/git.bzl golden/git.bzl
    53  
    54  "$buildifier" < test_dir/BUILD > stdout
    55  "$buildifier" -r test_dir
    56  "$buildifier" test.bzl
    57  "$buildifier" --path=foo.bzl test2.bzl
    58  "$buildifier2" test_dir/test.bzl > test_dir/test.bzl.out
    59  
    60  cat > golden/BUILD.golden <<EOF
    61  load(":foo.bzl", "foo")
    62  
    63  foo(
    64      srcs = [
    65          "c",
    66          "d",
    67      ],
    68      tags = [
    69          "a",
    70          "b",
    71      ],
    72  )
    73  EOF
    74  cat > golden/test.bzl.golden <<EOF
    75  load(":foo.bzl", "foo")
    76  
    77  foo(tags = ["b", "a"], srcs = ["d", "c"])
    78  EOF
    79  
    80  diff test_dir/BUILD golden/BUILD.golden
    81  diff test_dir/test.bzl golden/test.bzl.golden
    82  diff test_dir/subdir/test.bzl golden/test.bzl.golden
    83  diff test_dir/subdir/build golden/build
    84  diff test_dir/foo.bar golden/foo.bar
    85  diff test.bzl golden/test.bzl.golden
    86  diff test2.bzl golden/test.bzl.golden
    87  diff stdout golden/test.bzl.golden
    88  diff test_dir/test.bzl.out golden/test.bzl.golden
    89  diff test_dir/.git/git.bzl golden/git.bzl
    90  
    91  # Test run on a directory without -r
    92  "$buildifier" test_dir || ret=$?
    93  if [[ $ret -ne 3 ]]; then
    94    die "Directory without -r: expected buildifier to exit with 3, actual: $ret"
    95  fi
    96  
    97  # Test the linter
    98  
    99  cat > test_dir/to_fix.bzl <<EOF
   100  a = b / c
   101  d = {"b": 2, "a": 1}
   102  attr.foo(bar, cfg = "data")
   103  EOF
   104  
   105  cat > test_dir/fixed_golden.bzl <<EOF
   106  a = b // c
   107  d = {"b": 2, "a": 1}
   108  attr.foo(bar)
   109  EOF
   110  
   111  cat > test_dir/fixed_golden_all.bzl <<EOF
   112  a = b // c
   113  d = {"a": 1, "b": 2}
   114  attr.foo(bar)
   115  EOF
   116  
   117  cat > test_dir/fixed_golden_dict_cfg.bzl <<EOF
   118  a = b / c
   119  d = {"a": 1, "b": 2}
   120  attr.foo(bar)
   121  EOF
   122  
   123  cat > test_dir/fixed_golden_cfg.bzl <<EOF
   124  a = b / c
   125  d = {"b": 2, "a": 1}
   126  attr.foo(bar)
   127  EOF
   128  
   129  cat > test_dir/fix_report_golden <<EOF
   130  test_dir/to_fix_tmp.bzl: applied fixes, 1 warnings left
   131  fixed test_dir/to_fix_tmp.bzl
   132  EOF
   133  
   134  error_docstring="test_dir/to_fix_tmp.bzl:1: module-docstring: The file has no module docstring."$'\n'"A module docstring is a string literal (not a comment) which should be the first statement of a file (it may follow comment lines). (https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#module-docstring)"
   135  error_integer="test_dir/to_fix_tmp.bzl:1: integer-division: The \"/\" operator for integer division is deprecated in favor of \"//\". (https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#integer-division)"
   136  error_dict="test_dir/to_fix_tmp.bzl:2: unsorted-dict-items: Dictionary items are out of their lexicographical order. (https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#unsorted-dict-items)"
   137  error_cfg="test_dir/to_fix_tmp.bzl:3: attr-cfg: cfg = \"data\" for attr definitions has no effect and should be removed. (https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#attr-cfg)"
   138  
   139  test_lint () {
   140    ret=0
   141    cp test_dir/to_fix.bzl test_dir/to_fix_tmp.bzl
   142    echo "$4" > golden/error_golden
   143    echo "${4//test_dir\/to_fix_tmp.bzl/foo.bzl}" > golden/error_golden_foo
   144  
   145    cat > golden/fix_report_golden <<EOF
   146  test_dir/to_fix_tmp.bzl: applied fixes, $5 warnings left
   147  fixed test_dir/to_fix_tmp.bzl
   148  EOF
   149  
   150    # --lint=warn with --mode=check
   151    $buildifier --mode=check --lint=warn $2 test_dir/to_fix_tmp.bzl 2> test_dir/error || ret=$?
   152    if [[ $ret -ne 4 ]]; then
   153      die "$1: warn: Expected buildifier to exit with 4, actual: $ret"
   154    fi
   155    diff test_dir/error golden/error_golden || die "$1: wrong console output for --mode=check --lint=warn"
   156    diff test_dir/to_fix.bzl test_dir/to_fix.bzl || die "$1: --mode=check --lint=warn shouldn't modify files"
   157  
   158    # --lint=warn
   159    $buildifier --lint=warn $2 test_dir/to_fix_tmp.bzl 2> test_dir/error || ret=$?
   160    if [[ $ret -ne 4 ]]; then
   161      die "$1: warn: Expected buildifier to exit with 4, actual: $ret"
   162    fi
   163    diff test_dir/error golden/error_golden || die "$1: wrong console output for --lint=warn"
   164  
   165    # --lint=warn with --path
   166    $buildifier --lint=warn --path=foo.bzl $2 test_dir/to_fix_tmp.bzl 2> test_dir/error || ret=$?
   167    if [[ $ret -ne 4 ]]; then
   168      die "$1: warn: Expected buildifier to exit with 4, actual: $ret"
   169    fi
   170    diff test_dir/error golden/error_golden_foo || die "$1: wrong console output for --lint=warn and --path"
   171  
   172    # --lint=fix
   173    $buildifier --lint=fix $2 -v test_dir/to_fix_tmp.bzl 2> test_dir/fix_report || ret=$?
   174    if [[ $ret -ne 4 ]]; then
   175      die "$1: fix: Expected buildifier to exit with 4, actual: $ret"
   176    fi
   177    diff test_dir/to_fix_tmp.bzl $3 || die "$1: wrong file output for --lint=fix"
   178    diff test_dir/fix_report golden/fix_report_golden || die "$1: wrong console output for --lint=fix"
   179  }
   180  
   181  test_lint "default" "" "test_dir/fixed_golden.bzl" "$error_integer"$'\n'"$error_docstring"$'\n'"$error_cfg" 1
   182  test_lint "all" "--warnings=all" "test_dir/fixed_golden_all.bzl" "$error_integer"$'\n'"$error_docstring"$'\n'"$error_dict"$'\n'"$error_cfg" 1
   183  test_lint "cfg" "--warnings=attr-cfg" "test_dir/fixed_golden_cfg.bzl" "$error_cfg" 0
   184  test_lint "custom" "--warnings=-integer-division,+unsorted-dict-items" "test_dir/fixed_golden_dict_cfg.bzl" "$error_docstring"$'\n'"$error_dict"$'\n'"$error_cfg" 1
   185  
   186  # Test --format=json
   187  
   188  mkdir test_dir/json
   189  cp test_dir/to_fix.bzl test_dir/json
   190  
   191  # just not formatted
   192  cat > test_dir/json/to_fix_2.bzl <<EOF
   193  a=b
   194  EOF
   195  
   196  # not formatted with rewrites
   197  cat > test_dir/json/to_fix_3.bzl <<EOF
   198  x = 0123
   199  EOF
   200  
   201  # formatted, no warnings
   202  cat > test_dir/json/to_fix_4.bzl <<EOF
   203  a = b
   204  EOF
   205  
   206  # not a starlark file
   207  cat > test_dir/json/foo.bar <<EOF
   208  this is not a starlark file
   209  EOF
   210  
   211  cat > golden/json_report_golden <<EOF
   212  {
   213      "success": false,
   214      "files": [
   215          {
   216              "filename": "to_fix.bzl",
   217              "formatted": true,
   218              "valid": true,
   219              "warnings": [
   220                  {
   221                      "start": {
   222                          "line": 1,
   223                          "column": 5
   224                      },
   225                      "end": {
   226                          "line": 1,
   227                          "column": 10
   228                      },
   229                      "category": "integer-division",
   230                      "actionable": true,
   231                      "message": "The \"/\" operator for integer division is deprecated in favor of \"//\".",
   232                      "url": "https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#integer-division"
   233                  },
   234                  {
   235                      "start": {
   236                          "line": 3,
   237                          "column": 15
   238                      },
   239                      "end": {
   240                          "line": 3,
   241                          "column": 27
   242                      },
   243                      "category": "attr-cfg",
   244                      "actionable": true,
   245                      "message": "cfg = \"data\" for attr definitions has no effect and should be removed.",
   246                      "url": "https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#attr-cfg"
   247                  }
   248              ]
   249          },
   250          {
   251              "filename": "to_fix_2.bzl",
   252              "formatted": false,
   253              "valid": true,
   254              "warnings": []
   255          },
   256          {
   257              "filename": "to_fix_3.bzl",
   258              "formatted": false,
   259              "valid": true,
   260              "warnings": [],
   261              "rewrites": {
   262                  "editoctal": 1
   263              }
   264          },
   265          {
   266              "filename": "to_fix_4.bzl",
   267              "formatted": true,
   268              "valid": true,
   269              "warnings": []
   270          }
   271      ]
   272  }
   273  EOF
   274  
   275  cat > golden/json_report_small_golden <<EOF
   276  {
   277      "success": true,
   278      "files": [
   279          {
   280              "filename": "to_fix_4.bzl",
   281              "formatted": true,
   282              "valid": true,
   283              "warnings": []
   284          }
   285      ]
   286  }
   287  EOF
   288  
   289  cat > golden/json_report_stdin_golden <<EOF
   290  {
   291      "success": true,
   292      "files": [
   293          {
   294              "filename": "\u003cstdin\u003e",
   295              "formatted": true,
   296              "valid": true,
   297              "warnings": []
   298          }
   299      ]
   300  }
   301  EOF
   302  
   303  cat > golden/json_report_invalid_file_golden <<EOF
   304  {
   305      "success": false,
   306      "files": [
   307          {
   308              "filename": "to_fix_4.bzl",
   309              "formatted": true,
   310              "valid": true,
   311              "warnings": []
   312          },
   313          {
   314              "filename": "foo.bar",
   315              "formatted": false,
   316              "valid": false,
   317              "warnings": []
   318          }
   319      ]
   320  }
   321  EOF
   322  
   323  cd test_dir/json
   324  
   325  $buildifier --mode=check --format=json --lint=warn --warnings=-module-docstring -v to_fix.bzl to_fix_2.bzl to_fix_3.bzl to_fix_4.bzl > json_report
   326  diff json_report ../../golden/json_report_golden || die "$1: wrong console output for --mode=check --format=json --lint=warn with many files"
   327  
   328  $buildifier --mode=check --format=json --lint=warn --warnings=-module-docstring -v to_fix_4.bzl > json_report
   329  diff json_report ../../golden/json_report_small_golden || die "$1: wrong console output for --mode=check --format=json --lint=warn with a single file"
   330  
   331  $buildifier --mode=check --format=json --lint=warn --warnings=-module-docstring -v < to_fix_4.bzl > json_report
   332  diff json_report ../../golden/json_report_stdin_golden || die "$1: wrong console output for --mode=check --format=json --lint=warn with stdin"
   333  
   334  $buildifier --mode=check --format=json --lint=warn --warnings=-module-docstring -v to_fix_4.bzl foo.bar > json_report
   335  diff json_report ../../golden/json_report_invalid_file_golden || die "$1: wrong console output for --mode=check --format=json --lint=warn with an invalid file"