Verified Commit 04fcd4af authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3112393 by cmlara, smustgrave, AdamPS, alexpott: Allow vendor hardening...

Issue #3112393 by cmlara, smustgrave, AdamPS, alexpott: Allow vendor hardening to remove individual files

(cherry picked from commit 661f5453)
parent 1122c8d3
Loading
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -343,27 +343,27 @@ protected function cleanPathsForPackage(PackageInterface $package, $paths_for_pa
      return;
    }

    $this->io->writeError(sprintf('%sCleaning directories in <comment>%s</comment>', str_repeat(' ', 4), $package_name), TRUE, IOInterface::VERY_VERBOSE);
    $this->io->writeError(sprintf('%sCleaning paths in <comment>%s</comment>', str_repeat(' ', 4), $package_name), TRUE, IOInterface::VERY_VERBOSE);
    $fs = new Filesystem();
    foreach ($paths_for_package as $cleanup_item) {
      $cleanup_path = $package_dir . '/' . $cleanup_item;
      if (!is_dir($cleanup_path)) {
      if (!file_exists($cleanup_path)) {
        // If the package has changed or the --prefer-dist version does not
        // include the directory. This is not an error.
        $this->io->writeError(sprintf("%s<comment>Directory '%s' does not exist.</comment>", str_repeat(' ', 6), $cleanup_path), TRUE, IOInterface::VERY_VERBOSE);
        $this->io->writeError(sprintf("%s<comment>Path '%s' does not exist.</comment>", str_repeat(' ', 6), $cleanup_path), TRUE, IOInterface::VERY_VERBOSE);
        continue;
      }

      if (!$fs->removeDirectory($cleanup_path)) {
      if (!$fs->remove($cleanup_path)) {
        // Always display a message if this fails as it means something
        // has gone wrong. Therefore the message has to include the
        // package name as the first informational message might not
        // exist.
        $this->io->writeError(sprintf("%s<error>Failure removing directory '%s'</error> in package <comment>%s</comment>.", str_repeat(' ', 6), $cleanup_item, $package_name), TRUE, IOInterface::NORMAL);
        $this->io->writeError(sprintf("%s<error>Failure removing path '%s'</error> in package <comment>%s</comment>.", str_repeat(' ', 6), $cleanup_item, $package_name), TRUE, IOInterface::NORMAL);
        continue;
      }

      $this->io->writeError(sprintf("%sRemoving directory <info>'%s'</info>", str_repeat(' ', 4), $cleanup_item), TRUE, IOInterface::VERBOSE);
      $this->io->writeError(sprintf("%sRemoving path <info>'%s'</info>", str_repeat(' ', 4), $cleanup_item), TRUE, IOInterface::VERBOSE);
    }
  }

+4 −1
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ protected function setUp(): void {
          'tests' => [
            'SomeTest.php' => '<?php',
          ],
          'SomeFile.php' => '<?php',
        ],
      ],
    ]);
@@ -92,14 +93,16 @@ public function testCleanPathsForPackage() {
    $ref_io->setValue($plugin, $io->reveal());

    $this->assertFileExists(vfsStream::url('vendor/drupal/package/tests/SomeTest.php'));
    $this->assertFileExists(vfsStream::url('vendor/drupal/package/SomeFile.php'));

    $package = $this->prophesize(PackageInterface::class);
    $package->getName()->willReturn('drupal/package');

    $ref_clean = new \ReflectionMethod($plugin, 'cleanPathsForPackage');
    $ref_clean->invokeArgs($plugin, [$package->reveal(), ['tests']]);
    $ref_clean->invokeArgs($plugin, [$package->reveal(), ['tests', 'SomeFile.php']]);

    $this->assertFileDoesNotExist(vfsStream::url('vendor/drupal/package/tests'));
    $this->assertFileDoesNotExist(vfsStream::url('vendor/drupal/package/SomeFile.php'));
  }

  /**