def test_should_return_project_property_when_property_is_defined(self): project_mock = Mock(Project) project_mock.name = "my name" self.assertEquals("my name", ProjectDictWrapper(project_mock, Mock())["name"]) project_mock.get_property.assert_not_called()
def test_ensure_after_action_teardown_suppression_works_when_action_fails(self): task = Mock(name="task", dependencies=[]) action_regular = Mock(name="action_regular", execute_before=[], execute_after=["task"], teardown=False) action_regular.name = "action_regular" action_regular.execute.side_effect = ValueError("simulated action error") action_teardown = Mock(name="action_teardown", execute_before=[], execute_after=["task"], teardown=True) action_after_teardown = Mock(name="action_after_teardown", execute_before=[], execute_after=["task"], teardown=False) self.execution_manager.register_action(action_regular) self.execution_manager.register_action(action_teardown) self.execution_manager.register_action(action_after_teardown) self.execution_manager.register_task(task) self.execution_manager.resolve_dependencies() try: self.execution_manager.execute_task(task) self.assertTrue(False, "should not have reached here") except Exception as e: self.assertEqual(type(e), ValueError) self.assertEqual(str(e), "simulated action error") task.execute.assert_called_with(ANY, {}) action_regular.execute.assert_called_with({}) action_teardown.execute.assert_called_with({}) action_after_teardown.execute.assert_not_called()
def test_get_sphinx_apidoc_command_enabled_with_pep420(self): sphinx_mock = Mock() sys.modules["sphinx"] = sphinx_mock try: sphinx_mock.version_info = (1, 5, 3, 4, 5) self.project.set_property("sphinx_run_apidoc", True) self.project.set_property("dir_target", "dir_target") self.project.set_property("dir_source_main_python", "dir_source") self.project.set_property("sphinx_project_name", "project_name") self.assertEqual(get_sphinx_apidoc_command(self.project), ['sphinx.apidoc', '-H', 'project_name', '--implicit-namespaces', '-o', 'basedir/dir_target/sphinx_pyb/apidoc', 'basedir/dir_source' ] if sys.version_info[:3] >= (3, 3) else ['sphinx.apidoc', '-H', 'project_name', '-o', 'basedir/dir_target/sphinx_pyb/apidoc', 'basedir/dir_source' ] ) finally: del sys.modules["sphinx"]
def test_should_give_verbose_output(self, affected, execute, read, log): project = Mock() project.get_property.return_value = True # flake8_verbose_output == True logger = Mock() read.return_value = ['error', 'warning'] execute_tool_on_source_files(project, 'flake8', 'foo --bar', logger) log.assert_called_with(logger, 'flake8', ['error', 'warning'])
def test_should_publish_project(self, mock_run_command): mock_project = Mock(Project) mock_logger = Mock(Logger) publish(mock_project, mock_logger) mock_run_command.assert_called_with('publish', mock_project, mock_logger)
def test_should_package_project(self, mock_run_command): mock_project = Mock(Project) mock_logger = Mock(Logger) package(mock_project, mock_logger) mock_run_command.assert_called_with('package', mock_project, mock_logger)
def test_should_run_integration_tests(self, mock_run_command): mock_project = Mock(Project) mock_logger = Mock(Logger) run_integration_tests(mock_project, mock_logger) mock_run_command.assert_called_with('run_integration_tests', mock_project, mock_logger)
def test_should_update_pypi_plugin_with_non_exact_version_before_first_loading_attempt(self, install, load): project = Mock() downloader = DownloadingPluginLoader(Mock()) load.return_value = Mock() self.assertEqual(load.return_value, downloader.load_plugin(project, "pypi:spam", ">1.2")) install.assert_called_with(project, "pypi:spam", ">1.2", downloader.logger, None, True, False) self.assertEqual(install.call_count, 1)
def test_should_import_plugin_when_requiring_plugin_and_plugin_is_found_as_third_party(self, install, load): project = Mock() downloader = DownloadingPluginLoader(Mock()) load.return_value = Mock() self.assertEqual(load.return_value, downloader.load_plugin(project, "spam")) install.assert_not_called() self.assertEqual(install.call_count, 0)
def test_should_force_reinstall_vcs_plugin_before_first_loading_attempt(self, install, load): project = Mock() downloader = DownloadingPluginLoader(Mock()) load.return_value = Mock() self.assertEqual(load.return_value, downloader.load_plugin(project, "vcs:spam", plugin_module_name="spam")) install.assert_called_with(project, "vcs:spam", None, downloader.logger, "spam", False, True) self.assertEqual(install.call_count, 1)
def test_should_collect_all_tasks_when_there_is_a_simple_dependency(self): one = Mock(name="one", dependencies=[TaskDependency("two")]) two = Mock(name="two", dependencies=[]) self.execution_manager.register_task(one, two) self.execution_manager.resolve_dependencies() self.assertEqual( self.execution_manager.collect_all_transitive_tasks(["one"]), set([one, two]))
def test_ensure_that_dependencies_are_executed_before_root_task(self): one = Mock(name="one", dependencies=[]) two = Mock(name="two", dependencies=[TaskDependency("one")]) self.execution_manager.register_task(one, two) self.execution_manager.resolve_dependencies() self.assertEqual([one, two], self.execution_manager.build_execution_plan(["two"]))
def test_ensure_task_is_replaced_when_registering_two_tasks_with_same_name( self): original = Mock(name="spam") replacement = Mock(name="spam") self.execution_manager.register_task(original) self.execution_manager.register_task(replacement) original.extend.assert_called_with(replacement)
def test_should_not_load_module_twice_after_downloading_when_pypi_download_fails( self, install, load): install.side_effect = MissingPluginException("PyPI Install Boom") load.side_effect = MissingPluginException("PyPI Load Boom") downloader = DownloadingPluginLoader(Mock()) self.assertRaises(MissingPluginException, downloader.load_plugin, Mock(), "pypi:external_plugin") self.assertEquals(load.call_count, 1)
def test_should_raise_error_when_install_from_vcs_fails( self, execute, tempfile, read_file): read_file.return_value = ["something", "went wrong"] execute.return_value = 1 tempfile.NamedTemporaryFile().__enter__( ).name.__eq__.return_value = True self.assertRaises(MissingPluginException, _install_external_plugin, Mock(), "vcs:some VCS URL", None, Mock(), None)
def test_should_analyze_project(self, mock_run_command): mock_project = Mock(Project) mock_logger = Mock(Logger) analyze(mock_project, mock_logger) mock_run_command.assert_called_with('analyze', mock_project, mock_logger)
def test_should_install_plugin(self, execute, tempfile, read_file): read_file.return_value = ["no problems", "so far"] execute.return_value = 0 tempfile.NamedTemporaryFile().__enter__().name.__eq__.return_value = True _install_external_plugin(Mock(), "pypi:some-plugin", None, Mock(), None) execute.assert_called_with( PIP_EXEC_STANZA + ['install', '--index-url', ANY, '--extra-index-url', ANY, '--trusted-host', ANY, 'some-plugin'], shell=False, outfile_name=ANY, error_file_name=ANY, cwd=".", env=ANY)
def test_should_not_update_pypi_plugin_with_exact_version_before_first_loading_attempt(self, install, load): project = Mock() downloader = DownloadingPluginLoader(Mock()) plugin = Mock() load.side_effect = (MissingPluginException("no spam installed"), plugin) self.assertEqual(plugin, downloader.load_plugin(project, "pypi:spam", "===1.4")) install.assert_called_with(project, "pypi:spam", "===1.4", downloader.logger, None) self.assertEqual(install.call_count, 1)
def test_should_execute_tool_on_source_files(self, affected, read, log): project = Mock() project.expand_path.return_value = '/path/to/report' affected.return_value = ['file1', 'file2'] pyb_env = Mock() execute_tool_on_source_files(project, 'name', pyb_env, 'foo --bar') pyb_env.execute_command.assert_called_with( ['foo --bar', 'file1', 'file2'], '/path/to/report')
def setUp(self): self.project = Project('/base/dir') self.reactor = Mock() pyb_env = Mock() self.reactor.python_env_registry = {"pybuilder": pyb_env} self.reactor.pybuilder_venv = pyb_env self.command = ExternalCommandBuilder('command-name', self.project, self.reactor) self.command.use_argument('--foo').use_argument('--bar')
def test_should_only_collect_required_tasks(self): one = Mock(name="one", dependencies=[TaskDependency("three")]) two = Mock(name="two", dependencies=[]) three = Mock(name="three", dependencies=[]) self.execution_manager.register_task(one, two, three) self.execution_manager.resolve_dependencies() self.assertEqual( self.execution_manager.collect_all_transitive_tasks(["one"]), set([one, three]))
def test_should_inform_how_many_tests_are_going_to_be_executed( self, mock_execute_tests_matching): test_definitions = [Mock(), Mock(), Mock()] mock_logger = Mock(Logger) listener = TestListener(mock_logger) listener.before_suite(test_definitions) mock_logger.info.assert_called_with('Running %d pyfix tests', 3)
def test_ensure_that_required_tasks_are_not_optionally_excluded(self): one = Mock(name="one", dependencies=[]) two = Mock(name="two", dependencies=[TaskDependency("one")]) self.execution_manager.register_task(one, two) self.execution_manager.resolve_dependencies( exclude_optional_tasks=["one"]) self.assertRaises(RequiredTaskExclusionException, self.execution_manager.build_execution_plan, "two")
def test_should_raise_exception_when_circular_reference_is_detected_on_two_tasks( self): one = Mock(name="one", dependencies=[TaskDependency("two")]) two = Mock(name="two", dependencies=[TaskDependency("one")]) self.execution_manager.register_task(one, two) self.execution_manager.resolve_dependencies() self.assertRaises(CircularTaskDependencyException, self.execution_manager.build_execution_plan, ["one"])
def setUp(self): self.project = Project("any-project") self.reactor = Mock() pyb_env = Mock() self.reactor.python_env_registry = {"pybuilder": pyb_env} self.reactor.pybuilder_venv = pyb_env self.project.set_property("any-property-name", "any-property-value") self.sonar_builder = SonarCommandBuilder("sonar", self.project, self.reactor)
def test_should_delegate_to_project_get_property_when_attribute_is_not_defined( self): project_mock = Project(".") project_mock.has_property = Mock(return_value=True) project_mock.get_property = Mock(return_value="eggs") self.assertEquals("eggs", ProjectDictWrapper(project_mock, Mock())["spam"]) project_mock.get_property.assert_called_with("spam")
def test_ensure_that_required_tasks_are_force_excluded(self): one = Mock(name="one", dependencies=[]) two = Mock(name="two", dependencies=[TaskDependency("one")]) self.execution_manager.register_task(one, two) self.execution_manager.resolve_dependencies(exclude_tasks=["one"]) execution_plan = self.execution_manager.build_execution_plan("two") self.assertEqual([two], execution_plan)
def test_should_raise_exception_when_requiring_plugin_and_plugin_is_not_found( self, _, load): project = Mock() downloader = DownloadingPluginLoader(Mock()) load.side_effect = MissingPluginException("Load boom") self.assertRaises(MissingPluginException, downloader.load_plugin, project, "spam") load.assert_called_with("spam", "spam")
def setUp(self): self.project = Project("basedir") self.project.set_property("dir_source_main_python", "source") self.project.set_property("dir_reports", "reports") self.reactor = Mock() self.reactor.python_env_registry = {} self.reactor.python_env_registry["pybuilder"] = pyb_env = Mock() pyb_env.environ = {} self.reactor.pybuilder_venv = pyb_env
def test_should_return_execution_plan_with_two_tasks_when_two_tasks_are_to_be_executed( self): one = Mock(name="one", dependencies=[]) two = Mock(name="two", dependencies=[]) self.execution_manager.register_task(one, two) self.execution_manager.resolve_dependencies() self.assertEqual([one, two], self.execution_manager.build_execution_plan( ["one", "two"]))
def test_verify_error_unresolved_late_dependency(self): one = Mock(name="one", dependencies=[]) two = Mock(name="two", dependencies=[TaskDependency("one")]) three = Mock(name="three", dependencies=[TaskDependency("one")]) self.execution_manager.register_task(one, two) self.execution_manager.register_task(three) self.execution_manager.register_late_task_dependencies( {"four": [TaskDependency("three")]}) self.assertRaises(NoSuchTaskException, self.execution_manager.resolve_dependencies)
def test_ensure_tasks_are_executed(self): one = Mock(name="one", dependencies=[]) two = Mock(name="two", dependencies=[]) self.execution_manager.register_task(one, two) self.execution_manager.resolve_dependencies() self.execution_manager.execute_execution_plan([one, two]) one.execute.assert_called_with(ANY, {}, _executable=None) two.execute.assert_called_with(ANY, {}, _executable=None)
def test_ensure_that_tasks_are_not_executed_multiple_times_when_being_dependencies( self): one = Mock(name="one", dependencies=[]) two = Mock(name="two", dependencies=[TaskDependency("one")]) self.execution_manager.register_task(one, two) self.execution_manager.resolve_dependencies() self.assertEqual([one, two], self.execution_manager.build_execution_plan( ["one", "two"]))
def test_should_generate_sphinx_build_command_debug(self): self.project.set_property("sphinx_config_path", "docs/") self.project.set_property("sphinx_source_dir", "docs/") self.project.set_property("sphinx_output_dir", "docs/_build/") self.project.set_property("sphinx_doc_builder", 'JSONx') logger = Mock() logger.threshold = 1 logger.DEBUG = 1 sphinx_build_command = get_sphinx_build_command(self.project, logger, 'JSONx') self.assertEqual(sphinx_build_command, ["sphinx", "-b", "JSONx", "-vvvv", "basedir/docs/", "basedir/docs/_build/"])
def test_sphinx_generate(self, execute_command, mkdir, exists, rmtree, open ): execute_command.return_value = 0 exists.return_value = True sphinx_mock = Mock() sys.modules["sphinx"] = sphinx_mock try: sphinx_mock.version_info = (1, 5, 3, 4, 5) self.project.set_property("sphinx_source_dir", "sphinx_source_dir") self.project.set_property("sphinx_config_path", "sphinx_config_path") self.project.set_property("sphinx_output_dir", "sphinx_output_dir") self.project.set_property("dir_target", "dir_target") self.project.set_property("dir_source_main_python", "dir_source") self.project.set_property("sphinx_project_name", "project_name") self.project.set_property("sphinx_project_conf", {"a": 1, "b": "foo"}) self.project.set_property("sphinx_run_apidoc", True) self.project.set_property("sphinx_doc_builder", ['JSONx', 'pdf']) sphinx_generate(self.project, Mock()) finally: del sys.modules["sphinx"] exists.assert_called_with(nc("basedir/dir_target/sphinx_pyb/apidoc")) rmtree.assert_called_with(nc("basedir/dir_target/sphinx_pyb/apidoc")) mkdir.assert_called_with(nc("basedir/dir_target/sphinx_pyb/apidoc")) print(open().__enter__().write.call_args_list) open().__enter__().write.assert_has_calls([call("a = 1\n"), call("b = 'foo'\n"), call( "\nimport sys\nsys.path.insert(0, %r)\n" % nc("basedir/dir_source"))], any_order=True) execute_command.assert_has_calls([ call([sys.executable, '-m', 'sphinx.apidoc', '-H', 'project_name', '-o', nc('basedir/dir_target/sphinx_pyb/apidoc'), nc('basedir/dir_source')] if sys.version_info[:2] < (3, 3) else [sys.executable, '-m', 'sphinx.apidoc', '-H', 'project_name', '--implicit-namespaces', '-o', nc('basedir/dir_target/sphinx_pyb/apidoc'), nc('basedir/dir_source')], nc('basedir/dir_target/reports/sphinx-apidoc'), shell=False), call([sys.executable, '-m', 'sphinx', '-b', 'JSONx', nc('basedir/sphinx_config_path'), nc('basedir/sphinx_output_dir/JSONx')], nc('basedir/dir_target/reports/sphinx_JSONx'), shell=False), call([sys.executable, '-m', 'sphinx', '-b', 'pdf', nc('basedir/sphinx_config_path'), nc('basedir/sphinx_output_dir/pdf')], nc('basedir/dir_target/reports/sphinx_pdf'), shell=False)])
def test_get_sphinx_apidoc_command_enabled(self): sphinx_mock = Mock() sys.modules["sphinx"] = sphinx_mock try: sphinx_mock.version_info = (1, 2, 3, 4, 5) self.project.set_property("sphinx_run_apidoc", True) self.project.set_property("dir_target", "dir_target") self.project.set_property("dir_source_main_python", "dir_source") self.project.set_property("sphinx_project_name", "project_name") self.assertEqual(get_sphinx_apidoc_command(self.project), ['sphinx.apidoc', '-H', 'project_name', '-o', nc('basedir/dir_target/sphinx_pyb/apidoc'), nc('basedir/dir_source') ] ) finally: del sys.modules["sphinx"]
def test_build_coverage_report_two_module(self, coverage, render_report): execution_name = "mock coverage" execution_prefix = "mock_coverage" project = Mock() modules = [Mock(__name__='module_a', __file__='module_a.py'), Mock(__name__='module_b', __file__='module_b.py') ] project.get_property.side_effect = [70, 70, 70, False, False, False] module_a_coverage = Mock() module_a_coverage.statements = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] module_a_coverage.excluded = [] module_a_coverage.missing = [1, 2, 3, 4, 5] n = module_a_coverage.numbers n.n_statements = 10 n.n_excluded = 0 n.n_missing = 5 n.n_branches = 8 n.n_partial_branches = 5 n.n_missing_branches = 5 module_b_coverage = Mock() module_b_coverage.statements = [1, 2, 3, 4, 5] module_b_coverage.excluded = [] module_b_coverage.missing = [1, 2] n = module_b_coverage.numbers n.n_statements = 4 n.n_excluded = 0 n.n_missing = 2 n.n_branches = 8 n.n_partial_branches = 3 n.n_missing_branches = 3 coverage._analyze.side_effect = [module_a_coverage, module_b_coverage] self.assertTrue(_build_coverage_report(project, MagicMock(Logger), execution_name, execution_prefix, coverage, modules) is None) report = render_report.call_args[0][0] self.assertEquals(report['overall_coverage'], 50) self.assertEquals(report['overall_branch_coverage'], 50) self.assertEquals(report['overall_branch_partial_coverage'], 50)
def test_should_report_passed_tests_to_ci_server(self, teamcity, proxy): teamcity.return_value = False project = Project('basedir') mock_proxy = Mock() proxy.return_value = mock_proxy mock_proxy.and_test_name.return_value = mock_proxy mock_proxy.__enter__ = Mock(return_value=mock_proxy) mock_proxy.__exit__ = Mock(return_value=False) result = Mock() result.test_names = ['test1', 'test2', 'test3'] result.failed_test_names_and_reasons = {} report_to_ci_server(project, result) mock_proxy.fails.assert_not_called()
def test_version_exact_match(self): plugin_module = Mock() pluginloader.PYB_VERSION = Version("1.2.3") plugin_module.pyb_version = "===1.2.3" _check_plugin_version(plugin_module, "test plugin")
def test_version_compatible_match(self): plugin_module = Mock() pluginloader.PYB_VERSION = Version("1.2.3") plugin_module.pyb_version = "~=1.2" _check_plugin_version(plugin_module, "test plugin")
def test_version_multiple_specifier_match(self): plugin_module = Mock() pluginloader.PYB_VERSION = Version("1.2.3") plugin_module.pyb_version = ">=1.2.0,<=1.2.4" _check_plugin_version(plugin_module, "test plugin")
def test_version_no_match(self): plugin_module = Mock() pluginloader.PYB_VERSION = Version("1.2.3") plugin_module.pyb_version = ">=1.2.5" self.assertRaises(IncompatiblePluginException, _check_plugin_version, plugin_module, "test plugin")