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_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_describe_classes_no_classes(self): directory = get_test_path('java/javap') with FakeProcessContext([]): result = describe_classes(directory, True) assert len(result) == 0
def test_good_javac(self): java_directory = Path('src') classes_directory = Path('classes') expected = ['javac', '-d', str(classes_directory), Regex(r'@.*')] with FakeProcessContext(FakeProcess(expected)): run_compiler(java_directory, classes_directory, [])
def test_allowed_rc(self): cmd_line = ['ls', '-l'] with FakeEcho(None) as fe: with FakeProcessContext(FakeProcess(cmd_line, rc=2)): checked_run(cmd_line, 'Testing', allowed_rcs=[2]) assert not fe.was_called()
def test_javac_version_call_failure(self): # noinspection PyUnusedLocal def failing_call(args: Sequence[str], capture: bool, cwd: Path) -> CompletedProcess: raise FileNotFoundError('no javac!') with FakeProcessContext(failing_call): assert get_javac_version() == (None, None)
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_verbose_output(self): cmd_line = ['ls', '-l'] with Options(verbose=1): with FakeEcho.simple('Running: ls -l', fg='green') as fe: with FakeProcessContext(FakeProcess(cmd_line)): checked_run(cmd_line, 'Testing') assert fe.was_called()
def test_one_entry_point_found(self): path = Path('.') # Doesn't matter what it is. process = FakeProcess( None, get_test_path('java/javap/one-class-with-main.txt'), check_args=False) with FakeProcessContext(process): assert _find_entry_point(path, None) == 'com.example.ui.UIUtils'
def test_specified_entry_point_matches_many_discovered(self): path = Path('.') # Doesn't matter what it is. process = FakeProcess( None, get_test_path('java/javap/many-classes-multi-mains.txt'), check_args=False) with FakeProcessContext(process): assert _find_entry_point(path, 'com.example.App') == 'com.example.App'
def test_javac_compile_error(self): java_directory = Path('src') classes_directory = Path('classes') expected = ['javac', '-d', str(classes_directory), Regex(r'@.*')] with FakeProcessContext(FakeProcess(expected, rc=1)): with pytest.raises(ValueError) as info: run_compiler(java_directory, classes_directory, []) assert info.value.args[0] == 'Java source could not be compiled.'
def test_run_javap_with_public(self): directory = Path('.') class_file = Path('MyClass.class') expected_args = ['javap', '-public', str(class_file)] with FakeProcessContext( FakeProcess(expected_args, "line 1\nline 2", cwd=directory)): result = _run_describer(directory, [class_file], True) assert result == ['line 1', 'line 2']
def test_run_javap_with_verbose(self): directory = Path('.') class_file = Path('MyClass.class') expected_args = ['javap', '-verbose', str(class_file)] with Options(verbose=3): with FakeProcessContext( FakeProcess(expected_args, "line 1\nline 2", cwd=directory)): result = _run_describer(directory, [class_file], False) assert result == ['line 1', 'line 2']
def test_failure_output(self): cmd_line = ['ls', '-l'] expected = [ ExpectedEcho(r'ERROR: Testing failed with return code 2\.', fg='bright_red'), ExpectedEcho(r'ERROR: Command line: ls -l', fg='bright_red') ] with FakeEcho(expected) as fe: with FakeProcessContext(FakeProcess(cmd_line, rc=2)): with pytest.raises(SystemExit): checked_run(cmd_line, 'Testing') assert fe.was_called()
def test_no_entry_point_found(self): path = Path('.') # Doesn't matter what it is. process = FakeProcess( None, get_test_path('java/javap/one-class-no-main.txt'), check_args=False) with FakeProcessContext(process): with pytest.raises(ValueError) as info: _find_entry_point(path, None) assert info.value.args[ 0] == 'No entry point found for the application.'
def test_specified_entry_point_not_found_one_discovered(self): path = Path('.') # Doesn't matter what it is. process = FakeProcess( None, get_test_path('java/javap/one-class-with-main.txt'), check_args=False) with FakeProcessContext(process): with pytest.raises(ValueError) as info: _find_entry_point(path, 'com.bad.EntryPoint') assert info.value.args[ 0] == 'Specified entry point com.bad.EntryPoint not found in compiled classes.'
def test_too_many_entry_points_found(self): path = Path('.') # Doesn't matter what it is. process = FakeProcess( None, get_test_path('java/javap/many-classes-multi-mains.txt'), check_args=False) with FakeProcessContext(process): with pytest.raises(ValueError) as info: _find_entry_point(path, None) assert info.value.args[0] == 'Too many entry points found: com.example.ui.UIUtils, com.example.App. You ' \ 'will need to specify one.'
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_describe_classes_with_classes(self): directory = get_test_path('java/classes') class_file = Path('Fake.class') stdout = get_test_path('java/javap/one-class-with-main.txt') expected_args = ['javap', '-public', str(class_file)] with FakeProcessContext(FakeProcess(expected_args, stdout)): result = describe_classes(directory, True) assert len(result) == 1 java_class = result[0] assert isinstance(java_class, JavaClass) assert java_class.type() == 'class' assert java_class.name() == 'com.example.ui.UIUtils' assert java_class.is_entry_point()
def test_jar_no_manifest(self): jar_file = Path('file.jar') directory = Path('classes') expected = [ 'jar', '--create', '--file', str(jar_file), '-C', str(directory), '.' ] with FakeProcessContext(FakeProcess(expected)): with patch('builder.java.package.sign_path') as mock_signer: mock_signer.return_value = {} result = _run_packager(None, None, jar_file, directory, None) mock_signer.assert_called_once_with(jar_file) assert result == {}
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_jar_with_resources(self): jar_file = Path('file.jar') directory = Path('classes') resources = get_test_path('java/javap') expected = [ 'jar', '--create', '--file', str(jar_file), '--manifest', Regex('.*'), '-C', str(directory), '.', '-C', str(resources), '.' ] manifest = _create_manifest('1.2.3', 'my desc') with FakeProcessContext(FakeProcess(expected)): with patch('builder.java.package.sign_path') as mock_signer: mock_signer.return_value = {} result = _run_packager(manifest, None, jar_file, directory, resources) mock_signer.assert_called_once_with(jar_file) assert result == {}
def test_working_javac_version_call(self): for version in ['14', '14.3', '14.2.1']: process = FakeProcess(['javac', '-version'], stdout=f'Java {version}\n') with FakeProcessContext(process): assert get_javac_version() == (version, 14)