summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-11-27 18:04:24 +0200
committerLars Wirzenius <liw@liw.fi>2021-11-27 19:00:01 +0200
commit5b54da926b996819dacf69056020f02cba464c3d (patch)
tree4001b7b22cc9317f41fa0e98e45674346b8fab61 /src
parent1d317988771b05d9850718957befbba8cd90016e (diff)
downloadsubplot-5b54da926b996819dacf69056020f02cba464c3d.tar.gz
feat: warn about unused embedded files
This is merely a warning, for now, to stderr. subplot.md has two embedded files used as examples, and I'm too tired to add a way to indicate that it's OK for a specific file to not be used. That should probably be added later, though. Sponsored-by: author
Diffstat (limited to 'src')
-rw-r--r--src/bin/subplot.rs4
-rw-r--r--src/doc.rs43
2 files changed, 45 insertions, 2 deletions
diff --git a/src/bin/subplot.rs b/src/bin/subplot.rs
index 524535d..3353328 100644
--- a/src/bin/subplot.rs
+++ b/src/bin/subplot.rs
@@ -331,7 +331,9 @@ impl Docgen {
.unwrap_or("");
let template = template.to_string();
event!(Level::TRACE, ?template);
- if !doc.check_named_files_exist(&template)? || !doc.check_matched_steps_have_impl(&template)
+ if !doc.check_named_files_exist(&template)?
+ || !doc.check_matched_steps_have_impl(&template)
+ || !doc.check_embedded_files_are_used(&template)?
{
eprintln!("Continuing despite warnings");
}
diff --git a/src/doc.rs b/src/doc.rs
index a32d6da..9989bc4 100644
--- a/src/doc.rs
+++ b/src/doc.rs
@@ -364,6 +364,44 @@ impl<'a> Document {
Ok(okay)
}
+ /// Check that all embedded files are used by matched steps.
+ #[instrument(level = "trace", skip(self))]
+ pub fn check_embedded_files_are_used(&mut self, template: &str) -> Result<bool> {
+ let mut filenames: HashSet<_> = self
+ .files()
+ .iter()
+ .map(|f| f.filename().to_lowercase())
+ .collect();
+ event!(Level::TRACE, ?filenames, "Checking that files are used");
+ let scenarios = match self.matched_scenarios(template) {
+ Ok(scenarios) => scenarios,
+ Err(_) => return Ok(true), // We can't do the check, so say it's okay.
+ };
+ for scenario in scenarios {
+ for step in scenario.steps() {
+ for captured in step.parts() {
+ if let PartialStep::CapturedText { name, text } = captured {
+ if matches!(step.types().get(name.as_str()), Some(CaptureType::File)) {
+ filenames.remove(&text.to_lowercase());
+ }
+ }
+ }
+ }
+ }
+ for filename in filenames.iter() {
+ eprintln!(
+ "WARNING: embedded file is not used by any scenario: {}",
+ filename
+ );
+ }
+
+ // We always succeed. Subplot's own subplot had valid cases of
+ // an embedded file being used and we need to develop a way to
+ // mark such uses as OK, before we can make it an error to not
+ // use an embedded file in a scenario.
+ Ok(true)
+ }
+
/// Check that all matched steps actually have function implementations
#[instrument(level = "trace", skip(self))]
pub fn check_matched_steps_have_impl(&mut self, template: &str) -> bool {
@@ -521,7 +559,10 @@ pub fn codegen(filename: &Path, output: &Path, template: Option<&str>) -> Result
.unwrap_or_else(|| doc.template())?
.to_string();
event!(Level::TRACE, ?template);
- if !doc.check_named_files_exist(&template)? || !doc.check_matched_steps_have_impl(&template) {
+ if !doc.check_named_files_exist(&template)?
+ || !doc.check_matched_steps_have_impl(&template)
+ || !doc.check_embedded_files_are_used(&template)?
+ {
event!(Level::ERROR, "Found problems in document, cannot continue");
eprintln!("Unable to continue");
std::process::exit(1);