diff --git a/pubs/commands/doc_cmd.py b/pubs/commands/doc_cmd.py index c8f34eb..97bfe3a 100644 --- a/pubs/commands/doc_cmd.py +++ b/pubs/commands/doc_cmd.py @@ -59,12 +59,12 @@ def parser(subparsers, conf): return doc_parser + def command(conf, args): ui = get_ui() rp = repo.Repository(conf) - # print(args) # ui.exit() @@ -73,8 +73,10 @@ def command(conf, args): paper = rp.pull_paper(citekey) if paper.docpath is not None and not args.force: - msg = ("The publication {} has already the document {} assigned." + os.linesep + - "Overwrite? ").format(color.dye_out(paper.citekey, 'citekey'), color.dye_out(paper.docpath, 'filepath')) + msg = ("The publication {} has already the document {} assigned.{newline}Overwrite? " + ).format(color.dye_out(paper.citekey, 'citekey'), + color.dye_out(paper.docpath, 'filepath'), + newline=os.linesep) if not ui.input_yn(question=msg, default='n'): ui.exit(0) else: @@ -144,13 +146,13 @@ def command(conf, args): if paper.docpath is None: ui.error('No document associated with the entry {}.'.format( - color.dye_err(citekey, 'citekey'))) + color.dye_err(citekey, 'citekey'))) ui.exit() if with_command is None: with_command = conf['main']['open_cmd'] - if with_command is None: # default in conf have not been changed - pass # TODO platform specific + if with_command is None: # default in conf have not been changed + pass # TODO platform specific try: docpath = content.system_path(rp.databroker.real_docpath(paper.docpath)) diff --git a/pubs/content.py b/pubs/content.py index c529b77..3842295 100644 --- a/pubs/content.py +++ b/pubs/content.py @@ -168,13 +168,15 @@ def move_content(source, target, overwrite=False): def copy_content(source, target, overwrite=False): - source = system_path(source) + source_is_url = content_type(source) == u'url' + if not source_is_url: + source = system_path(source) target = system_path(target) if source == target: return if not overwrite and os.path.exists(target): raise IOError(u'{} file exists.'.format(target)) - if content_type(source) == u'url': + if source_is_url: _dump_byte_url_content(source, target) else: shutil.copy(source, target) diff --git a/tests/fake_env.py b/tests/fake_env.py index f420efc..00ad0da 100644 --- a/tests/fake_env.py +++ b/tests/fake_env.py @@ -95,7 +95,6 @@ class TestFakeFs(fake_filesystem_unittest.TestCase): self.fs.CreateDirectory(self.rootpath) os.chdir(self.rootpath) - def reset_fs(self): self._stubber.tearDown() # renew the filesystem self.setUp() diff --git a/tests/test_usecase.py b/tests/test_usecase.py index f6e15de..506ed03 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -9,6 +9,7 @@ import shutil import six import ddt +from pyfakefs.fake_filesystem import FakeFileOpen import dotdot import fake_env @@ -156,6 +157,34 @@ class DataCommandTestCase(CommandTestCase): self.assertEqual(content.get_content(path), expected_content) +class URLContentTestCase(DataCommandTestCase): + """Mocks access to online files by using files in data directory. + """ + + def setUp(self): + super(URLContentTestCase, self).setUp() + self._original_get_byte_url_content = content._get_byte_url_content + self._original_url_exist = content.url_exists + content._get_byte_url_content = self.url_to_byte_content + content.url_exists = self.url_exists + + def _url_to_path(self, url): + return p3.urlparse(url).path + + def url_exists(self, url): + return self.fs.Exists(self._url_to_path(url)) + + def url_to_byte_content(self, url, ui=None): + path = self._url_to_path(url) + with FakeFileOpen(self.fs)('data' + path, 'rb') as f: + return f.read() + + def tearDown(self): + super(URLContentTestCase, self).tearDown() + content._get_byte_url_content = self._original_get_byte_url_content + content.url_exists = self._original_url_exist + + # Actual tests class TestAlone(CommandTestCase): @@ -184,7 +213,7 @@ class TestInit(CommandTestCase): self.assertTrue(os.path.isfile(self.default_conf_path)) -class TestAdd(DataCommandTestCase): +class TestAdd(URLContentTestCase): def test_add(self): cmds = ['pubs init', @@ -206,7 +235,7 @@ class TestAdd(DataCommandTestCase): bib_dir = os.path.join(self.default_pubs_dir, 'bib') self.assertEqual(set(os.listdir(bib_dir)), {'Page99.bib'}) - def test_add2(self): + def test_add_other_repository_path(self): cmds = ['pubs init -p /not_default', 'pubs add data/pagerank.bib -d data/pagerank.pdf', ] @@ -255,14 +284,26 @@ class TestAdd(DataCommandTestCase): with self.assertRaises(FakeSystemExit): self.execute_cmds(cmds) + def test_add_urls(self): + cmds = ['pubs init', + 'pubs add http://host.xxx/pagerank.bib ' + '-d http://host.xxx/pagerank.pdf', + ] + self.execute_cmds(cmds) + bib_dir = os.path.join(self.default_pubs_dir, 'bib') + self.assertEqual(set(os.listdir(bib_dir)), {'Page99.bib'}) + meta_dir = os.path.join(self.default_pubs_dir, 'meta') + self.assertEqual(set(os.listdir(meta_dir)), {'Page99.yaml'}) + doc_dir = os.path.join(self.default_pubs_dir, 'doc') + self.assertEqual(set(os.listdir(doc_dir)), {'Page99.pdf'}) @unittest.expectedFailure def test_leading_citekey_space(self): - cmds = ['pubs init', - 'pubs add bibexamples/leadingspace.bib', - 'pubs rename LeadingSpace NoLeadingSpace', - ] - self.execute_cmds(cmds) + cmds = ['pubs init', + 'pubs add bibexamples/leadingspace.bib', + 'pubs rename LeadingSpace NoLeadingSpace', + ] + self.execute_cmds(cmds) class TestList(DataCommandTestCase):