def test_destroy_venv(self): builder = envbuilder._FadesEnvBuilder() # make sure the virtualenv exists on disk options = {"virtualenv_options": [], "pyvenv_options": ['--system-site-packages'], "pip-options": [], } def fake_create(*_): """Fake venv create. This is for the test to avoid network usage on venv creation, but also create and set the fake dir. """ os.mkdir(fake_venv_path) builder.env_path = fake_venv_path fake_venv_path = tempfile.TemporaryDirectory().name builder.create_with_virtualenv = fake_create builder.create_env('python', False, options=options) assert os.path.exists(builder.env_path) cache_mock = Mock() envbuilder.destroy_venv(builder.env_path, cache_mock) self.assertFalse(os.path.exists(builder.env_path)) cache_mock.remove.assert_called_with(builder.env_path)
def test_destroy_venv(self): builder = envbuilder._FadesEnvBuilder() # make sure the virtualenv exists on disk options = { "virtualenv_options": [], "pyvenv_options": ['--system-site-packages'], "pip-options": [], } def fake_create(*_): """Fake venv create. This is for the test to avoid network usage on venv creation, but also create and set the fake dir. """ os.mkdir(fake_venv_path) builder.env_path = fake_venv_path fake_venv_path = tempfile.TemporaryDirectory().name builder.create_with_virtualenv = fake_create builder.create_env('python', False, options=options) assert os.path.exists(builder.env_path) cache_mock = Mock() envbuilder.destroy_venv(builder.env_path, cache_mock) self.assertFalse(os.path.exists(builder.env_path)) cache_mock.remove.assert_called_with(builder.env_path)
def test_destroy_venv_if_env_path_not_found(self): builder = envbuilder._FadesEnvBuilder() assert not os.path.exists(builder.env_path) cache_mock = Mock() envbuilder.destroy_venv(builder.env_path, cache_mock) self.assertFalse(os.path.exists(builder.env_path)) cache_mock.remove.assert_called_with(builder.env_path)
def test_create_virtualenv(self): env_builder = envbuilder._FadesEnvBuilder() interpreter = 'pythonX.Y' is_current = False options = {"virtualenv_options": [], "pyvenv_options": [], } with patch.object(envbuilder._FadesEnvBuilder, 'create_with_virtualenv') as mock_create: env_builder.create_env(interpreter, is_current, options) mock_create.assert_called_with(interpreter, options['virtualenv_options'])
def test_create_pyvenv(self): env_builder = envbuilder._FadesEnvBuilder() interpreter = 'python3' is_current = True options = {"virtualenv_options": [], "pyvenv_options": [], } with patch.object(EnvBuilder, 'create') as mock_create: env_builder.create_env(interpreter, is_current, options) self.assertFalse(env_builder.system_site_packages) self.assertTrue(mock_create.called)
def test_executionerror_exception(self): env_builder = envbuilder._FadesEnvBuilder() interpreter = 'python3' is_current = False options = {"virtualenv_options": [], "pyvenv_options": ['--system-site-packages'], } with patch('fades.envbuilder.helpers.logged_exec') as mock_lexec: mock_lexec.side_effect = envbuilder.helpers.ExecutionError(1, 'cmd', ['stdout']) with self.assertRaises(FadesError) as cm: env_builder.create_env(interpreter, is_current, options) self.assertEqual(str(cm.exception), 'virtualenv could not be run')
def test_exit_3_on_any_Exception(self): env_builder = envbuilder._FadesEnvBuilder() interpreter = 'python3' is_current = False options = {"virtualenv_options": [], "pyvenv_options": ['--system-site-packages'], } with patch('fades.envbuilder.helpers.logged_exec') as mock_lexec: mock_lexec.side_effect = Exception() with self.assertRaises(SystemExit) as cm: env_builder.create_env(interpreter, is_current, options) self.assertEqual(cm.exception.code, 3)
def test_general_error_exception(self): env_builder = envbuilder._FadesEnvBuilder() interpreter = 'python3' is_current = False options = {"virtualenv_options": [], "pyvenv_options": ['--system-site-packages'], } with patch('fades.envbuilder.helpers.logged_exec') as mock_lexec: mock_lexec.side_effect = Exception() with self.assertRaises(FadesError) as cm: env_builder.create_env(interpreter, is_current, options) self.assertEqual(str(cm.exception), 'General error while running virtualenv')
def test_filenotfound_exception(self): env_builder = envbuilder._FadesEnvBuilder() interpreter = 'python3' is_current = False options = {"virtualenv_options": [], "pyvenv_options": ['--system-site-packages'], } with patch('fades.envbuilder.helpers.logged_exec') as mock_lexec: # mock_lexec.side_effect = envbuilder.helpers.ExecutionError('matanga!') mock_lexec.side_effect = FileNotFoundError('matanga!') with self.assertRaises(FadesError) as cm: env_builder.create_env(interpreter, is_current, options) self.assertEqual(str(cm.exception), 'virtualenv not found')
def test_exit_1_on_FileNotFound(self): env_builder = envbuilder._FadesEnvBuilder() interpreter = 'python3' is_current = False options = {"virtualenv_options": [], "pyvenv_options": ['--system-site-packages'], } with patch('fades.envbuilder.helpers.logged_exec') as mock_lexec: # mock_lexec.side_effect = envbuilder.helpers.ExecutionError('matanga!') mock_lexec.side_effect = FileNotFoundError('matanga!') with self.assertRaises(SystemExit) as cm: env_builder.create_env(interpreter, is_current, options) self.assertEqual(cm.exception.code, 1)
def test_general_error_exception(self): env_builder = envbuilder._FadesEnvBuilder() interpreter = 'python3' is_current = False options = { "virtualenv_options": [], "pyvenv_options": ['--system-site-packages'], } with patch('fades.envbuilder.helpers.logged_exec') as mock_lexec: mock_lexec.side_effect = Exception() with self.assertRaises(FadesError) as cm: env_builder.create_env(interpreter, is_current, options) self.assertEqual(str(cm.exception), 'General error while running virtualenv')
def test_destroy_venv(self): builder = envbuilder._FadesEnvBuilder() # make sure the virtualenv exists on disk options = {"virtualenv_options": [], "pyvenv_options": ['--system-site-packages'], "pip-options": [], } builder.create_env('python', False, options=options) assert os.path.exists(builder.env_path) cache_mock = Mock() envbuilder.destroy_venv(builder.env_path, cache_mock) self.assertFalse(os.path.exists(builder.env_path)) cache_mock.remove.assert_called_with(builder.env_path)
def test_executionerror_exception(self): env_builder = envbuilder._FadesEnvBuilder() interpreter = 'python3' is_current = False options = { "virtualenv_options": [], "pyvenv_options": ['--system-site-packages'], } with patch('fades.envbuilder.helpers.logged_exec') as mock_lexec: mock_lexec.side_effect = envbuilder.helpers.ExecutionError( 1, 'cmd', ['stdout']) with self.assertRaises(FadesError) as cm: env_builder.create_env(interpreter, is_current, options) self.assertEqual(str(cm.exception), 'virtualenv could not be run')
def test_destroy_venv(self): builder = envbuilder._FadesEnvBuilder() # make sure the virtualenv exists on disk options = { "virtualenv_options": [], "pyvenv_options": ['--system-site-packages'], "pip-options": [], } builder.create_env('python', False, options=options) assert os.path.exists(builder.env_path) cache_mock = Mock() envbuilder.destroy_venv(builder.env_path, cache_mock) self.assertFalse(os.path.exists(builder.env_path)) cache_mock.remove.assert_called_with(builder.env_path)
def test_custom_env_path(self): builder = envbuilder._FadesEnvBuilder('some-path') self.assertEqual(builder.env_path, 'some-path')