Skip to content
Open
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
313 changes: 83 additions & 230 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ readme = "README.md"
keywords = ["readme", "documentation", "cargo", "subcommand"]
categories = ["development-tools::cargo-plugins"]
license = "MIT OR Apache-2.0"
rust-version = "1.80"

[dependencies]
clap = { version = "4", features = [ "derive" ] }
toml = "0.8"
toml = "0.9"
regex = "1"
serde = { version = "1", features = ["derive"] }
percent-encoding = "2"
lazy_static = "1"

[dev-dependencies]
assert_cli = "0.6"
assert_cmd = "2.0"
predicates = { version = "3.1", default-features = false }

[badges]
github = { repository = "webern/cargo-readme" }
30 changes: 19 additions & 11 deletions src/readme/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@
//! - "```", "```no_run", "```ignore" and "```should_panic" are converted to "```rust"
//! - markdown heading are indentend to be one level lower, so the crate name is at the top level

use lazy_static::lazy_static;
use regex::Regex;
use std::iter::{IntoIterator, Iterator};

lazy_static! {
// Is this code block rust?
static ref RE_CODE_RUST: Regex = Regex::new(r"^(?P<delimiter>`{3,4}|~{3,4})(?:rust|(?:(?:rust,)?(?:no_run|ignore|should_panic)))?$").unwrap();
// Is this code block just text?
static ref RE_CODE_TEXT: Regex = Regex::new(r"^(?P<delimiter>`{3,4}|~{3,4})text$").unwrap();
// Is this code block a language other than rust?
static ref RE_CODE_OTHER: Regex = Regex::new(r"^(?P<delimiter>`{3,4}|~{3,4})\w[\w,\+]*$").unwrap();
}
use std::{
iter::{IntoIterator, Iterator},
sync::LazyLock,
};

// Is this code block rust?
static RE_CODE_RUST: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"^(?P<delimiter>`{3,4}|~{3,4})(?:rust|(?:(?:rust,)?(?:no_run|ignore|should_panic)))?$",
)
.unwrap()
});
// Is this code block just text?
static RE_CODE_TEXT: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(?P<delimiter>`{3,4}|~{3,4})text$").unwrap());

// Is this code block a language other than rust?
static RE_CODE_OTHER: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(?P<delimiter>`{3,4}|~{3,4})\w[\w,\+]*$").unwrap());

/// Process and concatenate the doc lines into a single String
///
Expand Down
41 changes: 19 additions & 22 deletions tests/alternate-input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use assert_cli::Assert;
use assert_cmd::Command;

#[test]
fn alternate_input_empty_docs() {
Expand All @@ -12,13 +12,12 @@ fn alternate_input_empty_docs() {
"src/no_docs.rs",
];

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is("# readme-test\n\nLicense: MIT")
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout("# readme-test\n\nLicense: MIT");
}

#[test]
Expand All @@ -41,13 +40,12 @@ Test crate for cargo-readme
License: MIT
"#;

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is(expected)
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout(expected);
}

#[test]
Expand All @@ -72,11 +70,10 @@ Test crate for cargo-readme
License: MIT
"#;

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is(expected)
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout(expected);
}
15 changes: 7 additions & 8 deletions tests/alternate-template.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use assert_cli::Assert;
use assert_cmd::Command;

const EXPECTED: &str = r#"
# readme-test
Expand Down Expand Up @@ -54,11 +54,10 @@ fn alternate_template() {
"NOTITLE.tpl",
];

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is(EXPECTED)
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout(EXPECTED);
}
29 changes: 14 additions & 15 deletions tests/append-license.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use assert_cli::Assert;
use assert_cmd::Command;
use predicates::prelude::*;

const EXPECTED: &str = r#"
# readme-test
Expand Down Expand Up @@ -54,13 +55,12 @@ fn append_license() {

let expected = format!("{}\n\n{}", EXPECTED.trim(), "License: MIT");

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is(&*expected)
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout(predicate::str::diff(expected).from_utf8());
}

#[test]
Expand All @@ -74,11 +74,10 @@ fn no_append_license() {
"--no-license",
];

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is(EXPECTED)
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout(EXPECTED);
}
15 changes: 7 additions & 8 deletions tests/badges.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use assert_cli::Assert;
use assert_cmd::Command;

const EXPECTED: &str = r#"
[![Build Status](https://ci.appveyor.com/api/projects/status/github/cargo-readme/test?branch=master&svg=true)](https://ci.appveyor.com/project/cargo-readme/test/branch/master)
Expand All @@ -21,11 +21,10 @@ License: MIT
fn badges() {
let args = ["readme", "--project-root", "tests/badges"];

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is(EXPECTED)
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout(EXPECTED);
}
15 changes: 7 additions & 8 deletions tests/default-behavior.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use assert_cli::Assert;
use assert_cmd::Command;

const EXPECTED: &str = r#"
[![Build Status](https://travis-ci.org/livioribeiro/cargo-readme.svg?branch=master)](https://travis-ci.org/livioribeiro/cargo-readme)
Expand Down Expand Up @@ -50,11 +50,10 @@ if condition {
fn default_behavior() {
let args = ["readme", "--project-root", "tests/test-project"];

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is(EXPECTED)
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout(EXPECTED);
}
54 changes: 25 additions & 29 deletions tests/entrypoint-resolution.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use assert_cli::Assert;
use assert_cmd::Command;

#[test]
fn entrypoint_resolution_main() {
Expand All @@ -10,13 +10,12 @@ fn entrypoint_resolution_main() {
"--no-license",
];

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is("main")
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout("main");
}

#[test]
Expand All @@ -29,13 +28,12 @@ fn entrypoint_resolution_lib() {
"--no-license",
];

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is("lib")
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout("lib");
}

#[test]
Expand All @@ -48,13 +46,12 @@ fn entrypoint_resolution_cargo_lib() {
"--no-license",
];

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is("cargo lib")
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout("cargo lib");
}

#[test]
Expand All @@ -67,11 +64,10 @@ fn entrypoint_resolution_cargo_bin() {
"--no-license",
];

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is("cargo bin")
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout("cargo bin");
}
15 changes: 7 additions & 8 deletions tests/multiline-doc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use assert_cli::Assert;
use assert_cmd::Command;

const EXPECTED: &str = r#"
[![Build Status](https://travis-ci.org/livioribeiro/cargo-readme.svg?branch=master)](https://travis-ci.org/livioribeiro/cargo-readme)
Expand Down Expand Up @@ -56,11 +56,10 @@ fn multiline_doc() {
"src/multiline.rs",
];

Assert::main_binary()
.with_args(&args)
.succeeds()
.and()
.stdout()
.is(EXPECTED)
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.success()
.stdout(EXPECTED);
}
15 changes: 7 additions & 8 deletions tests/multiple-bin-fail.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use assert_cli::Assert;
use assert_cmd::Command;

const EXPECTED: &str = "Error: Multiple binaries found, choose one: [src/entry1.rs, src/entry2.rs]";

#[test]
fn multiple_bin_fail() {
let args = ["readme", "--project-root", "tests/multiple-bin-fail"];

Assert::main_binary()
.with_args(&args)
.fails()
.and()
.stderr()
.is(EXPECTED)
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.failure()
.stderr(EXPECTED);
}
15 changes: 7 additions & 8 deletions tests/no-entrypoint-fail.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use assert_cli::Assert;
use assert_cmd::Command;

const EXPECTED: &str = "Error: No entrypoint found";

#[test]
fn no_entrypoint_fail() {
let args = ["readme", "--project-root", "tests/no-entrypoint-fail"];

Assert::main_binary()
.with_args(&args)
.fails()
.and()
.stderr()
.is(EXPECTED)
.unwrap();
Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.assert()
.failure()
.stderr(EXPECTED);
}
Loading