[pytest-dev] What's the best way of writing tests for terminal width resize behaviour in an assertrepr plugin?

Zach Kanzler they4kman at gmail.com
Wed Apr 8 23:48:20 EDT 2020


I wrote an icdiff-based pretty diffs pytest plugin for internal use, and
ran into the same issue. I ended up grabbing the terminal width at
module-level, as well. I think I went spelunking in the GH issues and found
a glimmer of possibility in the notion that get_terminal_width() returns
incorrect information from within the assertrepr hook, because it's called
when terminal capturing is enabled — perhaps even just a wrapped stream?

Here's the note I left myself:

# This is done during initialization, before any tests are run, instead of
# within our assertrepr hook — because the assertrepr hook is called while
# terminal capturing is enabled and all calls to get_terminal_width()
return 80
# ref:
https://github.com/pytest-dev/pytest/issues/4030#issuecomment-425672782
INITIAL_TERM_WIDTH, INITIAL_TERM_HEIGHT = util.get_terminal_size()


(Where util.get_terminal_size() just comes from this gist
<https://gist.github.com/jtriley/1108174>)

I wonder if a tool like screen or tmux could supply real sizing info and
resize events, while also appearing like an unwrapped stream — and perhaps
its output could be saved & compared against.

Cheers,
Zach

On Wed, Apr 8, 2020 at 4:55 PM Floris Bruynooghe <flub at devork.be> wrote:

> Hi Harry,
>
> To the best of my knowledge you stumbled into a hard and dark corner of
> pytest.  IIRC previous attempts at improving this have not gone too
> well.  But like this always goes, it sounds like you've set yourself up
> to be the expert now and are in a great position to clean this up and
> make it work nicely for people.  ;)
>
> Cheers,
> Floris
>
> On Tue 07 Apr 2020 at 19:26 +0100, Harry Percival wrote:
>
> > cf discussion here https://github.com/pytest-dev/pytest/issues/162
> >
> > I'm working on pytest-icdiff, trying to get it to print out well
> optimised
> > messages for different terminal sizes, and it's hard.
> >
> > when i'm running tests using runpytest, and I _think_ in real life too,
> > config.get_terminal_writer().fullwidth *inside the hook* is returning
> > incorrect values: resizing the terminal i'm using to run the tests has no
> > effect on the value i get from it.  and when I use that value irl, real
> > test output does not adjust correctly to the terminal size
> >
> > but calling py.io.TerminalWriter().fullwidth _outside_ the hook, eg at
> > module level, _does_ give me readings that look right.  but testing  that
> > is hard.
> >
> > intriguingly, if I do testdir.monkeypatch.setenv('COLUMNS', '50'), then
> the
> > fullwdith *inside* the hook varies.  but that's no use to me, since IRL
> > that value doesn't work.
> >
> > so my question is: a) does anyone know of any good examples of assertrepr
> > plugins that adjust dynamically to terminal width, and b) what's a good
> way
> > of writing tests for that?
> >
> > here's the repo fwiw
> >
> https://github.com/hjwp/pytest-icdiff/blob/master/tests/test_pytest_icdiff.py
> > _______________________________________________
> > pytest-dev mailing list
> > pytest-dev at python.org
> > https://mail.python.org/mailman/listinfo/pytest-dev
> _______________________________________________
> pytest-dev mailing list
> pytest-dev at python.org
> https://mail.python.org/mailman/listinfo/pytest-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20200408/9e9272f0/attachment.html>


More information about the pytest-dev mailing list