diff --git a/exercises-python/template/ex01_basics/main.py b/exercises-python/template/ex01_basics/main.py index dc750397..d0cba884 100755 --- a/exercises-python/template/ex01_basics/main.py +++ b/exercises-python/template/ex01_basics/main.py @@ -1,11 +1,11 @@ #!/usr/bin/env python3 -def read_ascii_file(param): +def read_ascii_file(filename): try: - f = open(param, 'r') + f = open(filename, 'r') return f.read() - except FileNotFoundError: - print("Oops! That was not a valid file. Try again...") + except FileNotFoundError as e: + raise FileNotFoundError(f"The file {filename} was not found.") def main(): read_ascii_file("file.txt") diff --git a/exercises-python/template/ex01_basics/test_wordcount.py b/exercises-python/template/ex01_basics/test_wordcount.py index f519156b..5e366918 100644 --- a/exercises-python/template/ex01_basics/test_wordcount.py +++ b/exercises-python/template/ex01_basics/test_wordcount.py @@ -16,5 +16,13 @@ def test_read_file(self, mock_file): self.assertEqual(content, expected_content) mock_file.assert_called_once_with(self.filename, 'r') + @patch('builtins.open', side_effect=FileNotFoundError) + def test_read_file_raise_file_found_error(self, mock_file): + with self.assertRaises(FileNotFoundError): + read_ascii_file(self.filename) + mock_file.assert_called_once_with(self.filename, 'r') + + +