Skip to content

Commit

Permalink
Bug fixes, new flags and better logs (#19)
Browse files Browse the repository at this point in the history
* Add test showing the issue
* Fix an issue preventing from using the schema defined in the config
* Add debugging to spot issue if the schema cannot be parsed
* Deps update
* Add ability to override schema
  • Loading branch information
chevdor authored Oct 27, 2023
1 parent c1c4e5d commit 4fedec4
Show file tree
Hide file tree
Showing 22 changed files with 290 additions and 159 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ target
.env*
!.env-example
executables
.prdoc-sdk.toml
75 changes: 34 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace.package]
version = "0.0.6"
version = "0.0.7"
authors = ["chevdor <[email protected]>", "Wilfried Kopp <[email protected]>"]
edition = "2021"
homepage = "https://github.com/paritytech/prdoc"
Expand Down
23 changes: 19 additions & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use prdoclib::{
},
common::PRNumber,
config::Config,
schema::Schema,
};
use std::{collections::HashSet, env, path::PathBuf};

Expand Down Expand Up @@ -66,7 +67,15 @@ fn main() -> color_eyre::Result<()> {
let results: HashSet<CheckResult> = prdoc_dir
.iter()
.flat_map(|d| {
CheckCmd::run(d, cmd_opts.file.clone(), cmd_opts.number.clone(), cmd_opts.list.clone()).unwrap()
CheckCmd::run(
&config,
cmd_opts.schema.clone(),
d,
cmd_opts.file.clone(),
cmd_opts.number.clone(),
cmd_opts.list.clone(),
)
.unwrap()
})
.collect();

Expand All @@ -89,12 +98,17 @@ fn main() -> color_eyre::Result<()> {
}

Some(SubCommand::Scan(cmd_opts)) => {
let schema_path = config.schema_path();
let schema = Schema::new(schema_path);

log::debug!("cmd_opts: {cmd_opts:#?}");
let files = ScanCmd::run(prdoc_dir, cmd_opts.all);
let files = ScanCmd::run(schema.clone(), prdoc_dir, cmd_opts.all);
let load_cmd = LoadCmd::new(schema);

let mut res: Vec<(Option<PRNumber>, PathBuf)> = files
.iter()
.map(|f| {
let prdoc = LoadCmd::load_file(f);
let prdoc = load_cmd.load_file(f);
let n = match prdoc {
Ok(p) => Some(p.doc_filename.number),
Err(_) => None,
Expand Down Expand Up @@ -128,7 +142,8 @@ fn main() -> color_eyre::Result<()> {
let result = prdoc_dir
.iter()
.map(|dir| {
LoadCmd::run(dir, cmd_opts.file.clone(), cmd_opts.number.clone(), cmd_opts.list.clone()).unwrap()
LoadCmd::run(&config, dir, cmd_opts.file.clone(), cmd_opts.number.clone(), cmd_opts.list.clone())
.unwrap()
})
.fold((true, HashSet::new()), |(acc_status, acc_wrapper), (status, wrapper)| {
let mut new_wrapper = acc_wrapper;
Expand Down
4 changes: 4 additions & 0 deletions cli/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ pub struct CheckOpts {
/// Get the list of PR numbers from a file
#[clap(short, long, conflicts_with_all = ["file", "number"])]
pub list: Option<PathBuf>,

/// Schema to be used. Passing this flag/ENV overrides the value from the config.
#[clap(short, long, env = "PRDOC_SCHEMA")]
pub schema: Option<PathBuf>,
}

/// Scan a directory for prdoc files based on their name
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod cli_tests {
use crate::common::*;

#[test]
fn it_check_passes_without_args() {
fn it_check_passes() {
let mut cmd = prdoc_bin();

let assert = cmd.arg("check").args(["-d", "../tests/data/all"]).assert();
Expand Down
1 change: 1 addition & 0 deletions cli/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ use prdoclib::config;
pub(crate) fn prdoc_bin() -> Command {
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).expect("Failed getting test bin");
cmd.env(config::env::PRDOC_FOLDERS, "../tests/data/all");
cmd.env(config::env::PRDOC_CONFIG, "../prdoc.toml");
cmd
}
28 changes: 19 additions & 9 deletions cli/tests/generate.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
mod common;

#[cfg(test)]
mod cli_tests {
#[cfg(test)]
mod generate {
use crate::common::prdoc_bin;
use std::fs;

use assert_cmd::Command;

#[test]
fn it_generate_fails_without_number() {
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).expect("Failed getting test bin");
let mut cmd = prdoc_bin();
let assert = cmd.arg("generate").assert();
assert.failure().code(2);
}

#[test]
fn it_generate_with_number() {
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).expect("Failed getting test bin");
let mut cmd = prdoc_bin();
let assert = cmd.args(["generate", "--dry-run", "42"]).assert();
assert.success().code(exitcode::OK);
}

#[test]
fn it_does_not_overwrite() {
// Ensure we start without a file so the first generate always work
let _ = fs::remove_file("/tmp/prdoc/pr_9999.prdoc");
let _ = fs::remove_file("/tmp/pr_9999.prdoc");

let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).expect("Failed getting test bin");
let assert = cmd.args(["generate", "--output-dir", "/tmp/prdoc", "9999"]).assert();
let mut cmd = prdoc_bin();
let assert = cmd.args(["generate", "--output-dir", "/tmp", "9999"]).assert();
assert.success().code(exitcode::OK);

let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).expect("Failed getting test bin");
let assert = cmd.args(["generate", "--output-dir", "/tmp/prdoc", "9999"]).assert();
let mut cmd = prdoc_bin();
let assert = cmd.args(["generate", "--output-dir", "/tmp", "9999"]).assert();
assert.failure().code(exitcode::IOERR);
}

#[test]
fn it_should_not_fail() {
let _ = fs::remove_file("/tmp/prdoc/pr_9998.prdoc");

let mut cmd = prdoc_bin();
let assert = cmd.args(["generate", "-d", "/tmp/prdoc/", "9998"]).assert();
assert.success().code(exitcode::OK);
}
}
}
Loading

0 comments on commit 4fedec4

Please sign in to comment.