def test_scripts_processor(self, mock_compile): logger: Logger = Mock() source_path = file_path_mock(io.StringIO("some line"), stem='script_fname') dest = io.BytesIO() target_path = file_path_mock(dest) target_dir: Path = Mock() target_dir.joinpath.return_value = target_path script: SourceScript = MagicMock(relative_path=Path("rel source"), script_path=source_path) dp: DirectiveProvider = Mock() target_path.relative_to.side_effect = [Path("rel target")] try: sp = script_set_processor.ScriptSetProcessor(logger, target_dir, "3.7", dp, [script]) sp.process() except Exception as e: raise self.assertEqual([ call.debug('Scripts to process: %s', [script]), call.debug('Parsing script: %s (%s)', Path("rel source"), script), call.debug('Temp output script is: %s (%s)', target_path, []), call.debug('Writing temp script: %s (%s)', Path('rel target'), target_path), ], logger.mock_calls) self.assertEqual([call(str(source_path), doraise=True)], mock_compile.mock_calls) self.assertEqual( b'# parsed by py4lo (https://github.com/jferard/py4lo)\nsome line', dest.getbuffer()) verify_open_path(self, source_path, 'r', encoding='utf-8')
def test_load_toml_python_exe(self): default_toml = file_path_mock(io.StringIO("python_exe='man'")) local_toml = file_path_mock(io.StringIO("b=2")) tdata = load_toml(default_toml, local_toml, {}) self.assertTrue({ 'python_exe': 'man', 'b': 2, 'log_level': 'INFO' }.items() <= tdata.items()) self.assertEqual({'python_exe', 'b', 'log_level', 'python_version'}, set(tdata.keys())) verify_open_path(self, default_toml, 'r', encoding="utf-8") verify_open_path(self, local_toml, 'r', encoding="utf-8")
def test_loader(self): default_toml = file_path_mock(io.StringIO("a=1")) local_toml = file_path_mock(io.StringIO("b=2")) tdata = TomlLoader(default_toml, local_toml, {}).load() self.assertTrue({ 'a': 1, 'b': 2, 'log_level': 'INFO' }.items() <= tdata.items()) self.assertEqual( {'a', 'b', 'log_level', 'python_exe', 'python_version'}, set(tdata.keys())) verify_open_path(self, default_toml, 'r', encoding="utf-8") verify_open_path(self, local_toml, 'r', encoding="utf-8")
def test_destinations_assets(self): path = file_path_mock(io.BytesIO(b'asset content')) assets_dir: Path = Mock() self._destinations.assets_dest_dir.joinpath.return_value = Path( "dest_s") self.assertEqual( [DestinationAsset(path=Path('dest_s'), content=b'asset content')], self._destinations.to_destination_assets( [SourceAsset(path, assets_dir)]))
def test_execute(self): proc = Mock() fpath = file_path_mock(io.BytesIO(b"content")) self._opt.joinpath.return_value = fpath fpath.is_dir.return_value = False self.assertEqual(True, self._directive.execute(proc, None, ["a/b.py"])) self.assertEqual([ call.add_script(TempScript(fpath, b"content", self._opt, [], None)) ], proc.mock_calls) verify_open_path(self, fpath, 'rb')
def test_exception(self): default_toml = file_path_error_mock() local_toml = file_path_mock(io.StringIO("b = 2")) err_bkp = sys.stderr sys.stderr = io.StringIO() tdata = load_toml(default_toml, local_toml, {}) self.assertRegex(sys.stderr.getvalue(), "Error when loading toml file") sys.stderr = err_bkp self.assertEqual({'b', 'log_level', 'python_exe', 'python_version'}, set(tdata.keys())) verify_open_path(self, local_toml, 'r', encoding="utf-8")
def test_one_line_function(self): self._dp.ignore_lines.return_value = False path = file_path_mock(io.StringIO("def f(x): return x")) sp = script_set_processor._ContentParser(self._logger, self._dp, path) self.assertEqual( ParsedScriptContent( """# parsed by py4lo (https://github.com/jferard/py4lo) def f(x): return x g_exportedScripts = (f,)""", ["f"]), sp.parse(True)) self.verify_open(path)
def test_script_parser_directve_line(self): self._dp.process_line.return_value = ["line1", "line2"] path = file_path_mock(io.StringIO("#some line")) sp = script_set_processor._ContentParser(self._logger, self._dp, path) self.assertEqual( ParsedScriptContent( '# parsed by py4lo (https://github.com/jferard/py4lo)\nline1\nline2', []), sp.parse(True)) self.assertEqual([], self._logger.mock_calls) self.assertEqual([call.process_line('#some line'), call.end()], self._dp.mock_calls) self.verify_open(path)
def test_script_parser_normal_line_ignore(self): self._dp.ignore_lines.return_value = True path = file_path_mock(io.StringIO("some line")) sp = script_set_processor._ContentParser(self._logger, self._dp, path) self.assertEqual( ParsedScriptContent( '# parsed by py4lo (https://github.com/jferard/py4lo)\n### py4lo ignore: some line', []), sp.parse(True)) self.assertEqual([], self._logger.mock_calls) self.assertEqual([call.ignore_lines(), call.end()], self._dp.mock_calls) self.verify_open(path)
def test_with_strip(self): proc = Mock() path = Mock() inc_path = file_path_mock(io.StringIO('"""docstring"""\n\'\'\'\nother docstring\'\'\'\n #comment\nsome line')) path.joinpath.return_value = inc_path inc_path.__str__.return_value = "[to inc]" d = Include(path) self.assertEqual(["include"], d.sig_elements()) self.assertEqual(True, d.execute(None, proc, ["a.py", "True"])) self.assertEqual([call.append( '# begin py4lo include: [to inc]\nsome line\n# end py4lo include\n')], proc.mock_calls) verify_open_path(self, inc_path, 'r', encoding="utf-8")
def test(self): logger: Logger = Mock() dp: DirectiveProcessor = Mock() source_script: SourceScript = Mock(relative_path="fname", script_path=file_path_mock( io.StringIO("some line")), export_funcs=True) target_dir: Path = Mock() target_dir.joinpath.side_effect = [Path('t')] sp = ScriptProcessor(logger, dp, source_script, target_dir) sp.parse_script() self.assertEqual([ call.debug('Parsing script: %s (%s)', 'fname', source_script), call.debug('Temp output script is: %s (%s)', Path('t'), []) ], logger.mock_calls) self.assertEqual([call.ignore_lines(), call.end()], dp.mock_calls) verify_open_path(self, source_script.script_path, 'r', encoding='utf-8')