Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions crates/cargo-heather/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,47 @@ impl CommentStyle {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn inner_attribute_is_not_cargo_script() {
// `#![...]` is a Rust inner attribute, not a shebang.
assert!(!is_cargo_script("#![allow(unused)]\n---\n"));
}

#[test]
fn shebang_without_frontmatter_is_not_cargo_script() {
assert!(!is_cargo_script("#!/usr/bin/env cargo\nfn main() {}\n"));
}

#[test]
fn valid_cargo_script() {
assert!(is_cargo_script("#!/usr/bin/env cargo\n---\n"));
}

#[test]
fn doc_comment_is_not_header_comment() {
let style = CommentStyle::DoubleSlash;
// `///` is a doc comment, not a header comment.
assert!(!style.is_header_comment_line("///"));
assert!(!style.is_header_comment_line("/// doc"));
}

#[test]
fn inner_doc_comment_is_not_header_comment() {
let style = CommentStyle::DoubleSlash;
// `//!` is an inner doc comment, not a header comment.
assert!(!style.is_header_comment_line("//!"));
assert!(!style.is_header_comment_line("//! module doc"));
}

#[test]
fn regular_comment_is_header_comment() {
let style = CommentStyle::DoubleSlash;
assert!(style.is_header_comment_line("// Copyright"));
assert!(style.is_header_comment_line("//"));
}
}
23 changes: 23 additions & 0 deletions crates/cargo-heather/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,26 @@ impl std::error::Error for HeatherError {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::error::Error;

#[test]
fn file_read_error_exposes_source() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "gone");
let err = HeatherError::FileRead {
path: PathBuf::from("/tmp/missing"),
source: io_err,
};
let source = err.source().expect("FileRead should have a source");
assert!(source.to_string().contains("gone"));
}

#[test]
fn non_io_variants_have_no_source() {
let err = HeatherError::ConfigInvalid("bad".into());
assert!(err.source().is_none());
}
}
21 changes: 21 additions & 0 deletions crates/cargo-heather/src/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,24 @@ pub fn supported_licenses() -> Vec<&'static str> {
ids.sort_unstable();
ids
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn supported_licenses_contains_known_ids() {
let ids = supported_licenses();
assert!(!ids.is_empty());
assert!(ids.contains(&"MIT"));
assert!(ids.contains(&"Apache-2.0"));
}

#[test]
fn supported_licenses_is_sorted() {
let ids = supported_licenses();
let mut sorted = ids.clone();
sorted.sort_unstable();
assert_eq!(ids, sorted);
}
}
Loading