Skip to content

Commit 1774f60

Browse files
committed
Add support for NO_COLOR environment variable
This overrides adding the `colorOption` argument to the restart command line if the NO_COLOR environment variable exists and no specific color argument has been provided. See: https://no-color.org/
1 parent fdbf7cf commit 1774f60

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Process.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public static function addColorOption(array $args, $colorOption)
5050
return $args;
5151
}
5252

53+
// Check for NO_COLOR variable (https://no-color.org/)
54+
if (false !== getenv('NO_COLOR')) {
55+
return $args;
56+
}
57+
5358
if (false !== ($index = array_search('--', $args))) {
5459
// Position option before double-dash delimiter
5560
array_splice($args, $index, 0, $colorOption);

tests/ColourOptionTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,33 @@
1818
*/
1919
class ColorOptionTest extends TestCase
2020
{
21+
private static $nocolor;
22+
23+
/**
24+
* Saves the NO_COLOR environment variable
25+
*/
26+
public static function setUpBeforeClass()
27+
{
28+
self::$nocolor = getenv('NO_COLOR');
29+
}
30+
31+
/**
32+
* Restores the original NO_COLOR environment variable
33+
*/
34+
public static function tearDownAfterClass()
35+
{
36+
$value = false !== self::$nocolor ? '='.self::$nocolor : '';
37+
putenv('NO_COLOR'.$value);
38+
}
39+
40+
/**
41+
* Unsets the NO_COLOR environment variable for each test
42+
*/
43+
protected function setUp()
44+
{
45+
putenv('NO_COLOR');
46+
}
47+
2148
/**
2249
* Tests that a colorOption is added to the arguments
2350
*
@@ -29,6 +56,18 @@ public function testOptionNeeded($args, $colorOption, $expected)
2956
$this->assertSame($expected, implode(' ', $result));
3057
}
3158

59+
/**
60+
* Tests that a colorOption is not added if NO_COLOR is set
61+
*
62+
* @dataProvider neededProvider
63+
*/
64+
public function testOptionNeededNoColor($args, $colorOption, $unused)
65+
{
66+
putenv('NO_COLOR=1');
67+
$result = Process::addColorOption($args, $colorOption);
68+
$this->assertSame($args, $result);
69+
}
70+
3271
public function neededProvider()
3372
{
3473
// $args, $colorOption, $expected

0 commit comments

Comments
 (0)