From c714662612ebe5dc2b5519b451d59df1d97e21c3 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 28 Jun 2025 18:10:33 +0200 Subject: [PATCH] Allow absolute paths in Environment::path() --- src/main/php/web/Environment.class.php | 3 +- .../web/unittest/EnvironmentTest.class.php | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/php/web/Environment.class.php b/src/main/php/web/Environment.class.php index 48d3fcda..ec1f826d 100755 --- a/src/main/php/web/Environment.class.php +++ b/src/main/php/web/Environment.class.php @@ -73,7 +73,8 @@ public function tempDir() { * @return io.Path */ public function path($path) { - return new Path($this->webroot, $path); + $p= $path instanceof Path ? $path : new Path($path); + return $p->isAbsolute() ? $p : new Path($this->webroot, $p); } /** diff --git a/src/test/php/web/unittest/EnvironmentTest.class.php b/src/test/php/web/unittest/EnvironmentTest.class.php index 8229f6ab..c72e83e3 100755 --- a/src/test/php/web/unittest/EnvironmentTest.class.php +++ b/src/test/php/web/unittest/EnvironmentTest.class.php @@ -3,6 +3,7 @@ use io\{File, Files, Path}; use lang\{ElementNotFoundException, Environment as System}; use test\{Assert, Expect, Test, Values}; +use test\verify\Runtime; use util\{Properties, PropertySource, RegisteredPropertySource}; use web\{Environment, Logging}; @@ -89,6 +90,33 @@ public function path() { ); } + #[Test] + public function relative_path() { + $environment= new Environment('dev', '.', 'static', []); + Assert::equals( + new Path($environment->webroot(), 'src/main/handlebars'), + $environment->path('src/main/handlebars') + ); + } + + #[Test, Runtime(os: 'BSD|Darwin|Solaris|Linux')] + public function absolute_unix_path() { + $environment= new Environment('dev', '.', 'static', []); + Assert::equals( + new Path('/etc'), + $environment->path('/etc') + ); + } + + #[Test, Runtime(os: 'Windows')] + public function absolute_windows_path() { + $environment= new Environment('dev', '.', 'static', []); + Assert::equals( + new Path('C:\\Windows'), + $environment->path('C:\\Windows') + ); + } + #[Test] public function non_existant_variable() { putenv('XP_TEST');