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

Question: How to invoke from within a test #96

Open
bemeyvif opened this issue Nov 18, 2024 · 2 comments
Open

Question: How to invoke from within a test #96

bemeyvif opened this issue Nov 18, 2024 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@bemeyvif
Copy link

Before You Begin

Before proceeding, please make sure to follow these steps:

  • [ x] I have checked for similar questions in the project's issue tracker to avoid duplicates.
  • [ x] I have searched existing issues to see if this question has been asked before.

Your Question

Is there a way to call license check from withing a pytest test-case in a github action?

  • I run tests as follows: uv run --frozen pytest tests/test_my_api.py
  • And run licensecheck as follows: licensecheck --ignore-packages package-without-license --ignore-license mpl --only-licenses mit bsd apache python --zero

I would like to call licensecheck from inside a unit test with parameters to skip certain package, ignore mpl licenses and allow only for mit/bsd/apacke and python.

Thanks!

@bemeyvif bemeyvif added the question Further information is requested label Nov 18, 2024
@FredHappyface
Copy link
Member

Thanks for raising this issue! The request to invoke licensecheck from within a pytest test case is an interesting use case, but it diverges somewhat from the original intent of this package. This tool was designed primarily to run in CI/CD pipelines or as part of pre-commit hooks, acting as a lightweight compliance check rather than a utility tightly coupled with unit tests.

That said, if you wish to call licensecheck programmatically within a pytest test case, it should be possible by leveraging Python's subprocess module. Or possibly by importing the library depending on what functionality you need. The tests in this repo may provide a good starting point

@Triavanicus
Copy link

I have a task.py file, which I use as a task runner, which does precisely this thing (sort of). The only code you would need inside of pytest would be:

def test_check_licenses():
    import subprocess
    assert subprocess.run("licensecheck -0 <whatever other args>".split(sep=" ")) == 0

However, this couples the tests for your code for analyzers ran on your code, and this sort of thing is best handled by something like tox, which is meant to run things on your code/project for validation in CI/CD (including running your tests). This has the added benefit of being able to view the output of the code and keep it logged in your CI/CD environment. Tox has the added benefit of managing its own virtual environment so as to make it more consistent. The reason why I have a task runner on top of this is that tox can take a minute to set up, and sometimes you just want to run your validations quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants