Skip to content

Commit

Permalink
Track environment provider in telemetry. (#1741)
Browse files Browse the repository at this point in the history
* Track environment provider in telemetry.

* Adding a test.

* Fix phpcs.

* Catch mutants

* Test edge case.

* Unset github.

* Skip mutation testing for getProviders().

* Updating comments.

* Change providers list.

* More mutation fixes.

* Cleanup

* Add ddev and lando.

* Change to TestBase.

* Remove unused includes

* Fix lando and ddev hijacking.

* PHPCS ignore.

---------

Co-authored-by: Dane Powell <[email protected]>
  • Loading branch information
grasmash and danepowell authored May 23, 2024
1 parent 50fdfff commit 12e1dc5
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Helpers/TelemetryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private function getTelemetryUserData(): array {
'ah_non_production' => getenv('AH_NON_PRODUCTION'),
'ah_realm' => getenv('AH_REALM'),
'CI' => getenv('CI'),
'env_provider' => $this->getEnvironmentProvider(),
'php_version' => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION,
];
try {
Expand All @@ -134,6 +135,20 @@ private function getTelemetryUserData(): array {
return $data;
}

public static function getEnvironmentProvider(): ?string {
$providers = self::getProviders();

// Check for environment variables.
foreach ($providers as $provider => $vars) {
foreach ($vars as $var) {
if (getenv($var) !== FALSE)
return $provider;
}
}

return NULL;
}

private function getUserId(): ?string {
$user = $this->getUserData();
if ($user && isset($user['uuid'])) {
Expand Down Expand Up @@ -180,4 +195,40 @@ private function getDefaultUserData(): array {
];
}

/**
* @infection-ignore-all
* Skipping infection testing for this because, it most cases, we expect that when a row from this array is changed
* it won't affect the return value.
* @return array<mixed>
* An array of providers and their associated environment variables.
*/
public static function getProviders(): array {
// Define the environment variables associated with each provider.
// phpcs:ignore SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys.IncorrectKeyOrder
return [
'lando' => ['LANDO'],
'ddev' => ['IS_DDEV_PROJECT'],
// Check Lando and DDEV first because the hijack AH_SITE_ENVIRONMENT.
'acquia' => ['AH_SITE_ENVIRONMENT'],
'bamboo' => ['BAMBOO_BUILDNUMBER'],
'beanstalk' => ['BEANSTALK_ENVIRONMENT'],
'bitbucket' => ['BITBUCKET_BUILD_NUMBER'],
'bitrise' => ['BITRISE_IO'],
'buddy' => ['BUDDY_WORKSPACE_ID'],
'circleci' => ['CIRCLECI'],
'codebuild' => ['CODEBUILD_BUILD_ID'],
'docksal' => ['DOCKSAL_VERSION'],
'drone' => ['DRONE'],
'github' => ['GITHUB_ACTIONS'],
'gitlab' => ['GITLAB_CI'],
'heroku' => ['HEROKU_TEST_RUN_ID'],
'jenkins' => ['JENKINS_URL'],
'pantheon' => ['PANTHEON_ENVIRONMENT'],
'pipelines' => ['PIPELINE_ENV'],
'platformsh' => ['PLATFORM_ENVIRONMENT'],
'teamcity' => ['TEAMCITY_VERSION'],
'travis' => ['TRAVIS'],
];
}

}
71 changes: 71 additions & 0 deletions tests/phpunit/src/Misc/TelemetryHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types = 1);

namespace Acquia\Cli\Tests\Misc;

use Acquia\Cli\Helpers\TelemetryHelper;
use Acquia\Cli\Tests\TestBase;

class TelemetryHelperTest extends TestBase {

const ENV_VAR_DEFAULT_VALUE = 'test';

public function tearDown(): void {
parent::tearDown();
$envVars = [];
foreach ($this->providerTestEnvironmentProvider() as $args) {
$envVars = array_merge($envVars, $args[1]);
}

TestBase::unsetEnvVars($envVars);
}

public function unsetGitHubEnvVars(): void {
$providers = TelemetryHelper::getProviders();

// Since we actually run our own tests on GitHub, getEnvironmentProvider() will return 'github' unless we unset it.
$github_env_vars = [];
foreach ($providers['github'] as $var) {
$github_env_vars[$var] = self::ENV_VAR_DEFAULT_VALUE;
}
TestBase::unsetEnvVars($github_env_vars);
}

/**
* @return array<mixed>
*/
public function providerTestEnvironmentProvider(): array {
$providersList = TelemetryHelper::getProviders();
$providersArray = [];
foreach ($providersList as $provider => $envVars) {
$env_vars_with_values = [];
foreach ($envVars as $var_name) {
$env_vars_with_values[$var_name] = self::ENV_VAR_DEFAULT_VALUE;
}
$providersArray[] = [$provider, $env_vars_with_values];
}

return $providersArray;
}

/**
* @dataProvider providerTestEnvironmentProvider()
*/
public function testEnvironmentProvider(string $provider, array $envVars): void {
$this->unsetGitHubEnvVars();
TestBase::setEnvVars($envVars);
$this->assertEquals($provider, TelemetryHelper::getEnvironmentProvider());
}

/**
* Test the getEnvironmentProvider method when no environment provider is detected.
*/
public function testGetEnvironmentProviderWithoutAnyEnvSet(): void {
$this->unsetGitHubEnvVars();

// Expect null since no provider environment variables are set.
$this->assertNull(TelemetryHelper::getEnvironmentProvider());
}

}

0 comments on commit 12e1dc5

Please sign in to comment.