Skip to content

Commit

Permalink
lint: add markdown hyperlink checker
Browse files Browse the repository at this point in the history
This adds a markdown hyperlink check task to the lint test_runner. It
relies on having the [`mlc`](https://crates.io/crates/mlc) binary found
on $PATH, but will fail with `success` if the binary is not found.

`mlc` is also added to the ci/04_install.sh script run by the
containerfile.

Note that broken markdown hyperlinks will be detected in untracked
markdown files found in a dirty working directory (including e.g.
.venv).
  • Loading branch information
willcl-ark committed May 7, 2024
1 parent f5b6f62 commit b6be80c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ci/lint/04_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ SHELLCHECK_VERSION=v0.8.0
curl -sL "https://github.com/koalaman/shellcheck/releases/download/${SHELLCHECK_VERSION}/shellcheck-${SHELLCHECK_VERSION}.linux.x86_64.tar.xz" | \
tar --xz -xf - --directory /tmp/
mv "/tmp/shellcheck-${SHELLCHECK_VERSION}/shellcheck" /usr/bin/

# Markdown Link Checker: https://github.com/becheran/mlc
MLC_VERSION=v0.16.3
MLC_BIN=mlc-x86_64-linux
curl -sL "https://github.com/becheran/mlc/releases/download/${MLC_VERSION}/${MLC_BIN}" -o "/usr/bin/mlc"
chmod +x /usr/bin/mlc
1 change: 1 addition & 0 deletions test/lint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Then you can use:
| [`lint-python-dead-code.py`](lint/lint-python-dead-code.py) | [vulture](https://github.com/jendrikseipp/vulture)
| [`lint-shell.py`](lint/lint-shell.py) | [ShellCheck](https://github.com/koalaman/shellcheck)
| [`lint-spelling.py`](lint/lint-spelling.py) | [codespell](https://github.com/codespell-project/codespell)
| markdown link check | [mlc](https://github.com/becheran/mlc)

In use versions and install instructions are available in the [CI setup](../../ci/lint/04_install.sh).

Expand Down
45 changes: 43 additions & 2 deletions test/lint/test_runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::process::ExitCode;
use std::process::{Command, ExitCode, Stdio};

type LintError = String;
type LintResult = Result<(), LintError>;
Expand Down Expand Up @@ -284,6 +283,47 @@ fn lint_doc() -> LintResult {
}
}

// Find a program on $PATH
fn which(program: &str) -> bool {
if let Ok(path) = env::var("PATH") {
for p in path.split(':') {
let p_str = format!("{}/{}", p, program);
if fs::metadata(p_str).is_ok() {
return true;
}
}
}
false
}

fn lint_markdown() -> LintResult {
let bin_name = "mlc";
if !which(bin_name) {
println!(
"{} was not found in $PATH, skipping markdown lint check.",
bin_name
);
return Ok(());
}
let mut md_ignore_paths = get_subtrees()
.into_iter()
.map(|path| path.to_string())
.collect::<Vec<_>>();
md_ignore_paths.push("./doc/README_doxygen.md".to_string());
let md_ignore_path_str = md_ignore_paths.join(",");

let mut cmd = Command::new(bin_name);
cmd.arg("--offline")
.arg("--ignore-path")
.arg(md_ignore_path_str)
.arg("--root-dir")
.arg(".")
.stdout(Stdio::null())
.stderr(Stdio::inherit());

check_output(&mut cmd).map(|_output| ())
}

fn lint_all() -> LintResult {
let mut good = true;
let lint_dir = get_git_root().join("test/lint");
Expand Down Expand Up @@ -317,6 +357,7 @@ fn main() -> ExitCode {
("no-tabs check", lint_tabs_whitespace),
("build config includes check", lint_includes_build_config),
("-help=1 documentation check", lint_doc),
("markdown hyperlink check", lint_markdown),
("lint-*.py scripts", lint_all),
];

Expand Down

0 comments on commit b6be80c

Please sign in to comment.