diff --git a/src/domain/switch.rs b/src/domain/switch.rs index e3b99e8..330cc17 100644 --- a/src/domain/switch.rs +++ b/src/domain/switch.rs @@ -68,7 +68,9 @@ where line.to_string() } else if let Some(cur_sections) = current_sections.clone() { let trimmed_line = line.trim_start_matches(['#', ' ']); - if should_enable_variable(&choose_sections, &cur_sections) { + if !is_variables(trimmed_line) { + line.to_string() + } else if should_enable_variable(&choose_sections, &cur_sections) { String::from(trimmed_line) } else if should_disable_variable(&choose_sections, &cur_sections) { format!("# {}", trimmed_line) @@ -87,6 +89,15 @@ where writer.flush().map_err(|_| Error::WriteData) } +fn is_variables(trimmed_line: &str) -> bool { + trimmed_line + .chars() + .filter(|ch| (*ch != '_' && ch.is_ascii_punctuation()) || ch.is_whitespace()) + .enumerate() + .find(|(_, ch)| *ch == '=') + .map_or(false, |(i, _)| i == 0) +} + fn is_section_end(line: &str) -> bool { line.trim().is_empty() } @@ -274,5 +285,20 @@ mod tests { &[Section::with_namespace("debug", "off")] )); } + + #[test] + fn should_check_line_on_variable() { + let test_cases = [ + ("VAR=10", true), + ("THIS_IS_MY_VAR='hello world'", true), + ("hello world", false), + ("staging/production", false), + ("staging/production=value", false), + ]; + + for (input, expected) in test_cases { + assert_eq!(is_variables(input), expected); + } + } } } diff --git a/test_data/all_env b/test_data/all_env index 1583d32..53785ee 100644 --- a/test_data/all_env +++ b/test_data/all_env @@ -2,6 +2,7 @@ VITE_STRIPE_PK= VITE_SENTRY_ENABLED= VITE_SENTRY_DSN= ### local,staging +# staging/production VITE_SENTRY_ENV=staging ### prod VITE_SENTRY_ENV=production diff --git a/test_data/base_env b/test_data/base_env index 6888eb6..40bfff7 100644 --- a/test_data/base_env +++ b/test_data/base_env @@ -2,6 +2,7 @@ VITE_STRIPE_PK= VITE_SENTRY_ENABLED= VITE_SENTRY_DSN= ### local,staging +# staging/production # VITE_SENTRY_ENV=staging ### prod # VITE_SENTRY_ENV=production diff --git a/test_data/should_enable_local b/test_data/should_enable_local index d6e4df7..9e8082f 100644 --- a/test_data/should_enable_local +++ b/test_data/should_enable_local @@ -2,6 +2,7 @@ VITE_STRIPE_PK= VITE_SENTRY_ENABLED= VITE_SENTRY_DSN= ### local,staging +# staging/production VITE_SENTRY_ENV=staging ### prod # VITE_SENTRY_ENV=production diff --git a/test_data/should_enable_staging b/test_data/should_enable_staging index a6b48fc..4456cea 100644 --- a/test_data/should_enable_staging +++ b/test_data/should_enable_staging @@ -2,6 +2,7 @@ VITE_STRIPE_PK= VITE_SENTRY_ENABLED= VITE_SENTRY_DSN= ### local,staging +# staging/production VITE_SENTRY_ENV=staging ### prod # VITE_SENTRY_ENV=production diff --git a/test_data/should_use_debug_in_staging b/test_data/should_use_debug_in_staging index 6681dda..91fe341 100644 --- a/test_data/should_use_debug_in_staging +++ b/test_data/should_use_debug_in_staging @@ -2,6 +2,7 @@ VITE_STRIPE_PK= VITE_SENTRY_ENABLED= VITE_SENTRY_DSN= ### local,staging +# staging/production VITE_SENTRY_ENV=staging ### prod # VITE_SENTRY_ENV=production diff --git a/test_data/should_use_staging_db_in_local b/test_data/should_use_staging_db_in_local index 8cb508a..04dc10a 100644 --- a/test_data/should_use_staging_db_in_local +++ b/test_data/should_use_staging_db_in_local @@ -2,6 +2,7 @@ VITE_STRIPE_PK= VITE_SENTRY_ENABLED= VITE_SENTRY_DSN= ### local,staging +# staging/production VITE_SENTRY_ENV=staging ### prod # VITE_SENTRY_ENV=production