-
Notifications
You must be signed in to change notification settings - Fork 62
fix --rules-dir in mathml2text CLI #543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
moritz-gross
wants to merge
1
commit into
daisy:main
Choose a base branch
from
moritz-gross:fix-rulesdir-in-mathml2text
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+104
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| mod common; | ||
|
|
||
| use std::path::PathBuf; | ||
| use std::process::Command; | ||
| use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
|
||
| fn mathml2text_command() -> Command { Command::new(env!("CARGO_BIN_EXE_mathml2text")) } | ||
|
|
||
| fn rules_dir() -> String { common::abs_rules_dir_path() } | ||
|
|
||
| fn unique_temp_file(name: &str) -> PathBuf { | ||
| let timestamp = SystemTime::now() | ||
| .duration_since(UNIX_EPOCH) | ||
| .expect("system time should be after UNIX_EPOCH") | ||
| .as_nanos(); | ||
| std::env::temp_dir().join(format!("mathml2text-{name}-{timestamp}.mml")) | ||
| } | ||
|
|
||
| /// Verifies that `mathml2text` uses an explicitly provided `--rules-dir`. | ||
| /// This protects the CLI override path instead of silently falling back to the default rules location. | ||
| #[test] | ||
| fn accepts_explicit_rules_dir() { | ||
| let input_file = unique_temp_file("explicit-rules"); | ||
| std::fs::write( | ||
| &input_file, | ||
| "<math><mn>4</mn></math>", | ||
| ) | ||
| .expect("should write temp input file"); | ||
|
|
||
| let output = mathml2text_command() | ||
| .args([ | ||
| "--rules-dir", | ||
| rules_dir().as_str(), | ||
| ]) | ||
| .arg(&input_file) | ||
| .output() | ||
| .expect("mathml2text should run"); | ||
|
|
||
| let _ = std::fs::remove_file(&input_file); | ||
|
|
||
| assert!(output.status.success(), "stderr: {}", String::from_utf8_lossy(&output.stderr)); | ||
| assert_eq!("4\n", String::from_utf8_lossy(&output.stdout)); | ||
| } | ||
|
|
||
| /// Verifies that the positional input-file argument still works on its own. | ||
| /// This keeps the default CLI file-input path covered while testing the rules-dir changes separately. | ||
| #[test] | ||
| fn still_accepts_input_file() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this test is really necessary. In practice I don't think you're really testing anything besides the argument parsing library itself. |
||
| let input_file = unique_temp_file("input-file"); | ||
| std::fs::write( | ||
| &input_file, | ||
| "<math><mn>2</mn></math>", | ||
| ) | ||
| .expect("should write temp input file"); | ||
|
|
||
| let output = mathml2text_command() | ||
| .arg(&input_file) | ||
| .output() | ||
| .expect("mathml2text should run"); | ||
|
|
||
| let _ = std::fs::remove_file(&input_file); | ||
|
|
||
| assert!(output.status.success(), "stderr: {}", String::from_utf8_lossy(&output.stderr)); | ||
| assert_eq!("2\n", String::from_utf8_lossy(&output.stdout)); | ||
| } | ||
|
|
||
| /// Verifies that an invalid explicit `--rules-dir` causes the CLI to fail. | ||
| /// This protects against regressions where the flag is parsed but then ignored at runtime. | ||
| #[test] | ||
| fn rejects_invalid_explicit_rules_dir() { | ||
| let missing_rules_dir = std::env::temp_dir().join(format!( | ||
| "mathml2text-missing-rules-{}", | ||
| SystemTime::now() | ||
| .duration_since(UNIX_EPOCH) | ||
| .expect("system time should be after UNIX_EPOCH") | ||
| .as_nanos() | ||
| )); | ||
| let input_file = unique_temp_file("invalid-rules"); | ||
| std::fs::write( | ||
| &input_file, | ||
| "<math><mn>5</mn></math>", | ||
| ) | ||
| .expect("should write temp input file"); | ||
|
|
||
| let output = mathml2text_command() | ||
| .args([ | ||
| "--rules-dir", | ||
| missing_rules_dir.to_str().expect("temp path should be valid UTF-8"), | ||
| ]) | ||
| .arg(&input_file) | ||
| .output() | ||
| .expect("mathml2text should run"); | ||
|
|
||
| let _ = std::fs::remove_file(&input_file); | ||
|
|
||
| assert!(!output.status.success(), "stdout: {}", String::from_utf8_lossy(&output.stdout)); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would put most of this boiler plate code in a fixture structure, so you can reuse more of the logic.