def test_main_update_dry_run(monkeypatch, capsys, tmp_path): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', str(tmp_path), '--add', '3.7,3.8', '--dry-run', ]) (tmp_path / "setup.py").write_text( textwrap.dedent("""\ from setuptools import setup setup( name='foo', classifiers=[ 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.6', ], ) """)) cpv.main() assert (capsys.readouterr().out.replace( str(tmp_path) + os.path.sep, 'tmp/').expandtabs().replace(' \n', '\n\n')) == textwrap.dedent("""\ setup.py says: 2.7, 3.6, 3.7, 3.8 """) assert (tmp_path / "setup.py").read_text() == textwrap.dedent("""\ from setuptools import setup setup( name='foo', classifiers=[ 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.6', ], ) """)
def test_main_handles_ctrl_c(monkeypatch): def raise_keyboard_interrupt(): raise KeyboardInterrupt() monkeypatch.setattr(cpv, '_main', raise_keyboard_interrupt) with pytest.raises(SystemExit): cpv.main()
def test_main_multiple(monkeypatch, capsys, tmp_path): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', str(tmp_path / "a"), str(tmp_path / "b"), '--expect', '3.6, 3.7' ]) (tmp_path / "a").mkdir() (tmp_path / "a" / "setup.py").write_text( textwrap.dedent("""\ from setuptools import setup setup( name='foo', classifiers=[ 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.6', ], ) """)) with pytest.raises(SystemExit) as exc_info: cpv.main() assert (capsys.readouterr().out + str(exc_info.value) + '\n').replace( str(tmp_path) + os.path.sep, 'tmp/') == textwrap.dedent("""\ tmp/a: setup.py says: 2.7, 3.6 expected: 3.6, 3.7 tmp/b: not a directory mismatch in tmp/a tmp/b! """)
def test_main_multiple_ok(monkeypatch, tmp_path, capsys): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', str(tmp_path / "a"), str(tmp_path / "b"), ]) (tmp_path / "a").mkdir() (tmp_path / "a" / "setup.py").write_text( textwrap.dedent("""\ from setuptools import setup setup( name='foo', classifiers=[ 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.6', ], ) """)) (tmp_path / "b").mkdir() (tmp_path / "b" / "setup.py").write_text( textwrap.dedent("""\ from setuptools import setup setup( name='foo', classifiers=[ 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', ], ) """)) cpv.main() assert (capsys.readouterr().out.endswith('\n\nall ok!\n'))
def test_main_expect_error_handling(monkeypatch, arg, capsys): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', '--expect', arg, ]) with pytest.raises(SystemExit): cpv.main() # the error is either 'bad version: ...' or 'bad range: ...' assert f'--expect: bad' in capsys.readouterr().err
def test_main_skip_non_packages(monkeypatch, capsys, tmp_path): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', '--skip-non-packages', str(tmp_path), ]) cpv.main() assert capsys.readouterr().out == ''
def test_main_multiple_ok(monkeypatch, capsys): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', '.', '.', ]) cpv.main() assert (capsys.readouterr().out.endswith('\n\nall ok!\n'))
def test_main_required_args(monkeypatch, tmp_path, capsys, arg): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', str(tmp_path), arg, ]) with pytest.raises(SystemExit): cpv.main() assert (f'argument {arg}: not allowed without --update/--add/--drop' in capsys.readouterr().err)
def test_main_single(monkeypatch, capsys, tmp_path): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', str(tmp_path / "a"), ]) with pytest.raises(SystemExit) as exc_info: cpv.main() assert (capsys.readouterr().out + str(exc_info.value) + '\n').replace( str(tmp_path), 'tmp') == textwrap.dedent("""\ not a directory mismatch! """)
def test_main_conflicting_args(monkeypatch, tmp_path, capsys, arg): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', str(tmp_path), arg, '3.8', '--update', '3.6-3.7', ]) with pytest.raises(SystemExit): cpv.main() assert (f'argument {arg}: not allowed with argument --update' in capsys.readouterr().err)
def test_main_update(monkeypatch, capsys, tmp_path): monkeypatch.setattr(sys, 'stdin', StringIO('y\n')) monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', str(tmp_path), '--add', '3.7,3.8', ]) (tmp_path / "setup.py").write_text(textwrap.dedent("""\ from setuptools import setup setup( name='foo', classifiers=[ 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.6', ], ) """)) cpv.main() assert ( capsys.readouterr().out .replace(str(tmp_path) + os.path.sep, 'tmp/') .expandtabs() .replace(' \n', '\n\n') ) == textwrap.dedent("""\ --- tmp/setup.py (original) +++ tmp/setup.py (updated) @@ -4,5 +4,7 @@ classifiers=[ 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', ], ) Write changes to tmp/setup.py? [y/N] setup.py says: 2.7, 3.6, 3.7, 3.8 """) assert (tmp_path / "setup.py").read_text() == textwrap.dedent("""\ from setuptools import setup setup( name='foo', classifiers=[ 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', ], ) """)
def test_main_diff_and_expect_and_dry_run_oh_my(monkeypatch, tmp_path, capsys): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', str(tmp_path), '--expect', '3.6-3.7', '--update', '3.6-3.7', '--diff', ]) with pytest.raises(SystemExit): cpv.main() assert ('argument --expect: not allowed with --diff,' ' unless you also add --dry-run' in capsys.readouterr().err)
def test_main_only(monkeypatch, capsys, tmp_path): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', str(tmp_path), '--only', 'tox.ini,setup.py', ]) setup_py = tmp_path / "setup.py" setup_py.write_text( textwrap.dedent("""\ from setuptools import setup setup( name='foo', classifiers=[ 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.6', ], ) """)) tox_ini = tmp_path / "tox.ini" tox_ini.write_text( textwrap.dedent("""\ [tox] envlist = py27,py36 """)) travis_yml = tmp_path / ".travis.yml" travis_yml.write_text( textwrap.dedent("""\ python: - 2.7 - 3.5 """)) cpv.main() assert (capsys.readouterr().out + '\n').replace( str(tmp_path) + os.path.sep, 'tmp/') == textwrap.dedent("""\ setup.py says: 2.7, 3.6 tox.ini says: 2.7, 3.6 """)
def test_main_here(monkeypatch, capsys): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions', ]) cpv.main() assert 'mismatch' not in capsys.readouterr().out
def test_main_help(monkeypatch): monkeypatch.setattr(sys, 'argv', ['check-python-versions', '--help']) with pytest.raises(SystemExit): cpv.main()