Skip to content

Proposed fix for Issue #22, create new directory if the directory passed into the --mutantDir flag does not exists.#42

Open
Ethan-Heimer wants to merge 7 commits intoagroce:masterfrom
Ethan-Heimer:master
Open

Proposed fix for Issue #22, create new directory if the directory passed into the --mutantDir flag does not exists.#42
Ethan-Heimer wants to merge 7 commits intoagroce:masterfrom
Ethan-Heimer:master

Conversation

@Ethan-Heimer
Copy link
Copy Markdown
Contributor

@Ethan-Heimer Ethan-Heimer commented Apr 10, 2026

Changes in this PR

Given the discussion under Issue #22, this proposes a fix that will create the missing directory if the directory passed into the flag --mutantDir does not exist. The code changes proposed occur after the --mutantDir flag has been parsed in the genmutants.py file, where after the --mutantDir flag has been removed from the args list, the following is ran to ensure the directory is created safely.

        mdirExists = os.path.isdir(mdir)
        if not mdirExists:
            print(f"THE DIRECTORY '{mdir}' PASSED INTO --mutantDir DID NOT EXIST, ATTEMPTING TO CREATE '{mdir}'")
            try:
                os.mkdir(mdir)
                print(f"DIRECTORY '{mdir}' WAS SUCCSESSFULLY CREATED")
            except FileNotFoundError:
                print(f"THE PARENT DIRECTORY FOR '{mdir}' WAS NOT FOUND. PLEASE CREATE IT AND TRY AGAIN.")
                sys.exit(1)
            except PermissionError:
                print(f"ERROR CREATING '{mdir}', PERMISSION DENIED.")
                sys.exit(1)
            except OSError as error:
                print(f"AN OS ERROR WAS THROWN WHILE ATTEMPTING TO CREATE '{mdir}': '{error}'")
                sys.exit(1)
            except Exception as error:
                print(f"AN ERROR WAS THROWN WHILE ATTEMPTING TO CREATE '{mdir}': '{error}'")
                sys.exit(1)

Should an exception be raised while creating the missing directory, the program exits a bit more gracefully, with an error printed for the user, without leading into a crash later on in the program.

Changes have be made to the documentation printed by the --help flag was changed to reflect this new behavior by --mutantDir as well.

--mutantDir: directory to put generated mutants in, creates directory if it does not exist; defaults to current directory.

Decisions made

It was ultimately decided that checking if the directory existed immediately after the the --mutantDir flag was parsed was the best place for this check to happen. Code below this point tends to assume that the directory passed into --mutantDir exists, but that false assumption is what caused the crash to occur in the first place. Adding the check and creation guard at the earliest possible point where it would make since to do so allows the code following the parsing of --mutantDir to operate in with this assumption safely.

New Features

In issue #22, you made a point that users could be reliant on the previous behavior. To account for this, I've proposed a solution that adds a new flag named --legacyMutantDir. When this flag is used, the program skips checking to see of the directory passed into --mutantDir exists, therefore simulating the old --mutantDir behavior for users who might need it.

This also includes the proper documentation printed by the --help flag to explain the use-case of --legacyMutantDir.

Testing

The unit tests for this change can be found in /tests/mutantDir_tests.py and tests for the following:

  • If the directory passed into --mutantDir is created when it does not exists and the program is able to create it.
  • If the directory passed into --mutantDir does happen to already exist, does the program still run normally.
  • If the directory passed into --mutantDir contains a parent directory (./parent/child) and that parent does not exist, does the proper get error thrown and program exited.
  • If the directory passed into --mutantDir contains is read only, does the program throw the proper error and exit.
  • If --legacyMutantDir is passed into the program, and the directory passed into --mutantDir does not exist, does the program skip the checks added and crash like previously.
  • If --legacyMutantDir is passed into the program, and the directory passed into --mutantDir does exist, does the program run like normal and not crash.

Thank you for your time!

This is a submission to the final project for CS386. Group: Ethan Heimer and Ryan Menaugh. No AI was used to generate any code, and all tests passed on MacOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant