From a05ce2caa69a7741aba58c32881631660391b16d Mon Sep 17 00:00:00 2001 From: briggySmalls Date: Sun, 17 May 2020 08:43:36 +0100 Subject: [PATCH 1/3] Remove dead code The equality expression was not used anywhere. Furthermore subprocess.check_call already checks the return code of the call --- tests/test_bake_project.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_bake_project.py b/tests/test_bake_project.py index f50426273..aa5945afc 100644 --- a/tests/test_bake_project.py +++ b/tests/test_bake_project.py @@ -87,7 +87,7 @@ def test_bake_with_defaults(cookies): def test_bake_and_run_tests(cookies): with bake_in_temp_dir(cookies) as result: assert result.project.isdir() - run_inside_dir('python setup.py test', str(result.project)) == 0 + run_inside_dir('python setup.py test', str(result.project)) print("test_bake_and_run_tests path", str(result.project)) @@ -98,7 +98,7 @@ def test_bake_withspecialchars_and_run_tests(cookies): extra_context={'full_name': 'name "quote" name'} ) as result: assert result.project.isdir() - run_inside_dir('python setup.py test', str(result.project)) == 0 + run_inside_dir('python setup.py test', str(result.project)) def test_bake_with_apostrophe_and_run_tests(cookies): @@ -108,7 +108,7 @@ def test_bake_with_apostrophe_and_run_tests(cookies): extra_context={'full_name': "O'connor"} ) as result: assert result.project.isdir() - run_inside_dir('python setup.py test', str(result.project)) == 0 + run_inside_dir('python setup.py test', str(result.project)) # def test_bake_and_run_travis_pypi_setup(cookies): @@ -220,9 +220,9 @@ def test_using_pytest(cookies): lines = test_file_path.readlines() assert "import pytest" in ''.join(lines) # Test the new pytest target - run_inside_dir('python setup.py pytest', str(result.project)) == 0 + run_inside_dir('python setup.py pytest', str(result.project)) # Test the test alias (which invokes pytest) - run_inside_dir('python setup.py test', str(result.project)) == 0 + run_inside_dir('python setup.py test', str(result.project)) def test_not_using_pytest(cookies): From ff00810e9ba00161b83ccb5aea5321c728b07531 Mon Sep 17 00:00:00 2001 From: briggySmalls Date: Sun, 17 May 2020 08:50:58 +0100 Subject: [PATCH 2/3] Use pytest parametrization to keep DRY --- tests/test_bake_project.py | 101 +++++++++++++------------------------ 1 file changed, 35 insertions(+), 66 deletions(-) diff --git a/tests/test_bake_project.py b/tests/test_bake_project.py index aa5945afc..79fef0f07 100644 --- a/tests/test_bake_project.py +++ b/tests/test_bake_project.py @@ -8,6 +8,7 @@ from cookiecutter.utils import rmtree from click.testing import CliRunner +import pytest import importlib @@ -84,31 +85,19 @@ def test_bake_with_defaults(cookies): assert 'tests' in found_toplevel_files -def test_bake_and_run_tests(cookies): - with bake_in_temp_dir(cookies) as result: - assert result.project.isdir() - run_inside_dir('python setup.py test', str(result.project)) - print("test_bake_and_run_tests path", str(result.project)) - - -def test_bake_withspecialchars_and_run_tests(cookies): - """Ensure that a `full_name` with double quotes does not break setup.py""" +pytest.mark.parametrize("extra_context", [ + {}, + {'full_name': 'name "quote" name'}, + {'full_name': "O'connor"} +]) +def test_bake_and_run_tests(cookies, extra_context): with bake_in_temp_dir( - cookies, - extra_context={'full_name': 'name "quote" name'} - ) as result: - assert result.project.isdir() - run_inside_dir('python setup.py test', str(result.project)) - - -def test_bake_with_apostrophe_and_run_tests(cookies): - """Ensure that a `full_name` with apostrophes does not break setup.py""" - with bake_in_temp_dir( - cookies, - extra_context={'full_name': "O'connor"} + cookies, + extra_context=extra_context, ) as result: assert result.project.isdir() run_inside_dir('python setup.py test', str(result.project)) + print("test_bake_and_run_tests path", str(result.project)) # def test_bake_and_run_travis_pypi_setup(cookies): @@ -178,23 +167,22 @@ def test_make_help(cookies): output -def test_bake_selecting_license(cookies): - license_strings = { - 'MIT license': 'MIT ', - 'BSD license': 'Redistributions of source code must retain the ' + - 'above copyright notice, this', - 'ISC license': 'ISC License', - 'Apache Software License 2.0': - 'Licensed under the Apache License, Version 2.0', - 'GNU General Public License v3': 'GNU GENERAL PUBLIC LICENSE', - } - for license, target_string in license_strings.items(): - with bake_in_temp_dir( - cookies, - extra_context={'open_source_license': license} - ) as result: - assert target_string in result.project.join('LICENSE').read() - assert license in result.project.join('setup.py').read() +@pytest.mark.parametrize("license_info", [ + ('MIT license', 'MIT '), + ('BSD license', 'Redistributions of source code must retain the ' + + 'above copyright notice, this'), + ('ISC license', 'ISC License'), + ('Apache Software License 2.0', 'Licensed under the Apache License, Version 2.0'), + ('GNU General Public License v3', 'GNU GENERAL PUBLIC LICENSE'), +]) +def test_bake_selecting_license(cookies, license_info): + license, target_string = license_info + with bake_in_temp_dir( + cookies, + extra_context={'open_source_license': license} + ) as result: + assert target_string in result.project.join('LICENSE').read() + assert license in result.project.join('setup.py').read() def test_bake_not_open_source(cookies): @@ -257,40 +245,21 @@ def test_not_using_pytest(cookies): # "missing password config in .travis.yml" -def test_bake_with_no_console_script(cookies): - context = {'command_line_interface': "No command-line interface"} - result = cookies.bake(extra_context=context) - project_path, project_slug, project_dir = project_info(result) - found_project_files = os.listdir(project_dir) - assert "cli.py" not in found_project_files - - setup_path = os.path.join(project_path, 'setup.py') - with open(setup_path, 'r') as setup_file: - assert 'entry_points' not in setup_file.read() - - -def test_bake_with_console_script_files(cookies): - context = {'command_line_interface': 'click'} - result = cookies.bake(extra_context=context) - project_path, project_slug, project_dir = project_info(result) - found_project_files = os.listdir(project_dir) - assert "cli.py" in found_project_files - - setup_path = os.path.join(project_path, 'setup.py') - with open(setup_path, 'r') as setup_file: - assert 'entry_points' in setup_file.read() - - -def test_bake_with_argparse_console_script_files(cookies): - context = {'command_line_interface': 'argparse'} +@pytest.mark.parametrize("args", [ + ({'command_line_interface': "No command-line interface"}, False), + ({'command_line_interface': 'click'}, True), + ({'command_line_interface': 'argparse'}, True), +]) +def test_bake_with_console_script(cookies, args): + context, is_present = args result = cookies.bake(extra_context=context) project_path, project_slug, project_dir = project_info(result) found_project_files = os.listdir(project_dir) - assert "cli.py" in found_project_files + assert ("cli.py" in found_project_files) == is_present setup_path = os.path.join(project_path, 'setup.py') with open(setup_path, 'r') as setup_file: - assert 'entry_points' in setup_file.read() + assert ('entry_points' in setup_file.read()) == is_present def test_bake_with_console_script_cli(cookies): From 152c2cf82021740bb47c53eeb149b054db993bce Mon Sep 17 00:00:00 2001 From: briggySmalls Date: Sun, 17 May 2020 08:57:01 +0100 Subject: [PATCH 3/3] Fix decorator syntax --- tests/test_bake_project.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_bake_project.py b/tests/test_bake_project.py index 79fef0f07..f4ddab26c 100644 --- a/tests/test_bake_project.py +++ b/tests/test_bake_project.py @@ -85,7 +85,7 @@ def test_bake_with_defaults(cookies): assert 'tests' in found_toplevel_files -pytest.mark.parametrize("extra_context", [ +@pytest.mark.parametrize("extra_context", [ {}, {'full_name': 'name "quote" name'}, {'full_name': "O'connor"}