1# Copyright 2024 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 sys 17import unittest 18 19 20class NoUnsafePathsTest(unittest.TestCase): 21 def test_no_unsafe_paths_in_search_path(self): 22 # Based on sys.path documentation, the first item added is the zip 23 # archive 24 # (see: https://docs.python.org/3/library/sys_path_init.html) 25 # 26 # We can use this as a marker to verify that during bootstrapping, 27 # (1) no unexpected paths were prepended, and (2) no paths were 28 # accidentally dropped. 29 # 30 major, minor, *_ = sys.version_info 31 archive = f"python{major}{minor}.zip" 32 33 # < Python 3.11 behaviour 34 if (major, minor) < (3, 11): 35 # Because of https://github.com/bazelbuild/rules_python/blob/0.39.0/python/private/stage2_bootstrap_template.py#L415-L436 36 self.assertEqual(os.path.dirname(sys.argv[0]), sys.path[0]) 37 self.assertEqual(os.path.basename(sys.path[1]), archive) 38 # >= Python 3.11 behaviour 39 else: 40 self.assertEqual(os.path.basename(sys.path[0]), archive) 41 42 43if __name__ == '__main__': 44 unittest.main()