Fix virtualenv environment detection

There seems to be some combination of factors that cause the string
matching of the VIRTUAL_ENV envvar to fail on trybots/pre-cq.  Given
that, I think its generally a bad idea to do path comparisons against
the virtualenv environment and reverting back to a dumber, but more
robust implementation of the virtualenv check using a magic variable.

BUG=chromium:673926
TEST=Run bin/venv_check and unit tests

Change-Id: Ifaafafc0abf5b079585a168f5d6a4b731c1a2d2e
Reviewed-on: https://chromium-review.googlesource.com/419722
Commit-Ready: David Riley <davidriley@chromium.org>
Tested-by: Allen Li <ayatane@chromium.org>
Reviewed-by: Aviv Keshet <akeshet@chromium.org>
diff --git a/scripts/virtualenv_wrapper_unittest.py b/scripts/virtualenv_wrapper_unittest.py
index eafe80d..ed82691 100644
--- a/scripts/virtualenv_wrapper_unittest.py
+++ b/scripts/virtualenv_wrapper_unittest.py
@@ -9,6 +9,7 @@
 import os
 
 from chromite.lib import cros_test_lib
+from chromite.scripts import virtualenv_wrapper
 
 _MODULE_DIR = os.path.dirname(os.path.realpath(__file__))
 _VENV_DIR = os.path.abspath(os.path.join(_MODULE_DIR, '..', '.venv'))
@@ -29,6 +30,23 @@
 
   def testInsideVenv(self):
     """Test that we are inside a virtualenv."""
-    self.assertIn('VIRTUAL_ENV', os.environ)
-    venv_dir = os.environ['VIRTUAL_ENV']
-    self.assertEqual(_VENV_DIR, venv_dir)
+    # pylint: disable=protected-access
+    self.assertTrue(virtualenv_wrapper._IsInsideVenv(os.environ))
+
+
+  def testVenvMarkers(self):
+    """Test that the virtualenv marker functions work."""
+    # pylint: disable=protected-access
+    test_env = {'PATH': '/bin:/usr/bin'}
+    self.assertFalse(virtualenv_wrapper._IsInsideVenv(test_env))
+    new_test_env = virtualenv_wrapper._CreateVenvEnvironment(test_env)
+    self.assertTrue(virtualenv_wrapper._IsInsideVenv(new_test_env))
+
+
+  def testCreateVenvEnvironmentNoSideEffect(self):
+    """Test that _CreateVenvEnvironment doesn't modify input dict."""
+    # pylint: disable=protected-access
+    test_env = {'PATH': '/bin:/usr/bin'}
+    original = test_env.copy()
+    virtualenv_wrapper._CreateVenvEnvironment(test_env)
+    self.assertEqual(test_env, original)