Skip to content

Commit

Permalink
do not overwrite outputfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbrooks committed Feb 18, 2015
1 parent a7f01f8 commit fa45a9f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/basic_stream/test_basic_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ def test_construct_listener_with_file(self, PrintingListener):
PrintingListener.assert_called_once_with(out=mock_open.return_value)
self.assertEquals(result, PrintingListener.return_value)

@mock.patch('os.path.exists')
@mock.patch('twitter_monitor.basic_stream.PrintingListener')
def test_construct_listener_with_existing_file(self, PrintingListener, os_path_exists):
PrintingListener.return_value = 53234

outfile = "some_file.txt"

mock_open = mock.mock_open()
mock_open.return_value = 348374387

os_path_exists.return_value = True

with mock.patch('twitter_monitor.basic_stream.open', mock_open, create=True):
# An error should be raised
self.assertRaises(IOError, basic_stream.construct_listener, outfile)

# The file should not have been opened
self.assertEquals(mock_open.call_count, 0)


def test_get_tweepy_auth(self):
twitter_api_key = 'ak'
twitter_api_secret = 'as'
Expand Down
3 changes: 3 additions & 0 deletions twitter_monitor/basic_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def get_tweepy_auth(twitter_api_key,
def construct_listener(outfile=None):
"""Create the listener that prints tweets"""
if outfile is not None:
if os.path.exists(outfile):
raise IOError("File %s already exists" % outfile)

outfile = open(outfile, 'wb')

return PrintingListener(out=outfile)
Expand Down

0 comments on commit fa45a9f

Please sign in to comment.