Skip to content

Commit

Permalink
Slightly improve error reporting
Browse files Browse the repository at this point in the history
Reviewed By: AndreasBackx

Differential Revision: D64753922

fbshipit-source-id: da331d0b06fdae1943ee4f219fd06c90a443c655
  • Loading branch information
abesto authored and facebook-github-bot committed Oct 22, 2024
1 parent 8c344bc commit 6fc5c61
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ shell-escape = "0.1"
strip-ansi-escapes = "0.1"
subprocess = "0.2.7"
tempfile = "3.8"
thiserror = "1.0"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
unicode_categories = "0.1"
Expand Down
6 changes: 2 additions & 4 deletions selftest/complex/missing-language/missing-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

```scrut
$ $SCRUT_BIN test --match-markdown "*.mdtest" "$TESTDIR"/missing-language.mdtest 2>&1
* parse test from "*missing-language.mdtest" with markdown parser (glob)
* Failed to parse test from "*missing-language.mdtest" with markdown parser (glob)
Caused by:
Code block starting at line 2 is missing language specifier. Use ```scrut to make this block a Scrut test, or any other language to make Scrut skip this block.
* (glob?)
[1]
```

# Error on EMPTY code block with no language specified

```scrut
$ $SCRUT_BIN test --match-markdown "*.mdtest" "$TESTDIR"/missing-language-empty-block.mdtest 2>&1
* parse test from "*missing-language-empty-block.mdtest" with markdown parser (glob)
* Failed to parse test from "*missing-language-empty-block.mdtest" with markdown parser (glob)
Caused by:
Code block starting at line 2 is missing language specifier. Use ```scrut to make this block a Scrut test, or any other language to make Scrut skip this block.
* (glob?)
[1]
```
10 changes: 2 additions & 8 deletions src/bin/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

use std::collections::BTreeMap;
use std::fmt::Display;
use std::io::stdout;
use std::io::IsTerminal;
use std::path::Path;
Expand Down Expand Up @@ -47,15 +46,10 @@ use crate::utils::make_executor;
use crate::utils::FileParser;
use crate::utils::TestEnvironment;

#[derive(Debug, Clone)]
#[derive(Debug, thiserror::Error)]
#[error("validation failed")]
pub struct ValidationFailedError;

impl Display for ValidationFailedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "validation failed")
}
}

/// Run tests from files or directories
#[derive(Debug, ClapParser)]
pub struct Args {
Expand Down
23 changes: 23 additions & 0 deletions src/bin/fb_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

extern crate scrut;

mod commands;
mod fb_main_common;
mod utils;

use anyhow::Result;
use cli::ExitCode;
use fb_main_common::main_impl;
use fb_main_common::Args;
use fbinit::FacebookInit;

#[cli::main("scrut", error_logging)]
pub fn main(fb: FacebookInit, args: Args) -> Result<ExitCode> {
main_impl(fb, args)
}
58 changes: 58 additions & 0 deletions src/bin/fb_main_common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

use anyhow::anyhow;
use anyhow::Result;
use clap::Parser;
use cli::ExitCode;
use fbinit::FacebookInit;
use scrut::parsers::markdown::MarkdownParserError;
use tracing::error;

use crate::commands::root::Commands;
use crate::commands::root::GlobalParameters;
use crate::commands::test::ValidationFailedError;

#[derive(Debug, Parser)]
#[clap(
about = "A testing toolkit to scrutinize CLI applications",
author = "clifoundation"
)]
pub struct Args {
#[clap(subcommand)]
commands: Commands,

#[clap(flatten)]
global: GlobalParameters,
}

/// Implemented here because it's only used in `fb-main*.rs`, and so
/// there's little point in exposing it to the OSS version.
pub fn is_user_error(err: &anyhow::Error) -> bool {
if let Some(MarkdownParserError::MissingLanguageSpecifier { .. }) =
err.downcast_ref::<MarkdownParserError>()
{
return true;
}
false
}

pub fn main_impl(_fb: FacebookInit, args: Args) -> Result<ExitCode> {
if let Err(err) = args.commands.run() {
if err.downcast_ref::<ValidationFailedError>().is_some() {
return Ok(ExitCode::from(50));
}
if is_user_error(&err) {
error!("{:?}", err);
Ok(ExitCode::from(1))
} else {
Err(anyhow!(err))
}
} else {
Ok(ExitCode::SUCCESS)
}
}
23 changes: 23 additions & 0 deletions src/bin/fb_main_nodeps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

extern crate scrut;

mod commands;
mod fb_main_common;
mod utils;

use anyhow::Result;
use cli::ExitCode;
use fb_main_common::main_impl;
use fb_main_common::Args;
use fbinit::FacebookInit;

#[cli::main("scrut", usage_logging = false)]
pub fn main(fb: FacebookInit, args: Args) -> Result<ExitCode> {
main_impl(fb, args)
}
2 changes: 1 addition & 1 deletion src/bin/utils/file_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<'a> FileParser<'a> {
let (parser_type, parser) = self.parser(&test_file_path, cram_compat)?;
let (config, testcases) = parser.parse(&test_file_content).with_context(|| {
format!(
"parse {} from {:?} with {} parser",
"Failed to parse {} from {:?} with {} parser",
name, &test_file_path, parser_type
)
})?;
Expand Down
15 changes: 11 additions & 4 deletions src/parsers/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ lazy_static! {

pub const DEFAULT_MARKDOWN_LANGUAGES: &[&str] = &["scrut"];

#[derive(Debug, thiserror::Error)]
pub enum MarkdownParserError {
#[error(
"Code block starting at line {line} is missing language specifier. Use ```scrut to make this block a Scrut test, or any other language to make Scrut skip this block."
)]
MissingLanguageSpecifier { line: usize },
}

/// A parser for Cram `.t` files, which reads [`crate::testcase::TestCase`]s
/// that are encoded in the form:
///
Expand Down Expand Up @@ -102,10 +110,9 @@ impl Parser for MarkdownParser {
lines: _,
} => {
if language.is_empty() {
anyhow::bail!(
"Code block starting at line {} is missing language specifier. Use ```scrut to make this block a Scrut test, or any other language to make Scrut skip this block.",
starting_line_number
);
anyhow::bail!(MarkdownParserError::MissingLanguageSpecifier {
line: starting_line_number,
});
}
}
MarkdownToken::TestCodeBlock {
Expand Down

0 comments on commit 6fc5c61

Please sign in to comment.