lychee/lychee-bin/tests/usage.rs
Lucius Hu 228e5df6a3
Major refactor of codebase (#208)
- The binary component and library component are separated as two
  packages in the same workspace.
  - `lychee` is the binary component, in `lychee-bin/*`.
  - `lychee-lib` is the library component, in `lychee-lib/*`.
  - Users can now install only the `lychee-lib`, instead of both
    components, that would require fewer dependencies and faster
    compilation.
  - Dependencies for each component are adjusted and updated. E.g.,
    no CLI dependencies for `lychee-lib`.
  - CLI tests are only moved to `lychee`, as it has nothing to do
    with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
  `ErrorKind`.
  - The motivation is to delay the formatting of errors to strings.
    Note that `e.to_string()` is not necessarily cheap (though
    trivial in many cases). The formatting is no delayed until the
    error is needed to be displayed to users. So in some cases, if
    the error is never used, it means that it won't be formatted at
    all.
- Replaced `regex` based matching with one of the following:
  - Simple string equality test in the case of 'false positivie'.
  - URL parsing based test, in the case of extracting repository and
    user name for GitHub links.
  - Either cases would be much more efficient than `regex` based
    matching. First, there's no need to construct a state machine for
    regex. Second, URL is already verified and parsed on its creation,
    and extracting its components is fairly cheap. Also, this removes
    the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
  separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
  `test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
  - Imports from `std`
  - Imports from 3rd-party crates, and `lychee-lib`.
  - Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
  `clippy:pedantic`.

Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-15 01:24:11 +02:00

71 lines
2.3 KiB
Rust

#[cfg(test)]
mod readme {
use std::{
fs::File,
io::{BufReader, Read},
path::Path,
};
use assert_cmd::Command;
use pretty_assertions::assert_eq;
fn main_command() -> Command {
// this gets the "main" binary name (e.g. `lychee`)
Command::cargo_bin(env!("CARGO_PKG_NAME")).expect("Couldn't get cargo package name")
}
fn load_readme_text() -> String {
let readme_path = Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("README.md");
let file = File::open(readme_path).expect("Couldn't open README.md");
let mut buf_reader = BufReader::new(file);
let mut text = String::new();
buf_reader
.read_to_string(&mut text)
.expect("Unable to read README.md file contents");
text
}
/// Test that the USAGE section in `README.md` is up to date with
/// `lychee --help`.
/// Only unix: might not work with windows CRLF line-endings returned from
/// process output (making it fully portable would probably require more
/// involved parsing).
#[test]
#[cfg(unix)]
fn test_readme_usage_up_to_date() {
let mut cmd = main_command();
let result = cmd.env_clear().arg("--help").assert().success();
let help_output = std::str::from_utf8(&result.get_output().stdout)
.expect("Invalid utf8 output for `--help`");
let readme = load_readme_text();
const BACKTICKS_OFFSET: usize = 9; // marker: ```ignore
const NEWLINE_OFFSET: usize = 1;
let usage_start = BACKTICKS_OFFSET
+ NEWLINE_OFFSET
+ readme
.find("```ignore\nUSAGE:\n")
.expect("Couldn't find USAGE section in README.md");
let usage_end = readme[usage_start..]
.find("\n```")
.expect("Couldn't find USAGE section end in README.md");
// include final newline in usage text
let usage_in_readme = &readme[usage_start..usage_start + usage_end + NEWLINE_OFFSET];
let usage_in_help_start = help_output
.find("USAGE:\n")
.expect("Couldn't find USAGE section in `--help` output");
let usage_in_help = &help_output[usage_in_help_start..];
assert_eq!(usage_in_readme, usage_in_help);
}
}