aboutsummaryrefslogtreecommitdiff
path: root/tests/cli_test.rs
blob: 5209af46e994959818522d7017861f88a847d384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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"));
}