def test_create_module_data(self): project = Project.from_dir(Path('/path/to/project'), version='1.3.4') with Options(project=project): module_data = _create_module_data() self._compare_to_file(module_data, 'basic_module')
def test_create_config_object(self): project = Project.from_dir(Path('/path/to/dir')) thing = project._create_config_object(Bob, None) assert thing == Bob() thing = project._create_config_object(Bob, 'bogus') assert thing == Bob() thing = project._create_config_object(Bob, {}) assert thing == Bob() thing = project._create_config_object(Bob, {'name': 'Larry', 'extra': True}) assert thing == Bob('Larry') assert hasattr(thing, 'extra') is True assert thing.extra is True thing = project._create_config_object(Bob, {'age': 12}) assert thing == Bob(age=12) thing = project._create_config_object(Bob, {'name': 'Larry', 'age': 12}) assert thing == Bob('Larry', 12)
def test_format_args_task_config(self): config = {'lib_target': 'library'} project = Project.from_dir(Path('/path/to/project')) engine = Engine(project) module = Language(None, 'java') task = Task('myTask', func_task_config) mock_get_config = MagicMock() mock_get_config.return_value = config project.get_config = mock_get_config # noinspection PyProtectedMember args, kwargs = engine._format_args(module, task) assert args == [config] assert kwargs == {} task.function = func_named_task_config # noinspection PyProtectedMember args, kwargs = engine._format_args(module, task) assert args == [] assert kwargs == {'task_config': config}
def test_add_variant_with_dependencies(self, tmpdir): path = Path(str(tmpdir)) / 'fake.jar' project = Project.from_dir(Path('/path/to/project'), version='1.3.4') path.write_text("Testing\n", encoding='utf-8') with Options(project=project): module_data = _create_module_data() dependency = Dependency('dep', { 'location': 'remote', 'version': '1.2.3', 'scope': 'scope' }) transient = dependency.derive_from('group', 'name', '1.2.4') transient.transient = True path_sets = [ DependencyPathSet(dependency, path), DependencyPathSet(transient, path) ] _add_variant(module_data, API_ELEMENTS, path, {}, 'library', 'api', None, path_sets) self._compare_to_file(module_data, 'variant_2')
def test_no_source(self, tmpdir): project_dir = Path(str(tmpdir)) project = Project.from_dir(project_dir, name='test', version='1.2.3') with Options(project=project): java_config = JavaConfiguration() task_config = PackageConfiguration() task_config.sources = False task_config.doc = False jar_file = java_config.library_dist_dir( ensure=True) / project.name / 'test-1.2.3.jar' _ = java_config.classes_dir(ensure=True) _ = java_config.resources_dir(ensure=True) expected = [ 'jar', '--create', '--file', str(jar_file), '--manifest', Regex('.*'), '-C', Regex('.*/jar_content'), '.' ] with FakeProcessContext(FakeProcess(expected)): with Options(project=project): with patch('builder.java.package.sign_path') as mock_signer: mock_signer.return_value = {} java_package(java_config, task_config, []) mock_signer.assert_called_once_with(jar_file)
def test_sources_no_dir(self, tmpdir): project_dir = Path(str(tmpdir)) project = Project.from_dir(project_dir, name='test', version='1.2.3') with Options(project=project): config = JavaConfiguration() task_config = PackageConfiguration() task_config.doc = False jar_file = config.library_dist_dir( ensure=True) / project.name / 'test-1.2.3.jar' _ = config.classes_dir(ensure=True) _ = config.resources_dir(ensure=True) code_dir = config.code_dir() expected = [ 'jar', '--create', '--file', str(jar_file), '--manifest', Regex('.*'), '-C', Regex('.*/jar_content'), '.' ] with FakeProcessContext(FakeProcess(expected)): with pytest.raises(ValueError) as info: with Options(project=project): with patch( 'builder.java.package.sign_path') as mock_signer: mock_signer.return_value = {} java_package(config, task_config, []) mock_signer.assert_called_once_with(jar_file) assert info.value.args[ 0] == f'Cannot build a sources archive since {code_dir} does not exist.'
def _test_java_doc(self, tmpdir, verbose: int): project_dir = Path(str(tmpdir)) project = Project.from_dir(project_dir) with Options(project=project): config = JavaConfiguration() # These need to exist for the test to work. code_dir = config.code_dir(ensure=True) doc_dir = config.doc_dir(ensure=True) self._create_fake_source_structure(code_dir) expected = [ 'javadoc', '-d', str(doc_dir), '--source-path', str(code_dir), 'com.test.pkg', 'com.test.pkg.sub' ] if verbose == 0: expected.insert(1, '-quiet') with FakeProcessContext(FakeProcess(expected)): with Options(verbose=verbose): java_doc(config, [])
def test_plain_project_dir(self, tmpdir): root = Path(tmpdir) expected = root / 'dir' project = Project.from_dir(root) directory = project.project_dir(Path('dir')) assert directory == expected
def test_get_nonexistent_config(self): project = Project.from_dir(Path('/path/to/dir')) config = project.get_config('testing') assert config == {} # Make sure caching works. assert project.get_config('testing') is config
def test_engine_construction(self): project = Project.from_dir(Path('/path/to/project')) engine = Engine(project) validate_attributes(engine, { '_project': project, '_module_set': project.get_module_set(), '_rc': 0 })
def test_project(self, tmpdir): options = GlobalOptions() project = Project.from_dir(Path(tmpdir)) assert options.project() is None options.set_project(project) assert options.project() is project
def test_project_description(self): project = Project.from_dir(Path('/path/to/dir')) assert project.description == 'dir' # noinspection PyProtectedMember project._info['title'] = 'The Title' assert project.description == 'dir -- The Title'
def test_get_var_value(self): project = Project.from_dir(Path('/path/to/dir')) assert project.get_var_value('variable') is None # noinspection PyProtectedMember project._content['vars'] = {'variable': 'the value'} assert project.get_var_value('variable') == 'the value'
def test_create_project_from_directory(self): path = Path('/path/to/dir') project = Project.from_dir(path) # noinspection PyProtectedMember assert project._content == { 'info': { 'name': 'dir', 'version': '0.0.1', 'languages': [] }, 'vars': {} } project = Project.from_dir(path, name='test') # noinspection PyProtectedMember assert project._content == { 'info': { 'name': 'test', 'version': '0.0.1', 'languages': [] }, 'vars': {} } project = Project.from_dir(path, version='1.2.3') # noinspection PyProtectedMember assert project._content == { 'info': { 'name': 'dir', 'version': '1.2.3', 'languages': [] }, 'vars': {} } project = Project.from_dir(path, language='java') # noinspection PyProtectedMember assert project._content == { 'info': { 'name': 'dir', 'version': '0.0.1', 'languages': ['java'] }, 'vars': {} }
def test_format_args_no_args(self): project = Project.from_dir(Path('/path/to/project')) engine = Engine(project) module = Language(None, 'java') task = Task('myTask', func_no_args) # noinspection PyProtectedMember args, kwargs = engine._format_args(module, task) assert args == [] assert kwargs == {}
def test_has_no_languages(self): project = Project.from_dir(Path('/path/to/dir')) assert project.has_no_languages() is True project = Project.from_file(Path(get_test_path('project') / 'good_project.yaml')) assert project.has_no_languages() is False project = Project.from_file(Path(get_test_path('project') / 'good_project_unknown_language.yaml')) assert project.has_no_languages() is False
def test_get_config_with_schema(self): project = Project.from_dir(Path('/path/to/dir')) int_validator = SchemaValidator({'type': 'integer'}) str_validator = SchemaValidator({'type': 'string'}) # noinspection PyProtectedMember project._content['name'] = 'Bob' with pytest.raises(ValueError) as info: project.get_config('name', schema=int_validator) assert info.value.args[0] == 'Configuration for "name" is not valid: #/name violates the "type" constraint: ' \ 'it is not an integer.' assert project.get_config('name', str_validator) == 'Bob'
def test_add_variant_no_dependencies(self, tmpdir): path = Path(str(tmpdir)) / 'fake.jar' project = Project.from_dir(Path('/path/to/project'), version='1.3.4') path.write_text("Testing\n", encoding='utf-8') with Options(project=project): module_data = _create_module_data() _add_variant(module_data, API_ELEMENTS, path, {}, 'library', 'api', None, []) self._compare_to_file(module_data, 'variant_1')
def test_get_tasks_in_execution_order_independent(self): project = Project.from_dir(Path('/path/to/project'), language='java') ms = project.get_module_set() engine = Engine(project) with patch('builder.engine.global_options') as go: go.tasks.return_value = ['test', 'compile'] go.independent_tasks.return_value = True # noinspection PyProtectedMember tasks = engine._get_tasks_in_execution_order() assert tasks == [ms.get_task('test'), ms.get_task('compile')]
def test_get_task_config(self): config = {'field': 'value'} project = Project.from_dir(Path('/path/to/project')) engine = Engine(project) task = Task('myTask', None) mock_get_config = MagicMock() mock_get_config.return_value = config project.get_config = mock_get_config # noinspection PyProtectedMember assert engine._get_task_config(task) == config assert mock_get_config.mock_calls == [call('myTask', None, None)]
def test_get_config_with_class(self): project = Project.from_dir(Path('/path/to/dir')) # noinspection PyProtectedMember project._content['bob'] = { 'name': 'Bob', 'age': 12 } thing = project.get_config('bob', config_class=Bob) assert isinstance(thing, Bob) is True assert thing.name == 'Bob' assert thing.age == 12 assert project.get_config('bob') is thing
def test_simple_java_compile(self, tmpdir): project_dir = Path(str(tmpdir)) project = Project.from_dir(project_dir) with Options(project=project): config = JavaConfiguration() # This needs to exist for the test to work. _ = config.code_dir(ensure=True) expected = ['javac', '-d', str(config.classes_dir()), Regex(r'@.*')] with FakeProcessContext(FakeProcess(expected)): java_compile(config, [])
def test_get_language_config(self): config = {'lib_target': 'library'} project = Project.from_dir(Path('/path/to/project')) engine = Engine(project) module = Language(None, 'java') mock_get_config = MagicMock() mock_get_config.return_value = config project.get_config = mock_get_config # noinspection PyProtectedMember assert engine._get_language_config(module) == config assert mock_get_config.mock_calls == [call('java', None, None)]
def test_show_existing_tasks(self): project = Project.from_dir(Path('/path/to/project')) engine = Engine(project) mock_print = MagicMock() project.get_module_set().print_available_tasks = mock_print with patch('builder.engine.warn') as mock_warn: with patch('builder.engine.out') as mock_out: assert engine.run() == 1 assert mock_warn.mock_calls == [ call('No tasks specified. Available tasks are:', None) ] assert mock_out.mock_calls == [call()] assert mock_print.mock_calls == [call()]
def test_required_project_dir(self, tmpdir): root = Path(tmpdir) expected = root / 'dir' project = Project.from_dir(root) with pytest.raises(ValueError) as error: project.project_dir(Path('dir'), required=True) assert error.value.args[0] == f'Required directory, {expected}, does not exist or is not a directory.' expected.mkdir() directory = project.project_dir(Path('dir'), required=True) assert directory == expected
def test_expected_project_dir(self, tmpdir): root = Path(tmpdir) expected = root / 'dir' project = Project.from_dir(root) assert expected.is_dir() is False directory = project.project_dir(Path('dir'), ensure=True) assert directory == expected assert directory.is_dir() is True directory = project.project_dir(Path('dir'), ensure=True) assert directory == expected directory = project.project_dir(Path('dir'), required=True) assert directory == expected
def test_sources_with_dir(self, tmpdir): project_dir = Path(str(tmpdir)) project = Project.from_dir(project_dir, name='test', version='1.2.3') with Options(project=project): config = JavaConfiguration() task_config = PackageConfiguration() task_config.doc = False jar_file = config.library_dist_dir( ensure=True) / project.name / 'test-1.2.3.jar' sources_jar_file = config.library_dist_dir( ) / project.name / 'test-1.2.3-sources.jar' _ = config.classes_dir(ensure=True) resources_dir = config.resources_dir(ensure=True) code_dir = config.code_dir(ensure=True) expected_jar = [ 'jar', '--create', '--file', str(jar_file), '--manifest', Regex('.*'), '-C', Regex('.*/jar_content'), '.' ] expected_sources = [ 'jar', '--create', '--file', str(sources_jar_file), '-C', str(code_dir), '.', '-C', str(resources_dir), '.' ] with FakeProcessContext( [FakeProcess(expected_jar), FakeProcess(expected_sources)]): with Options(project=project): with patch('builder.java.package.sign_path') as mock_signer: mock_signer.return_value = {} java_package(config, task_config, []) assert mock_signer.mock_calls == [ call(jar_file), call(sources_jar_file) ]
def _test_for_type(tmpdir, project_type, dist_dir): project_dir = Path(str(tmpdir)) project = Project.from_dir(project_dir) code_dir = project_dir / Path('src/code') classes_dir = project_dir / Path('build/code/classes') doc_dir = project_dir / Path('build/code/javadoc') resources_dir = project_dir / Path('src/resources') output_dir = project_dir / Path(f'dist/{dist_dir}') expected = (code_dir, classes_dir, doc_dir, resources_dir, output_dir) classes_dir.mkdir(parents=True) doc_dir.mkdir(parents=True) with Options(project=project): config = JavaConfiguration() config.type = project_type result = _get_packaging_dirs(config) assert isinstance(result, tuple) assert result == expected
def test_format_args_dependencies(self): project = Project.from_dir(Path('/path/to/project')) engine = Engine(project) module = Language(None, 'java') task = Task('myTask', func_dependencies) module.resolver = MagicMock(return_value=None) # noinspection PyProtectedMember args, kwargs = engine._format_args(module, task) assert args == [[]] assert kwargs == {} task.function = func_named_dependencies # noinspection PyProtectedMember args, kwargs = engine._format_args(module, task) assert args == [] assert kwargs == {'dependencies': []}
def test_format_args_dependencies_not_accepted(self): project = Project.from_dir(Path('/path/to/project')) dependency = Dependency('name', { 'location': 'remote', 'version': '1.2.3', 'scope': 'myTask' }) project.get_dependencies()._dependencies = {'name': dependency} engine = Engine(project) module = Language(None, 'java') task = Task('myTask', func_no_args) with patch('builder.engine.end') as mock_end: # noinspection PyProtectedMember _, _ = engine._format_args(module, task) assert mock_end.mock_calls == [ call( 'Dependencies were specified for task myTask but it does not accept dependencies.' ) ]