Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Exists Builtin Command (#504)
- Loading branch information
Showing
6 changed files
with
739 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,96 @@ | ||
# Same tests as in src/builtins/exists.rs:test_evaluate_arguments() | ||
# TODO: find a better way than to write "echo $?" between each test cases | ||
exists | ||
echo $? | ||
exists foo bar | ||
echo $? | ||
|
||
exists --help | ||
echo $? | ||
exists --help unused params | ||
echo $? | ||
|
||
|
||
exists "" | ||
echo $? | ||
exists string | ||
echo $? | ||
exists "string with spaces" | ||
echo $? | ||
exists "-startswithdash" | ||
echo $? | ||
|
||
|
||
exists -a | ||
echo $? | ||
let emptyarray = [] | ||
echo @emptyarray | ||
exists -a emptyarray | ||
echo $? | ||
let array = [ "element" ] | ||
echo @array | ||
exists -a array | ||
echo $? | ||
exists -a array | ||
echo $? | ||
|
||
|
||
exists -b | ||
echo $? | ||
let OLDPATH = $PATH | ||
let PATH = testing/ | ||
echo "PATH = $PATH" | ||
ls -1 $PATH | ||
exists -b executable_file | ||
echo $? | ||
exists -b empty_file | ||
echo $? | ||
exists -b file_does_not_exist | ||
echo $? | ||
let PATH = $OLDPATH | ||
echo "Reset PATH to old path" | ||
|
||
exists -d | ||
echo $? | ||
exists -d testing/ | ||
echo $? | ||
exists -d testing/empty_file | ||
echo $? | ||
exists -d does/not/exist | ||
echo $? | ||
|
||
|
||
exists -f | ||
echo $? | ||
exists -f testing/ | ||
echo $? | ||
exists -f testing/empty_file | ||
echo $? | ||
exists -f does-not-exist | ||
echo $? | ||
|
||
fn testFunc a | ||
echo $a | ||
end | ||
exists --fn testFunc | ||
echo $? | ||
|
||
exists -s | ||
echo $? | ||
let emptyvar = "" | ||
echo "emptyvar = $emptyvar" | ||
exists -s emptyvar | ||
echo $? | ||
let testvar = "foobar" | ||
echo "testvar = $testvar" | ||
exists -s testvar | ||
echo $? | ||
drop testvar | ||
echo "testvar = $testvar" | ||
exists -s testvar | ||
echo $? | ||
|
||
exists --foo | ||
echo $? | ||
exists -x | ||
echo $? |
This file contains 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,152 @@ | ||
1 | ||
0 | ||
NAME | ||
exists - check whether items exist | ||
|
||
SYNOPSIS | ||
exists [EXPRESSION] | ||
|
||
DESCRIPTION | ||
Checks whether the given item exists and returns an exit status of 0 if it does, else 1. | ||
|
||
OPTIONS | ||
-a ARRAY | ||
array var is not empty | ||
|
||
-b BINARY | ||
binary is in PATH | ||
|
||
-d PATH | ||
path is a directory | ||
This is the same as test -d | ||
|
||
-f PATH | ||
path is a file | ||
This is the same as test -f | ||
|
||
--fn FUNCTION | ||
function is defined | ||
|
||
-s STRING | ||
string var is not empty | ||
|
||
STRING | ||
string is not empty | ||
This is the same as test -n | ||
|
||
EXAMPLES | ||
Test if the file exists: | ||
exists -f FILE && echo "The FILE exists" || echo "The FILE does not exist" | ||
|
||
Test if some-command exists in the path and is executable: | ||
exists -b some-command && echo "some-command exists" || echo "some-command does not exist" | ||
|
||
Test if variable exists AND is not empty | ||
exists -s myVar && echo "myVar exists: $myVar" || echo "myVar does not exist or is empty" | ||
NOTE: Don't use the '$' sigil, but only the name of the variable to check | ||
|
||
Test if array exists and is not empty | ||
exists -a myArr && echo "myArr exists: @myArr" || echo "myArr does not exist or is empty" | ||
NOTE: Don't use the '@' sigil, but only the name of the array to check | ||
|
||
Test if a function named 'myFunc' exists | ||
exists --fn myFunc && myFunc || echo "No function with name myFunc found" | ||
|
||
AUTHOR | ||
Written by Fabian Würfl. | ||
Heavily based on implementation of the test builtin, which was written by Michael Murph. | ||
0 | ||
NAME | ||
exists - check whether items exist | ||
|
||
SYNOPSIS | ||
exists [EXPRESSION] | ||
|
||
DESCRIPTION | ||
Checks whether the given item exists and returns an exit status of 0 if it does, else 1. | ||
|
||
OPTIONS | ||
-a ARRAY | ||
array var is not empty | ||
|
||
-b BINARY | ||
binary is in PATH | ||
|
||
-d PATH | ||
path is a directory | ||
This is the same as test -d | ||
|
||
-f PATH | ||
path is a file | ||
This is the same as test -f | ||
|
||
--fn FUNCTION | ||
function is defined | ||
|
||
-s STRING | ||
string var is not empty | ||
|
||
STRING | ||
string is not empty | ||
This is the same as test -n | ||
|
||
EXAMPLES | ||
Test if the file exists: | ||
exists -f FILE && echo "The FILE exists" || echo "The FILE does not exist" | ||
|
||
Test if some-command exists in the path and is executable: | ||
exists -b some-command && echo "some-command exists" || echo "some-command does not exist" | ||
|
||
Test if variable exists AND is not empty | ||
exists -s myVar && echo "myVar exists: $myVar" || echo "myVar does not exist or is empty" | ||
NOTE: Don't use the '$' sigil, but only the name of the variable to check | ||
|
||
Test if array exists and is not empty | ||
exists -a myArr && echo "myArr exists: @myArr" || echo "myArr does not exist or is empty" | ||
NOTE: Don't use the '@' sigil, but only the name of the array to check | ||
|
||
Test if a function named 'myFunc' exists | ||
exists --fn myFunc && myFunc || echo "No function with name myFunc found" | ||
|
||
AUTHOR | ||
Written by Fabian Würfl. | ||
Heavily based on implementation of the test builtin, which was written by Michael Murph. | ||
0 | ||
1 | ||
0 | ||
0 | ||
0 | ||
0 | ||
|
||
1 | ||
element | ||
0 | ||
0 | ||
0 | ||
PATH = testing/ | ||
empty_file | ||
executable_file | ||
file_with_text | ||
symlink | ||
0 | ||
1 | ||
1 | ||
Reset PATH to old path | ||
0 | ||
0 | ||
1 | ||
1 | ||
0 | ||
1 | ||
0 | ||
1 | ||
0 | ||
0 | ||
emptyvar = | ||
1 | ||
testvar = foobar | ||
0 | ||
testvar = | ||
1 | ||
0 | ||
0 |
This file contains 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.