Skip to content

Commit

Permalink
Install dotenv, include logic for overrding reference file with gener…
Browse files Browse the repository at this point in the history
…ated content, override Dockerfile references for previous tests
  • Loading branch information
Kathryn Anne S Tan committed Apr 4, 2024
1 parent fc40e21 commit 2bb23f2
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 25 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OVERRIDE_TEST_REFERENCES=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.vscode
/.vagrant
.phpunit.result.cache
.env
40 changes: 15 additions & 25 deletions tests/Feature/GenerateCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;

function ignoreFiles( )
{
return ['composer.json','frankenphp','rr','.rr.yaml'];
}

function assertEqualsIgnoreNewLine( $ins, $expected, $generated, $msg )
{
try{
// Ignore newlines when comparing strings; do this by removing new lines from strings to compare
$il_expected = str_replace( PHP_EOL, '', $expected );
$il_generated = str_replace( PHP_EOL, '', $generated );
$ins->assertEquals( $il_expected, $il_generated, $msg);
}catch (ExpectationFailedException $f) {

// However, if variants without newlines failed to match!
// That's a true failed match!
// To get a better diff error message, use original strings( with their newlines intact ) in comparison
$ins->assertEquals( $expected, $generated, $msg);
}
}

function getTestOptions( string $directory ): string
{
$composerContent = (new \App\Services\File())->composerJsonContent( $directory );
Expand Down Expand Up @@ -57,26 +40,33 @@ function getTestOptions( string $directory ): string
$options = getTestOptions( $dir );

// Generate Dockerfile using options
// First assert: command successfully runs and exits
// FIRST assert: command successfully runs and exits
$this->artisan('generate '.$options)->assertExitCode(0);

// Compare expected files from test directory with generated files
// Compare reference files from test directory with generated files
$referenceFiles = \File::files( $dir );
foreach( $referenceFiles as $reference ){
$failedForMsg = 'Failed for: "'.$reference->getPathName().'"';

// Skip if a setup file
if( in_array( $reference->getFileName(), ignoreFiles()) ) continue;

// Second assert: a new file with the reference file's name was created-it should exist!
// SECOND assert: a new file with the reference file's name was created; it should exist!
$this->assertFileExists( $reference->getFileName(), $failedForMsg );

// Get contents i.e. /10_base/Dockerfile vs Dockerfile
$expected = file_get_contents( $reference->getPathName() ); // expected content from reference file
$generated = file_get_contents( $reference->getFileName() ); // new file content
// Contents of generated file
$generated = file_get_contents( $reference->getFileName() );

// Override the reference file with generated file content if needed; PLEASE double check diff and re-test this new ref manually!
if( env('OVERRIDE_TEST_REFERENCES')===true )
file_put_contents( $reference->getPathName(), $generated );

// Contents of reference file
$expected = file_get_contents( $reference->getPathName() );

// Third assert: contents are the same
// THIRD assert: contents are the same
// TODO: ignore different ARG VALUES
assertEqualsIgnoreNewLine( $this, $expected, $generated, $failedForMsg);
$this->assertEquals( $expected, $generated, $failedForMsg);

// Clean UP: Delete generated file, no longer needed
unlink( $reference->getFileName() );
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Supported/10_base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ RUN composer install --optimize-autoloader --no-dev \
if [ -d .fly ]; then cp .fly/entrypoint.sh /entrypoint; chmod +x /entrypoint; fi;



# Multi-stage build: Build static assets
# This allows us to not include Node within the final container
FROM node:${NODE_VERSION} as node_modules_go_brrr
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Supported/10_octane_frankenphp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ RUN rm -rf /etc/supervisor/conf.d/fpm.conf; \
rm /etc/nginx/sites-enabled/default; \
ln -sf /etc/nginx/sites-available/default-octane /etc/nginx/sites-enabled/default;


# Multi-stage build: Build static assets
# This allows us to not include Node within the final container
FROM node:${NODE_VERSION} as node_modules_go_brrr
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Supported/10_octane_rr/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ RUN rm -rf /etc/supervisor/conf.d/fpm.conf; \
rm /etc/nginx/sites-enabled/default; \
ln -sf /etc/nginx/sites-available/default-octane /etc/nginx/sites-enabled/default;


# Multi-stage build: Build static assets
# This allows us to not include Node within the final container
FROM node:${NODE_VERSION} as node_modules_go_brrr
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Supported/10_octane_swoole/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ RUN rm -rf /etc/supervisor/conf.d/fpm.conf; \
rm /etc/nginx/sites-enabled/default; \
ln -sf /etc/nginx/sites-available/default-octane /etc/nginx/sites-enabled/default;


# Multi-stage build: Build static assets
# This allows us to not include Node within the final container
FROM node:${NODE_VERSION} as node_modules_go_brrr
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Supported/11_octane_frankenphp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ RUN rm -rf /etc/supervisor/conf.d/fpm.conf; \
rm /etc/nginx/sites-enabled/default; \
ln -sf /etc/nginx/sites-available/default-octane /etc/nginx/sites-enabled/default;


# Multi-stage build: Build static assets
# This allows us to not include Node within the final container
FROM node:${NODE_VERSION} as node_modules_go_brrr
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Supported/11_octane_rr/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ RUN rm -rf /etc/supervisor/conf.d/fpm.conf; \
rm /etc/nginx/sites-enabled/default; \
ln -sf /etc/nginx/sites-available/default-octane /etc/nginx/sites-enabled/default;


# Multi-stage build: Build static assets
# This allows us to not include Node within the final container
FROM node:${NODE_VERSION} as node_modules_go_brrr
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Supported/11_octane_swoole/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ RUN rm -rf /etc/supervisor/conf.d/fpm.conf; \
rm /etc/nginx/sites-enabled/default; \
ln -sf /etc/nginx/sites-available/default-octane /etc/nginx/sites-enabled/default;


# Multi-stage build: Build static assets
# This allows us to not include Node within the final container
FROM node:${NODE_VERSION} as node_modules_go_brrr
Expand Down

0 comments on commit 2bb23f2

Please sign in to comment.