11

When I run phpunit from a command line, the control characters are being printed out instead of acting like control characters. Take look at this:

PHPUnit 3.6.5 by Sebastian Bergmann.

Configuration read from app\phpunit.xml.dist

...

Time: 1 second, Memory: 12.00Mb

‹[30;42m‹[2KOK (3 tests, 3 assertions)
‹[0m‹[2K

I assume that signs like ‹[30;42m< are some kind of control characters and should be used by console in different way (positioning the cursor, deleting characters etc.)

What can be wrong here?

edorian
  • 38,542
  • 15
  • 125
  • 143
Dawid Ohia
  • 16,129
  • 24
  • 81
  • 95
  • 1
    Cross Referenced: [How to see color Ant output in MSYS/Git Bash?](http://stackoverflow.com/questions/8129363/how-to-see-color-ant-output-in-msys-git-bash) – hakre Dec 30 '11 at 22:05
  • 1
    Super User: [PHPUnit Windows command box nice colors](http://superuser.com/q/208150/63279) – hakre Jun 24 '13 at 23:52

5 Answers5

11

This happens because you have configured phpunit to use colors.

<phpunit colors="true"

but sadly it is not possible to create colored output on a Windows terminal.

There is an open issue to not show those chars on windows where they can't be translated into colors on the phpunit issues tracker and I'm working on a patch for that.

For now all you can do is to ether accept it or to remove the color="true" from your phpunit.xml configuration file.

edorian
  • 38,542
  • 15
  • 125
  • 143
  • Why should it raise a notice when it was explicitly asked for the ANSI color codes? – hakre Dec 30 '11 at 12:45
  • @hakre To tell you that its not supported on Windows and so you know you should disable it in your config. After all, that's the problem the OP is facing and s/he wouldn't need to ask if there was a notice. – Gordon Dec 30 '11 at 12:53
  • Well if it's your config, then you must have enabled it, didn't you? Probably the pear installer should take care of platform dependent default values, but that would be a packaging problem, not a problem inside PHPUnit itself. – hakre Dec 30 '11 at 13:16
  • @harke not necessarily. you might work with someone else who is on linux and s/he enabled it and then you update your local repo and then you got ctrl chars. its a valid request to inform people about incompatible settings. i dont see what there is to argue about it. – Gordon Dec 30 '11 at 13:58
  • @hakre If I find a way to detect if the terminal is color compatible we'll use that. If not I guess I just turn it for windows stdout – edorian Dec 30 '11 at 14:02
  • @Gordon: That's probably better configured in the bootstrap.php instead of the XML file then. E.g. add the warning if you detect *your specific case* that needs a warning in your project. The Writer has other things to do. – hakre Dec 30 '11 at 15:11
  • @edorian: Probably create a `MonchromeOnWindowsTextWriter` that works for the intended purpose. – hakre Dec 30 '11 at 15:13
11

Alternatively, just use https://github.com/adoxa/ansicon/releases to get ansi colors on windows.

Source code: https://github.com/adoxa/ansicon

Musa Haidari
  • 2,109
  • 5
  • 30
  • 53
cweiske
  • 30,033
  • 14
  • 133
  • 194
  • A lot nicer than the options I found. Good to know that exists :) – edorian Dec 30 '11 at 13:59
  • it's down, can u reupload it. – Jürgen Paul Aug 18 '12 at 02:32
  • Updated URL is http://adoxa.altervista.org/ansicon/, which can be found from the github link. Use ansicon -i to install permenantly for the current user (or -I for local machine). Also enables colors for Git Bash on Windows. – Reuben May 08 '14 at 21:49
4

I just recently ran into this same problem in trying to run phpunit from the command line in git bash on windows 7. After conducting some research into possible various solutions I decided to share the solution I chose to implement for myself, here.

You can filter out the ANSI color control characters from git bash. Create a file named phpunit (note: the actual phpunit script wasn't in my path and I was mainly running unit tests from intellij only) and put it anywhere in your $PATH (I prefer ~/bin myself but there's no rule about that):

#!/bin/sh
/path/to/phpunit "$@" 2>&1 | perl -pe 's/(?<=\e\[)2;//g'

The "$@" tells bash to take the rest of the arguments passed to the script and forward them to phpunit. The 2>&1 redirects stderr to stdout, ensuring that any control characters generated in producing error output will also be filtered out.

Finally, all of the output produced by phpunit is piped through perl and run through the regular expression 's/(?<=\e\[)2;//g', which strips out the control characters.

The end result is that phpunit runs just fine, regardless of what <phpunit colors="" setting you are using.

Hope this helps!

Community
  • 1
  • 1
Daniel Miladinov
  • 1,582
  • 9
  • 20
0

Thanx Danny, your answer was very helpful.

For users that want to implement this a few tips:

  1. put the code from Danny in a shell file (for example ~/.phpunit.sh)
  2. add an alias to phpunit in ~/.bashrc (just add the following line). If you don't have a .bashrc yet, just create an empty file.

    alias phpunit="~/.phpunit.sh"

  3. close your bash and open it again

  4. things should work now, but test it to be sure.
Raymond
  • 29
  • 1
  • 3
0

Just like what they said, this worked for me.

Go to .bashrc in your ~/ directory and add

alias phpunitc="phpunit "$@" 2>&1 | perl -pe 's/(?<=\e\[)2;//g'"

Then I just use phpunitc in git bash. All parameters you send to phpunit will also pass through.

brunoais
  • 6,258
  • 8
  • 39
  • 59
paoloumali
  • 65
  • 2
  • 6