From f28429a2ec7e1ec9adadd4a313489ea749803b2f Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 15 Aug 2024 21:00:24 +0300 Subject: [PATCH] updated configuration and writing output action --- src/Action.php | 19 ++-- src/Configuration/Configuration.php | 17 ++-- tests/Configuration/ConfigurationTest.php | 100 +++++++++++++++++++++- 3 files changed, 115 insertions(+), 21 deletions(-) diff --git a/src/Action.php b/src/Action.php index a3ffb03..77adc71 100644 --- a/src/Action.php +++ b/src/Action.php @@ -84,21 +84,20 @@ public function run() - $GITHUB_OUTPUT = getenv('GITHUB_OUTPUT'); + $GITHUB_OUTPUT = (string) $this->configuration->get('GITHUB_OUTPUT'); - var_dump($GITHUB_OUTPUT); - exit; + if (!empty($GITHUB_OUTPUT)) { + $name = 'directory'; + $value = (string) $this->configuration->get('build.directory'); - $name = 'directory'; - $value = (string) $this->configuration->get('build.directory'); + file_put_contents($GITHUB_OUTPUT, "$name=$value\n", FILE_APPEND); - file_put_contents($GITHUB_OUTPUT, "$name=$value\n", FILE_APPEND); + $name = 'path'; + $value = (string) $this->configuration->get('build.file'); - $name = 'path'; - $value = (string) $this->configuration->get('build.file'); - - file_put_contents($GITHUB_OUTPUT, "$name=$value\n", FILE_APPEND); + file_put_contents($GITHUB_OUTPUT, "$name=$value\n", FILE_APPEND); + } } } diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index 7849f40..924d520 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -13,20 +13,23 @@ */ public function __construct () { - $root = (string) getenv('GITHUB_WORKSPACE'); + $GITHUB_WORKSPACE = (string) getenv('GITHUB_WORKSPACE'); + $GITHUB_OUTPUT = (string) getenv('GITHUB_OUTPUT'); $dirName = (string) getenv('BUILD_DIRECTORY_NAME'); $fileName = (string) getenv('BUILD_FILE_NAME'); - $root = !empty($root) ? $root : realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..'); + $GITHUB_WORKSPACE = !empty($GITHUB_WORKSPACE) ? $GITHUB_WORKSPACE : realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..'); + $GITHUB_OUTPUT = !empty($GITHUB_OUTPUT) ? $GITHUB_OUTPUT : $GITHUB_WORKSPACE . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'outputcmd.txt'; $dirName = !empty($dirName) ? $dirName : '.build'; $fileName = !empty($fileName) ? $fileName : 'package.zip'; $this->options = [ - 'root' => $root, + 'GITHUB_WORKSPACE' => $GITHUB_WORKSPACE, + 'GITHUB_OUTPUT' => $GITHUB_OUTPUT, 'build' => [ - 'directory' => $root . DIRECTORY_SEPARATOR . $dirName, - 'file' => $root . DIRECTORY_SEPARATOR . $dirName . DIRECTORY_SEPARATOR . $fileName, - ] + 'directory' => $GITHUB_WORKSPACE . DIRECTORY_SEPARATOR . $dirName, + 'file' => $GITHUB_WORKSPACE . DIRECTORY_SEPARATOR . $dirName . DIRECTORY_SEPARATOR . $fileName, + ], ]; } @@ -59,6 +62,6 @@ public function get (string $option): mixed */ public function getRootDirectory (): string { - return (string) $this->get('root'); + return (string) $this->get('GITHUB_WORKSPACE'); } } diff --git a/tests/Configuration/ConfigurationTest.php b/tests/Configuration/ConfigurationTest.php index c830a94..dab90df 100644 --- a/tests/Configuration/ConfigurationTest.php +++ b/tests/Configuration/ConfigurationTest.php @@ -14,6 +14,11 @@ class ConfigurationTest extends TestCase */ private const string DEFAULT_ENV_GITHUB_WORKSPACE = '/usr/bin/app'; + /** + * @var string + */ + private const string DEFAULT_ENV_GITHUB_OUTPUT = '/usr/bin/app/var/outputcmd.txt'; + /** * @var string */ @@ -29,6 +34,11 @@ class ConfigurationTest extends TestCase */ private const string OTHER_ENV_GITHUB_WORKSPACE = '/app'; + /** + * @var string + */ + private const string OTHER_ENV_GITHUB_OUTPUT = '/usr/bin/app/var/otheroutputcmd.txt'; + /** * @var string */ @@ -53,20 +63,59 @@ protected function setUp(): void * @covers Configuration::get * @return void */ - public function testCheckDefaultOptionRoot() + public function testCheckDefaultOptionGitHubWorkspace(): void { putenv('GITHUB_WORKSPACE=' . self::DEFAULT_ENV_GITHUB_WORKSPACE); + putenv('GITHUB_OUTPUT=' . self::DEFAULT_ENV_GITHUB_OUTPUT); putenv('BUILD_DIRECTORY_NAME=' . self::DEFAULT_ENV_BUILD_DIRECTORY_NAME); putenv('BUILD_FILE_NAME=' . self::DEFAULT_ENV_BUILD_FILE_NAME); $configuration = new Configuration(); $this->assertEquals( - $configuration->get('root'), + $configuration->get('GITHUB_WORKSPACE'), self::DEFAULT_ENV_GITHUB_WORKSPACE ); } + /** + * @covers Configuration::get + * @return void + */ + public function testCheckDefaultGetRootDirectory(): void + { + putenv('GITHUB_WORKSPACE=' . self::DEFAULT_ENV_GITHUB_WORKSPACE); + putenv('GITHUB_OUTPUT=' . self::DEFAULT_ENV_GITHUB_OUTPUT); + putenv('BUILD_DIRECTORY_NAME=' . self::DEFAULT_ENV_BUILD_DIRECTORY_NAME); + putenv('BUILD_FILE_NAME=' . self::DEFAULT_ENV_BUILD_FILE_NAME); + + $configuration = new Configuration(); + + $this->assertEquals( + $configuration->getRootDirectory(), + self::DEFAULT_ENV_GITHUB_WORKSPACE + ); + } + + /** + * @covers Configuration::get + * @return void + */ + public function testCheckDefaultOptionGitHubOutput(): void + { + putenv('GITHUB_WORKSPACE=' . self::DEFAULT_ENV_GITHUB_WORKSPACE); + putenv('GITHUB_OUTPUT=' . self::DEFAULT_ENV_GITHUB_OUTPUT); + putenv('BUILD_DIRECTORY_NAME=' . self::DEFAULT_ENV_BUILD_DIRECTORY_NAME); + putenv('BUILD_FILE_NAME=' . self::DEFAULT_ENV_BUILD_FILE_NAME); + + $configuration = new Configuration(); + + $this->assertEquals( + $configuration->get('GITHUB_OUTPUT'), + self::DEFAULT_ENV_GITHUB_OUTPUT + ); + } + /** * @covers Configuration::get * @return void @@ -74,6 +123,7 @@ public function testCheckDefaultOptionRoot() public function testCheckDefaultOptionBuildDirectory() { putenv('GITHUB_WORKSPACE=' . self::DEFAULT_ENV_GITHUB_WORKSPACE); + putenv('GITHUB_OUTPUT=' . self::DEFAULT_ENV_GITHUB_OUTPUT); putenv('BUILD_DIRECTORY_NAME=' . self::DEFAULT_ENV_BUILD_DIRECTORY_NAME); putenv('BUILD_FILE_NAME=' . self::DEFAULT_ENV_BUILD_FILE_NAME); @@ -92,6 +142,7 @@ public function testCheckDefaultOptionBuildDirectory() public function testCheckDefaultOptionBuildFile() { putenv('GITHUB_WORKSPACE=' . self::DEFAULT_ENV_GITHUB_WORKSPACE); + putenv('GITHUB_OUTPUT=' . self::DEFAULT_ENV_GITHUB_OUTPUT); putenv('BUILD_DIRECTORY_NAME=' . self::DEFAULT_ENV_BUILD_DIRECTORY_NAME); putenv('BUILD_FILE_NAME=' . self::DEFAULT_ENV_BUILD_FILE_NAME); @@ -108,20 +159,59 @@ public function testCheckDefaultOptionBuildFile() * @covers Configuration::get * @return void */ - public function testCheckOtherOptionRoot() + public function testCheckOtherOptionOptionGitHubOutput() { putenv('GITHUB_WORKSPACE=' . self::OTHER_ENV_GITHUB_WORKSPACE); + putenv('GITHUB_OUTPUT=' . self::OTHER_ENV_GITHUB_OUTPUT); putenv('BUILD_DIRECTORY_NAME=' . self::OTHER_ENV_BUILD_DIRECTORY_NAME); putenv('BUILD_FILE_NAME=' . self::OTHER_ENV_BUILD_FILE_NAME); $configuration = new Configuration(); $this->assertEquals( - $configuration->get('root'), + $configuration->get('GITHUB_WORKSPACE'), self::OTHER_ENV_GITHUB_WORKSPACE ); } + /** + * @covers Configuration::get + * @return void + */ + public function testCheckOtherGetRootDirectory(): void + { + putenv('GITHUB_WORKSPACE=' . self::OTHER_ENV_GITHUB_WORKSPACE); + putenv('GITHUB_OUTPUT=' . self::OTHER_ENV_GITHUB_OUTPUT); + putenv('BUILD_DIRECTORY_NAME=' . self::OTHER_ENV_BUILD_DIRECTORY_NAME); + putenv('BUILD_FILE_NAME=' . self::OTHER_ENV_BUILD_FILE_NAME); + + $configuration = new Configuration(); + + $this->assertEquals( + $configuration->getRootDirectory(), + self::OTHER_ENV_GITHUB_WORKSPACE + ); + } + + /** + * @covers Configuration::get + * @return void + */ + public function testCheckOtherOptionGitHubOutput(): void + { + putenv('GITHUB_WORKSPACE=' . self::OTHER_ENV_GITHUB_WORKSPACE); + putenv('GITHUB_OUTPUT=' . self::OTHER_ENV_GITHUB_OUTPUT); + putenv('BUILD_DIRECTORY_NAME=' . self::OTHER_ENV_BUILD_DIRECTORY_NAME); + putenv('BUILD_FILE_NAME=' . self::OTHER_ENV_BUILD_FILE_NAME); + + $configuration = new Configuration(); + + $this->assertEquals( + $configuration->get('GITHUB_OUTPUT'), + self::OTHER_ENV_GITHUB_OUTPUT + ); + } + /** * @covers Configuration::get * @return void @@ -129,6 +219,7 @@ public function testCheckOtherOptionRoot() public function testCheckOtherOptionBuildDirectory() { putenv('GITHUB_WORKSPACE=' . self::OTHER_ENV_GITHUB_WORKSPACE); + putenv('GITHUB_OUTPUT=' . self::OTHER_ENV_GITHUB_OUTPUT); putenv('BUILD_DIRECTORY_NAME=' . self::OTHER_ENV_BUILD_DIRECTORY_NAME); putenv('BUILD_FILE_NAME=' . self::OTHER_ENV_BUILD_FILE_NAME); @@ -147,6 +238,7 @@ public function testCheckOtherOptionBuildDirectory() public function testCheckOtherOptionBuildFile() { putenv('GITHUB_WORKSPACE=' . self::OTHER_ENV_GITHUB_WORKSPACE); + putenv('GITHUB_OUTPUT=' . self::OTHER_ENV_GITHUB_OUTPUT); putenv('BUILD_DIRECTORY_NAME=' . self::OTHER_ENV_BUILD_DIRECTORY_NAME); putenv('BUILD_FILE_NAME=' . self::OTHER_ENV_BUILD_FILE_NAME);