From d8e2ca244debc128d4c30c6317cfff3afd02faff Mon Sep 17 00:00:00 2001 From: Preston Evans <32944016+preston-evans98@users.noreply.github.com> Date: Mon, 25 Mar 2024 11:13:15 -0700 Subject: [PATCH] Silence spurious warnings. Add warning on empty block (#3) This commit silences warnings in two cases: - On the first `lang` tag of a code block - When the tag is the tag marking a block for bashtestmd to compile It adds a new warning when a code block is tagged for bashtestmd but does not include any commands. --- src/main.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9214f23..841baaf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -155,7 +155,7 @@ struct CodeBlockTags { } impl CodeBlockTags { - fn parse(code_block: &mdast::Code) -> Self { + fn parse(code_block: &mdast::Code, only_tag: &str) -> Self { let langs: Vec = code_block .lang .as_deref() @@ -171,7 +171,7 @@ impl CodeBlockTags { wait_until: None, }; - for lang in langs { + for (idx, lang) in langs.into_iter().enumerate() { if lang == "bashtestmd:long-running" { tags.long_running = true; } else if lang == "bashtestmd:compare-output" { @@ -185,7 +185,11 @@ impl CodeBlockTags { let wait_until = lang.split_once('=').unwrap().1.to_string(); tags.wait_until = Some(wait_until); } else { - println!("Unknown bashtestmd tag, ignoring: {}", lang); + // Don't warn on the first `lang` tag of if the tag is the one marking blocks for bashtestmd to compile + // This ensures that (i.e. ```rust,test-ci```) should not generate warnings. + if idx != 0 && lang != only_tag { + println!("Unknown bashtestmd tag, ignoring: {}", lang); + } } } @@ -210,7 +214,8 @@ fn convert_code_blocks_into_commands( { continue; } - let tags = CodeBlockTags::parse(&code_block); + let mut block_contains_command = false; + let tags = CodeBlockTags::parse(&code_block, only_tag); let mut cmd: Option = None; let mut output = String::new(); @@ -221,11 +226,19 @@ fn convert_code_blocks_into_commands( commands.push(Command::new(&cmd)); } cmd = Some(cmd_string.to_string()); + block_contains_command = true; } else { output.push_str(line); output.push('\n'); } } + if !block_contains_command { + println!( + "Warning: could not find command in block`:\n```\n{}\n```", + &code_block.value + ); + println!("^^^^^ remove the tag {only_tag} from the block or add a command beginning with `{PROMPT}` to fix this warning"); + } if let Some(cmd) = cmd { let mut cmd = Command::new(&cmd); cmd.long_running = tags.long_running;