diff options
| author | Stefan Boberg <[email protected]> | 2026-04-27 15:05:16 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2026-04-27 15:05:16 +0200 |
| commit | 6804dc6ff62c477399183dc85d2ad387298aa49d (patch) | |
| tree | f4153a901c758b9e385627b43f1606f8c2129a04 /src/zenutil/testartifactprovider.cpp | |
| parent | 5.8.9-pre3 (diff) | |
| download | archived-zen-6804dc6ff62c477399183dc85d2ad387298aa49d.tar.xz archived-zen-6804dc6ff62c477399183dc85d2ad387298aa49d.zip | |
GetEnvVariable: return std::optional<std::string> (#1017)
- `GetEnvVariable` now returns `std::optional<std::string>` so callers can distinguish an unset variable from one set to an empty value.
- Windows path uses `SetLastError(ERROR_SUCCESS)` + `ERROR_ENVVAR_NOT_FOUND` to detect "not found"; POSIX path returns `nullopt` when `getenv` returns `nullptr`.
- All call sites migrated. Most use `.value_or("")` to preserve current empty-or-unset behavior. The diagnostic helpers in `zen-test/artifactprovider-tests.cpp` now report `<unset>` vs `<empty>` distinctly.
- Added a check in the `ExpandEnvironmentVariables` test confirming `nullopt` for an unset variable; PATH/HOME lookups in that test use `REQUIRE(has_value())` so a missing var fails cleanly instead of throwing `bad_optional_access`.
Diffstat (limited to 'src/zenutil/testartifactprovider.cpp')
| -rw-r--r-- | src/zenutil/testartifactprovider.cpp | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/src/zenutil/testartifactprovider.cpp b/src/zenutil/testartifactprovider.cpp index 666a1758d..45b29d497 100644 --- a/src/zenutil/testartifactprovider.cpp +++ b/src/zenutil/testartifactprovider.cpp @@ -321,35 +321,35 @@ namespace { #if ZEN_PLATFORM_MAC return true; #else - std::string Disabled = GetEnvVariable("AWS_EC2_METADATA_DISABLED"); + std::string Disabled = GetEnvVariable("AWS_EC2_METADATA_DISABLED").value_or(""); return Disabled == "true" || Disabled == "TRUE" || Disabled == "1"; #endif } void ApplyAwsEnvDefaults(S3ClientOptions& Client) { - if (std::string Region = GetEnvVariable("AWS_DEFAULT_REGION"); !Region.empty()) + if (std::optional<std::string> Region = GetEnvVariable("AWS_DEFAULT_REGION"); Region && !Region->empty()) { - Client.Region = std::move(Region); + Client.Region = std::move(*Region); } - else if (std::string FallbackRegion = GetEnvVariable("AWS_REGION"); !FallbackRegion.empty()) + else if (std::optional<std::string> FallbackRegion = GetEnvVariable("AWS_REGION"); FallbackRegion && !FallbackRegion->empty()) { - Client.Region = std::move(FallbackRegion); + Client.Region = std::move(*FallbackRegion); } if (Client.Endpoint.empty()) { - Client.Endpoint = GetEnvVariable("AWS_ENDPOINT_URL"); + Client.Endpoint = GetEnvVariable("AWS_ENDPOINT_URL").value_or(""); } if (Client.Credentials.AccessKeyId.empty() && !Client.CredentialProvider) { - std::string AccessKeyId = GetEnvVariable("AWS_ACCESS_KEY_ID"); - if (!AccessKeyId.empty()) + std::optional<std::string> AccessKeyId = GetEnvVariable("AWS_ACCESS_KEY_ID"); + if (AccessKeyId && !AccessKeyId->empty()) { - Client.Credentials.AccessKeyId = std::move(AccessKeyId); - Client.Credentials.SecretAccessKey = GetEnvVariable("AWS_SECRET_ACCESS_KEY"); - Client.Credentials.SessionToken = GetEnvVariable("AWS_SESSION_TOKEN"); + Client.Credentials.AccessKeyId = std::move(*AccessKeyId); + Client.Credentials.SecretAccessKey = GetEnvVariable("AWS_SECRET_ACCESS_KEY").value_or(""); + Client.Credentials.SessionToken = GetEnvVariable("AWS_SESSION_TOKEN").value_or(""); } else if (!IsImdsDisabled()) { @@ -376,11 +376,11 @@ GetDefaultLocalTestArtifactPath() bool S3TestArtifactsAvailable() { - if (GetEnvVariable(kTestArtifactsS3EnvVar).empty()) + if (GetEnvVariable(kTestArtifactsS3EnvVar).value_or("").empty()) { return false; } - if (!GetEnvVariable("AWS_ACCESS_KEY_ID").empty() || !GetEnvVariable("AWS_SESSION_TOKEN").empty()) + if (!GetEnvVariable("AWS_ACCESS_KEY_ID").value_or("").empty() || !GetEnvVariable("AWS_SESSION_TOKEN").value_or("").empty()) { return true; } @@ -390,7 +390,7 @@ S3TestArtifactsAvailable() bool TestArtifactsAvailable() { - if (!GetEnvVariable(kTestArtifactsPathEnvVar).empty()) + if (!GetEnvVariable(kTestArtifactsPathEnvVar).value_or("").empty()) { return true; } @@ -402,10 +402,10 @@ CreateTestArtifactProvider(TestArtifactProviderOptions Options) { if (Options.CacheDir.empty()) { - std::string EnvValue = GetEnvVariable(kTestArtifactsPathEnvVar); - if (!EnvValue.empty()) + std::optional<std::string> EnvValue = GetEnvVariable(kTestArtifactsPathEnvVar); + if (EnvValue && !EnvValue->empty()) { - Options.CacheDir = std::filesystem::path(EnvValue); + Options.CacheDir = std::filesystem::path(*EnvValue); } } if (Options.CacheDir.empty()) @@ -419,10 +419,10 @@ CreateTestArtifactProvider(TestArtifactProviderOptions Options) if (Options.S3Client.BucketName.empty() || Options.S3KeyPrefix.empty()) { - std::string EnvValue = GetEnvVariable(kTestArtifactsS3EnvVar); - if (!EnvValue.empty()) + std::optional<std::string> EnvValue = GetEnvVariable(kTestArtifactsS3EnvVar); + if (EnvValue && !EnvValue->empty()) { - ApplyS3UrlToOptions(EnvValue, Options.S3Client, Options.S3KeyPrefix); + ApplyS3UrlToOptions(*EnvValue, Options.S3Client, Options.S3KeyPrefix); } } |