skallwar.suckit/tests/filters.rs

88 lines
2.8 KiB
Rust

//! Tests for using --exclude --include flags for suckit
mod fixtures;
use fixtures::get_file_count_with_pattern;
use std::fs::read_dir;
use std::process::Command;
use std::process::Stdio;
use std::sync::Once;
const SUCKIT: &'static str = "target/debug/suckit";
const ADDR: &'static str = "http://0.0.0.0:8000";
static START: Once = Once::new();
#[test]
fn test_include_exclude() {
// Spawn a single instance of a local http server usable by all tests in this module.
START.call_once(|| {
fixtures::spawn_local_http_server();
});
// Tests below are grouped together as they depend on the local_http_server above.
include_filter();
include_multiple_filters();
exclude_filter();
}
// Test to use include flag for downloading pages only matching the given pattern.
fn include_filter() {
let output_dir = "w2";
let mut cmd = Command::new(SUCKIT)
.args(&[ADDR, "-o", "w2", "-i", "mp[3-4]", "-j", "16"])
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.unwrap();
let status = cmd.wait().unwrap();
assert!(status.success());
let paths = read_dir(output_dir).unwrap();
assert_eq!(
paths.count(),
get_file_count_with_pattern("*_mp3", output_dir).unwrap()
);
std::fs::remove_dir_all(output_dir).unwrap();
}
// Test demonstrating usage of multiple include patterns for downloading pages only matching the given pattern.
fn include_multiple_filters() {
let output_dir = "w1";
let mut cmd = Command::new(SUCKIT)
.args(&[ADDR, "-o", output_dir, "-i", "(mp[3-4])|(txt)", "-j", "16"])
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.unwrap();
let status = cmd.wait().unwrap();
assert!(status.success());
let paths = read_dir(output_dir).unwrap();
let mp3_count = get_file_count_with_pattern("*_mp3", output_dir).unwrap();
let txt_count = get_file_count_with_pattern("*_txt", output_dir).unwrap();
assert_eq!(paths.count(), mp3_count + txt_count);
std::fs::remove_dir_all(output_dir).unwrap();
}
// Test to use exclude flag for excluding pages matching the given pattern.
fn exclude_filter() {
let output_dir = "w3";
let mut cmd = Command::new(SUCKIT)
.args(&[ADDR, "-o", output_dir, "-e", "jpe?g", "-j", "16"])
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.unwrap();
let status = cmd.wait().unwrap();
assert!(status.success());
let paths = read_dir(output_dir).unwrap();
let mp3_count = get_file_count_with_pattern("*_mp3", output_dir).unwrap();
let txt_count = get_file_count_with_pattern("*_txt", output_dir).unwrap();
let index_file = 1;
assert_eq!(paths.count(), mp3_count + txt_count + index_file);
std::fs::remove_dir_all(output_dir).unwrap();
}