From 92a221305ca41a29def4811b773a762ab12def67 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Tue, 28 Mar 2017 22:50:51 -0700 Subject: [PATCH] Added logging, fixed tests --- requirements.txt | 6 ++---- rtv/__main__.py | 9 +++++++++ rtv/packages/__init__.py | 8 +++++--- tests/conftest.py | 2 +- tests/test_packages.py | 8 ++++++++ tests/test_submission.py | 28 ++++++++++++++-------------- tests/test_terminal.py | 4 +++- 7 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 tests/test_packages.py diff --git a/requirements.txt b/requirements.txt index 94d3899..acc486e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,9 +2,7 @@ beautifulsoup4==4.5.1 decorator==4.0.10 kitchen==1.2.4 mailcap-fix==0.1.3 -praw==3.6.0 requests==2.11.0 six==1.10.0 -update-checker==0.11 -pytest -vcrpy +pytest==3.0.7 +vcrpy==1.10.5 \ No newline at end of file diff --git a/rtv/__main__.py b/rtv/__main__.py index 9a2759b..eeac9b5 100644 --- a/rtv/__main__.py +++ b/rtv/__main__.py @@ -12,6 +12,7 @@ import six import requests from . import docs +from . import packages from .packages import praw from .config import Config, copy_default_config, copy_default_mailcap from .oauth import OAuthHelper @@ -111,6 +112,14 @@ def main(): warnings.warn(text) config['ascii'] = True + # Check the praw version + if packages.__praw_bundled__: + _logger.info('Using packaged PRAW distribution') + _logger.info('PRAW commit hash %s' % packages.__praw_hash__) + else: + _logger.info('Packaged PRAW not found, falling back to system distribution') + _logger.info('PRAW version %s' % praw.__version__) + # Construct the reddit user agent user_agent = docs.AGENT.format(version=__version__) diff --git a/rtv/packages/__init__.py b/rtv/packages/__init__.py index 65807c6..f2e7689 100644 --- a/rtv/packages/__init__.py +++ b/rtv/packages/__init__.py @@ -1,7 +1,7 @@ """ -This stub allows the end-user to fallback to their system installation of praw -if the bundled package missing. This technique was inspired by the requests -library and how it handles dependencies. +This stub allows the the package to fallback to their system installation of +praw if the bundled package is missing. This technique was inspired by the +requests library and how it handles dependencies. Reference: https://github.com/kennethreitz/requests/blob/master/requests/packages/__init__.py @@ -11,6 +11,7 @@ import sys __praw_hash__ = 'a632ff005fc09e74a8d3d276adc10aa92638962c' +__praw_bundled__ = True try: @@ -21,3 +22,4 @@ except ImportError: msg = 'Invalid PRAW version {0}, exiting'.format(praw.__version__) raise RuntimeError(msg) sys.modules['%s.praw' % __name__] = praw + __praw_bundled__ = False diff --git a/tests/conftest.py b/tests/conftest.py index 2eabd8b..c14864e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -174,7 +174,7 @@ def reddit(vcr, request): os.remove(filename) with vcr.use_cassette(cassette_name): - with patch('praw.Reddit.get_access_information'): + with patch('rtv.packages.praw.Reddit.get_access_information'): reddit = praw.Reddit(user_agent='rtv test suite', decode_html_entities=False, disable_update_check=True) diff --git a/tests/test_packages.py b/tests/test_packages.py new file mode 100644 index 0000000..5e069ee --- /dev/null +++ b/tests/test_packages.py @@ -0,0 +1,8 @@ +from rtv import packages + + +def test_praw3_package(): + # Sanity check that the package was installed + assert packages.praw + assert len(packages.__praw_hash__) == 40 + assert packages.__praw_bundled__ is True diff --git a/tests/test_submission.py b/tests/test_submission.py index 49976d9..34778af 100644 --- a/tests/test_submission.py +++ b/tests/test_submission.py @@ -230,9 +230,9 @@ def test_submission_vote(submission_page, refresh_token): submission_page.oauth.authorize() # Test voting on the submission - with mock.patch('praw.objects.Submission.upvote') as upvote, \ - mock.patch('praw.objects.Submission.downvote') as downvote, \ - mock.patch('praw.objects.Submission.clear_vote') as clear_vote: + with mock.patch('rtv.packages.praw.objects.Submission.upvote') as upvote, \ + mock.patch('rtv.packages.praw.objects.Submission.downvote') as downvote, \ + mock.patch('rtv.packages.praw.objects.Submission.clear_vote') as clear_vote: data = submission_page.content.get(submission_page.nav.absolute_index) @@ -279,8 +279,8 @@ def test_submission_save(submission_page, refresh_token): submission_page.oauth.authorize() # Test save on the submission - with mock.patch('praw.objects.Submission.save') as save, \ - mock.patch('praw.objects.Submission.unsave') as unsave: + with mock.patch('rtv.packages.praw.objects.Submission.save') as save, \ + mock.patch('rtv.packages.praw.objects.Submission.unsave') as unsave: data = submission_page.content.get(submission_page.nav.absolute_index) @@ -311,8 +311,8 @@ def test_submission_comment_save(submission_page, terminal, refresh_token): submission_page.controller.trigger('j') # Test save on the coment submission - with mock.patch('praw.objects.Comment.save') as save, \ - mock.patch('praw.objects.Comment.unsave') as unsave: + with mock.patch('rtv.packages.praw.objects.Comment.save') as save, \ + mock.patch('rtv.packages.praw.objects.Comment.unsave') as unsave: data = submission_page.content.get(submission_page.nav.absolute_index) @@ -339,8 +339,8 @@ def test_submission_comment(submission_page, terminal, refresh_token): submission_page.oauth.authorize() # Leave a comment - with mock.patch('praw.objects.Submission.add_comment') as add_comment, \ - mock.patch.object(terminal, 'open_editor') as open_editor, \ + with mock.patch('rtv.packages.praw.objects.Submission.add_comment') as add_comment, \ + mock.patch.object(terminal, 'open_editor') as open_editor, \ mock.patch('time.sleep'): open_editor.return_value.__enter__.return_value = 'comment text' submission_page.controller.trigger('c') @@ -371,8 +371,8 @@ def test_submission_delete(submission_page, terminal, refresh_token): # Spoof the author and try to delete again data = submission_page.content.get(submission_page.nav.absolute_index) data['author'] = submission_page.reddit.user.name - with mock.patch('praw.objects.Comment.delete') as delete, \ - mock.patch.object(terminal.stdscr, 'getch') as getch, \ + with mock.patch('rtv.packages.praw.objects.Comment.delete') as delete, \ + mock.patch.object(terminal.stdscr, 'getch') as getch, \ mock.patch('time.sleep'): getch.return_value = ord('y') submission_page.controller.trigger('d') @@ -395,8 +395,8 @@ def test_submission_edit(submission_page, terminal, refresh_token): # Spoof the submission and try to edit again data = submission_page.content.get(submission_page.nav.absolute_index) data['author'] = submission_page.reddit.user.name - with mock.patch('praw.objects.Submission.edit') as edit, \ - mock.patch.object(terminal, 'open_editor') as open_editor, \ + with mock.patch('rtv.packages.praw.objects.Submission.edit') as edit, \ + mock.patch.object(terminal, 'open_editor') as open_editor, \ mock.patch('time.sleep'): open_editor.return_value.__enter__.return_value = 'submission text' @@ -411,7 +411,7 @@ def test_submission_edit(submission_page, terminal, refresh_token): # Spoof the author and edit the comment data = submission_page.content.get(submission_page.nav.absolute_index) data['author'] = submission_page.reddit.user.name - with mock.patch('praw.objects.Comment.edit') as edit, \ + with mock.patch('rtv.packages.praw.objects.Comment.edit') as edit, \ mock.patch.object(terminal, 'open_editor') as open_editor, \ mock.patch('time.sleep'): open_editor.return_value.__enter__.return_value = 'comment text' diff --git a/tests/test_terminal.py b/tests/test_terminal.py index 32ca996..67f6ee4 100644 --- a/tests/test_terminal.py +++ b/tests/test_terminal.py @@ -30,7 +30,9 @@ def test_terminal_properties(terminal, config): assert isinstance(terminal.guilded[0], six.text_type) terminal._display = None - with mock.patch.dict('os.environ', {'DISPLAY': ''}): + with mock.patch('rtv.terminal.sys') as sys, \ + mock.patch.dict('os.environ', {'DISPLAY': ''}): + sys.platform = 'linux' assert terminal.display is False terminal._display = None