def test_greppy_summary( cloned_repos: PathPlus, advanced_data_regression: AdvancedDataRegressionFixture, capsys, search_term: str, fixed_sort_order, ): runner = CliRunner(mix_stderr=False) result = runner.invoke(main, args=[search_term, "--dir", cloned_repos.as_posix()]) data = {} data["stdout"] = result.stdout.replace(cloned_repos.as_posix(), "...") data["stderr"] = result.stderr.replace(cloned_repos.as_posix(), "...") advanced_data_regression.check(data)
def test_build_additional_files( tmp_pathplus: PathPlus, advanced_data_regression: AdvancedDataRegressionFixture, file_regression: FileRegressionFixture, capsys, ): (tmp_pathplus / "pyproject.toml").write_lines([ COMPLETE_A, '', "additional-wheel-files = [", ' "include _virtualenv.py",', ']', ]) (tmp_pathplus / "whey").mkdir() (tmp_pathplus / "whey" / "__init__.py").write_clean("print('hello world)") (tmp_pathplus / "_virtualenv.py").write_clean("This is the _virtualenv.py file") (tmp_pathplus / "README.rst").write_clean("Spam Spam Spam Spam") (tmp_pathplus / "LICENSE").write_clean("This is the license") (tmp_pathplus / "requirements.txt").write_clean("domdf_python_tools") data = {} with tempfile.TemporaryDirectory() as tmpdir: wheel_builder = PthWheelBuilder( project_dir=tmp_pathplus, config=load_toml(tmp_pathplus / "pyproject.toml"), build_dir=tmpdir, out_dir=tmp_pathplus, verbose=True, colour=False, ) wheel = wheel_builder.build_wheel() assert (tmp_pathplus / wheel).is_file() zip_file = zipfile.ZipFile(tmp_pathplus / wheel) data["wheel_content"] = sorted(zip_file.namelist()) with zip_file.open("whey/__init__.py", mode='r') as fp: assert fp.read().decode("UTF-8") == "print('hello world)\n" with zip_file.open("whey-2021.0.0.dist-info/METADATA", mode='r') as fp: check_file_regression(fp.read().decode("UTF-8"), file_regression) with zip_file.open("my_project.pth", mode='r') as fp: assert fp.read().decode("UTF-8") == "import _virtualenv\n" with zip_file.open("_virtualenv.py", mode='r') as fp: assert fp.read().decode( "UTF-8") == "This is the _virtualenv.py file\n" outerr = capsys.readouterr() data["stdout"] = outerr.out.replace(tmp_pathplus.as_posix(), "...") data["stderr"] = outerr.err advanced_data_regression.check(data)
def test_greppy_non_utf8( tmp_pathplus: PathPlus, advanced_data_regression: AdvancedDataRegressionFixture, fixed_sort_order, ): (tmp_pathplus / "my_package").mkdir() (tmp_pathplus / "my_package" / "__init__.py").write_lines( ["# „…†‡ˆ‰Š", "def foo(path: PathPlus, name: str) -> int: ..."], encoding="cp1252", ) runner = CliRunner(mix_stderr=False) result = runner.invoke(main, args=["foo", "--dir", tmp_pathplus.as_posix()]) data = {} data["stdout"] = result.stdout.replace(tmp_pathplus.as_posix(), "...") data["stderr"] = result.stderr.replace(tmp_pathplus.as_posix(), "...") advanced_data_regression.check(data)
def test_greppy_summary( cloned_repos: PathPlus, advanced_data_regression: AdvancedDataRegressionFixture, capsys, search_term: str, fixed_sort_order, ): data = {} greppy(search_term, cloned_repos, summary=True) outerr = capsys.readouterr() data["stdout"] = outerr.out.replace(cloned_repos.as_posix(), "...") data["stderr"] = outerr.err.replace(cloned_repos.as_posix(), "...") advanced_data_regression.check(data)
def test_greppy_simple( tmp_pathplus: PathPlus, advanced_data_regression: AdvancedDataRegressionFixture, capsys, fixed_sort_order, ): (tmp_pathplus / "my_package").mkdir() (tmp_pathplus / "my_package" / "__init__.py").write_lines( ["def foo(path: PathPlus, name: str) -> int: ..."]) (tmp_pathplus / "my_package" / "extension.c").write_lines(["# foo"]) data = {} greppy("foo", tmp_pathplus) outerr = capsys.readouterr() data["stdout"] = outerr.out.replace(tmp_pathplus.as_posix(), "...") data["stderr"] = outerr.err.replace(tmp_pathplus.as_posix(), "...") advanced_data_regression.check(data)
def test_greppy_simple( tmp_pathplus: PathPlus, advanced_data_regression: AdvancedDataRegressionFixture, fixed_sort_order, ): (tmp_pathplus / "my_package").mkdir() (tmp_pathplus / "my_package" / "__init__.py").write_lines(["def foo(path: PathPlus, name: str) -> int: ..."]) (tmp_pathplus / "my_package" / "extension.c").write_lines(["# foo"]) runner = CliRunner(mix_stderr=False) result = runner.invoke(main, args=["foo", "--dir", tmp_pathplus.as_posix()]) data = {} data["stdout"] = result.stdout.replace(tmp_pathplus.as_posix(), "...") data["stderr"] = result.stderr.replace(tmp_pathplus.as_posix(), "...") advanced_data_regression.check(data) result = runner.invoke(main, args=["FOO", "--ignore-case", "--dir", tmp_pathplus.as_posix()]) data = {} data["stdout"] = result.stdout.replace(tmp_pathplus.as_posix(), "...") data["stderr"] = result.stderr.replace(tmp_pathplus.as_posix(), "...") advanced_data_regression.check(data) result = runner.invoke(main, args=["FOO", "-i", "--dir", tmp_pathplus.as_posix()]) data = {} data["stdout"] = result.stdout.replace(tmp_pathplus.as_posix(), "...") data["stderr"] = result.stderr.replace(tmp_pathplus.as_posix(), "...") advanced_data_regression.check(data)
class Reformatter: """ Reformat a Python source file. :param filename: The filename to reformat. :param config: The ``formate`` configuration, parsed from a TOML file (or similar). .. autosummary-widths:: 5/16 11/16 """ #: The filename being reformatted. filename: str #: The filename being reformatted, as a POSIX-style path. file_to_format: PathPlus #: The ``formate`` configuration, parsed from a TOML file (or similar). config: FormateConfigDict def __init__(self, filename: PathLike, config: FormateConfigDict): self.file_to_format = PathPlus(filename) self.filename = self.file_to_format.as_posix() self.config = config self._unformatted_source = self.file_to_format.read_text() self._reformatted_source: Optional[str] = None def run(self) -> bool: """ Run the reformatter. :return: Whether the file was changed. """ hooks = parse_hooks(self.config) reformatted_source = StringList(call_hooks(hooks, self._unformatted_source, self.filename)) reformatted_source.blankline(ensure_single=True) self._reformatted_source = str(reformatted_source) return self._reformatted_source != self._unformatted_source def get_diff(self) -> str: """ Returns the diff between the original and reformatted file content. """ # Based on yapf # Apache 2.0 License after = self.to_string().split('\n') before = self._unformatted_source.split('\n') return coloured_diff( before, after, self.filename, self.filename, "(original)", "(reformatted)", lineterm='', ) def to_string(self) -> str: """ Return the reformatted file as a string. :rtype: .. latex:clearpage:: """ if self._reformatted_source is None: raise ValueError("'Reformatter.run()' must be called first!") return self._reformatted_source def to_file(self) -> None: """ Write the reformatted source to the original file. """ self.file_to_format.write_text(self.to_string())
def test_build_complete_foreman( config: str, tmp_pathplus: PathPlus, advanced_data_regression: AdvancedDataRegressionFixture, file_regression: FileRegressionFixture, capsys, ): (tmp_pathplus / "pyproject.toml").write_clean(config) (tmp_pathplus / "whey").mkdir() (tmp_pathplus / "whey" / "__init__.py").write_clean("print('hello world)") (tmp_pathplus / "README.rst").write_clean("Spam Spam Spam Spam") (tmp_pathplus / "LICENSE").write_clean("This is the license") (tmp_pathplus / "requirements.txt").write_clean("domdf_python_tools") data = {} foreman = Foreman(project_dir=tmp_pathplus) with tempfile.TemporaryDirectory() as tmpdir: wheel = foreman.build_wheel( build_dir=tmpdir, out_dir=tmp_pathplus, verbose=True, colour=False, ) assert (tmp_pathplus / wheel).is_file() zip_file = zipfile.ZipFile(tmp_pathplus / wheel) data["wheel_content"] = sorted(zip_file.namelist()) with zip_file.open("whey/__init__.py", mode='r') as fp: assert fp.read().decode("UTF-8") == "print('hello world)\n" with zip_file.open("whey-2021.0.0.dist-info/METADATA", mode='r') as fp: check_file_regression(fp.read().decode("UTF-8"), file_regression) if config != WHEY_NO_PTH: with zip_file.open("my_project.pth", mode='r') as fp: assert fp.read().decode("UTF-8") == "import _virtualenv\n" with tempfile.TemporaryDirectory() as tmpdir: sdist = foreman.build_sdist( build_dir=tmpdir, out_dir=tmp_pathplus, verbose=True, colour=False, ) assert (tmp_pathplus / sdist).is_file() tar = tarfile.open(tmp_pathplus / sdist) data["sdist_content"] = sorted(tar.getnames()) with tar.extractfile( "whey-2021.0.0/whey/__init__.py") as fp: # type: ignore assert fp.read().decode("UTF-8") == "print('hello world)\n" with tar.extractfile("whey-2021.0.0/README.rst") as fp: # type: ignore assert fp.read().decode("UTF-8") == "Spam Spam Spam Spam\n" with tar.extractfile("whey-2021.0.0/LICENSE") as fp: # type: ignore assert fp.read().decode("UTF-8") == "This is the license\n" with tar.extractfile( "whey-2021.0.0/requirements.txt") as fp: # type: ignore assert fp.read().decode("UTF-8") == "domdf_python_tools\n" outerr = capsys.readouterr() data["stdout"] = outerr.out.replace(tmp_pathplus.as_posix(), "...") data["stderr"] = outerr.err advanced_data_regression.check(data)
class Reformatter: """ Reformat a Python source file. :param filename: :param yapf_style: The name of the yapf style, or the path to the yapf style file. :param isort_config: The filename of the isort configuration file. """ def __init__(self, filename: PathLike, yapf_style: str, isort_config: Config): self.file_to_format = PathPlus(filename) self.filename = self.file_to_format.as_posix() self.yapf_style = yapf_style self.isort_config = isort_config self._unformatted_source = self.file_to_format.read_text() self._reformatted_source: Optional[str] = None def run(self) -> bool: """ Run the reformatter. :return: Whether the file was changed. """ quote_formatted_code = reformat_quotes(self._unformatted_source) yapfed_code = FormatCode(quote_formatted_code, style_config=self.yapf_style)[0] generic_formatted_code = reformat_generics(yapfed_code) # TODO: support spaces try: isorted_code = StringList( isort.code(generic_formatted_code, config=self.isort_config)) except FileSkipComment: isorted_code = StringList(generic_formatted_code) isorted_code.blankline(ensure_single=True) self._reformatted_source = str(isorted_code) # Fix for noqa comments being pushed to new line self._reformatted_source = noqa_reformat(self._reformatted_source) return self._reformatted_source != self._unformatted_source def get_diff(self) -> str: """ Returns the diff between the original and reformatted file content. """ # Based on yapf # Apache 2.0 License if self._reformatted_source is None: raise ValueError("'Reformatter.run()' must be called first!") before = self._unformatted_source.splitlines() after = self._reformatted_source.splitlines() return coloured_diff( before, after, self.filename, self.filename, "(original)", "(reformatted)", lineterm='', ) def to_string(self) -> str: """ Return the reformatted file as a string. """ if self._reformatted_source is None: raise ValueError("'Reformatter.run()' must be called first!") return self._reformatted_source def to_file(self) -> None: """ Write the reformatted source to the original file. """ if self._reformatted_source is None: raise ValueError("'Reformatter.run()' must be called first!") self.file_to_format.write_text(self._reformatted_source)