ConfigPathsTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2023 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Test;
  11. use Psy\ConfigPaths;
  12. class ConfigPathsTest extends TestCase
  13. {
  14. public function testOverrideDirs()
  15. {
  16. $paths = new ConfigPaths([
  17. 'configDir' => 'foo',
  18. 'dataDir' => 'bar',
  19. 'runtimeDir' => 'baz',
  20. ], new TestableEnv());
  21. $this->assertSame(['foo'], $paths->configDirs());
  22. $this->assertSame(['bar'], $paths->dataDirs());
  23. $this->assertSame('baz', $paths->runtimeDir());
  24. $paths->overrideDirs([
  25. 'configDir' => 'qux',
  26. ]);
  27. $this->assertSame(['qux'], $paths->configDirs());
  28. $this->assertSame(['bar'], $paths->dataDirs());
  29. $this->assertSame('baz', $paths->runtimeDir());
  30. $paths->overrideDirs([
  31. 'configDir' => 'a',
  32. 'dataDir' => 'b',
  33. 'runtimeDir' => null,
  34. ]);
  35. $this->assertSame(['a'], $paths->configDirs());
  36. $this->assertSame(['b'], $paths->dataDirs());
  37. $this->assertSame(\sys_get_temp_dir().'/psysh', $paths->runtimeDir());
  38. }
  39. /**
  40. * @dataProvider envVariablesAndPathOverrides
  41. */
  42. public function testEnvVariables($env, $overrides, $configDir, $dataDirs, $runtimeDir)
  43. {
  44. $paths = new ConfigPaths($overrides, new TestableEnv($env));
  45. $this->assertSame(\realpath($configDir), \realpath($paths->currentConfigDir()));
  46. $this->assertEquals(\array_map('realpath', $dataDirs), \array_values(\array_filter(\array_map('realpath', $paths->dataDirs()))));
  47. $this->assertSame(\realpath($runtimeDir), \realpath($paths->runtimeDir()));
  48. }
  49. public function envVariablesAndPathOverrides()
  50. {
  51. $base = \realpath(__DIR__.'/fixtures');
  52. return [
  53. // Mimic actual config directory structure, with no XDG config set.
  54. [
  55. [
  56. 'HOME' => $base.'/default',
  57. ],
  58. [],
  59. $base.'/default/.config/psysh',
  60. [
  61. $base.'/default/.local/share/psysh',
  62. ],
  63. \sys_get_temp_dir().'/psysh',
  64. ],
  65. [
  66. [
  67. 'HOME' => $base.'/legacy',
  68. ],
  69. [],
  70. $base.'/legacy/.psysh',
  71. [
  72. $base.'/legacy/.psysh',
  73. ],
  74. \sys_get_temp_dir().'/psysh',
  75. ],
  76. [
  77. [
  78. 'HOME' => $base.'/mixed',
  79. ],
  80. [],
  81. $base.'/mixed/.psysh',
  82. [
  83. $base.'/mixed/.psysh',
  84. ],
  85. \sys_get_temp_dir().'/psysh',
  86. ],
  87. // Same as before but with XDG stuffs!
  88. [
  89. [
  90. 'HOME' => $base.'/default',
  91. 'XDG_CONFIG_HOME' => $base.'/xdg/config',
  92. 'XDG_DATA_HOME' => $base.'/xdg/data',
  93. 'XDG_RUNTIME_DIR' => $base.'/xdg/runtime',
  94. ],
  95. [],
  96. $base.'/xdg/config/psysh',
  97. [
  98. $base.'/xdg/data/psysh',
  99. ],
  100. $base.'/xdg/runtime/psysh',
  101. ],
  102. [
  103. [
  104. 'HOME' => $base.'/default',
  105. 'XDG_CONFIG_HOME' => $base.'/xdg/config',
  106. 'XDG_DATA_HOME' => $base.'/xdg/data',
  107. 'XDG_RUNTIME_DIR' => $base.'/xdg/runtime',
  108. ],
  109. [
  110. 'configDir' => $base.'/default/.config/psysh',
  111. 'dataDir' => $base.'/legacy/.psysh',
  112. 'runtimeDir' => $base.'/legacy/.psysh',
  113. ],
  114. $base.'/default/.config/psysh',
  115. [
  116. $base.'/legacy/.psysh',
  117. ],
  118. $base.'/legacy/.psysh',
  119. ],
  120. ];
  121. }
  122. /**
  123. * @dataProvider envVariablesForPathDirs
  124. */
  125. public function testPathDirs($pathEnv, $expectedPaths)
  126. {
  127. $paths = new ConfigPaths([], new TestableEnv(['PATH' => $pathEnv]));
  128. $this->assertEquals($expectedPaths, $paths->pathDirs());
  129. }
  130. public function envVariablesForPathDirs()
  131. {
  132. $base = \realpath(__DIR__.'/fixtures/which');
  133. return [
  134. [
  135. null,
  136. ['/usr/sbin', '/usr/bin', '/sbin', '/bin'],
  137. ],
  138. [
  139. "$base/usr/sbin:$base/usr/bin:$base/sbin:$base/home/foo/bin:$base/bin",
  140. [
  141. "$base/usr/sbin",
  142. "$base/usr/bin",
  143. "$base/sbin",
  144. "$base/home/foo/bin",
  145. "$base/bin",
  146. ],
  147. ],
  148. ];
  149. }
  150. public function testWhich()
  151. {
  152. $base = \realpath(__DIR__.'/fixtures/which');
  153. $paths = new ConfigPaths([], new TestableEnv([
  154. 'PATH' => "$base/home/username/bin:$base/usr/sbin:$base/usr/bin:$base/sbin:$base/bin",
  155. ]));
  156. $this->assertSame("$base/home/username/bin/foo", $paths->which('foo'));
  157. $this->assertSame("$base/usr/bin/bar", $paths->which('bar'));
  158. $this->assertNull($paths->which('baz'));
  159. $paths = new ConfigPaths([], new TestableEnv([
  160. 'PATH' => "$base/usr/bin",
  161. ]));
  162. $this->assertSame("$base/usr/bin/foo", $paths->which('foo'));
  163. $this->assertSame("$base/usr/bin/bar", $paths->which('bar'));
  164. $this->assertNull($paths->which('baz'));
  165. // Fakebin has a bunch of directories named the same thing as the
  166. // commands we're looking for...
  167. $paths = new ConfigPaths([], new TestableEnv([
  168. 'PATH' => "$base/fakebin:$base/usr/bin",
  169. ]));
  170. $this->assertSame("$base/usr/bin/foo", $paths->which('foo'));
  171. $this->assertSame("$base/usr/bin/bar", $paths->which('bar'));
  172. $this->assertNull($paths->which('baz'));
  173. // Notexec has a bunch of files without an executable bit named the same
  174. // thing as the commands we're looking for...
  175. $paths = new ConfigPaths([], new TestableEnv([
  176. 'PATH' => "$base/notexec:$base/usr/bin",
  177. ]));
  178. $this->assertSame("$base/usr/bin/foo", $paths->which('foo'));
  179. $this->assertSame("$base/usr/bin/bar", $paths->which('bar'));
  180. $this->assertNull($paths->which('baz'));
  181. // Paths defined but missing commands return null
  182. $paths = new ConfigPaths([], new TestableEnv([
  183. 'PATH' => "$base",
  184. ]));
  185. $this->assertNull($paths->which('foo'));
  186. $this->assertNull($paths->which('bar'));
  187. $this->assertNull($paths->which('baz'));
  188. }
  189. }