cdepillabout / pretty-simple

pretty-printer for Haskell data types that have a Show instance
https://hackage.haskell.org/package/pretty-simple
BSD 3-Clause "New" or "Revised" License
243 stars 29 forks source link

add tests to make sure tty detection works #48

Open cdepillabout opened 5 years ago

cdepillabout commented 5 years ago

PR #47 changes functions like pPrint so that they check whether the output Handle is a TTY device or not. If it is not, then outputting in color is disabled by default. (Thanks @metiulekm!)

This works well, but it would be nice to have tests to make sure that outputting in color is getting enabled/disabled when it should.

Basically, the tests should open a pseudo terminal and try outputting to it with a function like pHPrint. The tests should also try opening a normal file and outputting to it with the same function. The tests should check that when outputting to a PTY, the output is in color, but when outputting to a normal file, output is not in color.

I'm not too familiar with how PTY works on Linux. It looks like there is a function for opening a PTY:

http://hackage.haskell.org/package/unix-2.7.2.2/docs/System-Posix-Terminal.html#v:openPseudoTerminal

As well as a function for converting an FD to a Handle:

http://hackage.haskell.org/package/unix-2.7.2.2/docs/System-Posix-IO.html#v:fdToHandle

However, I'm not sure how to use the master and slave PTY FDs.

This book apparently has a description of how to use PTYs in chapter 64:

https://nostarch.com/tlpi

Currently there is no test suite for unit tests in pretty-simple, so you'll have to add one. I suggest using tasty or hspec.

cdepillabout commented 5 years ago

I added a beginner-friendly label to this issue, since the Haskell code you have to write should be relatively simple. However, figuring how to to write to and read from a PTY might be a little complex.

georgefst commented 4 years ago

FWIW, pPrint currently doesn't use colour when invoked from a test suite with cabal test. pPrintOpt NoCheckColorTty defaultOutputOptionsDarkBg works successfully, so it would be nice if the library detected that it's possible.

Perhaps some relevant info here, though the story seems to have improved with cabal 3.