Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
chore: add more tests
Browse files Browse the repository at this point in the history
chore: add more tests
  • Loading branch information
tsirysndr committed May 23, 2023
1 parent 89f4be1 commit 2c81698
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Superfile.hcl
32 changes: 32 additions & 0 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,35 @@ pub fn verify_if_config_file_is_present() -> Result<(String, String), Error> {
let config = std::fs::read_to_string(current_dir.join(SUPERFILE_TOML))?;
return Ok((config, String::from("toml")));
}

#[cfg(test)]
pub mod tests {
use super::*;

pub const CONFIG_EXAMPLE: &str = r#"
project = "demo"
service "demo" {
type = "exec"
command = "ping $GITHUB_DOMAIN"
working_dir = "/tmp"
description = "Ping Service Example"
depends_on = []
env = {
"GITHUB_DOMAIN" = "github.com"
}
stdout = "/tmp/demo-stdout.log"
stderr = "/tmp/demo-stderr.log"
}
"#;

#[test]
fn test_verify_if_config_file_is_present() {
// create a default config file
let current_dir = std::env::current_dir().unwrap();
let config_file = current_dir.join(SUPERFILE);
std::fs::write(config_file, CONFIG_EXAMPLE).unwrap();
let result = verify_if_config_file_is_present();
assert!(result.is_ok());
}
}
1 change: 0 additions & 1 deletion crates/core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use superviseur_types::{
command::SuperviseurCommand,
configuration::{ConfigurationData, Service},
events::{ProcessEvent, SuperviseurEvent},
log::Log,
process::{Process, State},
};

Expand Down
3 changes: 3 additions & 0 deletions crates/log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ version = "0.1.0"
[dependencies]
superviseur-types = {path = "../types", version = "0.1.0"}
tantivy = "0.19.2"

[dev-dependencies]
chrono = "0.4.23"
30 changes: 30 additions & 0 deletions crates/log/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,33 @@ impl LogEngine {
.collect())
}
}

#[cfg(test)]
pub mod tests {
use std::time::Duration;

#[test]
fn test_log_engine() {
use crate::log::LogEngine;
use superviseur_types::log::Log;

let log_engine = LogEngine::new();
let log = Log {
project: "demo_project".to_string(),
service: "demo_service".to_string(),
line: "demo_line".to_string(),
date: tantivy::DateTime::from_timestamp_secs(
chrono::DateTime::parse_from_rfc3339("2021-01-01T00:00:00Z")
.unwrap()
.timestamp(),
),
output: "demo_output".to_string(),
};
log_engine.insert(&log).unwrap();
std::thread::sleep(Duration::from_secs(1));
let logs = log_engine.search("demo_line").unwrap();
assert_eq!(logs.len(), 1);
let logs = log_engine.search_in_service("demo_line").unwrap();
assert_eq!(logs.len(), 1);
}
}
18 changes: 18 additions & 0 deletions crates/util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,21 @@ pub fn read_lines(path: &str) -> Result<Vec<String>, Error> {
}
Ok(lines)
}

#[cfg(test)]
pub mod tests {
#[test]
fn convert_dir_path_to_absolute_path() {
let current_dir = std::env::current_dir().unwrap();
let current_dir = current_dir.to_str().unwrap();
let dir = "./test";
let dir = super::convert_dir_path_to_absolute_path(dir, current_dir).unwrap();
assert_eq!(dir, format!("{}/test", current_dir));
}

#[test]
fn read_lines() {
let lines = super::read_lines("Cargo.toml").unwrap();
assert_eq!(lines[0], "[package]");
}
}

0 comments on commit 2c81698

Please sign in to comment.