summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-10-09 06:18:43 +0000
committerLars Wirzenius <liw@liw.fi>2021-10-09 06:18:43 +0000
commit6920a4f4ad38373b2d0cf03a0e31c6a22595ce8b (patch)
tree107cf6df78f91f1df553ecf788aebe5548fcf114
parent9ec6b0d8dbe69f0f8e0f8288aba28a4ceb812f54 (diff)
parent58110bf660c60ca8ba9ea692be916b9c641bb7d3 (diff)
downloadsubplot-6920a4f4ad38373b2d0cf03a0e31c6a22595ce8b.tar.gz
Merge branch 'zero-scenario-fail' into 'main'
Ensure codegen barfs on zero scenario documents Closes #236 See merge request subplot/subplot!219
-rw-r--r--reference.md6
-rw-r--r--src/codegen.rs8
-rw-r--r--src/error.rs4
-rw-r--r--subplot.md31
4 files changed, 45 insertions, 4 deletions
diff --git a/reference.md b/reference.md
index 1488ce0..62f827b 100644
--- a/reference.md
+++ b/reference.md
@@ -11,7 +11,7 @@ documents.
~~~scenario
given an installed subplot
-given a clone of https://gitlab.com/subplot/subplot.git in src at 8fe4a393ccbda6820b57ec873f37f08ce17d7c3d
+given a clone of https://gitlab.com/subplot/subplot.git in src at 409f32c5017d3f880e003138b0548cf74f965ce0
when I docgen subplot.md to test.pdf, in src
then file src/test.pdf exists
~~~
@@ -20,7 +20,7 @@ then file src/test.pdf exists
~~~scenario
given an installed subplot
-given a clone of https://gitlab.com/subplot/subplot.git in src at 8fe4a393ccbda6820b57ec873f37f08ce17d7c3d
+given a clone of https://gitlab.com/subplot/subplot.git in src at 409f32c5017d3f880e003138b0548cf74f965ce0
when I docgen subplot.md to test.html, in src
when I run, in src, subplot docgen subplot.md -o subplot.html
then file src/test.html exists
@@ -31,7 +31,7 @@ then file src/test.html exists
~~~scenario
given an installed subplot
given file run_test.sh
-given a clone of https://gitlab.com/subplot/subplot.git in src at 8fe4a393ccbda6820b57ec873f37f08ce17d7c3d
+given a clone of https://gitlab.com/subplot/subplot.git in src at 409f32c5017d3f880e003138b0548cf74f965ce0
when I run, in src, subplot codegen subplot.md -o test-inner.py
when I run bash run_test.sh
then command is successful
diff --git a/src/codegen.rs b/src/codegen.rs
index 89ea591..b67d94a 100644
--- a/src/codegen.rs
+++ b/src/codegen.rs
@@ -39,7 +39,8 @@ pub fn generate_test_program(
fn context(doc: &mut Document, template: &str) -> Result<Context, SubplotError> {
let mut context = Context::new();
- context.insert("scenarios", &doc.matched_scenarios(template)?);
+ let scenarios = doc.matched_scenarios(template)?;
+ context.insert("scenarios", &scenarios);
context.insert("files", doc.files());
let funcs_filenames = doc.meta().functions_filenames();
@@ -51,6 +52,11 @@ fn context(doc: &mut Document, template: &str) -> Result<Context, SubplotError>
}
context.insert("functions", &funcs);
+ // Any of the above could fail for more serious reasons, but if we get this far
+ // and our context would have no scenarios in it, then we complain.
+ if scenarios.is_empty() {
+ return Err(SubplotError::NoScenariosMatched(template.to_string()));
+ }
Ok(context)
}
diff --git a/src/error.rs b/src/error.rs
index e69bffe..c041eaf 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -233,6 +233,10 @@ pub enum SubplotError {
#[error("failure rendering pikchr diagram: {0}")]
PikchrRenderError(String),
+ /// When attempting to codegen, no scenarios matched the desired template language
+ #[error("no scenarios were found matching the `{0}` template")]
+ NoScenariosMatched(String),
+
/// I/O error
///
/// Subplot did some I/O, and it failed. This is a generic wrapper
diff --git a/subplot.md b/subplot.md
index a718490..2d7d760 100644
--- a/subplot.md
+++ b/subplot.md
@@ -1071,6 +1071,30 @@ and step "then bar was done" was run
and command is successful
~~~
+## No scenarios means codegen fails
+
+If you attempt to `subplot codegen` on a document which contains no scenarios, the
+tool will fail to execute with a reasonable error message.
+
+~~~scenario
+given file noscenarios.md
+and an installed subplot
+when I try to run subplot codegen noscenarios.md -o test.py
+then command fails
+and stderr contains "no scenarios were found"
+~~~
+
+~~~{#noscenarios.md .file .markdown .numberLines}
+---
+title: No scenarios in here
+template: python
+...
+
+# This is a title
+
+But there are no scenarios in this file, and thus nothing can be generated in a test suite.
+
+~~~
## Keywords
@@ -2099,6 +2123,13 @@ template: python
This is a very simple Markdown file that uses every kind of inline
markup in the title and chapter heading.
+
+To satisfy codegen, we *MUST* have a scenario here
+~~~~scenario
+when I do bar
+then bar was done
+~~~~
+
~~~