blob: 4eb63674bbabe11574df982f5371b731c78185bc [file] [log] [blame]
Aviv Keshet39853bd2016-09-22 15:12:05 -07001# Copyright 2016 The Chromium OS Authors. All rights reserved.
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 Keshet39853bd2016-09-22 15:12:05 -07007import os
Mike Frysinger1ca14432020-02-16 00:18:56 -05008import sys
Aviv Keshet39853bd2016-09-22 15:12:05 -07009
10from chromite.lib import cros_test_lib
Allen Li9f03f5b2016-12-13 15:23:52 -080011from chromite.scripts import virtualenv_wrapper
Aviv Keshet39853bd2016-09-22 15:12:05 -070012
Allen Li465a0d62016-11-30 14:55:08 -080013_MODULE_DIR = os.path.dirname(os.path.realpath(__file__))
Allen Li465a0d62016-11-30 14:55:08 -080014
15
Aviv Keshet39853bd2016-09-22 15:12:05 -070016class VirtualEnvTest(cros_test_lib.TestCase):
17 """Test that we are running in a virtualenv."""
18
Allen Li8f0e8452016-12-16 15:33:12 -080019 # pylint: disable=protected-access
20
Allen Li465a0d62016-11-30 14:55:08 -080021
Aviv Keshet39853bd2016-09-22 15:12:05 -070022 def testModuleIsFromVenv(self):
Allen Li465a0d62016-11-30 14:55:08 -080023 """Test that we import |six| from the virtualenv."""
Aviv Keshet39853bd2016-09-22 15:12:05 -070024 # Note: The |six| module is chosen somewhat arbitrarily, but it happens to
25 # be provided inside the chromite virtualenv.
Allen Li465a0d62016-11-30 14:55:08 -080026 six = __import__('six')
Aviv Keshet39853bd2016-09-22 15:12:05 -070027 req_path = os.path.dirname(os.path.realpath(six.__file__))
Allen Li68f42e22017-03-27 17:08:59 -070028 self.assertIn('/.cache/cros_venv/', req_path)
Allen Li465a0d62016-11-30 14:55:08 -080029
30
31 def testInsideVenv(self):
32 """Test that we are inside a virtualenv."""
Allen Li9f03f5b2016-12-13 15:23:52 -080033 # pylint: disable=protected-access
Chris McDonaldd1f620f2020-04-18 09:35:05 -060034 self.assertTrue(
35 (hasattr(sys, 'real_prefix') or
36 (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix)))
Allen Li9f03f5b2016-12-13 15:23:52 -080037
38 def testVenvMarkers(self):
39 """Test that the virtualenv marker functions work."""
40 # pylint: disable=protected-access
41 test_env = {'PATH': '/bin:/usr/bin'}
42 self.assertFalse(virtualenv_wrapper._IsInsideVenv(test_env))
43 new_test_env = virtualenv_wrapper._CreateVenvEnvironment(test_env)
44 self.assertTrue(virtualenv_wrapper._IsInsideVenv(new_test_env))
45
46
47 def testCreateVenvEnvironmentNoSideEffect(self):
48 """Test that _CreateVenvEnvironment doesn't modify input dict."""
49 # pylint: disable=protected-access
50 test_env = {'PATH': '/bin:/usr/bin'}
51 original = test_env.copy()
52 virtualenv_wrapper._CreateVenvEnvironment(test_env)
53 self.assertEqual(test_env, original)