Permalink
Browse files

Handle errors in 'source' builtin.

- When file doesn't exist
- When no arguments are given

Fixes issue #23.
  • Loading branch information...
Andy Chu
Andy Chu committed Aug 6, 2017
1 parent 2fbc360 commit ae8804a4e216af5e5809d14ab04318fb1cbb5ae3
Showing with 26 additions and 2 deletions.
  1. +13 −2 core/cmd_exec.py
  2. +13 −0 spec/builtins.test.sh
View
@@ -174,8 +174,19 @@ def _Eval(self, argv):
def _Source(self, argv):
# TODO: Use LineReader
with open(argv[0]) as f:
code_str = f.read()
try:
path = argv[0]
except IndexError:
# TODO: Should point to the source statement that failed.
util.error('source: missing required argument')
return 1
try:
with open(path) as f:
code_str = f.read()
except IOError as e:
# TODO: Should point to the source statement that failed.
util.error('source %r failed: %s', path, os.strerror(e.errno))
return 1
return self._EvalHelper(code_str)
def _Exec(self, argv):
View
@@ -69,6 +69,19 @@ echo 'LIBVAR=libvar' > $lib
echo $LIBVAR
# stdout: libvar
### Source nonexistent
source /nonexistent/path
echo status=$?
# stdout: status=1
# OK dash stdout: status=127
### Source with no arguments
source
echo status=$?
# stdout: status=1
# OK bash stdout: status=2
# OK dash stdout: status=127
### Exit builtin
exit 3
# status: 3

0 comments on commit ae8804a

Please sign in to comment.