def test_output(file_regression: FileRegressionFixture, capsys): with pytest.raises(SystemExit): cli.main([str(bad_code), "--select", "F401,F404,F821,F701,E303", "--format", "github"]) stdout = capsys.readouterr().out.replace(str(bad_code), "bad_code.py") check_file_regression(stdout, file_regression) assert not capsys.readouterr().err
def check_html_regression(page: BeautifulSoup, file_regression: FileRegressionFixture): """ Check an HTML page generated by Sphinx for regressions, using `pytest-regressions <https://pypi.org/project/pytest-regressions/>`_ :param page: The page to test. :param file_regression: The file regression fixture. **Example usage** .. code-block:: python @pytest.mark.parametrize("page", ["index.html"], indirect=True) def test_page(page: BeautifulSoup, file_regression: FileRegressionFixture): check_html_regression(page, file_regression) """ # noqa D400 __tracebackhide__ = True page = remove_html_footer(page) page = remove_html_link_tags(page) for div in page.select("script"): if "_static/language_data.js" in str(div): div.extract() check_file_regression( StringList(page.prettify()), file_regression, extension=".html", )
def test_rewrite_readme( tmp_pathplus, demo_environment, file_regression: FileRegressionFixture, filename, fixed_date, ): demo_environment.globals["version"] = "1.2.3" demo_environment.globals["enable_docs"] = True demo_environment.globals["docker_shields"] = False demo_environment.globals["docker_name"] = '' demo_environment.globals["enable_pre_commit"] = True demo_environment.globals["license"] = "MIT" demo_environment.globals["primary_conda_channel"] = "octocat" readme_file = tmp_pathplus / "README.rst" with resource(tests.test_files.test_readme_input, filename) as p: readme_file.write_clean(PathPlus(p).read_text()) managed_files = rewrite_readme(tmp_pathplus, demo_environment) assert managed_files == ["README.rst"] check_file_output(readme_file, file_regression) rendered = render(readme_file.read_text(), stream=sys.stderr) assert rendered is not None check_file_regression(rendered, file_regression, extension=".html")
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_check_file_regression(tmp_pathplus: PathPlus, file_regression: FileRegressionFixture): with pytest.raises(FileNotFoundError, match=no_such_file_pattern): check_file_output(tmp_pathplus / "file.txt", file_regression) check_file_regression("Success!\n\nThis is a test.", file_regression) result = StringList("Success!") result.blankline() result.blankline(ensure_single=True) result.append("This is a test.") check_file_regression(result, file_regression)
def test_make_issue_templates(tmp_pathplus, demo_environment, file_regression: FileRegressionFixture): managed_files = make_issue_templates(tmp_pathplus, demo_environment) assert managed_files == [ ".github/ISSUE_TEMPLATE/bug_report.md", ".github/ISSUE_TEMPLATE/feature_request.md" ] data = (tmp_pathplus / managed_files[0]).read_text() check_file_regression(data, file_regression, ".bug.md") data = (tmp_pathplus / managed_files[1]).read_text() check_file_regression(data, file_regression, ".feature.md")
def test_coloured_diff(file_regression: FileRegressionFixture): data_dir = PathPlus(__file__).parent / "test_diff_" original = data_dir / "original" modified = data_dir / "modified" diff = coloured_diff( original.read_lines(), modified.read_lines(), fromfile="original_file.txt", tofile="modified_file.txt", fromfiledate="(original)", tofiledate="(modified)", lineterm='', ) check_file_regression(diff, file_regression)
def test_via_run_repo_helper( temp_empty_repo, capsys, file_regression: FileRegressionFixture, monkeypatch, example_config, ): (temp_empty_repo.path / "repo_helper.yml").write_text(example_config) assert run_repo_helper( temp_empty_repo.path, force=False, initialise=True, commit=True, message="Testing Testing", enable_pre_commit=False, ) == 0 stat = status(temp_empty_repo.path) assert not stat.untracked assert not stat.unstaged assert not stat.staged["add"] assert not stat.staged["modify"] assert not stat.staged["delete"] assert run_repo_helper( temp_empty_repo.path, force=False, initialise=False, commit=True, message="Updated", ) == 0 stat = status(temp_empty_repo.path) assert not stat.untracked assert not stat.unstaged assert not stat.staged["add"] assert not stat.staged["modify"] assert not stat.staged["delete"] sha = "6d8cf72fff6adc4e570cb046ca417db7f2e10a3b" stdout = re.sub(f"Committed as [A-Za-z0-9]{{{len(sha)}}}", f"Committed as {sha}", capsys.readouterr().out) assert not capsys.readouterr().err check_file_regression(stdout, file_regression)
def test_make_contributing(tmp_pathplus, demo_environment, file_regression: FileRegressionFixture): assert make_contributing(tmp_pathplus, demo_environment) == [ "CONTRIBUTING.rst", "CONTRIBUTING.md" ] assert not (tmp_pathplus / "CONTRIBUTING.md").is_file() assert (tmp_pathplus / "CONTRIBUTING.rst").is_file() check_file_output(tmp_pathplus / "CONTRIBUTING.rst", file_regression) (tmp_pathplus / "CONTRIBUTING.md").touch() assert (tmp_pathplus / "CONTRIBUTING.md").is_file() assert make_contributing(tmp_pathplus, demo_environment) == [ "CONTRIBUTING.rst", "CONTRIBUTING.md" ] assert not (tmp_pathplus / "CONTRIBUTING.md").is_file() assert (tmp_pathplus / "CONTRIBUTING.rst").is_file() rendered = render((tmp_pathplus / "CONTRIBUTING.rst").read_text(), stream=sys.stderr) check_file_regression(rendered, file_regression, extension=".html")
def test_make_tree(advanced_file_regression: AdvancedFileRegressionFixture): check_file_regression( '\n'.join( make_tree([ "apeye>=0.3.0", [ "appdirs>=1.4.4", "cachecontrol[filecache]>=0.12.6", [ "requests", [ "chardet<4,>=3.0.2", "idna<3,>=2.5", "urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1", "certifi>=2017.4.17", ], "msgpack>=0.5.2", ], ], "domdf_python_tools==2.2.0", ])), advanced_file_regression)
def check_file(filename: PathPlus, extension: Optional[str] = None): data = filename.read_text(encoding="UTF-8") assert expected_version in data extension = extension or filename.suffix if extension == ".py": extension = "._py_" return check_file_regression(data, advanced_file_regression, extension)
def check_stdout( self, file_regression: FileRegressionFixture, extension: str = ".txt", **kwargs, ) -> Literal[True]: r""" Perform a regression check on the standard output from the command. :param file_regression: :param extension: The extension of the reference file. :param \*\*kwargs: Additional keyword arguments passed to :meth:`.FileRegressionFixture.check`. """ __tracebackhide__ = True check_file_regression(self.stdout.rstrip(), file_regression, extension=extension, **kwargs) return True
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)
def test_generics(input, file_regression: FileRegressionFixture): # noqa: A002 # pylint: disable=redefined-builtin check_file_regression(reformat_generics(input), file_regression, extension="._py")
def test_github_bash_block(file_regression: FileRegressionFixture, commands): check_file_regression(github_bash_block(*commands), file_regression, extension=".rst")
def test_make_alabaster_theming(file_regression): check_file_regression(make_alabaster_theming(), file_regression, "_style.css")
def test_make_readthedocs_theming(file_regression): check_file_regression(make_readthedocs_theming(), file_regression, "style.css")
def do_test_release(temp_repo: Repo, advanced_file_regression: AdvancedFileRegressionFixture, expected_version: str, command: Command, args: Optional[List[str]] = None, force: bool = False): (temp_repo.path / ".bumpversion.cfg").write_lines([ "[bumpversion]", "current_version = 0.0.1", "commit = True", "tag = True", '', "[bumpversion:file:repo_helper.yml]", ]) (temp_repo.path / "pyproject.toml").write_lines([ "[project]", 'name = "modname: repo_helper_demo"', 'version = "0.0.1"', ]) (temp_repo.path / "setup.cfg").write_lines([ "[metadata]", "name = domdf_python_tools", "version = 0.0.1", "author = Dominic Davis-Foster", "author_email = [email protected]", "license = GNU Lesser General Public License v3 or later (LGPLv3+)", "keywords = utilities", ]) (temp_repo.path / "repo_helper_demo").maybe_make() (temp_repo.path / "repo_helper_demo" / "__init__.py").write_clean('__version__ = "0.0.1"') (temp_repo.path / "README.rst").write_lines([ ".. image:: https://img.shields.io/github/commits-since/domdfcoding/repo_helper/v0.0.1", "\t:target: https://github.com/domdfcoding/repo_helper/pulse", "\t:alt: GitHub commits since tagged version", ]) (temp_repo.path / "doc-source").maybe_make() (temp_repo.path / "doc-source" / "index.rst").write_lines([ ".. github-shield::", "\t:commits-since: v0.0.1", "\t:alt: GitHub commits since tagged version", ]) if args is None: args = [] if force: args.append("--force") # TODO: check updating files with in_directory(temp_repo.path): runner = CliRunner(mix_stderr=False) result: Result = runner.invoke(command, args=args) assert result.exit_code == 0 if force: assert result.stderr.splitlines() == [ "Git working directory is not clean:", " A file.txt", "Proceeding anyway", ] else: assert not result.stderr check_file_regression('\n'.join(result.stdout.splitlines()[:-1]), advanced_file_regression, extension="_stdout.txt") m = re.match("Committed as ([A-Za-z0-9]{40})", result.stdout.splitlines()[-1]) assert m is not None def check_file(filename: PathPlus, extension: Optional[str] = None): data = filename.read_text(encoding="UTF-8") assert expected_version in data extension = extension or filename.suffix if extension == ".py": extension = "._py_" return check_file_regression(data, advanced_file_regression, extension) check_file(temp_repo.path / ".bumpversion.cfg", extension=".bumpversion.cfg") check_file(temp_repo.path / "pyproject.toml", extension="_pyproject.toml") check_file(temp_repo.path / "repo_helper.yml", extension="_repo_helper.yml") check_file(temp_repo.path / "README.rst", extension="_readme.rst") check_file(temp_repo.path / "repo_helper_demo" / "__init__.py", extension="_init._py") check_file(temp_repo.path / "doc-source" / "index.rst", extension="_index.rst") tags = get_tags(temp_repo) assert f"v{expected_version}" in tags.values() assert tags[m.group(1)] == f"v{expected_version}"