Commit 97b6f7d9 authored by Eirik Morland's avatar Eirik Morland
Browse files

Issue #3254427: Make it possible to alter the env of the show command

parent b0b9bb44
Loading
Loading
Loading
Loading
+6 −9
Original line number Diff line number Diff line
@@ -160,7 +160,7 @@ class NeedsUpdateManager {
   *
   * @throws \Drupal\violinist_projects\Exception\NeedsUpdateException
   */
  public function checkNeedsUpdate(UpdateCheckData $data, $is_daily = FALSE, $directory = NULL) {
  public function checkNeedsUpdate(UpdateCheckData $data, array $env, $is_daily = FALSE, $directory = NULL) {
    // First check the repo, so the composer.json gets updated.
    $url = $this->slug->getUrl();
    /** @var \Drupal\vcs_provider_client\ClientInterface $client */
@@ -230,7 +230,7 @@ class NeedsUpdateManager {
        }
        if (!$sha = $data->getShaForPackage($dep)) {
          // For sure needs an update.
          if ($new_sha = $this->getNewShaForPackage($dep, $composer_json, $auth_json)) {
          if ($new_sha = $this->getNewShaForPackage($dep, $composer_json, $env, $auth_json)) {
            $this->logger->info('Current package sha for package @package is @sha', [
              '@package' => $dep,
              '@sha' => $new_sha,
@@ -243,7 +243,7 @@ class NeedsUpdateManager {
          ]);
          throw new NeedsUpdateException("No sha stored for package $dep");
        }
        $current_package_sha = $this->getNewShaForPackage($dep, $composer_json, $auth_json);
        $current_package_sha = $this->getNewShaForPackage($dep, $composer_json, $env, $auth_json);
        $this->logger->info('Current package sha for package @package is @sha', [
          '@package' => $dep,
          '@sha' => $current_package_sha,
@@ -271,7 +271,7 @@ class NeedsUpdateManager {
  /**
   * Gets the computed sha for package data for a package.
   */
  public function getNewShaForPackage($package, $composer_json, $auth_json = NULL) {
  public function getNewShaForPackage($package, $composer_json, array $env, $auth_json = NULL) {
    // We trust the fact that other packages that require the same name does not
    // do so from a completely different source.
    $cid = sprintf('violinist_project_cache_package_%s', md5($package));
@@ -324,6 +324,7 @@ class NeedsUpdateManager {
        'filename_pub' => $filename_pub,
      ];
    }, ['userKey', 'projectKey']);
    $env['COMPOSER_HOME'] = $project_root;
    $process = new Process([
      $composer,
      "show",
@@ -331,11 +332,7 @@ class NeedsUpdateManager {
      "--all",
      "-d",
      $directory,
    ], NULL, [
      'COMPOSER_DISABLE_XDEBUG_WARN' => 1,
      'COMPOSER_ALLOW_SUPERUSER' => 1,
      'COMPOSER_HOME' => $project_root,
    ]);
    ], NULL, $env);
    $process->setTimeout(30);
    $our_sha = FALSE;
    try {
+6 −1
Original line number Diff line number Diff line
@@ -158,7 +158,12 @@ class UpdateWorker extends QueueWorkerBase implements ContainerFactoryPluginInte
      if ($node->hasField('field_composer_json_path') && !$node->get('field_composer_json_path')->isEmpty()) {
        $directory = $node->get('field_composer_json_path')->first()->getString();
      }
      $this->updateManager->checkNeedsUpdate($data, $is_daily, $directory);
      $env = [
        'COMPOSER_DISABLE_XDEBUG_WARN' => 1,
        'COMPOSER_ALLOW_SUPERUSER' => 1,
      ];
      $this->moduleHandler->alter('violinist_projects_show_command_env', $env, $node);
      $this->updateManager->checkNeedsUpdate($data, $env, $is_daily, $directory);
    }
    catch (NeedsUpdateException $e) {
      $this->handleNeedsUpdate($node, $e);
+2 −2
Original line number Diff line number Diff line
@@ -40,14 +40,14 @@ class UpdateManagerTest extends KernelTestBase implements ServiceModifierInterfa
    $mng->setUserKey($key);
    $mng->setProjectKey($key);
    $this->setSetting('violinist_projects_debug', 1);
    $sha1 = $mng->getNewShaForPackage('psr/log', $composer_json);
    $sha1 = $mng->getNewShaForPackage('psr/log', $composer_json, []);
    /** @var \Drupal\Tests\violinist_projects\Kernel\ProjectsLoggerFactory $logger_factory */
    $logger_factory = $this->container->get('logger.factory');
    /** @var \Drupal\Tests\violinist_projects\Kernel\ProjectsLogger $logger */
    $logger = $logger_factory->get('violinist_projects');
    self::assertNotFalse($sha1);
    // And again...
    $sha2 = $mng->getNewShaForPackage('psr/log', $composer_json);
    $sha2 = $mng->getNewShaForPackage('psr/log', $composer_json, []);
    self::assertNotFalse($sha2);
    self::assertEquals($sha1, $sha2);
    // We expect the logger to have logged exactly one event, since the second
+3 −3
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ class NeedsUpdateManagerTest extends TestCase {
  public function testNeedsUpdateBecauseOfDaily() {
    $this->expectExceptionMessage('Daily queue run');
    $this->expectException(NeedsUpdateException::class);
    $this->updateManager->checkNeedsUpdate($this->data, TRUE);
    $this->updateManager->checkNeedsUpdate($this->data, [], TRUE);
  }

  /**
@@ -91,7 +91,7 @@ class NeedsUpdateManagerTest extends TestCase {
    $this->expectExceptionMessage("Current sha efefef is not the same as last sha unknown (from unknown)");
    $this->expectException(NeedsUpdateException::class);
    $new_data = new UpdateCheckData();
    $this->updateManager->checkNeedsUpdate($new_data);
    $this->updateManager->checkNeedsUpdate($new_data, []);
  }

  /**
@@ -107,7 +107,7 @@ class NeedsUpdateManagerTest extends TestCase {
    $sha = new UpdateCheckSha('efefef', time());
    $new_data->setLastSha($sha);
    $new_data->setShaForPackage('psr/log', $psr_sha);
    $this->updateManager->checkNeedsUpdate($new_data);
    $this->updateManager->checkNeedsUpdate($new_data, []);
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ class TestUpdateManager extends NeedsUpdateManager {
  /**
   * {@inheritdoc}
   */
  public function getNewShaForPackage($package, $composer_json, $auth_json = NULL) {
  public function getNewShaForPackage($package, $composer_json, array $env, $auth_json = NULL) {
    return $this->mockedSha;
  }