Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2016 The ChromiumOS Authors |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Unittests for virtualenv_wrapper""" |
| 6 | |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 7 | import os |
Mike Frysinger | 1ca1443 | 2020-02-16 00:18:56 -0500 | [diff] [blame] | 8 | import sys |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 9 | |
| 10 | from chromite.lib import cros_test_lib |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 11 | from chromite.scripts import virtualenv_wrapper |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 12 | |
Mike Frysinger | 807d828 | 2022-04-28 22:45:17 -0400 | [diff] [blame] | 13 | |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 14 | _MODULE_DIR = os.path.dirname(os.path.realpath(__file__)) |
Allen Li | 465a0d6 | 2016-11-30 14:55:08 -0800 | [diff] [blame] | 15 | |
| 16 | |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 17 | class VirtualEnvTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 18 | """Test that we are running in a virtualenv.""" |
Aviv Keshet | 39853bd | 2016-09-22 15:12:05 -0700 | [diff] [blame] | 19 | |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 20 | # pylint: disable=protected-access |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 21 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 22 | def testModuleIsFromVenv(self): |
| 23 | """Test that we import |six| from the virtualenv.""" |
| 24 | # Note: The |six| module is chosen somewhat arbitrarily, but it happens to |
| 25 | # be provided inside the chromite virtualenv. |
| 26 | six = __import__("six") |
| 27 | req_path = os.path.dirname(os.path.realpath(six.__file__)) |
| 28 | self.assertIn("/.cache/cros_venv/", req_path) |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 29 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 30 | def testInsideVenv(self): |
| 31 | """Test that we are inside a virtualenv.""" |
| 32 | # pylint: disable=protected-access |
| 33 | self.assertTrue( |
| 34 | ( |
| 35 | hasattr(sys, "real_prefix") |
| 36 | or ( |
| 37 | hasattr(sys, "base_prefix") |
| 38 | and sys.base_prefix != sys.prefix |
| 39 | ) |
| 40 | ) |
| 41 | ) |
Allen Li | 9f03f5b | 2016-12-13 15:23:52 -0800 | [diff] [blame] | 42 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | def testVenvMarkers(self): |
| 44 | """Test that the virtualenv marker functions work.""" |
| 45 | # pylint: disable=protected-access |
| 46 | test_env = {"PATH": "/bin:/usr/bin"} |
| 47 | self.assertFalse(virtualenv_wrapper._IsInsideVenv(test_env)) |
| 48 | new_test_env = virtualenv_wrapper._CreateVenvEnvironment(test_env) |
| 49 | self.assertTrue(virtualenv_wrapper._IsInsideVenv(new_test_env)) |
| 50 | |
| 51 | def testCreateVenvEnvironmentNoSideEffect(self): |
| 52 | """Test that _CreateVenvEnvironment doesn't modify input dict.""" |
| 53 | # pylint: disable=protected-access |
| 54 | test_env = {"PATH": "/bin:/usr/bin"} |
| 55 | original = test_env.copy() |
| 56 | virtualenv_wrapper._CreateVenvEnvironment(test_env) |
| 57 | self.assertEqual(test_env, original) |