Integration tests: homepage (WIP)
This commit is contained in:
parent
fb62288161
commit
1485110204
6 changed files with 556 additions and 41 deletions
|
|
@ -59,7 +59,6 @@ impl Connection {
|
|||
Self::new_from_file(path).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub async fn new_in_memory() -> Result<Connection> {
|
||||
Self::create_connection(SqlitePoolOptions::new().connect("sqlite::memory:").await?).await
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,13 +29,16 @@ const TRACING_LEVEL: tracing::Level = tracing::Level::INFO;
|
|||
const TRACING_DISPLAY_THREAD: bool = false;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Log {
|
||||
_guard: Arc<WorkerGuard>,
|
||||
directory: PathBuf,
|
||||
pub enum Log {
|
||||
FileAndStdout {
|
||||
_guard: Arc<WorkerGuard>,
|
||||
directory: PathBuf,
|
||||
},
|
||||
StdoutOnly,
|
||||
}
|
||||
|
||||
impl Log {
|
||||
pub fn new<P>(directory: P) -> Self
|
||||
pub fn new_to_directory<P>(directory: P) -> Self
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
|
|
@ -68,51 +71,69 @@ impl Log {
|
|||
.with(layer_stdout)
|
||||
.init();
|
||||
|
||||
Log {
|
||||
Log::FileAndStdout {
|
||||
_guard: Arc::new(guard),
|
||||
directory: directory.as_ref().to_path_buf(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_names(&self) -> std::io::Result<Vec<String>> {
|
||||
fn dir_entry_to_string(entry: Result<DirEntry, io::Error>) -> String {
|
||||
entry.map_or_else(
|
||||
|err| format!("Unable to read entry: {}", err),
|
||||
|entry| {
|
||||
entry
|
||||
.path()
|
||||
.file_name()
|
||||
.map_or("Unable to read filename".into(), |filename| {
|
||||
filename
|
||||
.to_str()
|
||||
.map_or("Unable to read filename".into(), |filename| {
|
||||
filename.to_string()
|
||||
})
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
pub fn new_stdout_only() -> Self {
|
||||
let layer_stdout = tracing_subscriber::fmt::layer()
|
||||
.with_writer(std::io::stdout.with_max_level(TRACING_LEVEL))
|
||||
.with_thread_ids(TRACING_DISPLAY_THREAD)
|
||||
.with_thread_names(TRACING_DISPLAY_THREAD);
|
||||
|
||||
Ok(self
|
||||
.directory
|
||||
.read_dir()?
|
||||
.map(dir_entry_to_string)
|
||||
.sorted()
|
||||
.rev()
|
||||
.collect())
|
||||
tracing_subscriber::Registry::default()
|
||||
.with(layer_stdout)
|
||||
.init();
|
||||
|
||||
Log::StdoutOnly
|
||||
}
|
||||
|
||||
pub fn file_names(&self) -> std::io::Result<Vec<String>> {
|
||||
match self {
|
||||
Log::FileAndStdout { _guard, directory } => {
|
||||
fn dir_entry_to_string(entry: Result<DirEntry, io::Error>) -> String {
|
||||
entry.map_or_else(
|
||||
|err| format!("Unable to read entry: {}", err),
|
||||
|entry| {
|
||||
entry.path().file_name().map_or(
|
||||
"Unable to read filename".into(),
|
||||
|filename| {
|
||||
filename
|
||||
.to_str()
|
||||
.map_or("Unable to read filename".into(), |filename| {
|
||||
filename.to_string()
|
||||
})
|
||||
},
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Ok(directory
|
||||
.read_dir()?
|
||||
.map(dir_entry_to_string)
|
||||
.sorted()
|
||||
.rev()
|
||||
.collect())
|
||||
}
|
||||
Log::StdoutOnly => Ok(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the content of a log file and return it as a vector of lines.
|
||||
pub fn read_content(&self, filename: &str) -> std::io::Result<Vec<String>> {
|
||||
let filepath = self.directory.join(filename);
|
||||
if filepath.is_file() {
|
||||
let file = File::open(filepath)?;
|
||||
Ok(BufReader::new(file)
|
||||
.lines()
|
||||
.map(|l| l.unwrap_or_default())
|
||||
.collect())
|
||||
} else {
|
||||
Ok(vec![])
|
||||
match self {
|
||||
Log::FileAndStdout { _guard, directory } => {
|
||||
let filepath = directory.join(filename);
|
||||
let file = File::open(filepath)?;
|
||||
Ok(BufReader::new(file)
|
||||
.lines()
|
||||
.map(|l| l.unwrap_or_default())
|
||||
.collect())
|
||||
}
|
||||
Log::StdoutOnly => Ok(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ use recipes::{
|
|||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config = config::load();
|
||||
let log = Log::new(&config.logs_directory);
|
||||
let log = Log::new_to_directory(&config.logs_directory);
|
||||
|
||||
event!(Level::INFO, "Configuration: {:?}", config);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue