blob: 8aed78289861ab9935199a90f2014ca03eac75c5 [file] [log] [blame]
Dan Willemsen7fa76462020-02-19 19:40:38 -08001# Copyright 2020 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
15import os
16import site
17import sys
18
19# This file checks the visible python state against expected values when run
20# using a prebuilt python.
21
22failed = False
23def 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
29assert_equal("__name__", __name__, "__main__")
30assert_equal("os.path.basename(__file__)", os.path.basename(__file__), "py-cmd_test.py")
31
32if os.getenv('ARGTEST', False):
33 assert_equal("len(sys.argv)", len(sys.argv), 3)
34 assert_equal("sys.argv[1]", sys.argv[1], "arg1")
35 assert_equal("sys.argv[2]", sys.argv[2], "arg2")
36elif os.getenv('ARGTEST2', False):
37 assert_equal("len(sys.argv)", len(sys.argv), 3)
38 assert_equal("sys.argv[1]", sys.argv[1], "--arg1")
39 assert_equal("sys.argv[2]", sys.argv[2], "arg2")
40else:
41 assert_equal("len(sys.argv)", len(sys.argv), 1)
42
43if os.getenv('ARGTEST_ONLY', False):
44 if failed:
45 sys.exit(1)
46 sys.exit(0)
47
48assert_equal("__package__", __package__, None)
49assert_equal("sys.argv[0]", sys.argv[0], 'py-cmd_test.py')
50if sys.version_info[0] == 2:
51 assert_equal("basename(sys.executable)", os.path.basename(sys.executable), 'py2-cmd')
52else:
53 assert_equal("basename(sys.executable)", os.path.basename(sys.executable), 'py3-cmd')
54assert_equal("sys.exec_prefix", sys.exec_prefix, sys.executable)
55assert_equal("sys.prefix", sys.prefix, sys.executable)
56assert_equal("site.ENABLE_USER_SITE", site.ENABLE_USER_SITE, None)
57
Dan Willemsen339a63f2023-08-15 22:17:03 -040058major = sys.version_info.major
59minor = sys.version_info.minor
60
61if major == 2:
Dan Willemsen7fa76462020-02-19 19:40:38 -080062 assert_equal("len(sys.path)", len(sys.path), 4)
Krzysztof KosiƄskiebb25bd2022-09-20 02:16:33 +000063 assert_equal("sys.path[0]", sys.path[0], os.path.abspath(os.path.dirname(__file__)))
Dan Willemsen7fa76462020-02-19 19:40:38 -080064 assert_equal("sys.path[1]", sys.path[1], "/extra")
65 assert_equal("sys.path[2]", sys.path[2], os.path.join(sys.executable, "internal"))
66 assert_equal("sys.path[3]", sys.path[3], os.path.join(sys.executable, "internal", "stdlib"))
67else:
Dan Willemsen339a63f2023-08-15 22:17:03 -040068 assert_equal("len(sys.path)", len(sys.path), 5)
Dan Willemsen7fa76462020-02-19 19:40:38 -080069 assert_equal("sys.path[0]", sys.path[0], os.path.abspath(os.path.dirname(__file__)))
70 assert_equal("sys.path[1]", sys.path[1], "/extra")
Dan Willemsen339a63f2023-08-15 22:17:03 -040071 assert_equal("sys.path[2]", sys.path[2], os.path.join(sys.executable, 'internal', 'python' + str(sys.version_info[0]) + str(sys.version_info[1]) + '.zip'))
72 assert_equal("sys.path[3]", sys.path[3], os.path.join(sys.executable, 'internal', 'python' + str(sys.version_info[0]) + '.' + str(sys.version_info[1])))
73 assert_equal("sys.path[4]", sys.path[4], os.path.join(sys.executable, 'internal', 'python' + str(sys.version_info[0]) + '.' + str(sys.version_info[1]), 'lib-dynload'))
Dan Willemsen7fa76462020-02-19 19:40:38 -080074
75if failed:
76 sys.exit(1)
77
78import testpkg.pycmd_test