diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-18 18:58:11 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-18 18:58:11 +0200 |
| commit | 323a4fa3c75794885199b580f266e436f6c646a2 (patch) | |
| tree | d4c2d16760e0a05ecd653f2bfa9348d436900e4f /tests | |
| parent | c10134b402544085aba900eaaa2aa485be2cd390 (diff) | |
Add integration tests
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/cli_test.rs | 102 |
1 files changed, 102 insertions, 0 deletions
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"), + "<h1> This is my post.</h1>", + ); + + 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")); +} |