Skip to content

Commit

Permalink
test: validate most utility functions (#97)
Browse files Browse the repository at this point in the history
* test(parser): test all version code paths

* test(parser): test all variable code paths

* test(parser): test all uri code paths

* test(parser): test all method code paths

* test(parser): add more tests for tokenize and parse_requests

* test(parser): reject header if variable is missing

* test: validate find_http_files ignores non http files

* test(request): validate input is set

* test: use from_static instead of from_str when possible

* test(cli): utility functions
  • Loading branch information
hougesen authored Dec 29, 2023
1 parent 1b0aa2b commit bea55d1
Show file tree
Hide file tree
Showing 14 changed files with 1,187 additions and 207 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ test:
make lint
RUST_BACKTRACE=full cargo test --release

test-coverage:
cargo llvm-cov clean --workspace
cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info
cargo llvm-cov --open

publish-crates:
make build
make test
Expand Down
6 changes: 6 additions & 0 deletions hitt-cli/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ mod test_find_http_files {

std::fs::create_dir_all(dir.path().join("nested")).expect("it to create dir");

std::fs::File::create(dir.path().join("not-a-http-file.js"))
.expect("it to create the file");

std::fs::File::create(dir.path().join("nested/file1.http")).expect("it to create a file");

std::fs::File::create(dir.path().join("nested/file2.http")).expect("it to create a file");
Expand All @@ -111,6 +114,9 @@ mod test_find_http_files {
std::fs::create_dir_all(dir.path().join("ignored_folder"))
.expect("it to create directories");

std::fs::File::create(dir.path().join("not-a-http-file.js"))
.expect("it to create the file");

std::fs::File::create(dir.path().join("not-ignored-file.http"))
.expect("it to create the file");

Expand Down
52 changes: 52 additions & 0 deletions hitt-cli/src/terminal/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ fn __print_body(term: &console::Term, body: &str) -> Result<(), std::io::Error>
term.write_line(&format!("\n{TEXT_YELLOW}{body}{TEXT_RESET}"))
}

#[cfg(test)]
mod __test_print_body {
use super::__print_body;

#[test]
fn it_should_print_without_errors() {
let term = console::Term::stdout();

// TODO: validate what is written to stdout
__print_body(&term, "body").expect("it not to return an error");
}
}

#[inline]
pub fn print_body(
term: &console::Term,
Expand All @@ -24,3 +37,42 @@ pub fn print_body(

__print_body(term, body)
}

#[cfg(test)]
mod test_print_body {
use hitt_formatter::ContentType;

use super::print_body;

#[test]
fn it_should_print_without_errors() {
let term = console::Term::stdout();

print_body(&term, "body", ContentType::Unknown, false).expect("it not to return an error");

print_body(&term, "body", ContentType::Unknown, true).expect("it not to return an error");
}

#[test]
fn it_should_format_json() {
let term = console::Term::stdout();

let input = "{\"key\":\"value\"}";

// TODO: test stdout is formatted
print_body(&term, input, ContentType::Json, false).expect("it not to return an error");

// TODO: test stdout is not formatted
print_body(&term, input, ContentType::Json, true).expect("it not to return an error");
}

#[test]
fn it_should_format_json_if_pretty_printing_disable() {
let term = console::Term::stdout();

let input = "{\"key\":\"value\"}";

// TODO: test stdout is not formatted
print_body(&term, input, ContentType::Json, true).expect("it not to return an error");
}
}
175 changes: 163 additions & 12 deletions hitt-cli/src/terminal/editor.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,68 @@
use std::io::Write;
use std::{ffi::OsString, io::Write};

use console::{Key, Term};

use super::input::confirm_input;

#[inline]
fn get_default_editor() -> std::ffi::OsString {
if let Some(prog) = std::env::var_os("VISUAL") {
return prog;
if let Ok(prog) = std::env::var("VISUAL") {
return OsString::from(prog);
}

if let Some(prog) = std::env::var_os("EDITOR") {
return prog;
if let Ok(prog) = std::env::var("EDITOR") {
return OsString::from(prog);
}

if cfg!(windows) {
"notepad.exe".into()
} else {
"vi".into()
#[cfg(windows)]
{
return "notepad.exe".into();
}

"vi".into()
}

#[cfg(test)]
mod test_get_default_editor {
use super::get_default_editor;

#[test]
fn test_default_values() {
std::env::remove_var("EDITOR");
std::env::remove_var("VISUAL");

#[cfg(windows)]
{
assert_eq!("notepad.exe", get_default_editor());
}
#[cfg(not(windows))]
{
assert_eq!("vi", get_default_editor());
}
}

#[test]
fn test_editor_env_works() {
std::env::remove_var("EDITOR");
std::env::remove_var("VISUAL");

std::env::set_var("EDITOR", "vim");

assert_eq!("vim", get_default_editor());

std::env::remove_var("EDITOR");
}

#[test]
fn test_visual_env_works() {
std::env::remove_var("EDITOR");
std::env::remove_var("VISUAL");

std::env::set_var("VISUAL", "nvim");

assert_eq!("nvim", get_default_editor());

std::env::remove_var("VISUAL");
}
}

Expand All @@ -43,6 +88,18 @@ fn create_temp_file(ext: &str) -> Result<tempfile::NamedTempFile, std::io::Error
.tempfile()
}

#[cfg(test)]
mod test_create_temp_file {
use super::create_temp_file;

#[test]
fn it_should_return_a_file() {
let file = create_temp_file(".http").expect("it to not throw an error");

assert!(file.path().exists());
}
}

#[inline]
fn build_editor_cmd(editor_cmd: String) -> (String, Vec<String>) {
shell_words::split(&editor_cmd).map_or_else(
Expand All @@ -54,6 +111,35 @@ fn build_editor_cmd(editor_cmd: String) -> (String, Vec<String>) {
)
}

#[cfg(test)]
mod test_build_editor_cmd {
use crate::terminal::editor::build_editor_cmd;

#[test]
fn it_should_return_command() {
{
let cmd = "nvim";

assert_eq!(
(cmd.to_owned(), Vec::new()),
build_editor_cmd(cmd.to_owned())
);
};

{
let cmd = "nvim --mads --was --here";

assert_eq!(
(
"nvim".to_owned(),
vec!["--mads".to_owned(), "--was".to_owned(), "--here".to_owned()]
),
build_editor_cmd(cmd.to_owned())
);
};
}
}

#[inline]
fn content_type_to_ext(content_type: Option<&str>) -> &'static str {
match content_type {
Expand All @@ -71,6 +157,73 @@ fn content_type_to_ext(content_type: Option<&str>) -> &'static str {
}
}

#[cfg(test)]
mod test_content_type_to_ext {
use crate::terminal::editor::content_type_to_ext;

#[test]
fn application_json() {
assert_eq!(".json", content_type_to_ext(Some("application/json")));
}

#[test]
fn text_css() {
assert_eq!(".css", content_type_to_ext(Some("text/css")));
}

#[test]
fn text_csv() {
assert_eq!(".csv", content_type_to_ext(Some("text/csv")));
}

#[test]
fn text_html() {
assert_eq!(".html", content_type_to_ext(Some("text/html")));
}

#[test]
fn text_javascript() {
assert_eq!(".js", content_type_to_ext(Some("text/javascript")));
}

#[test]
fn application_ld_json() {
assert_eq!(".jsonld", content_type_to_ext(Some("application/ld+json")));
}

#[test]
fn application_x_httpd_php() {
assert_eq!(".php", content_type_to_ext(Some("application/x-httpd-php")));
}

#[test]
fn application_x_sh() {
assert_eq!(".sh", content_type_to_ext(Some("application/x-sh")));
}

#[test]
fn image_svg_xml() {
assert_eq!(".svg", content_type_to_ext(Some("image/svg+xml")));
}

#[test]
fn application_xml() {
assert_eq!(".xml", content_type_to_ext(Some("application/xml")));
}

#[test]
fn text_xml() {
assert_eq!(".xml", content_type_to_ext(Some("text/xml")));
}

#[test]
fn unknown_content_type() {
for i in u8::MIN..u8::MAX {
assert_eq!(".txt", content_type_to_ext(Some(&i.to_string())));
}
}
}

pub fn editor_input(
term: &Term,
content_type: Option<&str>,
Expand Down Expand Up @@ -106,8 +259,6 @@ pub fn editor_input(
continue;
}

let file_contents = std::fs::read_to_string(path)?;

return Ok(Some(file_contents));
return std::fs::read_to_string(path).map(Some);
}
}
22 changes: 22 additions & 0 deletions hitt-cli/src/terminal/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,25 @@ pub fn print_headers(

Ok(())
}

#[cfg(test)]
mod test_print_headers {
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};

use super::print_headers;

#[test]
fn it_should_print_without_errors() {
let term = console::Term::stdout();

let mut headers = HeaderMap::new();

headers.insert(
HeaderName::from_static("mads"),
HeaderValue::from_static("hougesen"),
);

// TODO: validate what is written to stdout
print_headers(&term, &headers).expect("it to not error");
}
}
28 changes: 28 additions & 0 deletions hitt-cli/src/terminal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,35 @@ pub fn write_prompt(term: &Term, prompt: &str) -> Result<(), std::io::Error> {
term.write_line(prompt)
}

#[cfg(test)]
mod test_write_prompt {
use super::write_prompt;

#[test]
fn it_should_not_error() {
let term = console::Term::stdout();

// TODO: actually validate stdout
write_prompt(&term, "What is your prefered http testing tool?")
.expect("it not to raise an error");
}
}

#[inline]
pub fn write_prompt_answer(term: &Term, prompt: &str, answer: &str) -> Result<(), std::io::Error> {
term.write_line(&format!("{prompt} {TEXT_GREEN}[{answer}]{TEXT_RESET}"))
}

#[cfg(test)]
mod test_write_prompt_answer {
use super::write_prompt_answer;

#[test]
fn it_should_not_error() {
let term = console::Term::stdout();

// TODO: actually validate stdout
write_prompt_answer(&term, "What is your prefered http testing tool?", "hitt")
.expect("it not to raise an error");
}
}
Loading

0 comments on commit bea55d1

Please sign in to comment.