From 0e3ea59f3ed312d6ba3f8100e607068fab5e6d7f Mon Sep 17 00:00:00 2001 From: Abhas Bhattacharya Date: Sat, 5 Dec 2015 01:06:13 +0530 Subject: [PATCH 1/6] Document how to store ini path result from plugin --- pytest_watch/ini_config_helpers.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pytest_watch/ini_config_helpers.py b/pytest_watch/ini_config_helpers.py index 69c1eb9..7afab64 100644 --- a/pytest_watch/ini_config_helpers.py +++ b/pytest_watch/ini_config_helpers.py @@ -11,7 +11,9 @@ def pytest_cmdline_main(self, config): class CollectorIniPath(object): - """ Object for storing the path from CollectIniPathPlugin """ + """ Object for storing the path from CollectIniPathPlugin. + As things cant be returned from the plugin, a reasonable way is to set \ + path here and read back later """ pytest_ini_path = None From 5a23419206912885eebcab0cca2f949ce8b04663 Mon Sep 17 00:00:00 2001 From: Abhas Bhattacharya Date: Sat, 5 Dec 2015 03:14:37 +0530 Subject: [PATCH 2/6] Added two test cases --- tests/test_cases/case_1/pytest.ini | 3 +++ tests/test_cases/case_2/pytest.ini | 6 ++++++ 2 files changed, 9 insertions(+) create mode 100644 tests/test_cases/case_1/pytest.ini create mode 100644 tests/test_cases/case_2/pytest.ini diff --git a/tests/test_cases/case_1/pytest.ini b/tests/test_cases/case_1/pytest.ini new file mode 100644 index 0000000..a6a7656 --- /dev/null +++ b/tests/test_cases/case_1/pytest.ini @@ -0,0 +1,3 @@ +[pytest-watch] + +verbose = True diff --git a/tests/test_cases/case_2/pytest.ini b/tests/test_cases/case_2/pytest.ini new file mode 100644 index 0000000..31171ce --- /dev/null +++ b/tests/test_cases/case_2/pytest.ini @@ -0,0 +1,6 @@ +[pytest-watch] + +verbose = True +version = False + +onpass = jump \ No newline at end of file From 62faab2033f79df3ffaf0fad358b34e4b02e2f86 Mon Sep 17 00:00:00 2001 From: Abhas Bhattacharya Date: Sat, 5 Dec 2015 03:14:57 +0530 Subject: [PATCH 3/6] Added tests - Check ini path, check merged args --- tests/test_config.py | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/test_config.py diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..bda5203 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,68 @@ +import pytest +from pytest_watch.config import CollectConfig, merge_config +from pytest_watch.util import silence + +import os +import tempfile +import shutil + +def create_temp_case(case_name): + path_test_cases = os.path.abspath("test_cases/") + path_case = os.path.join(path_test_cases, case_name) + + path_tmpdir = tempfile.mkdtemp() + path_tmpdir_case = os.path.join(path_tmpdir, case_name) + shutil.copytree(path_case, path_tmpdir_case) + + return path_tmpdir_case + + +def test_path_collected_1(): + ''' Check pytest.ini location is correct''' + # always reset pwd to cur directory + os.chdir(os.path.dirname(__file__)) + path_case = create_temp_case("case_1") + os.chdir(path_case) + + collect_config = CollectConfig() + with silence(): + pytest.main(['--collect-only'], plugins=[collect_config]) + + path_pytest_ini = os.path.join(path_case, "pytest.ini") + assert(str(collect_config.path) == path_pytest_ini) + + +def test_path_collected_2(): + ''' Check pytest.ini location is correct, also from a inner directory''' + os.chdir(os.path.dirname(__file__)) + path_case = create_temp_case("case_1") + path_case_inner = os.path.join(path_case, "dir_1") + os.chdir(path_case_inner) + + collect_config = CollectConfig() + with silence(): + pytest.main(['--collect-only'], plugins=[collect_config]) + + path_pytest_ini = os.path.join(path_case, "pytest.ini") + assert(str(collect_config.path) == path_pytest_ini) + + +def test_merge_config_basic(): + ''' Check ini config works alone''' + os.chdir(os.path.dirname(__file__)) + path_case = create_temp_case("case_1") + os.chdir(path_case) + + args = {"--verbose":False, + "--help" : False, + "--ignore" : None} + merge_config(args) + + assert(args["--verbose"] is True) + assert(args["--help"] is False) + assert(args["--ignore"] is None) + + + + + From bfe6e7a518833160a0cc8516606258e502d2aa13 Mon Sep 17 00:00:00 2001 From: Abhas Bhattacharya Date: Sat, 5 Dec 2015 14:42:56 +0530 Subject: [PATCH 4/6] with patched_chdir and more tests, put in Test_ class --- tests/test_cases/case_2/pytest.ini | 5 +- tests/test_cases/case_4/pytest.ini | 3 + tests/test_config.py | 151 +++++++++++++++++++++++------ 3 files changed, 128 insertions(+), 31 deletions(-) create mode 100644 tests/test_cases/case_4/pytest.ini diff --git a/tests/test_cases/case_2/pytest.ini b/tests/test_cases/case_2/pytest.ini index 31171ce..c787fbb 100644 --- a/tests/test_cases/case_2/pytest.ini +++ b/tests/test_cases/case_2/pytest.ini @@ -1,6 +1,5 @@ [pytest-watch] -verbose = True version = False - -onpass = jump \ No newline at end of file +ignore = dir_b +help = True \ No newline at end of file diff --git a/tests/test_cases/case_4/pytest.ini b/tests/test_cases/case_4/pytest.ini new file mode 100644 index 0000000..e2ac50f --- /dev/null +++ b/tests/test_cases/case_4/pytest.ini @@ -0,0 +1,3 @@ +[pytest] + +verbose = True diff --git a/tests/test_config.py b/tests/test_config.py index bda5203..d3f2a07 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -6,21 +6,37 @@ import tempfile import shutil + +class patched_chdir(object): + + """Just like chdir, but can be used in with - sets back the old path at exit""" + + def __init__(self, new_path): + self.old_path = os.getcwd() + self.new_path = new_path + + def __enter__(self): + os.chdir(self.new_path) + + def __exit__(self, type, value, traceback): + os.chdir(self.old_path) + + def create_temp_case(case_name): - path_test_cases = os.path.abspath("test_cases/") - path_case = os.path.join(path_test_cases, case_name) - - path_tmpdir = tempfile.mkdtemp() - path_tmpdir_case = os.path.join(path_tmpdir, case_name) - shutil.copytree(path_case, path_tmpdir_case) - + with patched_chdir(os.path.dirname(__file__)): + path_test_cases = os.path.abspath("test_cases/") + path_case = os.path.join(path_test_cases, case_name) + + path_tmpdir = tempfile.mkdtemp() + path_tmpdir_case = os.path.join(path_tmpdir, case_name) + shutil.copytree(path_case, path_tmpdir_case) + return path_tmpdir_case def test_path_collected_1(): ''' Check pytest.ini location is correct''' # always reset pwd to cur directory - os.chdir(os.path.dirname(__file__)) path_case = create_temp_case("case_1") os.chdir(path_case) @@ -34,7 +50,6 @@ def test_path_collected_1(): def test_path_collected_2(): ''' Check pytest.ini location is correct, also from a inner directory''' - os.chdir(os.path.dirname(__file__)) path_case = create_temp_case("case_1") path_case_inner = os.path.join(path_case, "dir_1") os.chdir(path_case_inner) @@ -47,22 +62,102 @@ def test_path_collected_2(): assert(str(collect_config.path) == path_pytest_ini) -def test_merge_config_basic(): - ''' Check ini config works alone''' - os.chdir(os.path.dirname(__file__)) - path_case = create_temp_case("case_1") - os.chdir(path_case) - - args = {"--verbose":False, - "--help" : False, - "--ignore" : None} - merge_config(args) - - assert(args["--verbose"] is True) - assert(args["--help"] is False) - assert(args["--ignore"] is None) - - - - - +class Test_merge_config(object): + + """All tests for func merge_config""" + + def test_works_alone(self): + ''' Check ini config works alone''' + os.chdir(os.path.dirname(__file__)) + path_case = create_temp_case("case_1") + os.chdir(path_case) + + args = {"--verbose": False, + "--help": False, + "--ignore": None} + merge_config(args) + + assert(args["--verbose"] is True) + assert(args["--help"] is False) + assert(args["--ignore"] is None) + + def test_works_with_cmdline(self): + ''' + Check ini config works alongwith cmdline args. + Set pytest.ini: + [pytest-watch] + version = True + ''' + path_case = create_temp_case("case_1") + os.chdir(path_case) + + args = {"--verbose": False, + "--help": False, + "--ignore": "dir_a"} + merge_config(args) + + assert(args["--verbose"] is True) + assert(args["--help"] is False) + assert(args["--ignore"] is "dir_a") + + def test_works_with_cmdline_precedence(self): + ''' + Check ini config works alongwith cmdline args, \ + with cmdline precedence + Set pytest.ini: + [pytest-watch] + version = False + ignore = dir_b + help = True + ''' + path_case = create_temp_case("case_2") + os.chdir(path_case) + + args = {"--verbose": True, + "--help": False, + "--ignore": "dir_a"} + merge_config(args) + + assert(args["--verbose"] is True) + assert(args["--help"] is True) + assert(args["--ignore"] is "dir_a") + + def test_works_wo_ini(self): + ''' + Check ini config works correctly with only cmdline \ + args, without any pytest.ini. + No pytest.ini present + ''' + path_case = create_temp_case("case_3") + os.chdir(path_case) + + args = {"--verbose": True, + "--help": False, + "--ignore": "dir_a"} + merge_config(args) + + assert(args["--verbose"] is True) + assert(args["--help"] is False) + assert(args["--ignore"] is "dir_a") + + def test_works_wo_pytest_watch_section(self): + ''' + Check ini config works correctly with only cmdline \ + args, without any [pytest-watch] section in pytest.ini. + Also, shouldnt read pytest settings by mistake. + Set pytest.ini: + [pytest] + verbose = True + ''' + path_case = create_temp_case("case_4") + os.chdir(path_case) + + args = {"--verbose": False, + "--help": False, + "--onpass": None, + "--ignore": "dir_a"} + merge_config(args) + + assert(args["--verbose"] is False) + assert(args["--help"] is False) + assert(args["--ignore"] is "dir_a") From d32debbf34f601668fad4f7f74b99f2acd292c89 Mon Sep 17 00:00:00 2001 From: Abhas Bhattacharya Date: Sat, 5 Dec 2015 15:09:41 +0530 Subject: [PATCH 5/6] add xfail test + brush some tests --- tests/test_cases/case_5/pytest.ini | 5 ++++ tests/test_config.py | 45 +++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 tests/test_cases/case_5/pytest.ini diff --git a/tests/test_cases/case_5/pytest.ini b/tests/test_cases/case_5/pytest.ini new file mode 100644 index 0000000..02d4f55 --- /dev/null +++ b/tests/test_cases/case_5/pytest.ini @@ -0,0 +1,5 @@ +[pytest-watch] + +directories = dir_c,dir_d + + = dir_c,dir_d \ No newline at end of file diff --git a/tests/test_config.py b/tests/test_config.py index d3f2a07..735d258 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -67,7 +67,12 @@ class Test_merge_config(object): """All tests for func merge_config""" def test_works_alone(self): - ''' Check ini config works alone''' + ''' + Check ini config works alone. + Set pytest.ini: + [pytest-watch] + verbose = True + ''' os.chdir(os.path.dirname(__file__)) path_case = create_temp_case("case_1") os.chdir(path_case) @@ -86,7 +91,7 @@ def test_works_with_cmdline(self): Check ini config works alongwith cmdline args. Set pytest.ini: [pytest-watch] - version = True + verbose = True ''' path_case = create_temp_case("case_1") os.chdir(path_case) @@ -133,11 +138,13 @@ def test_works_wo_ini(self): args = {"--verbose": True, "--help": False, + "--onpass": None, "--ignore": "dir_a"} merge_config(args) assert(args["--verbose"] is True) assert(args["--help"] is False) + assert(args["--onpass"] is None) assert(args["--ignore"] is "dir_a") def test_works_wo_pytest_watch_section(self): @@ -153,11 +160,41 @@ def test_works_wo_pytest_watch_section(self): os.chdir(path_case) args = {"--verbose": False, - "--help": False, + "--help": True, "--onpass": None, "--ignore": "dir_a"} merge_config(args) assert(args["--verbose"] is False) - assert(args["--help"] is False) + assert(args["--help"] is True) + assert(args["--onpass"] is None) assert(args["--ignore"] is "dir_a") + + @pytest.mark.xfail + def test_works_directories_ini_option(self): + ''' + Currently, "directories" or "" argument \ + in pytest.ini is not respected. So, it is marked to xfail. + + When it'll pass, "directories" option should be added to \ + more test cases. + Set pytest.ini: + [pytest-watch] + directories = dir_c,dir_d + = dir_c,dir_d + ''' + path_case = create_temp_case("case_5") + os.chdir(path_case) + + args = {"--verbose": False, + "--help": True, + "--onpass": None, + "": "dir_a,dir_b"} + merge_config(args) + + # main condition + assert(args[""] is ["dir_c", "dir_d"]) + # auxiliary conditions + assert(args["--verbose"] is False) + assert(args["--help"] is True) + assert(args["--onpass"] is None) From 2b7f06c2d8ce72c1c29435aed261583f04c72dd0 Mon Sep 17 00:00:00 2001 From: Abhas Bhattacharya Date: Sat, 5 Dec 2015 15:14:23 +0530 Subject: [PATCH 6/6] Move path tests to its own class --- tests/test_config.py | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 735d258..ca61b75 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -34,32 +34,35 @@ def create_temp_case(case_name): return path_tmpdir_case -def test_path_collected_1(): - ''' Check pytest.ini location is correct''' - # always reset pwd to cur directory - path_case = create_temp_case("case_1") - os.chdir(path_case) +class Test_ini_path_collected(object): - collect_config = CollectConfig() - with silence(): - pytest.main(['--collect-only'], plugins=[collect_config]) + """ Check pytest.ini location collected from plugin is correct """ - path_pytest_ini = os.path.join(path_case, "pytest.ini") - assert(str(collect_config.path) == path_pytest_ini) + def test_basic(self): + ''' Check pytest.ini location is correct''' + # always reset pwd to cur directory + path_case = create_temp_case("case_1") + os.chdir(path_case) + collect_config = CollectConfig() + with silence(): + pytest.main(['--collect-only'], plugins=[collect_config]) -def test_path_collected_2(): - ''' Check pytest.ini location is correct, also from a inner directory''' - path_case = create_temp_case("case_1") - path_case_inner = os.path.join(path_case, "dir_1") - os.chdir(path_case_inner) + path_pytest_ini = os.path.join(path_case, "pytest.ini") + assert(str(collect_config.path) == path_pytest_ini) + + def test_from_inner_dir(self): + ''' Check pytest.ini location is correct, also from a inner directory''' + path_case = create_temp_case("case_1") + path_case_inner = os.path.join(path_case, "dir_1") + os.chdir(path_case_inner) - collect_config = CollectConfig() - with silence(): - pytest.main(['--collect-only'], plugins=[collect_config]) + collect_config = CollectConfig() + with silence(): + pytest.main(['--collect-only'], plugins=[collect_config]) - path_pytest_ini = os.path.join(path_case, "pytest.ini") - assert(str(collect_config.path) == path_pytest_ini) + path_pytest_ini = os.path.join(path_case, "pytest.ini") + assert(str(collect_config.path) == path_pytest_ini) class Test_merge_config(object):