From aaf2ed52c133c6e3103aa81fca5a0df212e1597b Mon Sep 17 00:00:00 2001 From: ksunden Date: Tue, 22 May 2018 10:31:11 -0500 Subject: [PATCH] Use mock to test url command --- tests/requirements.txt | 1 + tests/test_usecase.py | 53 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/tests/requirements.txt b/tests/requirements.txt index c3ee05b..2790d94 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -2,3 +2,4 @@ six pyfakefs==3.3 ddt +mock diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 7d98492..8273901 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -6,6 +6,7 @@ import os import re import sys import shutil +import mock import six import ddt @@ -23,7 +24,6 @@ import fixtures from pubs.commands import init_cmd, import_cmd - # makes the tests very noisy PRINT_OUTPUT = False CAPTURE_OUTPUT = True @@ -104,13 +104,14 @@ class CommandTestCase(fake_env.TestFakeFs): actual_cmd = cmd if not isinstance(cmd, p3.ustr): actual_cmd = cmd[0] - if len(cmd) == 2: # Inputs provided + if len(cmd) >= 2 and cmd[1] is not None: # Inputs provided inputs = cmd[1] - if len(cmd) == 3: # Expected output provided + if len(cmd) >= 3: # Expected output provided capture_output = True - expected_out = color.undye(cmd[2]) - if len(cmd) == 4: # Expected error output provided - expected_err = color.undye(cmd[3]) + if cmd[2] is not None: + expected_out = color.undye(cmd[2]) + if len(cmd) >= 4 and cmd[3] is not None: # Expected error output provided + expected_err = color.undye(cmd[3]) # Always set fake input: test should not ask unexpected user input input = fake_env.FakeInput(inputs, [content, uis, p3]) input.as_global() @@ -153,6 +154,7 @@ class DataCommandTestCase(CommandTestCase): super(DataCommandTestCase, self).setUp(nsec_stat=nsec_stat) self.fs.add_real_directory(os.path.join(self.rootpath, 'data'), read_only=False) self.fs.add_real_directory(os.path.join(self.rootpath, 'bibexamples'), read_only=False) + self.fs.CreateFile('/dev/null') # fake_env.copy_dir(self.fs, os.path.join(os.path.dirname(__file__), 'data'), 'data') # fake_env.copy_dir(self.fs, os.path.join(os.path.dirname(__file__), 'bibexamples'), 'bibexamples') @@ -497,6 +499,45 @@ class TestTag(DataCommandTestCase): with self.assertRaises(FakeSystemExit): self.execute_cmds(cmds) +class TestURL(DataCommandTestCase): + + def setUp(self): + super(TestURL, self).setUp() + init = ['pubs init', + 'pubs add data/pagerank.bib', + 'pubs add data/turing1950.bib', + 'pubs add data/martius.bib', + ] + #self.fs.add_real_file('/dev/null') + #webbrowser.register('true', None, webbrowser.GenericBrowser('/usr/bin/true'), -1) + self.execute_cmds(init) + + @mock.patch('webbrowser.open') + def test_url_open_one(self, wb_open): + cmds = [('pubs url Page99', 'qy'), + ] + correct = ['info: opening url http://ilpubs.stanford.edu:8090/422/\n', + ] + out = self.execute_cmds(cmds) + self.assertEqual(out, correct) + + @mock.patch('webbrowser.open') + def test_url_open_missing(self, wb_open): + cmds = [('pubs url turing1950computing', None, None, 'warning: turing1950computing has no url\n'), + ] + self.execute_cmds(cmds) + + @mock.patch('webbrowser.open') + def test_url_open_multiple(self, wb_open): + cmds = [('pubs url Page99 10.1371_journal.pone.0063400', 'qyqy'), + ] + correct = ['info: opening url http://ilpubs.stanford.edu:8090/422/\n' + + 'info: opening url http://dx.doi.org/10.1371%2Fjournal.pone.0063400\n', + ] + out = self.execute_cmds(cmds) + self.assertEqual(out, correct) + + class TestNote(DataCommandTestCase):