def _setup_test(self, constraints=None, mock_setup_paths_interpreters=None): mock_setup = mock.MagicMock().return_value type(mock_setup).interpreter_constraints = mock.PropertyMock(return_value=constraints) with temporary_dir() as path: mock_setup.interpreter_cache_dir = path cache = PythonInterpreterCache(mock_setup, mock.MagicMock()) cache._setup_cached = mock.Mock(return_value=[self._interpreter]) if mock_setup_paths_interpreters: cache._setup_paths = mock.Mock(return_value=[PythonInterpreter.from_binary(mock_setup_paths_interpreters[0]), PythonInterpreter.from_binary(mock_setup_paths_interpreters[1])]) else: cache._setup_paths = mock.Mock(return_value=[]) yield cache, path
def test_interpreter_from_relpath_purges_stale_interpreter(self): """ Simulates a stale interpreter cache and tests that _interpreter_from_relpath properly detects it and removes the stale dist directory. See https://github.com/pantsbuild/pants/issues/3416 for more info. """ with temporary_dir() as temp_dir: # Setup a interpreter distribution that we can safely mutate. test_interpreter_binary = os.path.join(temp_dir, 'python') os.symlink(sys.executable, test_interpreter_binary) with self._setup_cache(constraints=[]) as (cache, path): # Setup cache for test interpreter distribution. identity_str = str( PythonInterpreter.from_binary( test_interpreter_binary).identity) cached_interpreter_dir = os.path.join(cache._cache_dir, identity_str) safe_mkdir(cached_interpreter_dir) cached_symlink = os.path.join(cached_interpreter_dir, 'python') os.symlink(test_interpreter_binary, cached_symlink) # Remove the test interpreter binary from filesystem and assert that the cache is purged. os.remove(test_interpreter_binary) self.assertEqual(os.path.exists(test_interpreter_binary), False) self.assertEqual(os.path.exists(cached_interpreter_dir), True) cache._interpreter_from_relpath(identity_str) self.assertEqual(os.path.exists(cached_interpreter_dir), False)
def test_interpreter_from_relpath_purges_stale_interpreter(self): """ Simulates a stale interpreter cache and tests that _interpreter_from_relpath properly detects it and removes the stale dist directory. See https://github.com/pantsbuild/pants/issues/3416 for more info. """ with temporary_dir() as temp_dir: # Setup a interpreter distribution that we can safely mutate. test_interpreter_binary = os.path.join(temp_dir, 'python') os.symlink(sys.executable, test_interpreter_binary) with self._setup_cache(constraints=[]) as (cache, path): # Setup cache for test interpreter distribution. identity_str = str(PythonInterpreter.from_binary(test_interpreter_binary).identity) cached_interpreter_dir = os.path.join(cache._cache_dir, identity_str) safe_mkdir(cached_interpreter_dir) cached_symlink = os.path.join(cached_interpreter_dir, 'python') os.symlink(test_interpreter_binary, cached_symlink) # Remove the test interpreter binary from filesystem and assert that the cache is purged. os.remove(test_interpreter_binary) self.assertEqual(os.path.exists(test_interpreter_binary), False) self.assertEqual(os.path.exists(cached_interpreter_dir), True) cache._interpreter_from_relpath(identity_str) self.assertEqual(os.path.exists(cached_interpreter_dir), False)
def _setup_test(self, constraints=None, mock_setup_paths_interpreters=None): mock_setup = mock.MagicMock().return_value type(mock_setup).interpreter_constraints = mock.PropertyMock( return_value=constraints) with temporary_dir() as path: mock_setup.interpreter_cache_dir = path cache = PythonInterpreterCache(mock_setup, mock.MagicMock()) cache._setup_cached = mock.Mock(return_value=[self._interpreter]) if mock_setup_paths_interpreters: cache._setup_paths = mock.Mock(return_value=[ PythonInterpreter.from_binary( mock_setup_paths_interpreters[0]), PythonInterpreter.from_binary( mock_setup_paths_interpreters[1]) ]) else: cache._setup_paths = mock.Mock(return_value=[]) yield cache, path