Rockhopper-Technologies / enlighten

Enlighten Progress Bar for Python Console Apps
https://python-enlighten.readthedocs.io
Mozilla Public License 2.0
421 stars 25 forks source link

TestManager.test_autorefresh fails on macOS #44

Closed veprbl closed 3 years ago

veprbl commented 3 years ago

Describe the bug A following test fails on macOS, but not on linux (with the same dependency versions)

_________________________ TestManager.test_autorefresh _________________________

self = <tests.test_manager.TestManager testMethod=test_autorefresh>

    def test_autorefresh(self):
        """
        Ensure auto-refreshed counters are updated when others are
        """

        manager = enlighten.Manager(stream=self.tty.stdout)
        counter1 = manager.counter(count=1, total=0, counter_format=u'counter1', autorefresh=True)
        counter2 = manager.counter(count=1, total=0, counter_format=u'counter2')
        self.tty.clear()

        # Counter 1 in auto-refresh list
        self.assertIn(counter1, manager.autorefresh)

        # If auto-refreshed counter hasn't been refreshed recently refresh
        counter1.last_update = 0
        counter2.refresh()
        self.tty.stdout.write(u'X\n')
        output = self.tty.stdread.readline()
>       self.assertRegex(output, 'counter2.+counter1')
E       AssertionError: Regex didn't match: 'counter2.+counter1' not found in '\x1b[25;1H\n'

tests/test_manager.py:364: AssertionError

To Reproduce

  1. install enlighten
  2. run tests
    git clone https://github.com/Rockhopper-Technologies/enlighten.git
    cd enlighten
    pytest

Environment (please complete the following information):

avylove commented 3 years ago

It looks like termios.tcflush(stream, termios.TCIFLUSH) is not behaving as expected on MacOS.

You can see here where self.tty.clear() is called after the counters are setup. This removes anything written to stdout on the mock TTY.

https://github.com/Rockhopper-Technologies/enlighten/blob/d296133c921f6d071c687212b943f84ed964baf3/tests/test_manager.py#L351-L354

In tests.MockTTY.clear(), termios.tcflush() gets called

https://github.com/Rockhopper-Technologies/enlighten/blob/d296133c921f6d071c687212b943f84ed964baf3/tests/__init__.py#L138-L139

According to the man page this should behave the same in MacOS, but, there are a lot of undocumented "features" on Macs.

I don't have a Mac to test with, so I'll need your help on this. Can you try changing it from TCIFLUSH to TCIOFLUSH and let me know if that works. It does more than necessary, but might provide the functionality we need.

If that doesn't work, I'm tempted just to skip this test on Macs. The alternative is to read the number of lines produced by the setup. This is done in other places in the code, but I'm trying to get away from that because it's harder to maintain.

veprbl commented 3 years ago

I don't have a Mac to test with, so I'll need your help on this. Can you try changing it from TCIFLUSH to TCIOFLUSH and let me know if that works. It does more than necessary, but might provide the functionality we need.

That worked like a charm. Let me know if you need to try something else.

avylove commented 3 years ago

Fix is now in the master branch. So your original workflow should succeed now. Thanks for your help!