Repository: dotenv-linter/dotenv-linter
Pull request #253 (2020-08-10 13:22:45)

Add fixer: SpaceCharacterFixer (#250)
<!--
Thank you for your pull request. Please provide a description above and review
the requirements below.

Bug fixes and new features should include tests.
-->

<!-- _Please make sure to review and check all of these items:_ -->

#### ✔ Checklist:
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] This PR has been added to [CHANGELOG.md](https://github.com/dotenv-linter/dotenv-linter/blob/master/CHANGELOG.md) (at the top of the list);
- [X] Tests for the changes have been added (for bug fixes / features);
- [X] Docs have been added / updated (for bug fixes / features).

<!-- _NOTE: these things are not required to open a PR and can be done afterwards / while the PR is open._ -->



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 56b267f..5d10a92 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@ # Changelog
 
 ## [Unreleased]
 ### 🚀 Added
+- Add fixer: SpaceCharacterFixer [#253](https://github.com/dotenv-linter/dotenv-linter/pull/253) ([@DDtKey](https://github.com/DDtKey))
 - Add total problems to output and `--quiet` argument [#242](https://github.com/dotenv-linter/dotenv-linter/pull/242) ([@wesleimp](https://github.com/wesleimp), [@mgrachev](https://github.com/mgrachev))
 - Add autofix feature (for LowercaseKey check) [#228](https://github.com/dotenv-linter/dotenv-linter/pull/228) ([@evgeniy-r](https://github.com/evgeniy-r))
 - Add installation CI test for Windows (via `install.sh`) [#235](https://github.com/dotenv-linter/dotenv-linter/pull/235) ([@DDtKey](https://github.com/DDtKey))
diff --git a/src/fixes.rs b/src/fixes.rs
index 9b0d670..162dc4b 100644
--- a/src/fixes.rs
+++ b/src/fixes.rs
@@ -1,6 +1,7 @@
 use crate::common::*;
 
 mod lowercase_key;
+mod space_character;
 
 trait Fix {
     fn name(&self) -> &str;
@@ -34,6 +35,7 @@ fn fixlist() -> Vec<Box<dyn Fix>> {
         // At first we run the fixers that handle a single line entry (they use default
         // implementation of the fix_warnings() function)
         Box::new(lowercase_key::LowercaseKeyFixer::default()),
+        Box::new(space_character::SpaceCharacterFixer::default()),
         // Then we should run the fixers that handle the line entry collection at whole.
         // And at the end we should run the fixer for ExtraBlankLine check (because the previous
         // fixers can create additional extra blank lines).
diff --git a/src/fixes/space_character.rs b/src/fixes/space_character.rs
new file mode 100644
index 0000000..e08137e
--- /dev/null
+++ b/src/fixes/space_character.rs
@@ -0,0 +1,119 @@
+use super::Fix;
+use crate::common::*;
+
+pub(crate) struct SpaceCharacterFixer<'a> {
+    name: &'a str,
+}
+
+impl Default for SpaceCharacterFixer<'_> {
+    fn default() -> Self {
+        Self {
+            name: "SpaceCharacter",
+        }
+    }
+}
+
+impl Fix for SpaceCharacterFixer<'_> {
+    fn name(&self) -> &str {
+        self.name
+    }
+
+    fn fix_line(&self, line: &mut LineEntry) -> Option<()> {
+        let key = line.get_key()?;
+        let value = line.get_value()?;
+        line.raw_string = format!("{}={}", key.trim_end(), value.trim_start());
+
+        Some(())
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use std::path::PathBuf;
+
+    #[test]
+    fn fix_line_test() {
+        let fixer = SpaceCharacterFixer::default();
+        let mut line = LineEntry {
+            number: 1,
+            file: FileEntry {
+                path: PathBuf::from(".env"),
+                file_name: ".env".to_string(),
+                total_lines: 1,
+            },
+            raw_string: String::from("FOO = BAR"),
+        };
+        assert_eq!(Some(()), fixer.fix_line(&mut line));
+        assert_eq!("FOO=BAR", line.raw_string);
+    }
+
+    #[test]
+    fn trailing_should_not_be_fixed() {
+        let fixer = SpaceCharacterFixer::default();
+        let mut line = LineEntry {
+            number: 1,
+            file: FileEntry {
+                path: PathBuf::from(".env"),
+                file_name: ".env".to_string(),
+                total_lines: 1,
+            },
+            raw_string: String::from("DEBUG_HTTP=true "),
+        };
+        assert_eq!(Some(()), fixer.fix_line(&mut line));
+        assert_eq!("DEBUG_HTTP=true ", line.raw_string);
+    }
+
+    #[test]
+    fn fix_warnings_test() {
+        let fixer = SpaceCharacterFixer::default();
+        let mut lines = vec![
+            LineEntry {
+                number: 1,
+                file: FileEntry {
+                    path: PathBuf::from(".env"),
+                    file_name: ".env".to_string(),
+                    total_lines: 3,
+                },
+                raw_string: String::from("FOO= BAR"),
+            },
+            LineEntry {
+                number: 2,
+                file: FileEntry {
+                    path: PathBuf::from(".env"),
+                    file_name: ".env".to_string(),
+                    total_lines: 3,
+                },
+                raw_string: String::from("Z =Y"),
+            },
+            LineEntry {
+                number: 3,
+                file: FileEntry {
+                    path: PathBuf::from(".env"),
+                    file_name: ".env".to_string(),
+                    total_lines: 3,
+                },
+                raw_string: String::from("\n"),
+            },
+        ];
+        let mut warnings = vec![
+            Warning::new(
+                lines[0].clone(),
+                "SpaceCharacter",
+                String::from("The line has spaces around equal sign"),
+            ),
+            Warning::new(
+                lines[1].clone(),
+                "SpaceCharacter",
+                String::from("The line has spaces around equal sign"),
+            ),
+        ];
+
+        assert_eq!(
+            Some(2),
+            fixer.fix_warnings(warnings.iter_mut().collect(), &mut lines)
+        );
+        assert_eq!("FOO=BAR", lines[0].raw_string);
+        assert_eq!("Z=Y", lines[1].raw_string);
+    }
+}


Test patch:
diff --git a/tests/fixes/mod.rs b/tests/fixes/mod.rs
index 880965f..f121b55 100644
--- a/tests/fixes/mod.rs
+++ b/tests/fixes/mod.rs
@@ -1,5 +1,7 @@
 use crate::common::TestDir;
 
+mod space_character;
+
 #[test]
 fn correct_file() {
     let testdir = TestDir::new();
diff --git a/tests/fixes/space_character.rs b/tests/fixes/space_character.rs
new file mode 100644
index 0000000..46fdf8f
--- /dev/null
+++ b/tests/fixes/space_character.rs
@@ -0,0 +1,17 @@
+use crate::common::TestDir;
+
+#[test]
+fn space_character() {
+    let testdir = TestDir::new();
+    let testfile = testdir.create_testfile(".env", "ABC = DEF\n\nFOO= BAR\n");
+    let expected_output = String::from(
+        "Fixed warnings:\n\
+        .env:1 SpaceCharacter: The line has spaces around equal sign\n\
+        .env:3 SpaceCharacter: The line has spaces around equal sign\n",
+    );
+    testdir.test_command_fix_success(expected_output);
+
+    assert_eq!(testfile.contents().as_str(), "ABC=DEF\n\nFOO=BAR\n");
+
+    testdir.close();
+}