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
def test_cache_setup_with_no_filters_uses_repo_default_excluded(self, MockSetup): interpreter = PythonInterpreter.get() mock_setup = MockSetup.return_value type(mock_setup).interpreter_requirement = mock.PropertyMock(return_value=None) with temporary_dir() as path: mock_setup.scratch_dir.return_value = path cache = PythonInterpreterCache(mock.MagicMock()) def set_interpreters(_): cache._interpreters.add(interpreter) cache._setup_cached = mock.Mock(side_effect=set_interpreters) self.assertEqual(cache.setup(), [interpreter])
def test_cache_setup_with_no_filters_uses_repo_default_excluded( self, MockSetup): interpreter = PythonInterpreter.get() mock_setup = MockSetup.return_value type(mock_setup).interpreter_requirement = mock.PropertyMock( return_value=None) with temporary_dir() as path: mock_setup.scratch_dir.return_value = path cache = PythonInterpreterCache(mock.MagicMock()) def set_interpreters(_): cache._interpreters.add(interpreter) cache._setup_cached = mock.Mock(side_effect=set_interpreters) self.assertEqual(cache.setup(), [interpreter])
def test_cache_setup_with_filter_overrides_repo_default(self, MockSetup): interpreter = PythonInterpreter.get() mock_setup = MockSetup.return_value # Explicitly set a repo-wide requirement that excludes our one interpreter type(mock_setup).interpreter_requirement = mock.PropertyMock( return_value=self._make_bad_requirement(interpreter.identity.requirement)) with temporary_dir() as path: mock_setup.scratch_dir.return_value = path cache = PythonInterpreterCache(mock.MagicMock()) def set_interpreters(_): cache._interpreters.add(interpreter) cache._setup_cached = mock.Mock(side_effect=set_interpreters) self.assertEqual(cache.setup(filters=(str(interpreter.identity.requirement),)), [interpreter])
def test_cache_setup_with_no_filters_uses_repo_default_excluded(self, MockSetup): # This is the interpreter we'll inject into the cache interpreter = PythonInterpreter.get() mock_setup = MockSetup.return_value # Explicitly set a repo-wide requirement that excludes our one interpreter type(mock_setup).interpreter_requirement = mock.PropertyMock( return_value=self._make_bad_requirement(interpreter.identity.requirement)) with temporary_dir() as path: mock_setup.scratch_dir.return_value = path cache = PythonInterpreterCache(mock.MagicMock()) def set_interpreters(_): cache._interpreters.add(interpreter) cache._setup_cached = mock.Mock(side_effect=set_interpreters) cache._setup_paths = mock.Mock() self.assertEqual(len(cache.setup()), 0)
def test_cache_setup_with_filter_overrides_repo_default(self, MockSetup): interpreter = PythonInterpreter.get() mock_setup = MockSetup.return_value # Explicitly set a repo-wide requirement that excludes our one interpreter type(mock_setup).interpreter_requirement = mock.PropertyMock( return_value=self._make_bad_requirement( interpreter.identity.requirement)) with temporary_dir() as path: mock_setup.scratch_dir.return_value = path cache = PythonInterpreterCache(mock.MagicMock()) def set_interpreters(_): cache._interpreters.add(interpreter) cache._setup_cached = mock.Mock(side_effect=set_interpreters) self.assertEqual( cache.setup(filters=(str(interpreter.identity.requirement), )), [interpreter])
def test_cache_setup_with_no_filters_uses_repo_default_excluded( self, MockSetup): # This is the interpreter we'll inject into the cache interpreter = PythonInterpreter.get() mock_setup = MockSetup.return_value # Explicitly set a repo-wide requirement that excludes our one interpreter type(mock_setup).interpreter_requirement = mock.PropertyMock( return_value=self._make_bad_requirement( interpreter.identity.requirement)) with temporary_dir() as path: mock_setup.scratch_dir.return_value = path cache = PythonInterpreterCache(mock.MagicMock()) def set_interpreters(_): cache._interpreters.add(interpreter) cache._setup_cached = mock.Mock(side_effect=set_interpreters) cache._setup_paths = mock.Mock() self.assertEqual(len(cache.setup()), 0)
def setUp(self): super(TestInterpreterCache, self).setUp() self._interpreter = PythonInterpreter.get()
def setUp(self): super().setUp() self._interpreter = PythonInterpreter.get()
def setUp(self): self._interpreter = PythonInterpreter.get()