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

refactor: some optimization #17

Merged
merged 11 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pt.4
  • Loading branch information
shixinhuang99 committed Oct 17, 2023
commit e039cae0c7da4d2ca1d60a6c0c8da18b77aaa816
2 changes: 1 addition & 1 deletion src/colorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mod tests {
use super::Colorize;

#[test]
fn no_color_in_test() {
fn test_no_color() {
assert_eq!("foo".primary(), "foo");
assert_eq!("foo".to_string().primary(), "foo");
}
Expand Down
102 changes: 22 additions & 80 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,115 +40,57 @@ impl Config {

#[cfg(test)]
mod tests {
use std::{fs, io::Write, path::PathBuf};
use std::{fs, io::Write};

use anyhow::Result;
use pretty_assertions::assert_eq;
use tempfile::{tempdir, TempDir};

use super::{Config, ConfigContent, TomlContent};
use super::Config;

fn create_temp_file(with_content: bool) -> Result<(TempDir, PathBuf)> {
fn build(create_file: bool) -> Result<(Config, TempDir)> {
let temp_dir = tempdir()?;
let config_file_path = temp_dir.path().join("config.toml");

if with_content {
let mut file = fs::File::create(&config_file_path)?;
let content = "token = \"token\"\n";
file.write_all(content.as_bytes())?;
if create_file {
let file_path = temp_dir.path().join("config.toml");
let mut file = fs::File::create(file_path)?;
file.write_all(b"token = \"token\"\n")?;
}

Ok((temp_dir, config_file_path))
}

fn build_config_content(
with_content: bool,
) -> Result<(ConfigContent, TempDir, PathBuf)> {
let (dir, file_path) = create_temp_file(with_content)?;
let cc = ConfigContent::load(&file_path)?;

Ok((cc, dir, file_path))
}

fn build_config(with_content: bool) -> Result<(Config, TempDir, PathBuf)> {
let (dir, file_path) = create_temp_file(with_content)?;
let config = Config::new(dir.path())?;

Ok((config, dir, file_path))
}

#[test]
fn config_content_new_file_exists_with_content() -> Result<()> {
let (cc, _dir, _) = build_config_content(true)?;

assert!(cc.token.is_some());
assert_eq!(cc.token.unwrap(), "token");

Ok(())
}

#[test]
fn config_content_new_file_exists_no_content() -> Result<()> {
let (cc, _dir, _) = build_config_content(false)?;
let config = Config::new(temp_dir.path())?;

assert!(cc.token.is_none());

Ok(())
Ok((config, temp_dir))
}

#[test]
fn config_content_new_file_not_exist() -> Result<()> {
let dir = tempdir()?;
let config_file_path = dir.path().join("config.toml");
fn test_config_new_not_exists() -> Result<()> {
let (config, dir) = build(false)?;

let cc = ConfigContent::load(&config_file_path)?;

assert!(cc.token.is_none());
assert_eq!(config.path, dir.path());
assert_eq!(config.token(), None);

Ok(())
}

#[test]
fn config_content_save() -> Result<()> {
let (mut cc, _dir, file_path) = build_config_content(true)?;

cc.token = Some("123".to_string());
cc.save(&file_path)?;
fn test_config_new_exists() -> Result<()> {
let (config, _dir) = build(true)?;

let content = fs::read_to_string(&file_path)?;
let expected_content = "token = \"123\"\n";
assert_eq!(content, expected_content);
assert_eq!(config.token(), Some("token"));

Ok(())
}

#[test]
fn config_new_no_content() -> Result<()> {
let (config, _dir, file_path) = build_config(false)?;

assert_eq!(config.path, file_path);
assert_eq!(config.content.token, None);

Ok(())
}
fn test_config_save() -> Result<()> {
let (mut config, _dir) = build(false)?;

#[test]
fn config_new_with_content() -> Result<()> {
let (config, _dir, file_path) = build_config(true)?;

assert_eq!(config.path, file_path);
assert_eq!(config.content.token, Some("token".to_string()));

Ok(())
}

#[test]
fn config_save_ok() -> Result<()> {
let (mut config, _dir, _) = build_config(true)?;
config.set_token("token2");
config.save()?;

config.set_token("123");
let content = fs::read_to_string(&config.path)?;

assert_eq!(config.content.token, Some("123".to_string()));
assert_eq!(content, "token = \"token2\"\n");

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use thiserror::Error;
#[derive(Error, Debug)]
pub enum ScafalraError {
#[error("Error accessing specified path: `{}`", .0)]
IOError(String),
IO(String),
#[error("No GitHub personal access token configured")]
NoToken,
#[error("Call to GitHub api error")]
Expand Down
28 changes: 17 additions & 11 deletions src/toml_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ where
pub trait TomlContent: DeserializeOwned + Serialize + Default {
fn load(file: &Path) -> Result<Self> {
load_or_default(file, Self::default())
.context(ScafalraError::IOError(file.display().to_string()))
.context(ScafalraError::IO(file.display().to_string()))
}

fn save(&self, file: &Path) -> Result<()> {
save_content(file, self)
.context(ScafalraError::IOError(file.display().to_string()))
.context(ScafalraError::IO(file.display().to_string()))
}
}

Expand Down Expand Up @@ -77,28 +77,34 @@ mod tests {
}

#[test]
fn test_load_file_exists() {
let (foo, _dir, _file_path) = Foo::build(true).unwrap();
fn test_load_file_exists() -> Result<()> {
let (foo, _dir, _file_path) = Foo::build(true)?;

assert_eq!(foo.bar, "bar");

Ok(())
}

#[test]
fn test_load_file_not_exists() {
let (foo, _dir, file_path) = Foo::build(false).unwrap();
fn test_load_file_not_exists() -> Result<()> {
let (foo, _dir, file_path) = Foo::build(false)?;

assert_eq!(foo.bar, "");
assert!(file_path.exists());

Ok(())
}

#[test]
fn test_save() {
let (mut foo, _dir, file_path) = Foo::build(true).unwrap();
fn test_save() -> Result<()> {
let (mut foo, _dir, file_path) = Foo::build(true)?;

foo.bar = "bar2".to_string();
foo.save(&file_path).unwrap();
foo.save(&file_path)?;

let content = fs::read_to_string(&file_path)?;
assert_eq!(content, "bar = \"bar2\"\n");

let content = fs::read_to_string(&file_path).unwrap();
assert_eq!(content, "bar = \"bar2\"\n")
Ok(())
}
}
Loading