Skip to content

Commit

Permalink
Collectors: Raise SystemExit on ValueError
Browse files Browse the repository at this point in the history
  • Loading branch information
sils committed Mar 22, 2015
1 parent d753060 commit 407685c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
27 changes: 19 additions & 8 deletions coalib/collecting/Collectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,28 @@ def _import_bears(file_path, kinds):


@yield_once
def icollect(file_paths, files=True, dirs=True):
def icollect(file_paths, files=True, dirs=True, log_printer=ConsolePrinter()):
"""
Evaluate globs in file paths and return all matching files
:param file_paths: list of file paths that can include globs
:param files: True if files are to be collected
:param dirs: True if dirs are to be collected
:return: iterator that yields paths of all matching files
Evaluate globs in file paths and return all matching files.
:param file_paths: list of file paths that can include globs
:param files: True if files are to be collected
:param dirs: True if dirs are to be collected
:param log_printer: where to log things that go wrong
:return: iterator that yields paths of all matching files
:raises SystemExit: when getting an invalid pattern
"""
for file_path in file_paths:
for match in iglob(file_path, files=files, dirs=dirs):
yield match
try:
for match in iglob(file_path, files=files, dirs=dirs):
yield match
except ValueError as exception:
log_printer.err(
_("The given glob '{glob}' contains an invalid pattern. "
"Detailed error is: {error_message}").format(
glob=file_path,
error_message=str(exception)))
raise SystemExit(-1)


def collect_files(file_paths):
Expand Down
9 changes: 9 additions & 0 deletions coalib/tests/collecting/CollectorsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def test_file_empty(self):
def test_file_invalid(self):
self.assertEqual(collect_files(["invalid_path"]), [])

def test_expression_invalid(self):
self.assertRaises(SystemExit, collect_files, ["**d"])

def test_file_collection(self):
self.assertEqual(collect_files([os.path.join(self.collectors_test_dir,
"others",
Expand All @@ -47,6 +50,9 @@ def test_dir_empty(self):
def test_dir_invalid(self):
self.assertEqual(collect_dirs(["invalid_path"]), [])

def test_expression_invalid(self):
self.assertRaises(SystemExit, collect_files, ["**d"])

def test_dir_collection(self):
self.assertEqual(
sorted(collect_dirs([os.path.join(self.collectors_test_dir,
Expand Down Expand Up @@ -74,6 +80,9 @@ def test_bear_invalid(self):
["invalid_name"],
["invalid kind"]), [])

def test_expression_invalid(self):
self.assertRaises(SystemExit, collect_files, ["**d"])

def test_simple_single(self):
self.assertEqual(len(collect_bears([os.path.join(
self.collectors_test_dir, "bears")],
Expand Down

1 comment on commit 407685c

@Udayan12167
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack.

Please sign in to comment.