blob: ed82691db25ca736644a6fdd9afc4859c164df38 [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
7from __future__ import print_function
8
9import os
Aviv Keshet39853bd2016-09-22 15:12:05 -070010
11from chromite.lib import cros_test_lib
Allen Li9f03f5b2016-12-13 15:23:52 -080012from chromite.scripts import virtualenv_wrapper
Aviv Keshet39853bd2016-09-22 15:12:05 -070013
Allen Li465a0d62016-11-30 14:55:08 -080014_MODULE_DIR = os.path.dirname(os.path.realpath(__file__))
15_VENV_DIR = os.path.abspath(os.path.join(_MODULE_DIR, '..', '.venv'))
16
17
Aviv Keshet39853bd2016-09-22 15:12:05 -070018class VirtualEnvTest(cros_test_lib.TestCase):
19 """Test that we are running in a virtualenv."""
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 Li465a0d62016-11-30 14:55:08 -080028 self.assertIn(_VENV_DIR, req_path)
29
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
34 self.assertTrue(virtualenv_wrapper._IsInsideVenv(os.environ))
35
36
37 def testVenvMarkers(self):
38 """Test that the virtualenv marker functions work."""
39 # pylint: disable=protected-access
40 test_env = {'PATH': '/bin:/usr/bin'}
41 self.assertFalse(virtualenv_wrapper._IsInsideVenv(test_env))
42 new_test_env = virtualenv_wrapper._CreateVenvEnvironment(test_env)
43 self.assertTrue(virtualenv_wrapper._IsInsideVenv(new_test_env))
44
45
46 def testCreateVenvEnvironmentNoSideEffect(self):
47 """Test that _CreateVenvEnvironment doesn't modify input dict."""
48 # pylint: disable=protected-access
49 test_env = {'PATH': '/bin:/usr/bin'}
50 original = test_env.copy()
51 virtualenv_wrapper._CreateVenvEnvironment(test_env)
52 self.assertEqual(test_env, original)