Skip to content

Commit

Permalink
Implement Exists Builtin Command (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
BafDyce authored and mmstick committed Aug 17, 2017
1 parent 5d35de2 commit abdb02a
Show file tree
Hide file tree
Showing 6 changed files with 739 additions and 1 deletion.
96 changes: 96 additions & 0 deletions examples/exists.ion
@@ -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 $?
152 changes: 152 additions & 0 deletions examples/exists.out
@@ -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
2 changes: 1 addition & 1 deletion examples/glob.out
Expand Up @@ -5,7 +5,7 @@ Cargo.toml
Cargo.lock Cargo.toml
Cargo.toml
Cargo.toml
examples/else_if.ion examples/fail.ion examples/fibonacci.ion examples/fn.ion examples/for.ion examples/function_piping.ion
examples/else_if.ion examples/exists.ion examples/fail.ion examples/fibonacci.ion examples/fn.ion examples/for.ion examples/function_piping.ion
one three two
three two
three two

0 comments on commit abdb02a

Please sign in to comment.