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