-
-
Notifications
You must be signed in to change notification settings - Fork 52
feat(spec,parse): add repeatable clause groups #1321
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
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
33f743f
feat(spec,parse): add repeatable clause groups
jdx cf27005
fix(parse): include clause args in exclusivity
jdx 374dc82
style: format clause implementation
jdx ef8995d
fix(complete): reset trailing state at clauses
jdx 485705e
fix(parse): preserve token role discriminants
jdx 2859852
feat(spec): negotiate clauses as usage 6.6
jdx 83872ea
fix(parse): include clause args in relationships
jdx 9e7528e
fix(parse): isolate clause relationships by instance
jdx d083c29
fix(parse): preserve clause double dash values
jdx 233c87f
chore(spec): update runtime fixture lockfile
jdx 7db6c6c
style(parse): simplify clause predicate
jdx 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,153 @@ | ||
| use usage::parse::ParseValue; | ||
| use usage::Spec; | ||
|
|
||
| fn spec() -> Spec { | ||
| r#" | ||
| min_usage_version "6.6" | ||
| name "clause" | ||
| bin "clause" | ||
| clause "tasks" separator=":::" { | ||
| arg "<task>" | ||
| arg "[args]..." var=#true double_dash="automatic" | ||
| } | ||
| "# | ||
| .parse() | ||
| .expect("valid clause spec") | ||
| } | ||
|
|
||
| fn strings<'a>( | ||
| mut instance: impl Iterator<Item = (&'a std::sync::Arc<usage::SpecArg>, &'a ParseValue)>, | ||
| name: &str, | ||
| ) -> Vec<String> { | ||
| instance | ||
| .find(|(arg, _)| arg.name == name) | ||
| .map(|(_, value)| match value { | ||
| ParseValue::String(value) => vec![value.clone()], | ||
| ParseValue::MultiString(values) => values.clone(), | ||
| other => panic!("unexpected value: {other:?}"), | ||
| }) | ||
| .unwrap_or_default() | ||
| } | ||
|
|
||
| #[test] | ||
| fn clause_instances_preserve_values_and_restart_flags() { | ||
| let parsed = usage::Parser::new(&spec()) | ||
| .parse(&["clause", "lint", "--fix", ":::", "test", "--all"].map(str::to_string)) | ||
| .expect("valid invocation"); | ||
| let instances = &parsed.clauses["tasks"]; | ||
| assert_eq!(instances.len(), 2); | ||
| assert_eq!(strings(instances[0].iter(), "task"), ["lint"]); | ||
| assert_eq!(strings(instances[0].iter(), "args"), ["--fix"]); | ||
| assert_eq!(strings(instances[1].iter(), "task"), ["test"]); | ||
| assert_eq!(strings(instances[1].iter(), "args"), ["--all"]); | ||
| assert!(parsed.args.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn explicit_double_dash_protects_a_literal_separator() { | ||
| let parsed = usage::Parser::new(&spec()) | ||
| .parse(&["clause", "lint", "--", ":::", "tail"].map(str::to_string)) | ||
| .expect("valid invocation"); | ||
| let instances = &parsed.clauses["tasks"]; | ||
| assert_eq!(instances.len(), 1); | ||
| assert_eq!(strings(instances[0].iter(), "args"), [":::", "tail"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn clause_variadic_can_preserve_double_dash() { | ||
| let spec: Spec = r#" | ||
| name "clause" | ||
| bin "clause" | ||
| clause "tasks" separator=":::" { | ||
| arg "<task>" | ||
| arg "[args]..." double_dash="preserve" | ||
| } | ||
| "# | ||
| .parse() | ||
| .expect("valid clause spec"); | ||
| let parsed = usage::Parser::new(&spec) | ||
| .parse(&["clause", "lint", "--", "--fix"].map(str::to_string)) | ||
| .expect("preserved double dash is clause data"); | ||
| assert_eq!( | ||
| strings(parsed.clauses["tasks"][0].iter(), "args"), | ||
| ["--", "--fix"] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn clause_round_trips_through_canonical_kdl() { | ||
| let spec = spec(); | ||
| let emitted = spec.to_string(); | ||
| let reparsed: Spec = emitted.parse().expect("emitted clause spec reparses"); | ||
| let clause = reparsed.cmd.clause.expect("clause retained"); | ||
| assert_eq!(clause.name, "tasks"); | ||
| assert_eq!(clause.separator, ":::"); | ||
| assert_eq!(clause.args.len(), 2); | ||
| } | ||
|
|
||
| #[test] | ||
| fn clause_arguments_participate_in_relationship_checks() { | ||
| for (spec, argv, expected) in [ | ||
| ( | ||
| r#"name "clause" | ||
| bin "clause" | ||
| clause "items" separator=":::" { | ||
| arg "[output]" requires="input" | ||
| arg "[input]" | ||
| } | ||
| "#, | ||
| vec!["clause", "artifact"], | ||
| "input", | ||
| ), | ||
| ( | ||
| r#"name "clause" | ||
| bin "clause" | ||
| flag "--json" conflicts="task" | ||
| clause "items" separator=":::" { arg "[task]" } | ||
| "#, | ||
| vec!["clause", "--json", "lint"], | ||
| "conflicts with task", | ||
| ), | ||
| ( | ||
| r#"name "clause" | ||
| bin "clause" | ||
| clause "items" separator=":::" { | ||
| arg "[trigger]" | ||
| arg "[dependent]" required_if="trigger" | ||
| } | ||
| "#, | ||
| vec!["clause", "yes"], | ||
| "dependent", | ||
| ), | ||
| ( | ||
| r#"name "clause" | ||
| bin "clause" | ||
| clause "items" separator=":::" { | ||
| arg "[output]" requires="input" | ||
| arg "[input]" | ||
| } | ||
| "#, | ||
| vec!["clause", "first", ":::", "second", "input"], | ||
| "instance 1", | ||
| ), | ||
| ( | ||
| r#"name "clause" | ||
| bin "clause" | ||
| clause "items" separator=":::" { | ||
| arg "[trigger]" | ||
| arg "[dependent]" { | ||
| required_if_eq "trigger" "yes" | ||
| } | ||
| } | ||
| "#, | ||
| vec!["clause", "yes", ":::", "no", "present"], | ||
| "instance 1", | ||
| ), | ||
| ] { | ||
| let spec: Spec = spec.parse().expect("valid relationship spec"); | ||
| let error = usage::Parser::new(&spec) | ||
| .parse(&argv.into_iter().map(str::to_string).collect::<Vec<_>>()) | ||
| .unwrap_err(); | ||
| assert!(format!("{error:?}").contains(expected), "{error:?}"); | ||
| } | ||
| } |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.