xref: /aosp_15_r20/external/bazelbuild-rules_python/examples/bzlmod/entry_points/tests/pylint_deps_test.py (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Copyright 2023 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import pathlib
17import subprocess
18import tempfile
19import unittest
20
21from python.runfiles import runfiles
22
23
24class ExampleTest(unittest.TestCase):
25    def __init__(self, *args, **kwargs):
26        self.maxDiff = None
27
28        super().__init__(*args, **kwargs)
29
30    def test_pylint_entry_point(self):
31        rlocation_path = os.environ.get("ENTRY_POINT")
32        assert (
33            rlocation_path is not None
34        ), "expected 'ENTRY_POINT' env variable to be set to rlocation of the tool"
35
36        entry_point = pathlib.Path(runfiles.Create().Rlocation(rlocation_path))
37        self.assertTrue(entry_point.exists(), f"'{entry_point}' does not exist")
38
39        # Let's run the entrypoint and check the tool version.
40        #
41        # NOTE @aignas 2023-08-24: the Windows python launcher with Python 3.9 and bazel 6 is not happy if we start
42        # passing extra files via `subprocess.run` and it starts to fail with an error that the file which is the
43        # entry_point cannot be found. However, just calling `--version` seems to be fine.
44        proc = subprocess.run(
45            [str(entry_point), "--version"],
46            check=True,
47            stdout=subprocess.PIPE,
48            stderr=subprocess.PIPE,
49        )
50        self.assertEqual(
51            "",
52            proc.stderr.decode("utf-8").strip(),
53        )
54        self.assertRegex(proc.stdout.decode("utf-8").strip(), "^pylint 2\.15\.9")
55
56    def test_pylint_report_has_expected_warnings(self):
57        rlocation_path = os.environ.get("PYLINT_REPORT")
58        assert (
59            rlocation_path is not None
60        ), "expected 'PYLINT_REPORT' env variable to be set to rlocation of the report"
61
62        pylint_report = pathlib.Path(runfiles.Create().Rlocation(rlocation_path))
63        self.assertTrue(pylint_report.exists(), f"'{pylint_report}' does not exist")
64
65        self.assertRegex(
66            pylint_report.read_text().strip(),
67            "W8201: Logging should be used instead of the print\(\) function\. \(print-function\)",
68        )
69
70
71if __name__ == "__main__":
72    unittest.main()
73