]> git.lizzy.rs Git - rust.git/commitdiff
Add a path to the parent dir of rustfmt.toml as a prefix
authortopecongiro <seuchida@gmail.com>
Mon, 22 Apr 2019 23:12:02 +0000 (08:12 +0900)
committertopecongiro <seuchida@gmail.com>
Mon, 22 Apr 2019 23:12:02 +0000 (08:12 +0900)
Paths users specify in `ignore` configuraiton option is relative to the
directory which contains the rustfmt.toml file. When processing the ignore paths
internally, rustfmt should add a path to the directory as a  prefix since
RealPath passed from libsyntax is a full path.

src/config/config_type.rs
src/config/mod.rs
src/ignore_path.rs
src/test/mod.rs

index 40da2986808a941fa0c9da48e5a1bd2d417b98f3..7c4298dcf1c099b59e149c666780ad58110b900f 100644 (file)
@@ -141,7 +141,7 @@ pub fn was_set(&self) -> ConfigWasSet<'_> {
                 ConfigWasSet(self)
             }
 
-            fn fill_from_parsed_config(mut self, parsed: PartialConfig) -> Config {
+            fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Config {
             $(
                 if let Some(val) = parsed.$i {
                     if self.$i.3 {
@@ -160,6 +160,7 @@ fn fill_from_parsed_config(mut self, parsed: PartialConfig) -> Config {
             )+
                 self.set_heuristics();
                 self.set_license_template();
+                self.set_ignore(dir);
                 self
             }
 
@@ -286,6 +287,9 @@ fn set_license_template(&mut self) {
                 }
             }
 
+            fn set_ignore(&mut self, dir: &Path) {
+                self.ignore.2.add_prefix(dir);
+            }
 
             /// Returns `true` if the config key was explicitly set and is the default value.
             pub fn is_default(&self, key: &str) -> bool {
index 839e9bb6e21856dd6a1d60ea3cdde079447a3588..c0221e93e0259b5ca383a4df3ce63e50ba3a09fd 100644 (file)
@@ -190,7 +190,8 @@ pub(super) fn from_toml_path(file_path: &Path) -> Result<Config, Error> {
         let mut file = File::open(&file_path)?;
         let mut toml = String::new();
         file.read_to_string(&mut toml)?;
-        Config::from_toml(&toml).map_err(|err| Error::new(ErrorKind::InvalidData, err))
+        Config::from_toml(&toml, file_path.parent().unwrap())
+            .map_err(|err| Error::new(ErrorKind::InvalidData, err))
     }
 
     /// Resolves the config for input in `dir`.
@@ -252,7 +253,7 @@ fn resolve_project_file(dir: &Path) -> Result<Option<PathBuf>, Error> {
         }
     }
 
-    pub(crate) fn from_toml(toml: &str) -> Result<Config, String> {
+    pub(crate) fn from_toml(toml: &str, dir: &Path) -> Result<Config, String> {
         let parsed: ::toml::Value = toml
             .parse()
             .map_err(|e| format!("Could not parse TOML: {}", e))?;
@@ -271,7 +272,7 @@ pub(crate) fn from_toml(toml: &str) -> Result<Config, String> {
                 if !err.is_empty() {
                     eprint!("{}", err);
                 }
-                Ok(Config::default().fill_from_parsed_config(parsed_config))
+                Ok(Config::default().fill_from_parsed_config(parsed_config, dir))
             }
             Err(e) => {
                 err.push_str("Error: Decoding config file failed:\n");
@@ -425,7 +426,7 @@ fn test_config_used_to_toml() {
 
     #[test]
     fn test_was_set() {
-        let config = Config::from_toml("hard_tabs = true").unwrap();
+        let config = Config::from_toml("hard_tabs = true", Path::new("")).unwrap();
 
         assert_eq!(config.was_set().hard_tabs(), true);
         assert_eq!(config.was_set().verbose(), false);
index 6299aedf4a755199870f88c4d6f73ac2f44411b3..b0ebef621706fcbc493bc237297b2f844f58add2 100644 (file)
@@ -1,6 +1,7 @@
-use ignore::{self, gitignore};
 use std::path::PathBuf;
 
+use ignore::{self, gitignore};
+
 use crate::config::{FileName, IgnoreList};
 
 pub struct IgnorePathSet {
@@ -33,16 +34,19 @@ pub fn is_match(&self, file_name: &FileName) -> bool {
 
 #[cfg(test)]
 mod test {
+    use std::path::{Path, PathBuf};
+
     use crate::config::{Config, FileName};
     use crate::ignore_path::IgnorePathSet;
-    use std::path::PathBuf;
 
     #[test]
     fn test_ignore_path_set() {
         match option_env!("CFG_RELEASE_CHANNEL") {
             // this test requires nightly
             None | Some("nightly") => {
-                let config = Config::from_toml(r#"ignore = ["foo.rs", "bar_dir/*"]"#).unwrap();
+                let config =
+                    Config::from_toml(r#"ignore = ["foo.rs", "bar_dir/*"]"#, Path::new(""))
+                        .unwrap();
                 let ignore_path_set = IgnorePathSet::from_ignore_list(&config.ignore()).unwrap();
 
                 assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("src/foo.rs"))));
index c084742a217ae0417380928a11010bc5d84f39ab..6766ba2af5462b3879a8a3c7252a76704421a94e 100644 (file)
@@ -564,7 +564,7 @@ fn get_config(config_file: Option<&Path>) -> Config {
         .read_to_string(&mut def_config)
         .expect("Couldn't read config");
 
-    Config::from_toml(&def_config).expect("invalid TOML")
+    Config::from_toml(&def_config, Path::new("tests/config/")).expect("invalid TOML")
 }
 
 // Reads significant comments of the form: `// rustfmt-key: value` into a hash map.