- help / version flag report correct application name when generated
with clap_derive and an Enum.
- add clap_derive unit tests for application name:
  file: clap_derive/tests/app_name.rs
  tests: app_name_in_[short|long]_[help|version]_from_[struct|enum]()
This commit is contained in:
Logan SQUIREL 2021-02-22 14:54:15 +01:00
parent 90a74044ee
commit cd8660fbd0
No known key found for this signature in database
GPG key ID: E8BDBE1DA96E9B2B
3 changed files with 116 additions and 10 deletions

View file

@ -68,7 +68,7 @@ fn gen_for_struct(
}
fn gen_for_enum(name: &Ident, attrs: &[Attribute], e: &DataEnum) -> TokenStream {
let into_app = into_app::gen_for_enum(name);
let into_app = into_app::gen_for_enum(name, attrs);
let from_arg_matches = from_arg_matches::gen_for_enum(name);
let subcommand = subcommand::gen_for_enum(name, attrs, e);
let arg_enum = arg_enum::gen_for_enum(name, attrs, e);

View file

@ -42,7 +42,7 @@ pub fn derive_into_app(input: &DeriveInput) -> TokenStream {
fields: Fields::Unit,
..
}) => gen_for_struct(ident, &Punctuated::<Field, Comma>::new(), &input.attrs).0,
Data::Enum(_) => gen_for_enum(ident),
Data::Enum(_) => gen_for_enum(ident, &input.attrs),
_ => abort_call_site!("`#[derive(IntoApp)]` only supports non-tuple structs and enums"),
}
}
@ -77,9 +77,18 @@ pub fn gen_for_struct(
(tokens, attrs)
}
pub fn gen_for_enum(name: &Ident) -> TokenStream {
pub fn gen_for_enum(enum_name: &Ident, attrs: &[Attribute]) -> TokenStream {
let app_name = env::var("CARGO_PKG_NAME").ok().unwrap_or_default();
let attrs = Attrs::from_struct(
Span::call_site(),
attrs,
Name::Assigned(quote!(#app_name)),
Sp::call_site(DEFAULT_CASING),
Sp::call_site(DEFAULT_ENV_CASING),
);
let name = attrs.cased_name();
quote! {
#[allow(dead_code, unreachable_code, unused_variables)]
#[allow(
@ -93,24 +102,24 @@ pub fn gen_for_enum(name: &Ident) -> TokenStream {
clippy::cargo
)]
#[deny(clippy::correctness)]
impl clap::IntoApp for #name {
impl clap::IntoApp for #enum_name {
fn into_app<'b>() -> clap::App<'b> {
let app = clap::App::new(#app_name)
let app = clap::App::new(#name)
.setting(clap::AppSettings::SubcommandRequiredElseHelp);
<#name as clap::IntoApp>::augment_clap(app)
<#enum_name as clap::IntoApp>::augment_clap(app)
}
fn augment_clap<'b>(app: clap::App<'b>) -> clap::App<'b> {
<#name as clap::Subcommand>::augment_subcommands(app)
<#enum_name as clap::Subcommand>::augment_subcommands(app)
}
fn into_app_for_update<'b>() -> clap::App<'b> {
let app = clap::App::new(#app_name);
<#name as clap::IntoApp>::augment_clap_for_update(app)
let app = clap::App::new(#name);
<#enum_name as clap::IntoApp>::augment_clap_for_update(app)
}
fn augment_clap_for_update<'b>(app: clap::App<'b>) -> clap::App<'b> {
<#name as clap::Subcommand>::augment_subcommands_for_update(app)
<#enum_name as clap::Subcommand>::augment_subcommands_for_update(app)
}
}
}

View file

@ -0,0 +1,97 @@
use clap::Clap;
use clap::IntoApp;
#[test]
fn app_name_in_short_help_from_struct() {
#[derive(Clap)]
#[clap(name = "my-app")]
struct MyApp {}
let mut help = Vec::new();
MyApp::into_app().write_help(&mut help).unwrap();
let help = String::from_utf8(help).unwrap();
assert!(help.contains("my-app"));
}
#[test]
fn app_name_in_long_help_from_struct() {
#[derive(Clap)]
#[clap(name = "my-app")]
struct MyApp {}
let mut help = Vec::new();
MyApp::into_app().write_long_help(&mut help).unwrap();
let help = String::from_utf8(help).unwrap();
assert!(help.contains("my-app"));
}
#[test]
fn app_name_in_short_help_from_enum() {
#[derive(Clap)]
#[clap(name = "my-app")]
enum MyApp {}
let mut help = Vec::new();
MyApp::into_app().write_help(&mut help).unwrap();
let help = String::from_utf8(help).unwrap();
assert!(help.contains("my-app"));
}
#[test]
fn app_name_in_long_help_from_enum() {
#[derive(Clap)]
#[clap(name = "my-app")]
enum MyApp {}
let mut help = Vec::new();
MyApp::into_app().write_long_help(&mut help).unwrap();
let help = String::from_utf8(help).unwrap();
assert!(help.contains("my-app"));
}
#[test]
fn app_name_in_short_version_from_struct() {
#[derive(Clap)]
#[clap(name = "my-app")]
struct MyApp {}
let version = MyApp::into_app().render_version();
assert!(version.contains("my-app"));
}
#[test]
fn app_name_in_long_version_from_struct() {
#[derive(Clap)]
#[clap(name = "my-app")]
struct MyApp {}
let version = MyApp::into_app().render_long_version();
assert!(version.contains("my-app"));
}
#[test]
fn app_name_in_short_version_from_enum() {
#[derive(Clap)]
#[clap(name = "my-app")]
enum MyApp {}
let version = MyApp::into_app().render_version();
assert!(version.contains("my-app"));
}
#[test]
fn app_name_in_long_version_from_enum() {
#[derive(Clap)]
#[clap(name = "my-app")]
enum MyApp {}
let version = MyApp::into_app().render_long_version();
assert!(version.contains("my-app"));
}