blob: b1b79ba89532a869add27465c40c788b2aa18a19 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Aviv Keshet39853bd2016-09-22 15:12:05 -07002# 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
8from __future__ import print_function
9
10import os
Mike Frysinger1ca14432020-02-16 00:18:56 -050011import sys
Aviv Keshet39853bd2016-09-22 15:12:05 -070012
13from chromite.lib import cros_test_lib
Allen Li9f03f5b2016-12-13 15:23:52 -080014from chromite.scripts import virtualenv_wrapper
Aviv Keshet39853bd2016-09-22 15:12:05 -070015
Allen Li465a0d62016-11-30 14:55:08 -080016_MODULE_DIR = os.path.dirname(os.path.realpath(__file__))
Allen Li465a0d62016-11-30 14:55:08 -080017
18
Mike Frysinger1ca14432020-02-16 00:18:56 -050019assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
20
21
Aviv Keshet39853bd2016-09-22 15:12:05 -070022class VirtualEnvTest(cros_test_lib.TestCase):
23 """Test that we are running in a virtualenv."""
24
Allen Li8f0e8452016-12-16 15:33:12 -080025 # pylint: disable=protected-access
26
Allen Li465a0d62016-11-30 14:55:08 -080027
Aviv Keshet39853bd2016-09-22 15:12:05 -070028 def testModuleIsFromVenv(self):
Allen Li465a0d62016-11-30 14:55:08 -080029 """Test that we import |six| from the virtualenv."""
Aviv Keshet39853bd2016-09-22 15:12:05 -070030 # Note: The |six| module is chosen somewhat arbitrarily, but it happens to
31 # be provided inside the chromite virtualenv.
Allen Li465a0d62016-11-30 14:55:08 -080032 six = __import__('six')
Aviv Keshet39853bd2016-09-22 15:12:05 -070033 req_path = os.path.dirname(os.path.realpath(six.__file__))
Allen Li68f42e22017-03-27 17:08:59 -070034 self.assertIn('/.cache/cros_venv/', req_path)
Allen Li465a0d62016-11-30 14:55:08 -080035
36
37 def testInsideVenv(self):
38 """Test that we are inside a virtualenv."""
Allen Li9f03f5b2016-12-13 15:23:52 -080039 # pylint: disable=protected-access
Chris McDonaldd1f620f2020-04-18 09:35:05 -060040 self.assertTrue(
41 (hasattr(sys, 'real_prefix') or
42 (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix)))
Allen Li9f03f5b2016-12-13 15:23:52 -080043
44 def testVenvMarkers(self):
45 """Test that the virtualenv marker functions work."""
46 # pylint: disable=protected-access
47 test_env = {'PATH': '/bin:/usr/bin'}
48 self.assertFalse(virtualenv_wrapper._IsInsideVenv(test_env))
49 new_test_env = virtualenv_wrapper._CreateVenvEnvironment(test_env)
50 self.assertTrue(virtualenv_wrapper._IsInsideVenv(new_test_env))
51
52
53 def testCreateVenvEnvironmentNoSideEffect(self):
54 """Test that _CreateVenvEnvironment doesn't modify input dict."""
55 # pylint: disable=protected-access
56 test_env = {'PATH': '/bin:/usr/bin'}
57 original = test_env.copy()
58 virtualenv_wrapper._CreateVenvEnvironment(test_env)
59 self.assertEqual(test_env, original)