Skip to content

Commit

Permalink
Allow debug logs to work with clap tests themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Apr 22, 2020
1 parent 3de8af4 commit 1446b98
Show file tree
Hide file tree
Showing 24 changed files with 409 additions and 520 deletions.
8 changes: 4 additions & 4 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ Please use the following template to assist with creating an issue and to ensure

### Debug output

Compile clap with cargo features `"debug"` such as:
Run your binary with `RUST_LOG=debug`:

```toml
[dependencies]
clap = { version = "2", features = ["debug"] }
```bash
→ RUST_LOG=debug cargo run -- options...
```

The output may be very long, so feel free to link to a gist or attach a text file

<details>
Expand Down
21 changes: 21 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,24 @@ I think *this* should happen instead.
### Additional context

Add any other context about the problem here.

### Debug output

Run your binary with `RUST_LOG=debug`:

```bash
→ RUST_LOG=debug cargo run -- options...
```

The output may be very long, so feel free to link to a gist or attach a text file

<details>
<summary> Debug Output </summary>
<pre>
<code>

Paste Debug Output Here

</code>
</pre>
</details>
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ lazy_static = "1"
version-sync = "0.8"
criterion = { git = "git://github.com/pksunkara/criterion.rs", version = "0.3" }

[target."cfg(debug_assertions)".dependencies]
log = "0.4"
env_logger = "0.7"

[features]
default = ["suggestions", "color", "vec_map", "derive", "std", "cargo"]
std = [] # support for no_std in a backwards-compatible way
Expand All @@ -88,7 +92,6 @@ derive = ["clap_derive", "lazy_static"]
yaml = ["yaml-rust"]
cargo = [] # Disable if you're not using Cargo, enables Cargo-env-var-dependent macros
unstable = ["clap_derive/unstable"] # for building with unstable clap features (doesn't require nightly Rust) (currently none)
debug = ["clap_derive/debug"] # Enables debug messages
doc = ["yaml"] # All the features which add to documentation

[profile.test]
Expand Down
1 change: 0 additions & 1 deletion clap_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ version-sync = "0.8"
[features]
default = []
unstable = []
debug = []
doc = []

[package.metadata.docs.rs]
Expand Down
5 changes: 4 additions & 1 deletion clap_generate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ clap = { path = "../", version = "3.0.0-beta.1" }
pretty_assertions = "0.6"
version-sync = "0.8"

[target."cfg(debug_assertions)".dependencies]
log = "0.4"
env_logger = "0.7"

[features]
default = []
unstable = ["clap/unstable"]
debug = ["clap/debug"]
doc = []

[package.metadata.docs.rs]
Expand Down
12 changes: 6 additions & 6 deletions clap_generate/src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ pub trait Generator {
/// Subcommand `rustup toolchain install` would be converted to
/// `("install", "rustup toolchain install")`.
fn subcommands(p: &App) -> Vec<(String, String)> {
debugln!("subcommands: name={}", p.get_name());
debugln!("subcommands: Has subcommands...{:?}", p.has_subcommands());
debug!("subcommands: name={}", p.get_name());
debug!("subcommands: Has subcommands...{:?}", p.has_subcommands());

let mut subcmds = vec![];

Expand All @@ -104,7 +104,7 @@ pub trait Generator {
for sc in p.get_subcommands() {
let sc_bin_name = sc.get_bin_name().unwrap();

debugln!(
debug!(
"subcommands:iter: name={}, bin_name={}",
sc.get_name(),
sc_bin_name
Expand All @@ -119,7 +119,7 @@ pub trait Generator {
/// Gets all the short options and flags of a [`clap::App`](../clap/struct.App.html).
/// Includes `h` and `V` depending on the [`clap::AppSettings`](../clap/enum.AppSettings.html).
fn shorts<'b>(p: &'b App<'b>) -> Vec<char> {
debugln!("shorts: name={}", p.get_name());
debug!("shorts: name={}", p.get_name());

let mut shorts: Vec<char> = p
.get_arguments()
Expand Down Expand Up @@ -147,7 +147,7 @@ pub trait Generator {
/// Gets all the long options and flags of a [`clap::App`](../clap/struct.App.html).
/// Includes `help` and `version` depending on the [`clap::AppSettings`](../clap/enum.AppSettings.html).
fn longs<'b>(p: &'b App<'b>) -> Vec<String> {
debugln!("longs: name={}", p.get_name());
debug!("longs: name={}", p.get_name());

let mut longs: Vec<String> = p
.get_arguments()
Expand Down Expand Up @@ -177,7 +177,7 @@ pub trait Generator {
/// Gets all the flags of a [`clap::App`](../clap/struct.App.html).
/// Includes `help` and `version` depending on the [`clap::AppSettings`](../clap/enum.AppSettings.html).
fn flags<'b>(p: &'b App<'b>) -> Vec<Arg> {
debugln!("flags: name={}", p.get_name());
debug!("flags: name={}", p.get_name());

let mut flags: Vec<_> = flags!(p).cloned().collect();

Expand Down
10 changes: 5 additions & 5 deletions clap_generate/src/generators/shells/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ complete -F _{name} -o bashdefault -o default {name}
}

fn all_subcommands(app: &App) -> String {
debugln!("Bash::all_subcommands;");
debug!("all_subcommands");

let mut subcmds = String::new();
let mut scs = Bash::all_subcommands(app)
Expand All @@ -101,7 +101,7 @@ fn all_subcommands(app: &App) -> String {
}

fn subcommand_details(app: &App) -> String {
debugln!("Bash::subcommand_details;");
debug!("subcommand_details");

let mut subcmd_dets = String::new();
let mut scs = Bash::all_subcommands(app)
Expand Down Expand Up @@ -141,7 +141,7 @@ fn subcommand_details(app: &App) -> String {
}

fn option_details_for_path(app: &App, path: &str) -> String {
debugln!("Bash::option_details_for_path: path={}", path);
debug!("option_details_for_path: path={}", path);

let p = Bash::find_subcommand_with_path(app, path.split("__").skip(1).collect());
let mut opts = String::new();
Expand Down Expand Up @@ -178,7 +178,7 @@ fn option_details_for_path(app: &App, path: &str) -> String {
}

fn vals_for(o: &Arg) -> String {
debugln!("Bash::vals_for: o={}", o.get_name());
debug!("vals_for: o={}", o.get_name());

if let Some(ref vals) = o.get_possible_values() {
format!("$(compgen -W \"{}\" -- ${{cur}})", vals.join(" "))
Expand All @@ -188,7 +188,7 @@ fn vals_for(o: &Arg) -> String {
}

fn all_options_for_path(app: &App, path: &str) -> String {
debugln!("Bash::all_options_for_path: path={}", path);
debug!("all_options_for_path: path={}", path);

let p = Bash::find_subcommand_with_path(app, path.split("__").skip(1).collect());
let scs: Vec<_> = Bash::subcommands(p).iter().map(|x| x.0.clone()).collect();
Expand Down
2 changes: 1 addition & 1 deletion clap_generate/src/generators/shells/elvish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn generate_inner<'b>(
previous_command_name: &str,
names: &mut Vec<&'b str>,
) -> String {
debugln!("Elvish::generate_inner;");
debug!("generate_inner");

let command_name = if previous_command_name.is_empty() {
p.get_bin_name().expect(INTERNAL_ERROR_MSG).to_string()
Expand Down
4 changes: 2 additions & 2 deletions clap_generate/src/generators/shells/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn escape_string(string: &str) -> String {
}

fn gen_fish_inner(root_command: &str, app: &App, buffer: &mut String) {
debugln!("Fish::gen_fish_inner;");
debug!("gen_fish_inner");
// example :
//
// complete
Expand All @@ -52,7 +52,7 @@ fn gen_fish_inner(root_command: &str, app: &App, buffer: &mut String) {
basic_template.push_str(format!("\"__fish_seen_subcommand_from {}\"", bin_name).as_str());
}

debugln!("Fish::gen_fish_inner; bin_name={}", bin_name);
debug!("gen_fish_inner: bin_name={}", bin_name);

for option in opts!(app) {
let mut template = basic_template.clone();
Expand Down
2 changes: 1 addition & 1 deletion clap_generate/src/generators/shells/powershell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn generate_inner<'b>(
previous_command_name: &str,
names: &mut Vec<&'b str>,
) -> String {
debugln!("PowerShell::generate_inner;");
debug!("generate_inner");

let command_name = if previous_command_name.is_empty() {
p.get_bin_name().expect(INTERNAL_ERROR_MSG).to_string()
Expand Down
41 changes: 20 additions & 21 deletions clap_generate/src/generators/shells/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ _{name} \"$@\"",
// _describe -t commands 'rustup commands' commands "$@"
//
fn subcommand_details(p: &App) -> String {
debugln!("ZshGen::subcommand_details;");
debug!("subcommand_details");

let name = p.get_bin_name().unwrap();

Expand All @@ -106,7 +106,7 @@ _{bin_name_underscore}_commands() {{
all_subcommands.dedup();

for &(_, ref bin_name) in &all_subcommands {
debugln!("Zsh::subcommand_details:iter: bin_name={}", bin_name);
debug!("subcommand_details:iter: bin_name={}", bin_name);

ret.push(format!(
"\
Expand Down Expand Up @@ -138,12 +138,12 @@ _{bin_name_underscore}_commands() {{
// 'show:Show the active and installed toolchains'
// 'update:Update Rust toolchains'
fn subcommands_of(p: &App) -> String {
debugln!("Zsh::subcommands_of;");
debug!("subcommands_of");

let mut ret = vec![];

fn add_sc(sc: &App, n: &str, ret: &mut Vec<String>) {
debugln!("Zsh::add_sc;");
debug!("add_sc");

let s = format!(
"\"{name}:{help}\" \\",
Expand All @@ -162,7 +162,7 @@ fn subcommands_of(p: &App) -> String {

// The subcommands
for sc in p.get_subcommands() {
debugln!("Zsh::subcommands_of:iter: subcommand={}", sc.get_name());
debug!("subcommands_of:iter: subcommand={}", sc.get_name());

add_sc(sc, &sc.get_name(), &mut ret);

Expand Down Expand Up @@ -204,9 +204,8 @@ fn subcommands_of(p: &App) -> String {
// [repeat] = From the same recursive calls, but for all subcommands
// [subcommand_args] = The same as zsh::get_args_of
fn get_subcommands_of(p: &App) -> String {
debugln!("Zsh::get_subcommands_of;");
debugln!(
"Zsh::get_subcommands_of: Has subcommands...{:?}",
debug!(
"get_subcommands_of: Has subcommands...{:?}",
p.has_subcommands()
);

Expand Down Expand Up @@ -254,7 +253,7 @@ esac",
}

fn parser_of<'b>(p: &'b App<'b>, mut sc: &str) -> &'b App<'b> {
debugln!("Zsh::parser_of: sc={}", sc);
debug!("parser_of: sc={}", sc);

if sc == p.get_bin_name().unwrap_or(&String::new()) {
return p;
Expand Down Expand Up @@ -285,7 +284,7 @@ fn parser_of<'b>(p: &'b App<'b>, mut sc: &str) -> &'b App<'b> {
// -s: Allow stacking of short args (i.e. -a -b -c => -abc)
// -S: Do not complete anything after '--' and treat those as argument values
fn get_args_of(p: &App) -> String {
debugln!("Zsh::get_args_of;");
debug!("get_args_of");

let mut ret = vec![String::from("_arguments \"${_arguments_options[@]}\" \\")];
let opts = write_opts_of(p);
Expand Down Expand Up @@ -351,12 +350,12 @@ fn escape_value(string: &str) -> String {
}

fn write_opts_of(p: &App) -> String {
debugln!("Zsh::write_opts_of;");
debug!("write_opts_of");

let mut ret = vec![];

for o in opts!(p) {
debugln!("Zsh::write_opts_of:iter: o={}", o.get_name());
debug!("write_opts_of:iter: o={}", o.get_name());

let help = o.get_help().map_or(String::new(), escape_help);
let conflicts = arg_conflicts(p, o);
Expand Down Expand Up @@ -394,7 +393,7 @@ fn write_opts_of(p: &App) -> String {
help = help
);

debugln!("write_opts_of:iter: Wrote...{}", &*s);
debug!("write_opts_of:iter: Wrote...{}", &*s);
ret.push(s);
}

Expand All @@ -408,7 +407,7 @@ fn write_opts_of(p: &App) -> String {
help = help
);

debugln!("write_opts_of:iter: Wrote...{}", &*l);
debug!("write_opts_of:iter: Wrote...{}", &*l);
ret.push(l);
}
}
Expand Down Expand Up @@ -438,12 +437,12 @@ fn arg_conflicts(app: &App, arg: &Arg) -> String {
}

fn write_flags_of(p: &App) -> String {
debugln!("Zsh::write_flags_of;");
debug!("write_flags_of;");

let mut ret = vec![];

for f in Zsh::flags(p) {
debugln!("Zsh::write_flags_of:iter: f={}", f.get_name());
debug!("write_flags_of:iter: f={}", f.get_name());

let help = f.get_help().map_or(String::new(), escape_help);
let conflicts = arg_conflicts(p, &f);
Expand All @@ -463,7 +462,7 @@ fn write_flags_of(p: &App) -> String {
help = help
);

debugln!("Zsh::write_flags_of:iter: Wrote...{}", &*s);
debug!("write_flags_of:iter: Wrote...{}", &*s);

ret.push(s);
}
Expand All @@ -477,7 +476,7 @@ fn write_flags_of(p: &App) -> String {
help = help
);

debugln!("Zsh::write_flags_of:iter: Wrote...{}", &*l);
debug!("write_flags_of:iter: Wrote...{}", &*l);

ret.push(l);
}
Expand All @@ -487,12 +486,12 @@ fn write_flags_of(p: &App) -> String {
}

fn write_positionals_of(p: &App) -> String {
debugln!("Zsh::write_positionals_of;");
debug!("write_positionals_of;");

let mut ret = vec![];

for arg in positionals!(p) {
debugln!("Zsh::write_positionals_of:iter: arg={}", arg.get_name());
debug!("write_positionals_of:iter: arg={}", arg.get_name());

let optional = if !arg.is_set(ArgSettings::Required) {
":"
Expand Down Expand Up @@ -524,7 +523,7 @@ fn write_positionals_of(p: &App) -> String {
})
);

debugln!("Zsh::write_positionals_of:iter: Wrote...{}", a);
debug!("write_positionals_of:iter: Wrote...{}", a);

ret.push(a);
}
Expand Down
3 changes: 3 additions & 0 deletions clap_generate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ where
G: Generator,
S: Into<String>,
{
#[cfg(debug_assertions)]
let _ = env_logger::builder().format_timestamp(None).try_init();

app.set_bin_name(bin_name);

if !app.is_set(clap::AppSettings::Built) {
Expand Down
Loading

0 comments on commit 1446b98

Please sign in to comment.