From 6f5d8070ccfdfc57e21da043c221b1c38b5ffd88 Mon Sep 17 00:00:00 2001 From: muhd360 Date: Mon, 28 Oct 2024 12:48:49 +0530 Subject: [PATCH 1/2] first_try --- src/cosmic_ray/cli.py | 9 +++++-- .../unittests/test_command_line_processing.py | 24 +++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/cosmic_ray/cli.py b/src/cosmic_ray/cli.py index 04be0fa..7ac6b9d 100644 --- a/src/cosmic_ray/cli.py +++ b/src/cosmic_ray/cli.py @@ -63,12 +63,13 @@ def new_config(config_file): @cli.command() +@click.argument("force", type=bool, default=False) @click.argument("config_file") @click.argument( "session_file", # help="The filename for the database in which the work order will be stored." ) -def init(config_file, session_file): +def init(config_file, session_file,force): """Initialize a mutation testing session from a configuration. This primarily creates a session - a database of "work to be done" - which describes all of the mutations and test runs that need to be @@ -96,7 +97,11 @@ def init(config_file, session_file): log.info(" - %s: %s", directory, ", ".join(sorted(files))) with use_db(session_file) as database: - cosmic_ray.commands.init(modules, database, operators_cfg) + if database.num_results>0: + sys.exit(ExitCode.OK) + elif database.num_results==0 or force: + cosmic_ray.commands.init(modules, database, operators_cfg) + sys.exit(ExitCode.OK) diff --git a/tests/unittests/test_command_line_processing.py b/tests/unittests/test_command_line_processing.py index 2963e04..70f4413 100644 --- a/tests/unittests/test_command_line_processing.py +++ b/tests/unittests/test_command_line_processing.py @@ -5,6 +5,7 @@ import stat import pytest + from exit_codes import ExitCode import cosmic_ray.cli @@ -12,7 +13,7 @@ import cosmic_ray.modules import cosmic_ray.mutating import cosmic_ray.plugins - +from cosmic_ray.work_db import WorkDB, use_db @pytest.fixture def config_file(tmpdir): @@ -59,11 +60,24 @@ def test_non_existent_session_file_returns_EX_NOINPUT(local_unittest_config): assert cosmic_ray.cli.main(["exec", str(local_unittest_config), "foo.session"]) == ExitCode.NO_INPUT -def test_non_existent_config_file_returns_EX_NOINPUT(session, local_unittest_config): - cosmic_ray.cli.main(["init", local_unittest_config, str(session)]) +def test_non_existent_config_file_returns_EX_NOINPUT(session, local_unittest_config,force): + cosmic_ray.cli.main(["init", local_unittest_config, str(session),force]) assert cosmic_ray.cli.main(["exec", "no-such-file", str(session)]) == ExitCode.CONFIG + +def test_init_with_existing_results_no_force(session, local_unittest_config,force): + """Test that init exits without reinitializing when results exist and force=False""" + with use_db(session) as database: + database.num_results = 1 # Simulate existing results + result = cosmic_ray.cli.main(["init", local_unittest_config, str(session),force]) + assert result == ExitCode.OK +def test_init_with_existing_results_force(session, local_unittest_config,force=True): + """Test that init exits without reinitializing when results exist and force=False""" + + result = cosmic_ray.cli.main(["init", local_unittest_config, str(session),force]) + assert result != ExitCode.OK + @pytest.mark.skip("need to sort this API out") def test_unreadable_file_returns_EX_PERM(tmpdir, local_unittest_config): p = tmpdir.ensure("bogus.session.sqlite") @@ -80,8 +94,8 @@ def test_new_config_success_returns_EX_OK(monkeypatch, config_file): # NOTE: We have integration tests for the happy-path for many commands, so we don't cover them explicitly here. -def test_dump_success_returns_EX_OK(lobotomize, local_unittest_config, session): - errcode = cosmic_ray.cli.main(["init", local_unittest_config, str(session)]) +def test_dump_success_returns_EX_OK(lobotomize, local_unittest_config, session,force): + errcode = cosmic_ray.cli.main(["init", local_unittest_config, str(session),force]) assert errcode == ExitCode.OK errcode = cosmic_ray.cli.main(["dump", str(session)]) From 766d26c2e5ade962edbf692d65898e027f5d78ed Mon Sep 17 00:00:00 2001 From: muhd360 Date: Fri, 1 Nov 2024 00:58:44 +0530 Subject: [PATCH 2/2] second try --- src/cosmic_ray/cli.py | 12 ++-- tests/conftest.py | 4 +- .../unittests/test_command_line_processing.py | 66 +++++++++++++++---- 3 files changed, 62 insertions(+), 20 deletions(-) diff --git a/src/cosmic_ray/cli.py b/src/cosmic_ray/cli.py index 7ac6b9d..71a3ab4 100644 --- a/src/cosmic_ray/cli.py +++ b/src/cosmic_ray/cli.py @@ -63,7 +63,7 @@ def new_config(config_file): @cli.command() -@click.argument("force", type=bool, default=False) +@click.option("--force", is_flag=True, default=False, help="Force initialization") @click.argument("config_file") @click.argument( "session_file", @@ -97,10 +97,12 @@ def init(config_file, session_file,force): log.info(" - %s: %s", directory, ", ".join(sorted(files))) with use_db(session_file) as database: - if database.num_results>0: - sys.exit(ExitCode.OK) - elif database.num_results==0 or force: - cosmic_ray.commands.init(modules, database, operators_cfg) + + if database.num_results>0 or not force: + return + + + cosmic_ray.commands.init(modules, database, operators_cfg) sys.exit(ExitCode.OK) diff --git a/tests/conftest.py b/tests/conftest.py index 01abf41..49ae509 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,7 +2,9 @@ import pytest - +@pytest.fixture +def force(): + return "True" @pytest.fixture def tmpdir_path(tmpdir): """A temporary directory as a pathlib.Path.""" diff --git a/tests/unittests/test_command_line_processing.py b/tests/unittests/test_command_line_processing.py index 70f4413..e72e202 100644 --- a/tests/unittests/test_command_line_processing.py +++ b/tests/unittests/test_command_line_processing.py @@ -13,7 +13,8 @@ import cosmic_ray.modules import cosmic_ray.mutating import cosmic_ray.plugins -from cosmic_ray.work_db import WorkDB, use_db +from cosmic_ray.work_db import WorkDB +from cosmic_ray.work_item import WorkItem,MutationSpec @pytest.fixture def config_file(tmpdir): @@ -60,23 +61,60 @@ def test_non_existent_session_file_returns_EX_NOINPUT(local_unittest_config): assert cosmic_ray.cli.main(["exec", str(local_unittest_config), "foo.session"]) == ExitCode.NO_INPUT -def test_non_existent_config_file_returns_EX_NOINPUT(session, local_unittest_config,force): - cosmic_ray.cli.main(["init", local_unittest_config, str(session),force]) +def test_non_existent_config_file_returns_EX_NOINPUT(session, local_unittest_config): + cosmic_ray.cli.main(["init", local_unittest_config, str(session)]) assert cosmic_ray.cli.main(["exec", "no-such-file", str(session)]) == ExitCode.CONFIG -def test_init_with_existing_results_no_force(session, local_unittest_config,force): - """Test that init exits without reinitializing when results exist and force=False""" - with use_db(session) as database: - database.num_results = 1 # Simulate existing results - result = cosmic_ray.cli.main(["init", local_unittest_config, str(session),force]) - assert result == ExitCode.OK -def test_init_with_existing_results_force(session, local_unittest_config,force=True): +def test_init_with_existing_results_no_force(session, local_unittest_config): """Test that init exits without reinitializing when results exist and force=False""" + # Sample WorkItem creation + mutation_spec = MutationSpec( + module_path="src/example/test.py", + operator_name="delete_line", + occurrence=1, + start_pos=(10, 0), + end_pos=(10, 20), + operator_args={"comment": "Delete print statement"} + ) + + work_items = [ + WorkItem( + job_id="test_job_123", + mutations=(mutation_spec,) + ) + ] + + db=WorkDB(session,1) + db.add_work_items(work_items) + + # Verify initial database state has our test work items + initial_count = db.num_work_items + assert initial_count == len(work_items) + + + + result = cosmic_ray.cli.main(["init", local_unittest_config, str(session)]) + db=WorkDB(session,2) + final_count = db.num_results + assert final_count == initial_count # Confirm work items are unchanged + assert result == ExitCode.OK + + + + + + + + + + +def test_init_with_existing_results_force(session, local_unittest_config,force): + """Test that reinitialization occurs when force=True""" - result = cosmic_ray.cli.main(["init", local_unittest_config, str(session),force]) - assert result != ExitCode.OK + result = cosmic_ray.cli.main(["init", local_unittest_config, str(session),"--force"]) + assert result == ExitCode.OK @pytest.mark.skip("need to sort this API out") def test_unreadable_file_returns_EX_PERM(tmpdir, local_unittest_config): @@ -94,8 +132,8 @@ def test_new_config_success_returns_EX_OK(monkeypatch, config_file): # NOTE: We have integration tests for the happy-path for many commands, so we don't cover them explicitly here. -def test_dump_success_returns_EX_OK(lobotomize, local_unittest_config, session,force): - errcode = cosmic_ray.cli.main(["init", local_unittest_config, str(session),force]) +def test_dump_success_returns_EX_OK(lobotomize, local_unittest_config, session): + errcode = cosmic_ray.cli.main(["init", local_unittest_config, str(session)]) assert errcode == ExitCode.OK errcode = cosmic_ray.cli.main(["dump", str(session)])