File size: 4,190 Bytes
d5bfab8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
use super::Config;
use std::path::PathBuf;
pub enum ValidateConfigTask {
OeisMine,
}
pub trait ValidateConfig {
fn validate_config_for_task(&self, task: ValidateConfigTask) -> anyhow::Result<()>;
}
impl ValidateConfig for Config {
fn validate_config_for_task(&self, task: ValidateConfigTask) -> anyhow::Result<()> {
let mut inner = ValidateConfigInner::new(self.clone());
match task {
ValidateConfigTask::OeisMine => {
inner.validate_config_for_task_oeis_mine()?;
}
}
Ok(())
}
}
struct ValidateConfigInner {
config: Config,
messages: Vec<String>,
}
impl ValidateConfigInner {
fn new(config: Config) -> Self {
Self {
config,
messages: vec!()
}
}
fn validate_config_for_task_oeis_mine(&mut self) -> anyhow::Result<()> {
self.miner_sync_executable()?;
self.to_result()
}
fn to_result(&self) -> anyhow::Result<()> {
if self.messages.is_empty() {
return Ok(());
}
let messages_joined: String = self.messages.join("\n");
Err(anyhow::anyhow!("There is one or more issues with the ~/.loda-rust/config.toml file:\n{}", messages_joined))
}
fn miner_sync_executable(&mut self) -> anyhow::Result<()> {
let path: PathBuf = self.config.miner_sync_executable();
if !path.is_absolute() {
let s = format!("The 'miner_sync_executable' must be an absolute path, but got: {:?}", path);
self.messages.push(s);
return Ok(());
}
if !path.is_file() {
let s = format!("The 'miner_sync_executable' must be an absolute path to an executable file or script, but got: {:?}", path);
self.messages.push(s);
return Ok(());
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::{Path, PathBuf};
use std::error::Error;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use crate::config::config_from_toml_content;
#[test]
fn test_10000_miner_sync_executable_error() -> Result<(), Box<dyn Error>> {
// Arrange
let tempdir = tempfile::tempdir().unwrap();
let homedir = PathBuf::from(&tempdir.path()).join("test_10000_miner_sync_executable_error");
fs::create_dir(&homedir)?;
let content =
r#"
miner_sync_executable = "/non-existing-dir/non-existing-executable"
"#;
let basedir = PathBuf::from(Path::new("non-existing-basedir"));
let config: Config = config_from_toml_content(content.to_string(), basedir, homedir);
let mut instance = ValidateConfigInner::new(config);
// Act
instance.miner_sync_executable()?;
// Assert
let result = instance.to_result().expect_err("error");
let error_message = result.to_string();
assert_eq!(error_message.contains("The 'miner_sync_executable' must be an absolute path to an executable"), true);
Ok(())
}
#[test]
fn test_10001_miner_sync_executable_ok() -> Result<(), Box<dyn Error>> {
// Arrange
let tempdir = tempfile::tempdir().unwrap();
let homedir = PathBuf::from(&tempdir.path()).join("test_10001_miner_sync_executable_ok");
fs::create_dir(&homedir)?;
// Create a file for the `miner_sync_executable`
let path_miner_sync_executable = homedir.join("miner_sync_executable");
let mut file = File::create(&path_miner_sync_executable)?;
file.write_all("I'm a file".as_bytes())?;
// Create a config file that points to the `miner_sync_executable` file
let config_content = format!("miner_sync_executable = \"{}\"", path_miner_sync_executable.to_string_lossy());
let basedir = PathBuf::from(Path::new("non-existing-basedir"));
let config: Config = config_from_toml_content(config_content, basedir, homedir);
let mut instance = ValidateConfigInner::new(config);
// Act
instance.miner_sync_executable()?;
// Assert
instance.to_result().expect("no error");
Ok(())
}
}
|