Skip to content

Commit

Permalink
Convert license, sort, and update to Rust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Feb 10, 2024
1 parent 7fd2c4d commit a3b229a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 55 deletions.
13 changes: 2 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@ jobs:
rm -f "$HOME"/.cargo/bin/rustfmt
rustup install nightly
rustup component add rustfmt --toolchain nightly
cargo install cargo-license || true
cargo install cargo-rdme || true
cargo install cargo-sort || true
- name: Cargo sort
run: find . -name Cargo.toml -print0 | xargs -0 -n 1 dirname | xargs -n 1 cargo sort --check --grouped
- name: Format
run: ./scripts/format.sh && git diff --exit-code
Expand All @@ -101,12 +96,6 @@ jobs:
- name: Format util READMEs
run: ./scripts/update_util_READMEs.sh && git diff --exit-code

- name: Check lockfiles
run: ./scripts/update_lockfiles.sh && git diff --exit-code

- name: Check licenses
run: ./scripts/check_licenses.sh

- name: Lint
run: ./scripts/lint.sh

Expand Down Expand Up @@ -154,7 +143,9 @@ jobs:
npm install -g prettier
rustup install nightly
cargo install cargo-hack || true
cargo install cargo-license || true
cargo install cargo-msrv || true
cargo install cargo-sort || true
cargo install cargo-supply-chain || true
cargo install cargo-udeps --locked || true
cargo install cargo-unmaintained || true
Expand Down
124 changes: 108 additions & 16 deletions cargo-dylint/tests/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,44 @@ fn hack_feature_powerset_udeps() {
}

#[test]
fn markdown_does_not_use_inline_links() {
for entry in walkdir(false) {
fn license() {
let re = Regex::new(r"^[^:]*\b(Apache|BSD-3-Clause|ISC|MIT|N/A)\b").unwrap();

for entry in walkdir(false).with_file_name("Cargo.toml") {
let entry = entry.unwrap();
let path = entry.path();
if path.extension() != Some(OsStr::new("md"))
|| path.file_name() == Some(OsStr::new("CHANGELOG.md"))
for line in std::str::from_utf8(
&Command::new("cargo")
.args(["license", "--manifest-path", &path.to_string_lossy()])
.assert()
.success()
.get_output()
.stdout,
)
.unwrap()
.lines()
{
// smoelius: Exception for `dirs` dependencies.
if line == "MPL-2.0 (1): option-ext" {
continue;
}
// smoelius: Exception for Cargo dependencies.
if line == "MPL-2.0+ (3): bitmaps, im-rc, sized-chunks" {
continue;
}
// smoelius: Good explanation of the differences between the BSD-3-Clause and MIT
// licenses: https://opensource.stackexchange.com/a/582
assert!(re.is_match(line), "{line:?} does not match");
}
}
}

#[test]
fn markdown_does_not_use_inline_links() {
for entry in walkdir(false).with_extension("md") {
let entry = entry.unwrap();
let path = entry.path();
if path.file_name() == Some(OsStr::new("CHANGELOG.md")) {
continue;
}
let markdown = read_to_string(path).unwrap();
Expand All @@ -163,12 +194,10 @@ fn markdown_does_not_use_inline_links() {
#[test]
fn markdown_reference_links_are_sorted() {
let re = Regex::new(r"^\[[^\]]*\]:").unwrap();
for entry in walkdir(true) {
for entry in walkdir(true).with_extension("md") {
let entry = entry.unwrap();
let path = entry.path();
if path.extension() != Some(OsStr::new("md"))
|| path.file_name() == Some(OsStr::new("CHANGELOG.md"))
{
if path.file_name() == Some(OsStr::new("CHANGELOG.md")) {
continue;
}
let markdown = read_to_string(path).unwrap();
Expand All @@ -193,13 +222,12 @@ fn markdown_reference_links_are_valid_and_used() {
const CODE_BLOCK: &str = "```([^`]|`[^`]|``[^`])*```";
let ref_re = Regex::new(&format!(r#"(?m){CODE}|{CODE_BLOCK}|\[([^\]]*)\]([^:]|$)"#)).unwrap();
let link_re = Regex::new(r"(?m)^\[([^\]]*)\]:").unwrap();
for entry in walkdir(true) {
for entry in walkdir(true).with_extension("md") {
let entry = entry.unwrap();
let path = entry.path();
// smoelius: The ` ["\n```"] ` in `missing_doc_comment_openai`'s readme causes problems, and
// I haven't found a good solution/workaround.
if path.extension() != Some(OsStr::new("md"))
|| path.file_name() == Some(OsStr::new("CHANGELOG.md"))
if path.file_name() == Some(OsStr::new("CHANGELOG.md"))
|| path.ends_with("examples/README.md")
|| path
.components()
Expand Down Expand Up @@ -261,14 +289,10 @@ fn markdown_link_check() {
// smoelius: https://github.com/rust-lang/crates.io/issues/788
let config = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/markdown_link_check.json");

for entry in walkdir(true) {
for entry in walkdir(true).with_extension("md") {
let entry = entry.unwrap();
let path = entry.path();

if path.extension() != Some(OsStr::new("md")) {
continue;
}

let path_buf = Path::new(env!("CARGO_MANIFEST_DIR")).join("..").join(path);

let assert = Command::new("npx")
Expand Down Expand Up @@ -341,6 +365,20 @@ fn prettier_examples_and_template() {
});
}

#[test]
fn sort() {
for entry in walkdir(false).with_file_name("Cargo.toml") {
let entry = entry.unwrap();
let path = entry.path();
let parent = path.parent().unwrap();
Command::new("cargo")
.current_dir(parent)
.args(["sort", "--check", "--grouped"])
.assert()
.success();
}
}

const TARGETS: [&str; 4] = [
"aarch64-apple-darwin",
"x86_64-apple-darwin",
Expand Down Expand Up @@ -398,6 +436,26 @@ fn supply_chain() {
}
}

#[test]
fn update() {
for entry in walkdir(false).with_file_name("Cargo.lock") {
let entry = entry.unwrap();
let path = entry.path();
let manifest_path = path.with_file_name("Cargo.toml");
preserves_cleanliness("update", false, || {
Command::new("cargo")
.args([
"update",
"--workspace",
"--manifest-path",
&manifest_path.to_string_lossy(),
])
.assert()
.success();
});
}
}

#[test]
fn unmaintained() {
Command::new("cargo")
Expand Down Expand Up @@ -430,6 +488,40 @@ fn walkdir(include_examples: bool) -> impl Iterator<Item = walkdir::Result<walkd
})
}

trait IntoIterExt {
fn with_extension(
self,
extension: impl AsRef<OsStr> + 'static,
) -> impl Iterator<Item = walkdir::Result<walkdir::DirEntry>>;
fn with_file_name(
self,
file_name: impl AsRef<OsStr> + 'static,
) -> impl Iterator<Item = walkdir::Result<walkdir::DirEntry>>;
}

impl<T: Iterator<Item = walkdir::Result<walkdir::DirEntry>>> IntoIterExt for T {
fn with_extension(
self,
extension: impl AsRef<OsStr> + 'static,
) -> impl Iterator<Item = walkdir::Result<walkdir::DirEntry>> {
self.filter(move |entry| {
entry.as_ref().map_or(true, |entry| {
entry.path().extension() == Some(extension.as_ref())
})
})
}
fn with_file_name(
self,
file_name: impl AsRef<OsStr> + 'static,
) -> impl Iterator<Item = walkdir::Result<walkdir::DirEntry>> {
self.filter(move |entry| {
entry.as_ref().map_or(true, |entry| {
entry.path().file_name() == Some(file_name.as_ref())
})
})
}
}

static MUTEX: Mutex<()> = Mutex::new(());

fn preserves_cleanliness(test_name: &str, ignore_blank_lines: bool, f: impl FnOnce()) {
Expand Down
28 changes: 0 additions & 28 deletions scripts/check_licenses.sh

This file was deleted.

0 comments on commit a3b229a

Please sign in to comment.