Skip to content

Commit 3fd9ddf

Browse files
committed
test: coverage config-related functions
1 parent e50f15c commit 3fd9ddf

File tree

4 files changed

+73
-15
lines changed

4 files changed

+73
-15
lines changed

src/commands.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::variables;
1515
pub fn run_command<W: Write>(command: &Commands, config: Option<Config>, mut buffer: W) -> ExitCode {
1616
match command {
1717
Commands::InitConfig => {
18-
if let Err(error) = config::init_config() {
18+
if let Err(error) = config::init_config(config::get_config_file_path(), buffer) {
1919
error!("{}", error);
2020
return ExitCode::FAILURE;
2121
}

src/config.rs

+68-10
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,103 @@
1-
use std::{fs, io, path::PathBuf};
1+
use std::{fs, io::{self, Write}, path::PathBuf};
22

33
use dirs::config_dir;
44

55
use crate::models::{Config, ConfigParsingError};
66

7-
/// Get path to config file
7+
/// Get path to config directory
88
fn get_config_dir() -> PathBuf {
99
config_dir().unwrap_or_default()
1010
}
1111

12+
/// Get path to config file
13+
pub fn get_config_file_path() -> PathBuf {
14+
get_config_dir().join("envfetch.toml")
15+
}
16+
1217
/// Read config file
13-
pub fn read_config() -> Result<Config, ConfigParsingError> {
14-
let path = get_config_dir().join("envfetch.toml");
18+
pub fn read_config_from_file(path: PathBuf) -> Result<Config, ConfigParsingError> {
1519
if !path.exists() {
1620
return Err(ConfigParsingError::FileDoesntExists);
1721
}
1822

1923
let content =
2024
fs::read_to_string(path).map_err(|err| ConfigParsingError::FSError(err.to_string()))?;
2125

26+
read_config(content)
27+
}
28+
29+
/// Read config file
30+
fn read_config(content: String) -> Result<Config, ConfigParsingError> {
2231
toml::from_str::<Config>(&content)
2332
.map_err(|err| ConfigParsingError::ParsingError(err.to_string()))
2433
}
2534

2635
/// Initialize config file
27-
pub fn init_config() -> io::Result<()> {
28-
let default = include_str!("../assets/default_config.toml");
36+
pub fn init_config<W: Write>(path: PathBuf, mut buffer: W) -> io::Result<()> {
37+
let default = default_config();
2938

30-
let file = get_config_dir().join("envfetch.toml");
39+
fs::write(&path, default)
40+
.map(|_| writeln!(buffer, "Successfully initialized config at {}", path.display()).expect("Failed to write to buffer"))
41+
}
3142

32-
fs::write(&file, default)
33-
.map(|_| eprintln!("Successfully initialized config at {}", file.display()))
43+
/// Get default config ile content
44+
fn default_config() -> &'static str {
45+
include_str!("../assets/default_config.toml")
3446
}
3547

3648
#[cfg(test)]
3749
mod tests {
38-
use crate::config::get_config_dir;
50+
use super::*;
51+
use assert_fs::prelude::*;
3952
use dirs::config_dir;
4053

4154
#[test]
4255
fn test_get_config_dir() {
4356
assert_eq!(get_config_dir(), config_dir().unwrap_or_default());
4457
}
58+
59+
#[test]
60+
fn test_get_config_file() {
61+
assert_eq!(get_config_file_path(), config_dir().unwrap_or_default().join("envfetch.toml"));
62+
}
63+
64+
#[test]
65+
fn test_default_config() {
66+
assert_eq!(default_config(), include_str!("../assets/default_config.toml"));
67+
}
68+
69+
#[test]
70+
fn test_read_config_default() {
71+
let result = read_config(default_config().to_owned()).unwrap();
72+
assert_eq!(result, Config {
73+
print_format: None
74+
})
75+
}
76+
77+
#[test]
78+
fn test_read_config_from_existent_file() {
79+
let file = assert_fs::NamedTempFile::new("envfetch.toml").unwrap();
80+
file.write_str(default_config()).unwrap();
81+
let result = read_config_from_file(file.path().to_path_buf()).unwrap();
82+
assert_eq!(result, Config {
83+
print_format: None
84+
})
85+
}
86+
87+
#[test]
88+
fn test_read_config_from_unexistent_file() {
89+
let file = assert_fs::NamedTempFile::new("envfetch.toml").unwrap();
90+
let path = file.path().to_path_buf();
91+
file.close().unwrap();
92+
let result = read_config_from_file(path);
93+
assert_eq!(result, Err(ConfigParsingError::FileDoesntExists))
94+
}
95+
96+
#[test]
97+
fn test_init_config() {
98+
let file = assert_fs::NamedTempFile::new("envfetch.toml").unwrap();
99+
let mut buffer = vec![];
100+
init_config(file.path().to_path_buf(), &mut buffer).unwrap();
101+
assert!(String::from_utf8(buffer).unwrap().contains(&format!("Successfully initialized config at {}", file.display())));
102+
}
45103
}

src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod utils;
1212
mod variables;
1313

1414
use clap::Parser;
15-
use config::read_config;
15+
use config::{read_config_from_file, get_config_file_path};
1616
use std::{io::{stdout, Write}, process::ExitCode};
1717

1818
use log::error;
@@ -32,7 +32,7 @@ fn main() -> ExitCode {
3232
)
3333
})
3434
.init();
35-
let config = read_config();
35+
let config = read_config_from_file(get_config_file_path());
3636
let config = match config {
3737
Ok(config) => Some(config),
3838
Err(ConfigParsingError::FileDoesntExists) => None,

src/models.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub enum ErrorKind {
138138
NameValidationError(String),
139139
}
140140

141-
#[derive(Debug)]
141+
#[derive(Debug, PartialEq, Eq)]
142142
pub enum ConfigParsingError {
143143
FSError(String),
144144
ParsingError(String),
@@ -184,7 +184,7 @@ impl Display for ErrorKind {
184184
}
185185
}
186186

187-
#[derive(Serialize, Deserialize)]
187+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
188188
pub struct Config {
189189
/// Format, used to print variables using print command
190190
pub print_format: Option<String>,

0 commit comments

Comments
 (0)