diff --git a/src/config.rs b/src/config.rs index 6d2dbb5e3..cf1e009cf 100644 --- a/src/config.rs +++ b/src/config.rs @@ -318,6 +318,8 @@ impl DiagnosticsConfig { #[derive(Debug, Clone, Default, Deserialize)] #[serde(default)] pub struct FormattingConfig { + /// Explicitly disable all formatting + pub enabled: Option, /// Command (path or name) to run php-cs-fixer. /// /// - `None` (default) — check `require-dev` in `composer.json`; @@ -379,12 +381,9 @@ impl FormattingConfig { self.timeout.unwrap_or(10_000) } - /// Whether formatting is entirely disabled (every tool explicitly - /// set to an empty string). + /// Whether formatting is explicitly disabled pub fn is_disabled(&self) -> bool { - crate::formatting::Tool::ALL - .into_iter() - .all(|tool| tool.configured(self) == Some("")) + self.enabled == Some(false) } } @@ -1542,6 +1541,22 @@ paths = ["database/schema", "extra/schema.sql"] assert_eq!(config.formatting.php_cs_fixer.as_deref(), Some("")); assert_eq!(config.formatting.phpcbf.as_deref(), Some("")); assert_eq!(config.formatting.pint.as_deref(), Some("")); + assert!(!config.formatting.is_disabled()); + } + + #[test] + fn formatting_disabled_disables_tool() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join(CONFIG_FILE_NAME); + std::fs::write( + &path, + "[formatting]\nenabled = false\nphp-cs-fixer = \"\"\nphpcbf = \"\"\npint = \"\"\n", + ) + .unwrap(); + let config = load_config(dir.path()).unwrap(); + assert_eq!(config.formatting.php_cs_fixer.as_deref(), Some("")); + assert_eq!(config.formatting.phpcbf.as_deref(), Some("")); + assert_eq!(config.formatting.pint.as_deref(), Some("")); assert!(config.formatting.is_disabled()); } diff --git a/src/formatting/tests.rs b/src/formatting/tests.rs index 410c8dd84..be81d6e1a 100644 --- a/src/formatting/tests.rs +++ b/src/formatting/tests.rs @@ -130,6 +130,7 @@ fn malformed_mago_toml_returns_error_not_panic() { #[test] fn strategy_both_disabled() { let config = FormattingConfig { + enabled: Some(true), pint: Some(String::new()), php_cs_fixer: Some(String::new()), phpcbf: Some(String::new()), @@ -144,6 +145,7 @@ fn strategy_both_disabled() { #[test] fn strategy_explicit_commands() { let config = FormattingConfig { + enabled: Some(true), pint: None, php_cs_fixer: Some("/usr/bin/php-cs-fixer".to_string()), phpcbf: Some("/usr/bin/phpcbf".to_string()), @@ -167,6 +169,7 @@ fn strategy_explicit_commands() { #[test] fn strategy_one_explicit_one_disabled() { let config = FormattingConfig { + enabled: Some(true), pint: None, php_cs_fixer: Some("/usr/bin/php-cs-fixer".to_string()), phpcbf: Some(String::new()), @@ -468,6 +471,7 @@ fn strategy_explicit_overrides_require_dev() { // User explicitly set a different path. let config = FormattingConfig { + enabled: Some(true), pint: None, php_cs_fixer: Some("/opt/php-cs-fixer".to_string()), phpcbf: Some(String::new()), @@ -725,6 +729,7 @@ fn execute_builtin_reformats_messy_class() { fn execute_disabled_returns_none() { let content = "