Skip to content

Commit

Permalink
AL-1674 - Skip update check when in non-interactive mode (#1935)
Browse files Browse the repository at this point in the history
* Don't run the update check in non-interactive mode.

* Move update check supression

* Fixed unit tests for update check avoidance, added update check-setting attribute for testing purposes

* Make Windows skip running UpdateCheckerTest::testShouldCheckForUpdates
  • Loading branch information
TeslaDethray authored Jan 29, 2019
1 parent b45767d commit 2242739
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Terminus.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function run(InputInterface $input, OutputInterface $output)
$status_code = $this->runner->run($input, $output, null, $this->commands);
if (!empty($cassette) && !empty($mode)) {
$this->stopVCR();
} else {
} elseif ($input->isInteractive()) {
$this->runUpdateChecker();
}
return $status_code;
Expand Down
36 changes: 35 additions & 1 deletion src/Update/UpdateChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ class UpdateChecker implements ConfigAwareInterface, ContainerAwareInterface, Da
const UPDATE_COMMAND = 'curl -O https://raw.githubusercontent.com/pantheon-systems/terminus-installer/master/builds/installer.phar && php installer.phar update';
const UPDATE_NOTICE = <<<EOT
A new Terminus version v{latest_version} is available.
You are currently using version v{running_version}.
You are currently using version v{running_version}.
You can update Terminus by running `composer update` or using the Terminus installer:
{update_command}
EOT;
const UPDATE_NOTICE_COLOR = "\e[38;5;33m";
const UPDATE_VARS_COLOR = "\e[38;5;45m";

/**
* @var boolean
*/
private $should_check_for_updates;

/**
* @param DataStoreInterface $data_store
*/
Expand All @@ -45,6 +50,9 @@ public function __construct(DataStoreInterface $data_store)

public function run()
{
if (!$this->shouldCheckForUpdates()) {
return;
}
$running_version = $this->getRunningVersion();
try {
$latest_version = $this->getContainer()->get(LatestRelease::class, [$this->getDataStore(),])->get('version');
Expand All @@ -64,6 +72,32 @@ public function run()
}
}

/**
* Stores information on whether or not Terminus should check for updates
*
* @param boolean $status True to check for updates
*/
public function setCheckForUpdates($status)
{
$this->should_check_for_updates = $status;
}

/**
* Avoid running the update checker in instances where the output might
* interfere with scripts.
*/
private function shouldCheckForUpdates()
{
if (empty($this->should_check_for_updates)) {
if (!function_exists('posix_isatty')) {
$this->setCheckForUpdates(true);
} else {
$this->setCheckForUpdates(posix_isatty(STDOUT) && posix_isatty(STDIN));
}
}
return $this->should_check_for_updates;
}

/**
* Retrieves the version number of the running Terminus instance
*
Expand Down
44 changes: 44 additions & 0 deletions tests/unit_tests/Update/UpdateCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function testClientIsUpToDate()
$this->logger->expects($this->never())
->method('debug');

$this->update_checker->setCheckForUpdates(true);
$out = $this->update_checker->run();
$this->assertNull($out);
}
Expand Down Expand Up @@ -133,6 +134,7 @@ public function testClientIsOutOfDate()
$this->logger->expects($this->never())
->method('debug');

$this->update_checker->setCheckForUpdates(true);
$out = $this->update_checker->run();
$this->assertNull($out);
}
Expand Down Expand Up @@ -167,6 +169,7 @@ public function testClientIsOutOfDateButHideMessage()
$this->logger->expects($this->never())
->method('debug');

$this->update_checker->setCheckForUpdates(true);
$out = $this->update_checker->run();
$this->assertNull($out);
}
Expand Down Expand Up @@ -201,6 +204,47 @@ public function testCannotCheckVersion()
$this->equalTo('Terminus has no saved release information.')
);

$this->update_checker->setCheckForUpdates(true);
$out = $this->update_checker->run();
$this->assertNull($out);
}

/**
* Ensures that the checker does not run when inappropriate and that the
* state can be changed by using setCheckForUpdates
*/
public function testShouldCheckForUpdates()
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$this->markTestSkipped("Windows CI doesn't have the necessary extensions.");
}

$running_version_num = '1.0.0-beta.2';

$this->config->expects($this->once())
->method('get')
->with($this->equalTo('version'))
->willReturn($running_version_num);
$this->container->expects($this->once())
->method('get')
->with(
$this->equalTo(LatestRelease::class),
$this->equalTo([$this->data_store,])
)
->willReturn($this->latest_release);
$this->latest_release->expects($this->once())
->method('get')
->with($this->equalTo('version'))
->will($this->throwException(new TerminusNotFoundException()));
$this->logger->expects($this->once())
->method('debug')
->with(
$this->equalTo('Terminus has no saved release information.')
);
$out = $this->update_checker->run();
$this->assertNull($out);

$this->update_checker->setCheckForUpdates(true);
$out = $this->update_checker->run();
$this->assertNull($out);
}
Expand Down

0 comments on commit 2242739

Please sign in to comment.