Repository navigation
Add Glob, sparse Read lines, trusted output, caller errors #11
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,100 @@ | ||
| //! Unit tests for the `Glob` tool. | ||
|
|
||
| use runvil_plugin_sdk::testing::Files; | ||
| use runvil_plugin_sdk::{ToolError, ToolErrorKind, ToolOutput}; | ||
| use serde_json::{Value, json}; | ||
|
|
||
| use crate::fake::block_on; | ||
| use crate::source::Source; | ||
| use crate::spec::{self, Definition}; | ||
|
|
||
| /// The `glob` definition the Pack builds the tool from. | ||
| fn definition() -> &'static Definition { | ||
| spec::all() | ||
| .iter() | ||
| .find(|definition| definition.segment == "glob") | ||
| .expect("the glob definition exists") | ||
| } | ||
|
|
||
| /// Calls Glob over `files` with `args` as the Pack does. | ||
| fn glob(files: &impl Source, args: &Value) -> Result<ToolOutput, ToolError> { | ||
| let definition = definition(); | ||
| crate::check::check(&definition.wire_name, &definition.parameters, args)?; | ||
| block_on(super::call(files, definition, args)) | ||
| } | ||
|
|
||
| #[test] | ||
| fn matches_files_at_any_depth_and_returns_trusted_output() { | ||
| let files = Files::new() | ||
| .file("/a.rs", "") | ||
| .file("/src/main.rs", "") | ||
| .file("/src/lib.ts", "") | ||
| .file("/.git/config.rs", ""); | ||
| let output = glob(&files, &json!({ "glob_pattern": "*.rs" })).expect("glob succeeds"); | ||
| assert!(output.is_trusted()); | ||
| assert_eq!(output.text(), "/a.rs\n/src/main.rs"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn respects_target_directory_and_anchored_patterns() { | ||
| let files = Files::new() | ||
| .file("/a.md", "") | ||
| .file("/docs/guide.md", "") | ||
| .file("/docs/nested/ref.md", ""); | ||
| let scoped = glob( | ||
| &files, | ||
| &json!({ "glob_pattern": "*.md", "target_directory": "/docs" }), | ||
| ) | ||
| .expect("scoped glob succeeds"); | ||
| assert_eq!(scoped.text(), "/docs/guide.md\n/docs/nested/ref.md"); | ||
|
|
||
| let anchored = glob(&files, &json!({ "glob_pattern": "/*.md" })).expect("anchored succeeds"); | ||
| assert_eq!(anchored.text(), "/a.md"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn reports_no_files_matched_when_empty() { | ||
| let files = Files::new().file("/a.txt", ""); | ||
| let output = glob(&files, &json!({ "glob_pattern": "*.rs" })).expect("glob succeeds"); | ||
| assert!(output.is_trusted()); | ||
| assert_eq!(output.text(), "No files matched."); | ||
| } | ||
|
|
||
| #[test] | ||
| fn refuses_invalid_pattern_and_missing_target_directory() { | ||
| let files = Files::new(); | ||
| let bad_pattern = | ||
| glob(&files, &json!({ "glob_pattern": "a***" })).expect_err("invalid pattern fails"); | ||
| assert_eq!(bad_pattern.kind(), ToolErrorKind::InvalidArguments); | ||
|
|
||
| let missing = glob( | ||
| &files, | ||
| &json!({ "glob_pattern": "*.rs", "target_directory": "/nope" }), | ||
| ) | ||
| .expect_err("missing target fails"); | ||
| assert_eq!(missing.kind(), ToolErrorKind::Other); | ||
| assert_eq!(missing.message(), "Path does not exist: /nope"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn visits_subdirectories_in_sorted_path_order() { | ||
| let files = Files::new().file("/a/z.rs", "").file("/b.rs", ""); | ||
| let output = glob(&files, &json!({ "glob_pattern": "*.rs" })).expect("glob succeeds"); | ||
| assert_eq!(output.text(), "/a/z.rs\n/b.rs"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn truncates_when_output_exceeds_cap() { | ||
| let files = Files::new() | ||
| .file("/a.rs", "") | ||
| .file("/b.rs", "") | ||
| .file("/c.rs", ""); | ||
| let args = json!({ "glob_pattern": "*.rs" }); | ||
| let req = super::parse(definition(), &args).expect("parses"); | ||
| let output = block_on(super::glob_files(&files, "Glob", &req, 12)).expect("glob succeeds"); | ||
| assert!( | ||
| output | ||
| .text() | ||
| .starts_with("/a.rs\n/b.rs\n\n[Results truncated at 12 bytes.") | ||
| ); | ||
| } |
Oops, something went wrong.
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.