Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for commandline #28

Merged
merged 5 commits into from Nov 18, 2023

Conversation

WangGithub0
Copy link
Contributor

@WangGithub0 WangGithub0 commented Nov 16, 2023

write test for src/utils/commandline.py file, coverage from 70%->84%
image

closes #27

@Amnish04
Copy link
Owner

Amnish04 commented Nov 16, 2023

Hi Yumei, the logic for your unit test looks good. But it is not being collected or run by pytest since its not part of a function/method.

You'll need to add it to a method instead of directly putting in the class like so. image

Also, could you please make a separate file for this class, since the pattern is to have a test file for every source file. In your case, it would be tests/utils/test_commandline.py. And the root class name would be TestCommandline. You can put all your test logic in a method for this class.

Lastly, I just noticed that the integration test is failing when running from a pull request. This should be from my side, will check now

Just found that the integration test is being interupted by the commandline test you added. It runs fine without it, we need to find out why.

Copy link
Owner

@Amnish04 Amnish04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Yumei, the logic for your unit test looks good. But it is not being collected or run by pytest since its not part of a function/method.

You'll need to add it to a method instead of directly putting in the class like so.
image

Also, could you please make a separate file for this class, since the pattern is to have a test file for every source file.
In your case, it would be tests/utils/test_commandline.py.
And the root class name would be TestCommandline. You can put all your test logic in a method for this class.

@WangGithub0
Copy link
Contributor Author

hi @Amnish04, thanks for your comments, I've already revise my code, help me view please.

@Amnish04
Copy link
Owner

Amnish04 commented Nov 17, 2023

hi @Amnish04, thanks for your comments, I've already revise my code, help me view please.

@WangGithub0 Thanks for the changes.
I see there are a couple of lint errors with your imports which is failing the CI.
image

Fixing those should run the tests step.

@Amnish04
Copy link
Owner

Amnish04 commented Nov 17, 2023

@WangGithub0 Hi Yumei, I think the change to sys.argv in your test for commandline arguments is being carried on to other tests as well, hence interfering with the integration test run.

I would suggest storing the original state of that system variable before patching it, and then restore that state once you're done with your tests like this.

# Save the original sys.argv
original_argv = sys.argv.copy()

# Apply the patch to sys.argv to mimic command-line arguments
with patch.object(sys, "argv", ["commandline.py"] + arguments):
    # Create an instance of the CommandlineParser
    parser = CommandlineParser()

    # Get the parsed arguments
    args = parser.get_args()

    # Assert that the arguments are correctly parsed
    assert args.config == "config.toml"
    assert args.input_path == "input.md"
    assert args.output == "./dist/til"
    assert args.version

# After the block, revert sys.argv to its original state
sys.argv = original_argv

@Amnish04
Copy link
Owner

@WangGithub0
Found the problem!

My implementation of CommandlineParser was to only have one instance of the class in a single run.
image

which is why the new arguments are never parsed and loaded
image

Changing that to normal __init__ fixes the issue.
image

@Amnish04
Copy link
Owner

Amnish04 commented Nov 17, 2023

@WangGithub0 Just pushed the fix fbd0223

You can pull latest from master

@WangGithub0
Copy link
Contributor Author

@WangGithub0 Just pushed the fix fbd0223

You can pull latest from master

will the test run again automatically?

@Amnish04
Copy link
Owner

@WangGithub0 Just pushed the fix fbd0223
You can pull latest from master

will the test run again automatically?

Yes

@Amnish04
Copy link
Owner

Amnish04 commented Nov 18, 2023

@WangGithub0 Just pushed the fix fbd0223
You can pull latest from master

will the test run again automatically?

@WangGithub0
You'll have to pull the latest from upstream master and push to your branch on origin for the workflow to pass.

@Amnish04
Copy link
Owner

@WangGithub0 Thanks for the contribution!

@Amnish04 Amnish04 merged commit f345530 into Amnish04:master Nov 18, 2023
1 check passed
@WangGithub0 WangGithub0 deleted the add-test-for-commandline branch November 18, 2023 03:30
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.

Create test for src/utils/commandline.py file
2 participants