# vim: filetype=bash # # Copyright 2022 The TensorFlow 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. # ============================================================================== setup_file() { bazel version # Start the bazel server } # Do a bazel query specifically for the licenses checker. It searches for # targets matching the provided query, which start with // or @ but not # //tensorflow (so it looks for //third_party, //external, etc.), and then # gathers the list of all packages (i.e. directories) which contain those # targets. license_query() { bazel cquery --experimental_cc_shared_library "$1" --keep_going \ | grep -e "^//" -e "^@" \ | grep -E -v "^//tensorflow" \ | sed -e 's|:.*||' \ | sort -u } # Verify that, given a build target and a license-list generator target, all of # the dependencies of that target which include a license notice file are then # included when generating that license. Necessary because the license targets # in TensorFlow are manually enumerated rather than generated automatically. do_external_licenses_check(){ BUILD_TARGET="$1" LICENSES_TARGET="$2" # grep patterns for targets which are allowed to be missing from the licenses cat > $BATS_TEST_TMPDIR/allowed_to_be_missing < $BATS_TEST_TMPDIR/allowed_to_be_extra < $BATS_TEST_TMPDIR/expected_licenses license_query "deps($LICENSES_TARGET)" > $BATS_TEST_TMPDIR/actual_licenses # Column 1 is left only, Column 2 is right only, Column 3 is shared lines # Select lines unique to actual_licenses, i.e. extra licenses. comm -1 -3 $BATS_TEST_TMPDIR/expected_licenses $BATS_TEST_TMPDIR/actual_licenses | grep -v -f $BATS_TEST_TMPDIR/allowed_to_be_extra > $BATS_TEST_TMPDIR/actual_extra_licenses || true # Select lines unique to expected_licenses, i.e. missing licenses comm -2 -3 $BATS_TEST_TMPDIR/expected_licenses $BATS_TEST_TMPDIR/actual_licenses | grep -v -f $BATS_TEST_TMPDIR/allowed_to_be_missing > $BATS_TEST_TMPDIR/actual_missing_licenses || true if [[ -s $BATS_TEST_TMPDIR/actual_extra_licenses ]]; then echo "Please remove the following extra licenses from $LICENSES_TARGET:" cat $BATS_TEST_TMPDIR/actual_extra_licenses fi if [[ -s $BATS_TEST_TMPDIR/actual_missing_licenses ]]; then echo "Please include the missing licenses for the following packages in $LICENSES_TARGET:" cat $BATS_TEST_TMPDIR/actual_missing_licenses fi # Fail if either of the two "extras" or "missing" lists are present. If so, # then the user will see the above error messages. [[ ! -s $BATS_TEST_TMPDIR/actual_extra_licenses ]] && [[ ! -s $BATS_TEST_TMPDIR/actual_missing_licenses ]] } @test "Pip package generated license includes all dependencies' licenses" { do_external_licenses_check \ "//tensorflow/tools/pip_package:wheel" \ "//tensorflow/tools/pip_package:licenses" } # This test ensures that all the targets built into the Python package include # their dependencies. It's a rewritten version of the "smoke test", an older # Python script that was very difficult to understand. See # https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/pip_package/pip_smoke_test.py @test "Pip package includes all required //tensorflow dependencies" { # grep patterns for packages whose dependencies can be ignored cat > $BATS_TEST_TMPDIR/ignore_deps_for_these_packages < $BATS_TEST_TMPDIR/ignore_these_deps < $BATS_TEST_TMPDIR/pip_deps # Find all Python py_test targets not tagged "no_pip" or "manual", excluding # any targets in ignored packages. Combine this list of targets into a bazel # query list (e.g. the list becomes "target+target2+target3") bazel cquery --keep_going 'kind(py_test, //tensorflow/python/...) - attr("tags", "no_pip|manual", //tensorflow/python/...)' | grep -v -f $BATS_TEST_TMPDIR/ignore_deps_for_these_packages | paste -sd "+" - > $BATS_TEST_TMPDIR/deps # Find all one-step dependencies of those tests which are from //tensorflow # (since external deps will come from Python-level pip dependencies), # excluding dependencies and files that are known to be unneccessary. # This creates a list of targets under //tensorflow that are required for # TensorFlow python tests. bazel cquery --keep_going "deps($(cat $BATS_TEST_TMPDIR/deps), 1)" | grep "^//tensorflow" | grep -v -f $BATS_TEST_TMPDIR/ignore_these_deps | sort -u > $BATS_TEST_TMPDIR/required_deps # Find if any required dependencies are missing from the list of dependencies # included in the pip package. # (comm: Column 1 is left, Column 2 is right, Column 3 is shared lines) comm -2 -3 $BATS_TEST_TMPDIR/required_deps $BATS_TEST_TMPDIR/pip_deps > $BATS_TEST_TMPDIR/missing_deps || true if [[ -s $BATS_TEST_TMPDIR/missing_deps ]]; then cat < $BATS_TEST_TMPDIR/out cat < $BATS_TEST_TMPDIR/out cat <> errors.txt fi if [[ -e errors.txt ]]; then echo "Broken links found:" cat errors.txt rm errors.txt false fi done } @test "No duplicate files on Windows" { cat < /dev/null } teardown_file() { bazel shutdown }