Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add key without value fixer #254

Merged
merged 3 commits into from Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [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))
Expand Down
8 changes: 5 additions & 3 deletions src/fixes.rs
@@ -1,5 +1,6 @@
use crate::common::*;

mod key_without_value;
mod lowercase_key;
mod space_character;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -130,13 +132,13 @@ mod tests {
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that in the future we should remove this test (when all checks will have Fixer).
At the moment use unreachable check name is ok :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right 👍

String::from("The UNFIXABLE- key is not fixable"),
)];

assert_eq!(0, run(&mut warnings, &mut lines));
Expand Down
91 changes: 91 additions & 0 deletions 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);
}
}
15 changes: 15 additions & 0 deletions tests/fixes/mod.rs
Expand Up @@ -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();
Expand Down