Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Show linted files * Add tests for output * Move tests, enum, and add backup tests * Fix tests from rebase * Move mode delcaration outside for loop * Make mode copy Co-authored-by: Grachev Mikhail <work@mgrachev.com>
- Loading branch information
Showing
30 changed files
with
1,080 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use crate::common::{FileEntry, Warning}; | ||
use std::ffi::OsString; | ||
use std::fmt; | ||
|
||
/// Mode in which the program is run. | ||
#[derive(Clone, Copy)] | ||
pub enum Mode { | ||
Fix, | ||
Check, | ||
} | ||
|
||
/// Prefix for the backup output. | ||
const BACKUP_PREFIX: &str = "Original file was backed up to: "; | ||
|
||
/// Wraps warnings to provide more information when printing. | ||
pub struct Output { | ||
/// Path of the file the warnings originated from. | ||
path: FileEntry, | ||
|
||
/// Path of the file's backup. | ||
backup_path: Option<OsString>, | ||
|
||
/// List of warnings. | ||
pub warnings: Vec<Warning>, | ||
|
||
/// Mode of the program. | ||
mode: Mode, | ||
} | ||
|
||
impl Output { | ||
pub fn new( | ||
path: FileEntry, | ||
backup_path: Option<OsString>, | ||
warnings: Vec<Warning>, | ||
mode: Mode, | ||
) -> Self { | ||
Self { | ||
path, | ||
backup_path, | ||
warnings, | ||
mode, | ||
} | ||
} | ||
|
||
/// Prints warnings without any additional information. | ||
pub fn print_warnings(&self) { | ||
self.warnings.iter().for_each(|w| println!("{}", w)); | ||
} | ||
|
||
/// Prints the backup file's path. | ||
pub fn print_backup(&self) { | ||
if let Some(p) = &self.backup_path { | ||
println!("{}{:?}", BACKUP_PREFIX, p); | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for Output { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self.mode { | ||
Mode::Fix => { | ||
write!(f, "Fixing {}", self.path)?; | ||
} | ||
Mode::Check => { | ||
write!(f, "Checking {}", self.path)?; | ||
} | ||
} | ||
if let Some(p) = &self.backup_path { | ||
writeln!(f, "\n{}{:?}", BACKUP_PREFIX, p)?; | ||
} | ||
if !self.warnings.is_empty() { | ||
writeln!(f)?; | ||
} | ||
for w in self.warnings.iter() { | ||
writeln!(f, "{}", w)?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::common::tests::*; | ||
|
||
#[test] | ||
fn output_fmt_test() { | ||
let line = line_entry(1, 1, "FOO=BAR"); | ||
let warning = Warning::new( | ||
line.clone(), | ||
"DuplicatedKey", | ||
String::from("The FOO key is duplicated"), | ||
); | ||
let output = Output::new(line.file, None, vec![warning], Mode::Check); | ||
|
||
assert_eq!( | ||
"Checking .env\n.env:1 DuplicatedKey: The FOO key is duplicated\n", | ||
format!("{}", output) | ||
); | ||
} | ||
|
||
#[test] | ||
fn fix_output_fmt_test() { | ||
let line = line_entry(1, 1, "FOO=BAR"); | ||
let warning = Warning::new( | ||
line.clone(), | ||
"DuplicatedKey", | ||
String::from("The FOO key is duplicated"), | ||
); | ||
|
||
let backup_path = OsString::from(".env_1234"); | ||
let output = Output::new(line.file, Some(backup_path), vec![warning], Mode::Fix); | ||
|
||
assert_eq!( | ||
"Fixing .env\nOriginal file was backed up to: \".env_1234\"\n\n.env:1 DuplicatedKey: The FOO key is duplicated\n", | ||
format!("{}", output) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,38 @@ | ||
use crate::common::TestDir; | ||
use crate::common::*; | ||
|
||
#[test] | ||
fn exits_with_0_on_no_warnings() { | ||
let test_dir = TestDir::new(); | ||
test_dir.create_testfile(".env", "FOO=bar\n"); | ||
test_dir.test_command_success(); | ||
let expected_output = check_output(&[(".env", &[])]); | ||
test_dir.test_command_success(expected_output); | ||
} | ||
|
||
#[test] | ||
fn checks_current_dir() { | ||
let testdir = TestDir::new(); | ||
let testfile = testdir.create_testfile(".env", "FOO\n"); | ||
|
||
testdir.test_command_fail( | ||
format!( | ||
"{}:1 KeyWithoutValue: The FOO key should be with a value or have an equal sign\n\nFound 1 problem\n", | ||
testdir.test_command_fail(check_output(&[( | ||
testfile.shortname_as_str(), | ||
&[format!( | ||
"{}:1 KeyWithoutValue: The FOO key should be with a value or have an equal sign", | ||
testfile.shortname_as_str() | ||
) | ||
); | ||
.as_str()], | ||
)])); | ||
} | ||
|
||
#[test] | ||
fn checks_current_dir_with_dot_arg() { | ||
let testdir = TestDir::new(); | ||
let testfile = testdir.create_testfile("test.env", "foo=\n"); | ||
testdir.create_testfile("test.env", "foo=\n"); | ||
|
||
let args = &["."]; | ||
let expected_output = format!( | ||
"{}:1 LowercaseKey: The foo key should be in uppercase\n\nFound 1 problem\n", | ||
testfile.shortname_as_str(), | ||
); | ||
let expected_output = check_output(&[( | ||
"test.env", | ||
&["test.env:1 LowercaseKey: The foo key should be in uppercase"], | ||
)]); | ||
|
||
testdir.test_command_fail_with_args(args, expected_output); | ||
} |
Oops, something went wrong.