Repository: dotenv-linter/dotenv-linter
Pull request #254 (2020-08-10 19:41:32)

Add key without value fixer
<!--
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._ -->

fixes https://github.com/dotenv-linter/dotenv-linter/issues/247


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5d10a92..363013b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@ # Changelog
 
 ## [Unreleased]
 ### 🚀 Added
+- Add fixer: KeyWithoutValueFixer [#254](https://github.com/dotenv-linter/dotenv-linter/pull/254) ([@unexge](https://github.com/unexge))
 - 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))
diff --git a/src/fixes.rs b/src/fixes.rs
index 162dc4b..9020edd 100644
--- a/src/fixes.rs
+++ b/src/fixes.rs
@@ -1,5 +1,6 @@
 use crate::common::*;
 
+mod key_without_value;
 mod lowercase_key;
 mod space_character;
 
@@ -34,6 +35,7 @@ fn fixlist() -> Vec<Box<dyn Fix>> {
     vec![
         // At first we run the fixers that handle a single line entry (they use default
         // implementation of the fix_warnings() function)
+        Box::new(key_without_value::KeyWithoutValueFixer::default()),
         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.
@@ -130,13 +132,13 @@ fn run_with_fixable_warning_test() {
     fn run_with_unfixable_warning_test() {
         let mut lines = vec![
             line_entry(1, 3, "A=B"),
-            line_entry(2, 3, "C"),
+            line_entry(2, 3, "UNFIXABLE-"),
             blank_line_entry(3, 3),
         ];
         let mut warnings = vec![Warning::new(
             lines[1].clone(),
-            "KeyWithoutValue",
-            String::from("The C key should be with a value or have an equal sign"),
+            "Unfixable",
+            String::from("The UNFIXABLE- key is not fixable"),
         )];
 
         assert_eq!(0, run(&mut warnings, &mut lines));
diff --git a/src/fixes/key_without_value.rs b/src/fixes/key_without_value.rs
new file mode 100644
index 0000000..d0c3657
--- /dev/null
+++ b/src/fixes/key_without_value.rs
@@ -0,0 +1,91 @@
+use super::Fix;
+use crate::common::*;
+
+pub(crate) struct KeyWithoutValueFixer<'a> {
+    name: &'a str,
+}
+
+impl Default for KeyWithoutValueFixer<'_> {
+    fn default() -> Self {
+        Self {
+            name: "KeyWithoutValue",
+        }
+    }
+}
+
+impl Fix for KeyWithoutValueFixer<'_> {
+    fn name(&self) -> &str {
+        self.name
+    }
+
+    fn fix_line(&self, line: &mut LineEntry) -> Option<()> {
+        line.raw_string.push('=');
+
+        Some(())
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use std::path::PathBuf;
+
+    #[test]
+    fn fix_line_test() {
+        let fixer = KeyWithoutValueFixer::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"),
+        };
+        assert_eq!(Some(()), fixer.fix_line(&mut line));
+        assert_eq!("FOO=", line.raw_string);
+    }
+
+    #[test]
+    fn fix_warnings_test() {
+        let fixer = KeyWithoutValueFixer::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"),
+            },
+            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 warning = Warning::new(
+            lines[0].clone(),
+            "KeyWithoutValue",
+            String::from("The FOO key should be with a value or have an equal sign"),
+        );
+
+        assert_eq!(Some(1), fixer.fix_warnings(vec![&mut warning], &mut lines));
+        assert_eq!("FOO=", lines[0].raw_string);
+        assert!(warning.is_fixed);
+    }
+}


Test patch:
diff --git a/tests/fixes/mod.rs b/tests/fixes/mod.rs
index f121b55..f7d2475 100644
--- a/tests/fixes/mod.rs
+++ b/tests/fixes/mod.rs
@@ -30,6 +30,21 @@ fn lowercase_key() {
     testdir.close();
 }
 
+#[test]
+fn key_without_value() {
+    let testdir = TestDir::new();
+    let testfile = testdir.create_testfile(".env", "FOO\n\nBAR=\n\nBAZ=QUX\n");
+    let expected_output = String::from(
+        "Fixed warnings:\n\
+        .env:1 KeyWithoutValue: The FOO key should be with a value or have an equal sign\n",
+    );
+    testdir.test_command_fix_success(expected_output);
+
+    assert_eq!(testfile.contents().as_str(), "FOO=\n\nBAR=\n\nBAZ=QUX\n");
+
+    testdir.close();
+}
+
 #[test]
 fn unfixed_warnings() {
     let testdir = TestDir::new();