aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-07-18 18:58:11 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2025-07-18 18:58:11 +0200
commit323a4fa3c75794885199b580f266e436f6c646a2 (patch)
treed4c2d16760e0a05ecd653f2bfa9348d436900e4f
parentc10134b402544085aba900eaaa2aa485be2cd390 (diff)
Add integration tests
-rw-r--r--src/main.rs9
-rw-r--r--tests/cli_test.rs102
2 files changed, 109 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index 9245dcc..b84939c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,7 +12,7 @@ mod utils;
use command::{Command, available_commands, help::Help};
use configuration::Configuration;
use std::env::args;
-use std::io::Result;
+use std::io::{Error, Result};
use std::iter::once;
fn main() -> Result<()> {
@@ -35,6 +35,7 @@ fn run() -> Result<()> {
let configuration = Configuration::new()?;
let commands = available_commands();
let arguments: Vec<String> = args().collect();
+ let message;
if let Some(command_name) = arguments.get(1) {
if let Some(main_command) = commands.into_iter().find(|c| c.command() == command_name) {
@@ -53,7 +54,11 @@ fn run() -> Result<()> {
return Ok(());
}
+ message = format!("Command {command_name} does not exist.");
+ } else {
+ message = "You must provide at least one command.".to_string();
}
- Help::new().execute(None, &configuration, "help")
+ Help::new().execute(None, &configuration, "help")?;
+ Err(Error::other(message))
}
diff --git a/tests/cli_test.rs b/tests/cli_test.rs
new file mode 100644
index 0000000..5209af4
--- /dev/null
+++ b/tests/cli_test.rs
@@ -0,0 +1,102 @@
+use std::fs::create_dir_all;
+use std::process::Command;
+use test_utilities::*;
+
+#[test]
+fn test_basic_command() {
+ let test_dir = setup_test_dir();
+ let config_dir = test_dir.join("config");
+ let data_dir = test_dir.join("data");
+ let output_dir = test_dir.join("output");
+ create_dir_all(&config_dir).expect("Could not create config directory");
+ create_dir_all(&data_dir).expect("Could not create data directory");
+ create_dir_all(&output_dir).expect("Could not create output directory");
+
+ // Create test input files
+ create_test_file(
+ &test_dir.join("my_post.gmi"),
+ "\
+# This is my post.
+## I hope you like it.
+",
+ );
+
+ // Run the program from the test directory
+ let status = Command::new(env!("CARGO_BIN_EXE_blog"))
+ .arg("add")
+ .arg("my_post.gmi")
+ .current_dir(&test_dir)
+ .env("BLOG_CONFIG_DIRECTORY", &config_dir)
+ .env("BLOG_DATA_DIRECTORY", &data_dir)
+ .env("BLOG_OUTPUT_DIRECTORY", &output_dir)
+ .status()
+ .expect("Failed to execute add command");
+
+ assert!(status.success());
+
+ assert!(data_dir.join("blog/posts/0/my_post.gmi").exists());
+ assert!(data_dir.join("blog/archive").exists());
+
+ let status = Command::new(env!("CARGO_BIN_EXE_blog"))
+ .arg("generate")
+ .current_dir(&test_dir)
+ .env("BLOG_CONFIG_DIRECTORY", &config_dir)
+ .env("BLOG_DATA_DIRECTORY", &data_dir)
+ .env("BLOG_OUTPUT_DIRECTORY", &output_dir)
+ .status()
+ .expect("Failed to execute generate command");
+
+ assert!(status.success());
+
+ assert!(output_dir.join("blog/blog/index.html").exists());
+ assert_file_contains(
+ &output_dir.join("blog/blog/index.html"),
+ "<h1> This is my post.</h1>",
+ );
+ assert!(output_dir.join("blog/blog/index.txt").exists());
+ assert_file_contains(
+ &output_dir.join("blog/blog/index.txt"),
+ "# This is my post.",
+ );
+ assert!(output_dir.join("blog/blog/feed.xml").exists());
+ assert_file_contains(
+ &output_dir.join("blog/blog/feed.xml"),
+ "&lt;h1&gt; This is my post.&lt;/h1&gt;",
+ );
+
+ assert!(output_dir.join("blog/archive/index.gmi").exists());
+ assert!(output_dir.join("blog/archive/index.gph").exists());
+
+ cleanup_test_dir(&test_dir);
+}
+
+#[test]
+fn test_invalid_commands_are_error_but_print_help() {
+ let output = Command::new(env!("CARGO_BIN_EXE_blog"))
+ .arg("nobodyrunsthisever")
+ .output()
+ .expect("Failed to execute add command");
+
+ assert!(!output.status.success());
+
+ let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8 in stdout");
+ assert!(stdout.contains("Usage:"));
+ assert!(stdout.contains("blog add"));
+ assert!(stdout.contains("blog update"));
+ assert!(stdout.contains("blog generate"));
+}
+
+#[test]
+fn test_empty_commands_are_error_but_print_help() {
+ let output = Command::new(env!("CARGO_BIN_EXE_blog"))
+ .output()
+ .expect("Failed to execute add command");
+
+ assert!(!output.status.success());
+
+ let stdout = String::from_utf8(output.stdout).expect("Invalid UTF-8 in stdout");
+ assert!(stdout.contains("Usage:"));
+ assert!(stdout.contains("blog add"));
+ assert!(stdout.contains("blog update"));
+ assert!(stdout.contains("blog generate"));
+}