Skip to content

Commit

Permalink
Issue #3103090 by greg.1.anderson, alexpott, jungle, ravi.shankar, dw…
Browse files Browse the repository at this point in the history
…w, Mile23, larowlan: Avoid re-scaffolding unchanged files (and printing scaffold file information over and over)
  • Loading branch information
alexpott committed May 3, 2020
1 parent 8b7fc43 commit ba16468
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\Tests\Composer\Plugin\Scaffold\AssertUtilsTrait;
use Drupal\Tests\Composer\Plugin\Scaffold\ExecTrait;
use Drupal\Tests\Composer\Plugin\Scaffold\Fixtures;
use Drupal\Tests\PhpunitCompatibilityTrait;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -25,16 +26,7 @@
class ComposerHookTest extends TestCase {
use ExecTrait;
use AssertUtilsTrait;

/**
* The root of this project.
*
* Used to substitute this project's base directory into composer.json files
* so Composer can find it.
*
* @var string
*/
protected $projectRoot;
use PhpunitCompatibilityTrait;

/**
* Directory to perform the tests in.
Expand Down Expand Up @@ -64,7 +56,9 @@ protected function setUp() {
$this->fileSystem = new Filesystem();
$this->fixtures = new Fixtures();
$this->fixtures->createIsolatedComposerCacheDir();
$this->projectRoot = $this->fixtures->projectRoot();
$this->fixturesDir = $this->fixtures->tmpDir($this->getName());
$replacements = ['SYMLINK' => 'false', 'PROJECT_ROOT' => $this->fixtures->projectRoot()];
$this->fixtures->cloneFixtureProjects($this->fixturesDir, $replacements);
}

/**
Expand All @@ -79,9 +73,6 @@ protected function tearDown() {
* Test to see if scaffold operation runs at the correct times.
*/
public function testComposerHooks() {
$this->fixturesDir = $this->fixtures->tmpDir($this->getName());
$replacements = ['SYMLINK' => 'false', 'PROJECT_ROOT' => $this->projectRoot];
$this->fixtures->cloneFixtureProjects($this->fixturesDir, $replacements);
$topLevelProjectDir = 'composer-hooks-fixture';
$sut = $this->fixturesDir . '/' . $topLevelProjectDir;
// First test: run composer install. This is the same as composer update
Expand Down Expand Up @@ -136,4 +127,29 @@ public function testComposerHooks() {
$this->assertContains("Not scaffolding files for fixtures/scaffold-override-fixture, because it is not listed in the element 'extra.drupal-scaffold.allowed-packages' in the root-level composer.json file.", $stdout);
}

/**
* Test to see if scaffold messages are omitted when running scaffold twice.
*/
public function testScaffoldMessagesDoNotPrintTwice() {
$topLevelProjectDir = 'drupal-drupal';
$sut = $this->fixturesDir . '/' . $topLevelProjectDir;
// First test: run composer install. This is the same as composer update
// since there is no lock file. Ensure that scaffold operation ran.
$stdout = $this->mustExec("composer install --no-ansi", $sut);

$this->assertStringContainsString('- Copy [web-root]/index.php from assets/index.php', $stdout);
$this->assertStringContainsString('- Copy [web-root]/update.php from assets/update.php', $stdout);

// Run scaffold operation again. It should not print anything.
$stdout = $this->mustExec("composer scaffold --no-ansi", $sut);

$this->assertEquals('', $stdout);

// Delete a file and run it again. It should re-scaffold the removed file.
unlink("$sut/index.php");
$stdout = $this->mustExec("composer scaffold --no-ansi", $sut);
$this->assertStringContainsString('- Copy [web-root]/index.php from assets/index.php', $stdout);
$this->assertStringNotContainsString('- Copy [web-root]/update.php from assets/update.php', $stdout);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,9 @@ public function testProcess() {

$prepend = $fixtures->sourcePath('drupal-drupal-test-append', 'prepend-to-robots.txt');
$append = $fixtures->sourcePath('drupal-drupal-test-append', 'append-to-robots.txt');
$sut = new AppendOp($prepend, $append);
$sut = new AppendOp($prepend, $append, TRUE);
$sut->scaffoldAtNewLocation($destination);

// Test the system under test.
$sut->process($destination, $fixtures->io(), $options);
// Assert that the target file was created.
$this->assertFileExists($destination->fullPath());
// Assert the target contained the contents from the correct scaffold files.
$contents = trim(file_get_contents($destination->fullPath()));
$expected = <<<EOT
# robots.txt fixture scaffolded from "file-mappings" in drupal-drupal-test-append composer.json fixture.
# This content is prepended to the top of the existing robots.txt fixture.
Expand All @@ -48,6 +43,16 @@ public function testProcess() {
# This content is appended to the bottom of the existing robots.txt fixture.
# robots.txt fixture scaffolded from "file-mappings" in drupal-drupal-test-append composer.json fixture.
EOT;

$pre_calculated_contents = $sut->contents();
$this->assertEquals(trim($expected), trim($pre_calculated_contents));

// Test the system under test.
$sut->process($destination, $fixtures->io(), $options);
// Assert that the target file was created.
$this->assertFileExists($destination->fullPath());
// Assert the target contained the contents from the correct scaffold files.
$contents = trim(file_get_contents($destination->fullPath()));
$this->assertEquals(trim($expected), $contents);
// Confirm that expected output was written to our io fixture.
$output = $fixtures->getOutput();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPUnit\Framework\TestCase;
use Drupal\Tests\Composer\Plugin\Scaffold\Fixtures;
use Drupal\Composer\Plugin\Scaffold\Operations\ConjunctionOp;
use Drupal\Composer\Plugin\Scaffold\Operations\AppendOp;
use Drupal\Composer\Plugin\Scaffold\Operations\SkipOp;
use Drupal\Composer\Plugin\Scaffold\Operations\ScaffoldFileCollection;

Expand Down Expand Up @@ -41,8 +41,8 @@ public function testCreate() {
// Confirm that the keys of the output are the same as the keys of the
// input.
$this->assertEquals(array_keys($scaffold_file_fixtures), array_keys($resolved_file_mappings));
// '[web-root]/robots.txt' is now a SkipOp, as it is now part of a
// conjunction operation.
// '[web-root]/robots.txt' is now a SkipOp, as it is now part of an
// append operation.
$this->assertEquals([
'[web-root]/index.php',
'[web-root]/.htaccess',
Expand All @@ -62,8 +62,8 @@ public function testCreate() {

// Test that .htaccess is skipped.
$this->assertInstanceOf(SkipOp::class, $resolved_file_mappings['fixtures/drupal-assets-fixture']['[web-root]/.htaccess']->op());
// Test that the expected conjunction operation exists.
$this->assertInstanceOf(ConjunctionOp::class, $resolved_file_mappings['fixtures/drupal-drupal']['[web-root]/robots.txt']->op());
// Test that the expected append operation exists.
$this->assertInstanceOf(AppendOp::class, $resolved_file_mappings['fixtures/drupal-drupal']['[web-root]/robots.txt']->op());
}

}

0 comments on commit ba16468

Please sign in to comment.