From b379819cfade731d1509d3fa60632f87b52ea2e5 Mon Sep 17 00:00:00 2001 From: Matt W <436037+mlw@users.noreply.github.com> Date: Fri, 22 Mar 2024 16:44:45 -0400 Subject: [PATCH] Overrides disabled when running tests unless explicitly enabled (#1312) * Emit a log warning when overrides were applied * Overrides now disabled in tests unless explicitly enabled * Remove log message. Check for xctest instead of bazel env vars. * Typo --- Source/common/SNTConfigurator.m | 42 ++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/Source/common/SNTConfigurator.m b/Source/common/SNTConfigurator.m index 334ffa5b6..829bcb524 100644 --- a/Source/common/SNTConfigurator.m +++ b/Source/common/SNTConfigurator.m @@ -1222,18 +1222,20 @@ - (NSRegularExpression *)expressionForPattern:(NSString *)pattern { return [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL]; } -- (NSMutableDictionary *)readForcedConfig { - NSMutableDictionary *forcedConfig = [NSMutableDictionary dictionary]; - for (NSString *key in self.forcedConfigKeyTypes) { - id obj = [self forcedConfigValueForKey:key]; - forcedConfig[key] = [obj isKindOfClass:self.forcedConfigKeyTypes[key]] ? obj : nil; - // Create the regex objects now - if (self.forcedConfigKeyTypes[key] == [NSRegularExpression class]) { - NSString *pattern = [obj isKindOfClass:[NSString class]] ? obj : nil; - forcedConfig[key] = [self expressionForPattern:pattern]; - } - } +- (void)applyOverrides:(NSMutableDictionary *)forcedConfig { + // Overrides should only be applied under debug builds. #ifdef DEBUG + if ([[[NSProcessInfo processInfo] processName] isEqualToString:@"xctest"] && + ![[[NSProcessInfo processInfo] environment] objectForKey:@"ENABLE_CONFIG_OVERRIDES"]) { + // By default, config overrides are not applied when running tests to help + // mitigate potential issues due to unexpected config values. This behavior + // can be overriden if desired by using the env variable: `ENABLE_CONFIG_OVERRIDES`. + // + // E.g.: + // bazel test --test_env=ENABLE_CONFIG_OVERRIDES=1 ...other test args... + return; + } + NSDictionary *overrides = [NSDictionary dictionaryWithContentsOfFile:kConfigOverrideFilePath]; for (NSString *key in overrides) { id obj = overrides[key]; @@ -1242,13 +1244,31 @@ - (NSMutableDictionary *)readForcedConfig { ![obj isKindOfClass:[NSString class]])) { continue; } + forcedConfig[key] = obj; + if (self.forcedConfigKeyTypes[key] == [NSRegularExpression class]) { NSString *pattern = [obj isKindOfClass:[NSString class]] ? obj : nil; forcedConfig[key] = [self expressionForPattern:pattern]; } } #endif +} + +- (NSMutableDictionary *)readForcedConfig { + NSMutableDictionary *forcedConfig = [NSMutableDictionary dictionary]; + for (NSString *key in self.forcedConfigKeyTypes) { + id obj = [self forcedConfigValueForKey:key]; + forcedConfig[key] = [obj isKindOfClass:self.forcedConfigKeyTypes[key]] ? obj : nil; + // Create the regex objects now + if (self.forcedConfigKeyTypes[key] == [NSRegularExpression class]) { + NSString *pattern = [obj isKindOfClass:[NSString class]] ? obj : nil; + forcedConfig[key] = [self expressionForPattern:pattern]; + } + } + + [self applyOverrides:forcedConfig]; + return forcedConfig; }