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

running unittest with coverage fails with Pypy 3.10 for some tests #1902

Closed
xqt opened this issue Dec 7, 2024 · 7 comments
Closed

running unittest with coverage fails with Pypy 3.10 for some tests #1902

xqt opened this issue Dec 7, 2024 · 7 comments
Labels
bug Something isn't working fixed

Comments

@xqt
Copy link

xqt commented Dec 7, 2024

Describe the bug
running unittest with coverage > 7.6.1 fails with pypy 3.10 for some tests. It works with coverage 7.6.1 and below

To Reproduce

  1. What version of Python are you using?
    -> pypy 3.10
    Python: 3.10.14 (39dc8d3c85a7, Aug 27 2024, 14:32:27)
    [PyPy 7.3.17 with GCC 10.2.1 20210130 (Red Hat 10.2.1-11)]

  2. What version of coverage.py shows the problem?
    -> 7.6.2 - 7.6.9

  3. What versions of what packages do you have installed?
    -> see https://github.com/wikimedia/pywikibot/actions/runs/12211147365/job/34068904767 for example

  4. What code shows the problem?
    -> The specific test failing is this:

@patch('builtins.import', side_effect=ImportError, autospec=True)
class TestNoBeautifulSoup(TestCase):


    """Test functions when BeautifulSoup is not installed."""

    net = False

    def test_html_comparator(self, mocked_import):
        """Test html_comparator when bs4 not installed."""
        with self.assertRaises(ImportError):
            html_comparator('')
        self.assertEqual(mocked_import.call_count, 1)
        self.assertIn('bs4', mocked_import.call_args[0])

The code can be found here:
https://github.com/wikimedia/pywikibot/blob/master/tests/diff_tests.py

The shown problem is

======================================================================
ERROR: test_html_comparator (tests.diff_tests.TestNoBeautifulSoup)
Test html_comparator when bs4 not installed.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/hostedtoolcache/PyPy/3.10.14/x64/lib/pypy3.10/unittest/mock.py", line 1379, in patched
    return func(*newargs, **newkeywargs)
  File "/home/runner/work/pywikibot/pywikibot/tests/diff_tests.py", line 96, in test_html_comparator
    with self.assertRaises(ImportError):
  File "/opt/hostedtoolcache/PyPy/3.10.14/x64/lib/pypy3.10/site-packages/coverage/pytracer.py", line 258, in _trace
    cast(set[TLineNo], self.cur_file_data).add(flineno)
  File "<string>", line 3, in __import__
  File "/opt/hostedtoolcache/PyPy/3.10.14/x64/lib/pypy3.10/unittest/mock.py", line 1114, in __call__
    return self._mock_call(*args, **kwargs)
  File "/opt/hostedtoolcache/PyPy/3.10.14/x64/lib/pypy3.10/unittest/mock.py", line 1118, in _mock_call
    return self._execute_mock_call(*args, **kwargs)
  File "/opt/hostedtoolcache/PyPy/3.10.14/x64/lib/pypy3.10/unittest/mock.py", line 1173, in _execute_mock_call
    raise effect
ImportError

  1. What commands should we run to reproduce the problem?
    -> coverage run -m unittest discover -vv -p "*_tests.py";

Expected behavior
Test should run like it does with coverage 7.6.1

Additional context

@xqt xqt added the bug Something isn't working label Dec 7, 2024
wmfgerrit pushed a commit to wikimedia/pywikibot that referenced this issue Dec 7, 2024
see also:
nedbat/coveragepy#1902

Bug: T380732
Change-Id: I3a8942e91ef3a30f4b4fe80392030bd88c82040a
@nedbat
Copy link
Owner

nedbat commented Dec 7, 2024

When I try to run your command coverage run -m unittest discover -vv -p "*_tests.py", I get:

RuntimeError: No user-config.py found in directory '/System/Volumes/Data/src/bugs/bug1902/pywikibot'.

I see the .sample, and the pwb.py tool, but running them starts asking me questions I don't know the answer to. Please provide complete instructions for me to reproduce the failure.

@nedbat
Copy link
Owner

nedbat commented Dec 7, 2024

Trying a little harder to make a user-config.py file, I get:

FileNotFoundError: [Errno 2] No such file or directory: '/System/Volumes/Data/src/bugs/bug1902/pywikibot/pywikibot/scripts/i18n/redirect'

@nedbat nedbat added the question Further information is requested label Dec 7, 2024
@xqt
Copy link
Author

xqt commented Dec 10, 2024

Hi @nedbat: The content of user-config.py file does not care for the test, so you just can rename the given user-config.py.sample to run the test. Another way is to set a environment variable PYWIKIBOT_NO_USER_CONFIG=2 , see Pywikibot config. The test procedure could be as follows

  • checkout pywikibot from repository
    $ git clone https://gerrit.wikimedia.org/g/pywikibot/core
    or from github mirror: https://github.com/wikimedia/pywikibot
  • update submodule (I guess this step can be omitted)
    git submodule update --progress --init
  • rename user-config.py.sample to user-config.py
  • pip install -rrequirements.txt
  • pip install -rdev-requirements.txt
  • overwrite the installed coverage (7.6.1) to the newest release
    pip install --upgrade coverage
  • run the test from the repository's base directory
    coverage run -m unittest -vv tests/diff_tests.py or possibly also
    coverage run -m unittest discover -vv -p *_tests.py for the complete test run but this need a lot of time (30 min) to complete.

But be aware: the test fails for pypy3.10 only so you have to install it first and the dependencies is needed for it.

nedbat added a commit that referenced this issue Dec 11, 2024
@nedbat
Copy link
Owner

nedbat commented Dec 11, 2024

Thanks, those details made it reproducible for me. I have no idea what's going wrong, but git bisect found the commit, and reverting the change from that commit to the one line of mine in your stack trace fixed it! I think we really lucked out.

@nedbat
Copy link
Owner

nedbat commented Dec 11, 2024

This is now fixed in commit 80a479d.

@nedbat nedbat closed this as completed Dec 11, 2024
@nedbat nedbat added fixed and removed question Further information is requested labels Dec 11, 2024
@xqt
Copy link
Author

xqt commented Dec 11, 2024

Great job, thank you.

@nedbat
Copy link
Owner

nedbat commented Dec 26, 2024

This is now released as part of coverage 7.6.10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants