| Dan Willemsen | 22e9244 | 2018-12-03 15:56:10 -0800 | [diff] [blame] | 1 | # Copyright 2019 Google Inc. 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 |  | 
|  | 15 | import os | 
|  | 16 | import site | 
|  | 17 | import sys | 
|  | 18 |  | 
|  | 19 | # This file checks the visible python state against expected values when run | 
|  | 20 | # inside a hermetic par file. | 
|  | 21 |  | 
|  | 22 | failed = False | 
|  | 23 | def assert_equal(what, a, b): | 
|  | 24 | global failed | 
|  | 25 | if a != b: | 
|  | 26 | print("Expected %s('%s') == '%s'" % (what, a, b)) | 
|  | 27 | failed = True | 
|  | 28 |  | 
|  | 29 | assert_equal("__name__", __name__, "__main__") | 
| Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 30 | fileName = os.path.basename(__file__) | 
|  | 31 | if fileName.endswith('.pyc'): | 
|  | 32 | fileName = fileName[:-1] | 
|  | 33 | assert_equal("os.path.basename(__file__)", fileName, "par_test.py") | 
| Dan Willemsen | 22e9244 | 2018-12-03 15:56:10 -0800 | [diff] [blame] | 34 |  | 
|  | 35 | archive = os.path.dirname(__file__) | 
| Dan Willemsen | 339a63f | 2023-08-15 22:17:03 -0400 | [diff] [blame] | 36 | major = sys.version_info.major | 
|  | 37 | minor = sys.version_info.minor | 
| Dan Willemsen | 22e9244 | 2018-12-03 15:56:10 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | assert_equal("__package__", __package__, "") | 
|  | 40 | assert_equal("sys.argv[0]", sys.argv[0], archive) | 
|  | 41 | assert_equal("sys.executable", sys.executable, None) | 
|  | 42 | assert_equal("sys.exec_prefix", sys.exec_prefix, archive) | 
|  | 43 | assert_equal("sys.prefix", sys.prefix, archive) | 
|  | 44 | assert_equal("__loader__.archive", __loader__.archive, archive) | 
|  | 45 | assert_equal("site.ENABLE_USER_SITE", site.ENABLE_USER_SITE, None) | 
|  | 46 |  | 
| Dan Willemsen | 339a63f | 2023-08-15 22:17:03 -0400 | [diff] [blame] | 47 | assert_equal("len(sys.path)", len(sys.path), 4) | 
| Dan Willemsen | 22e9244 | 2018-12-03 15:56:10 -0800 | [diff] [blame] | 48 | assert_equal("sys.path[0]", sys.path[0], archive) | 
| Dan Willemsen | 339a63f | 2023-08-15 22:17:03 -0400 | [diff] [blame] | 49 | assert_equal("sys.path[1]", sys.path[1], os.path.join(archive, "internal", f"python{major}{minor}.zip")) | 
|  | 50 | assert_equal("sys.path[2]", sys.path[2], os.path.join(archive, "internal", f"python{major}.{minor}")) | 
|  | 51 | assert_equal("sys.path[3]", sys.path[3], os.path.join(archive, "internal", f"python{major}.{minor}", "lib-dynload")) | 
| Dan Willemsen | 22e9244 | 2018-12-03 15:56:10 -0800 | [diff] [blame] | 52 |  | 
| Dan Willemsen | 67d4258 | 2019-12-03 23:54:04 -0800 | [diff] [blame] | 53 | if os.getenv('ARGTEST', False): | 
|  | 54 | assert_equal("len(sys.argv)", len(sys.argv), 3) | 
|  | 55 | assert_equal("sys.argv[1]", sys.argv[1], "--arg1") | 
|  | 56 | assert_equal("sys.argv[2]", sys.argv[2], "arg2") | 
|  | 57 | else: | 
|  | 58 | assert_equal("len(sys.argv)", len(sys.argv), 1) | 
|  | 59 |  | 
| Dan Willemsen | 22e9244 | 2018-12-03 15:56:10 -0800 | [diff] [blame] | 60 | if failed: | 
|  | 61 | sys.exit(1) | 
|  | 62 |  | 
|  | 63 | import testpkg.par_test |