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

chore: add more tests #58

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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.

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<img src="https://img.shields.io/crates/dr/superviseur" />
</a>

<a href="https://codecov.io/gh/tsirysndr/superviseur" target="_blank">
<img src="https://codecov.io/gh/tsirysndr/superviseur/branch/master/graph/badge.svg?token=" />
</a>

<a href="https://feat-webui--640724a8e12e5a011d6d59fb.chromatic.com" target="_blank">
<img src="https://img.shields.io/badge/Storybook-FF4785?logo=storybook&logoColor=fff" />
</a>
Expand Down
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);
}
}
54 changes: 54 additions & 0 deletions crates/types/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,57 @@ impl From<Document> for Log {
}
}
}

#[cfg(test)]
pub mod tests {
use tantivy::{schema::Cardinality, DateOptions};

#[test]
fn test_from_document() {
use super::*;
use tantivy::schema::Schema;
use tantivy::Document;

let date_opts = DateOptions::from(schema::INDEXED)
.set_stored()
.set_fast(Cardinality::SingleValue)
.set_precision(tantivy::DatePrecision::Seconds);

let mut schema = Schema::builder();
let project_field = schema.add_text_field("project", schema::TEXT);
let service_field = schema.add_text_field("service", schema::TEXT);
let line_field = schema.add_text_field("line", schema::TEXT);
let date_field = schema.add_date_field("date", date_opts);
let output_field = schema.add_text_field("output", schema::TEXT);

schema.build();

let mut doc = Document::default();
doc.add_text(project_field, "project");
doc.add_text(service_field, "service");
doc.add_text(line_field, "line");
doc.add_date(
date_field,
tantivy::DateTime::from_timestamp_secs(
chrono::DateTime::parse_from_rfc3339("2021-01-01T00:00:00Z")
.unwrap()
.timestamp(),
),
);
doc.add_text(output_field, "output");

let log = Log::from(doc);
assert_eq!(log.project, "project");
assert_eq!(log.service, "service");
assert_eq!(log.line, "line");
assert_eq!(log.output, "output");
assert_eq!(
log.date,
tantivy::DateTime::from_timestamp_secs(
chrono::DateTime::parse_from_rfc3339("2021-01-01T00:00:00Z")
.unwrap()
.timestamp()
)
);
}
}
64 changes: 63 additions & 1 deletion crates/types/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,66 @@ fn display_command(command: &str) -> String {
} else {
format!("\"{}\"", command.to_string())
}
}
}

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

#[test]
fn test_display_command() {
assert_eq!(
display_command("echo \"Hello World\""),
"\"echo \"Hello World\"\""
);
assert_eq!(
display_command("echo \"Hello World\" && sleep 1"),
"\"echo \"Hello World\" &...\""
);
assert_eq!(display_port(&Some(0)), "-".to_string());
assert_eq!(display_port(&Some(8080)), "8080".to_string());
}

#[test]
fn test_display_option() {
assert_eq!(display_option(&Some(0)), "0".to_string());
assert_eq!(display_option(&Some("test")), "test".to_string());
assert_eq!(display_option(&None::<String>), "?".to_string());
}

#[test]
fn test_display_up_time() {
assert_eq!(
display_up_time(&Some(Utc::now() - Duration::seconds(10))),
"Up 10 seconds ago".to_string()
);
assert_eq!(
display_up_time(&Some(Utc::now() - Duration::minutes(1))),
"Up 1 minute ago".to_string()
);
assert_eq!(
display_up_time(&Some(Utc::now() - Duration::minutes(2))),
"Up 2 minutes ago".to_string()
);
assert_eq!(
display_up_time(&Some(Utc::now() - Duration::hours(1))),
"Up 1 hour ago".to_string()
);
assert_eq!(
display_up_time(&Some(Utc::now() - Duration::hours(2))),
"Up 2 hours ago".to_string()
);
assert_eq!(
display_up_time(&Some(Utc::now() - Duration::days(1))),
"Up 1 day ago".to_string()
);
assert_eq!(
display_up_time(&Some(Utc::now() - Duration::days(2))),
"Up 2 days ago".to_string()
);
assert_eq!(
display_up_time(&None::<DateTime<Utc>>),
"Stopped".to_string()
);
}
}
19 changes: 19 additions & 0 deletions crates/types/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,22 @@ fn display_port(port: &Option<u32>) -> String {
None => "-".to_string(),
}
}

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

#[test]
fn test_display_command() {
assert_eq!(
display_command("echo \"Hello World\""),
"\"echo \"Hello World\"\""
);
assert_eq!(
display_command("echo \"Hello World\" && sleep 1"),
"\"echo \"Hello World\" &...\""
);
assert_eq!(display_port(&Some(0)), "-".to_string());
assert_eq!(display_port(&Some(8080)), "8080".to_string());
}
}
17 changes: 17 additions & 0 deletions crates/types/src/status.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{fmt, str::FromStr};

#[derive(Debug, PartialEq)]
pub enum Status {
Running,
Stopped,
Expand Down Expand Up @@ -33,3 +34,19 @@ impl FromStr for Status {
}
}
}

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

#[test]
fn test_status_from_str() {
assert_eq!(Status::from_str("running").unwrap(), Status::Running);
assert_eq!(Status::from_str("stopped").unwrap(), Status::Stopped);
assert_eq!(
Status::from_str("unknown"),
Err("Unknown status: unknown".to_string())
);
assert!(Status::from_str("invalid").is_err());
}
}
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]");
}
}
Loading