Skip to content

Fix test_console failure finding pipdeptree in other environments #351

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

Merged
merged 2 commits into from
Apr 13, 2024

Conversation

xiacunshun
Copy link
Collaborator

When we are using pytest in packaging process, we may not get the /usr/bin/pipdeptree as we need. So, let's use the PATH env to get the binary instead.

fix: #348

Copy link
Member

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

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

I don't like this, if someone's has another pipdeptree on an earlier path we'd wrongfully accept it.

@kemzeb
Copy link
Collaborator

kemzeb commented Apr 11, 2024

Since it looks like this test exists to ensure that our pipdeptree console script exists, I believe an alternative way to test this without relying on a file path is to utilize importlib.metdata.Distribution.entry_points(). We can do something like the following:

d = distribution("pipdeptree")
our_entry_points = d.entry_points
assert len(our_entry_points) == 1
pipdeptree_entry_point = our_entry_points[0]
pipdeptree_module = pipdeptree_entry_point.load()
return_code = pipdeptree_module()
assert return_code == 0

The load() call will return the module associated to the entry point (so pipdeptree.__main__) and we can execute it from there.

@xiacunshun xiacunshun force-pushed the fix_bin_not_found_when_packaging branch from 4307c4a to 31ffbe7 Compare April 11, 2024 03:12
@xiacunshun xiacunshun force-pushed the fix_bin_not_found_when_packaging branch 2 times, most recently from 17c6317 to abb9863 Compare April 11, 2024 04:39
gaborbernat
gaborbernat previously approved these changes Apr 11, 2024
kemzeb
kemzeb previously approved these changes Apr 11, 2024
@xiacunshun
Copy link
Collaborator Author

Emm, sorry, I retry this test in packaging, but looks failed also?

____________________________________________________ test_console ____________________________________________________

    def test_console() -> None:
        d = Distribution.from_name("pipdeptree")
        our_entry_points = d.entry_points
>       assert len(our_entry_points) == 1
E       assert 0 == 1
E        +  where 0 = len([])

@kemzeb
Copy link
Collaborator

kemzeb commented Apr 11, 2024

Emm, sorry, I retry this test in packaging, but looks failed also?

____________________________________________________ test_console ____________________________________________________

    def test_console() -> None:
        d = Distribution.from_name("pipdeptree")
        our_entry_points = d.entry_points
>       assert len(our_entry_points) == 1
E       assert 0 == 1
E        +  where 0 = len([])

That's weird. It's able to find pipdeptree's metadata but can't find its console scripts? Can you provide the steps you ran before running the test

@xiacunshun
Copy link
Collaborator Author

I think I figure it out....
After the step "%generate_buildrequires" in spec file, a dist-info is created in source directory.

<mock-chroot> sh-5.2# ls pipdeptree-2.18.0/
LICENSE  README.md  pipdeptree-2.18.0.dist-info  pyproject.toml  src  tests  tox.ini

But it only has a METADATA under it.

<mock-chroot> sh-5.2# ls pipdeptree-2.18.0/pipdeptree-2.18.0.dist-info/
METADATA

When we run pytest, current directory is added to sys.path which cause distribution can find the fake dist-info and recognizes it as the pipdeptree.

@xiacunshun
Copy link
Collaborator Author

I am not sure why they do this, but it looks affect our testcases.
see https://src.fedoraproject.org/rpms/python-pipdeptree/blob/rawhide/f/python-pipdeptree.spec

@xiacunshun
Copy link
Collaborator Author

Emm, sorry, I retry this test in packaging, but looks failed also?

____________________________________________________ test_console ____________________________________________________

    def test_console() -> None:
        d = Distribution.from_name("pipdeptree")
        our_entry_points = d.entry_points
>       assert len(our_entry_points) == 1
E       assert 0 == 1
E        +  where 0 = len([])

That's weird. It's able to find pipdeptree's metadata but can't find its console scripts? Can you provide the steps you ran before running the test

I download their src.rpm(here), uncompress it, replace the sources with our new code and then rpmbuild -ba ~/rpmbuild/SPECS/python-pipdeptree.spec.
(There are also some steps to deal with dependencies.)

@kemzeb
Copy link
Collaborator

kemzeb commented Apr 11, 2024

So from reading the Fedora spec dealing with Python packages and this Python spec, it looks like packaging tools aren't required to create an entry_points.txt metadata file if they don't want to. But they must at least generate a METADATA file.

I'm not familiar with the packaging process, let alone with Fedora, but this does reflect your outputs since we only see the METADATA file (although the Fedora docs explain that the source and the metadata should be installed in "%{python3_sitelib} or %{python3_sitearch}" rather than within the source directory?).

I'm unsure why it is not required for entry_points.txt to be generated when there are console scripts that exist, but because of this testing by using the importlib API here may only work for package installers that do generate entry_points.txt if possible (e.g. pip).

@kemzeb
Copy link
Collaborator

kemzeb commented Apr 11, 2024

There looks to be a pytest plugin just for testing console scripts, and from looking at the source it seems that it will either:

  • Load the script using importlib.metadata.entry_points() (see here)
  • Look for the script in either PATH or the cwd if it doesn't find the entry point when using importlib.metadata (see here)

@xiacunshun xiacunshun dismissed stale reviews from kemzeb and gaborbernat via 0d277d7 April 12, 2024 07:18
@xiacunshun xiacunshun force-pushed the fix_bin_not_found_when_packaging branch from 77c2e32 to 0d277d7 Compare April 12, 2024 07:18
@xiacunshun
Copy link
Collaborator Author

There looks to be a pytest plugin just for testing console scripts, and from looking at the source it seems that it will either:

  • Load the script using importlib.metadata.entry_points() (see here)
  • Look for the script in either PATH or the cwd if it doesn't find the entry point when using importlib.metadata (see here)

This approach worked and looks solve the problem in packaging!
I retried, and console test passed.

When we are using pytest in packaging process, we may not
get the `/usr/bin/pipdeptree` as we need. So, let's use
`script_runner` to check it.

Signed-off-by: cunshunxia <[email protected]>
@xiacunshun xiacunshun force-pushed the fix_bin_not_found_when_packaging branch from d3d69a4 to d45827d Compare April 13, 2024 14:20
@kemzeb kemzeb changed the title fix test_console failed when doing packaging(#348). Fix test_console failure finding pipdeptree in other environments Apr 13, 2024
@kemzeb
Copy link
Collaborator

kemzeb commented Apr 13, 2024

Will release 2.18.1 after merging this

@kemzeb kemzeb enabled auto-merge (squash) April 13, 2024 15:53
@kemzeb kemzeb merged commit 6d4d4a7 into tox-dev:main Apr 13, 2024
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.

2.17.0: pytest fails in 3 units
3 participants