Add Cmd.check_available! - #466
Open
dmisiuk wants to merge 1 commit into
Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds
Cmd.check_available!, closes #223 — check whether a command can be found on the system before trying to run it.Discussed on Zulip. @lukewilliamboswell approved a pure-Roc implementation on the existing primitives, so there are no host or glue changes.
Implementation
PATHviaEnv.var!and splits it byte-preservingly throughOsStr.to_raw, so non-UTF-8 entries survive.Env.platform!for the:/;separator and, on Windows, the candidate extensions from%PATHEXT%(a name is tried as-is and with each extension appended).Directories are excluded.
Path.is_executable!(Unix) andPath.exists!(Windows) both return true for a directory, so a candidate is additionally required to not be a directory. Becauseis_dir!does not follow symlinks, a symlink to an executable still resolves while a real directory named like the command is rejected. The only residual is a symlink pointing at a directory, which is documented. (The triage note asked to avoid the directory failure mode; this does, in pure Roc.)Testing
Six
expectblocks cover the pure core —PATHsplitting for all threeOsStrrepresentations, byte preservation of a non-UTF-8 entry (verified to fail when the bytes are altered), empty-entry dropping, and separator detection.Verified end-to-end against a local build with a directory, a symlink, and a real executable sharing a name:
examples/check-command.rocdemonstrates the API and itstest_spec.jsoncase asserts a real command is found and a bogus one is not. Full./scripts/test.pypasses — host build plus all examples. The example's run case is Unix-only (likecommand.roc), since it checks a Unix command by name.