From c76c7607f9d23484cb2adb8baca1f3c97a554cd2 Mon Sep 17 00:00:00 2001 From: "Fabien C. Y. Benureau" Date: Wed, 1 Aug 2018 14:22:18 +0900 Subject: [PATCH] Upgrade pyfakefs to current version Fix #148. Also did some cleanup on the tests. --- pubs/uis.py | 3 +++ tests/fake_env.py | 19 ++++++++++++++----- tests/requirements.txt | 2 +- tests/test_bibstruct.py | 1 - tests/test_config.py | 2 +- tests/test_databroker.py | 5 ++--- tests/test_datacache.py | 1 - tests/test_pretty.py | 2 -- tests/test_usecase.py | 17 +++++++---------- 9 files changed, 28 insertions(+), 24 deletions(-) diff --git a/pubs/uis.py b/pubs/uis.py index c2a8bfc..326ab25 100644 --- a/pubs/uis.py +++ b/pubs/uis.py @@ -46,6 +46,9 @@ def _get_local_editor(): def _editor_input(editor, initial='', suffix='.tmp'): """Use an editor to get input""" str_initial = initial.encode('utf-8') # TODO: make it a configuration item + # tfile_name = '/tmp/pubs.tmp' + # with open(tfile_name, 'w') as temp_file: + # temp_file.write(str_initial) with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as temp_file: tfile_name = temp_file.name temp_file.write(str_initial) diff --git a/tests/fake_env.py b/tests/fake_env.py index 4f8af1b..d8404a3 100644 --- a/tests/fake_env.py +++ b/tests/fake_env.py @@ -98,11 +98,20 @@ class TestFakeFs(fake_filesystem_unittest.TestCase): def setUp(self): self.rootpath = os.path.abspath(os.path.dirname(__file__)) + self.homepath = os.path.expanduser('~') self.setUpPyfakefs() - self.fs.CreateDirectory(os.path.expanduser('~')) - self.fs.CreateDirectory(self.rootpath) - os.chdir(self.rootpath) + self.reset_fs() + # self.fs.create_dir(os.path.expanduser('~')) + # self.fs.create_dir(self.rootpath) + # os.chdir(self.rootpath) + def reset_fs(self): - self._stubber.tearDown() # renew the filesystem - self.setUp() + if self.fs.isdir(self.homepath): + self.fs.remove_object(self.homepath) + if self.fs.isdir(self.rootpath): + self.fs.remove_object(self.rootpath) + + self.fs.create_dir(os.path.expanduser('~')) + self.fs.create_dir(self.rootpath) + os.chdir(self.rootpath) diff --git a/tests/requirements.txt b/tests/requirements.txt index 2790d94..f3726e3 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,5 +1,5 @@ # those are the additional packages required to run the tests six -pyfakefs==3.3 +pyfakefs ddt mock diff --git a/tests/test_bibstruct.py b/tests/test_bibstruct.py index 7d56feb..05af028 100644 --- a/tests/test_bibstruct.py +++ b/tests/test_bibstruct.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -import os import unittest import copy diff --git a/tests/test_config.py b/tests/test_config.py index 23856a2..cecd75b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -2,7 +2,7 @@ import unittest import dotdot -from pubs.config import conf +# from pubs.config import conf # class TestConfig(unittest.TestCase): # diff --git a/tests/test_databroker.py b/tests/test_databroker.py index 57e5c66..041ac8e 100644 --- a/tests/test_databroker.py +++ b/tests/test_databroker.py @@ -5,8 +5,7 @@ import os import dotdot import fake_env -from pubs import content, filebroker, databroker, datacache -from pubs.config import conf +from pubs import content, databroker, datacache import str_fixtures from pubs import endecoder @@ -35,7 +34,7 @@ class TestDataBroker(fake_env.TestFakeFs): self.assertEqual(db.pull_metadata('citekey1'), page99_metadata) pulled = db.pull_bibentry('citekey1')['Page99'] - for key, value in pulled.items(): + for key in pulled.keys(): self.assertEqual(pulled[key], page99_bibentry['Page99'][key]) self.assertEqual(db.pull_bibentry('citekey1'), page99_bibentry) diff --git a/tests/test_datacache.py b/tests/test_datacache.py index 4709aca..bfb9223 100644 --- a/tests/test_datacache.py +++ b/tests/test_datacache.py @@ -3,7 +3,6 @@ import unittest import time import dotdot -import fake_env from pubs.datacache import CacheEntrySet diff --git a/tests/test_pretty.py b/tests/test_pretty.py index 0d2cf23..0c09211 100644 --- a/tests/test_pretty.py +++ b/tests/test_pretty.py @@ -1,10 +1,8 @@ from __future__ import unicode_literals import unittest -import os import dotdot -import fake_env from pubs import endecoder, pretty, color, config diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 8091f69..2b4199c 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -15,14 +15,12 @@ from pyfakefs.fake_filesystem import FakeFileOpen import dotdot import fake_env -from pubs import pubs_cmd, update, color, content, filebroker, uis, p3, endecoder +from pubs import pubs_cmd, color, content, uis, p3, endecoder from pubs.config import conf -import configobj import str_fixtures import fixtures -from pubs.commands import init_cmd, import_cmd # makes the tests very noisy PRINT_OUTPUT = False @@ -66,12 +64,12 @@ class TestFakeInput(unittest.TestCase): def test_editor_input(self): other_input = fake_env.FakeInput(['yes', 'no'], - module_list=[uis, color]) + module_list=[uis]) other_input.as_global() - self.assertEqual(uis._editor_input(), 'yes') - self.assertEqual(uis._editor_input(), 'no') + self.assertEqual(uis._editor_input('fake_editor'), 'yes') + self.assertEqual(uis._editor_input('fake_editor'), 'no') with self.assertRaises(fake_env.FakeInput.UnexpectedInput): - color.input() + uis._editor_input() class CommandTestCase(fake_env.TestFakeFs): @@ -82,7 +80,6 @@ class CommandTestCase(fake_env.TestFakeFs): def setUp(self, nsec_stat=True): super(CommandTestCase, self).setUp() os.stat_float_times(nsec_stat) - # self.fs = fake_env.create_fake_fs([content, filebroker, conf, init_cmd, import_cmd, configobj, update], nsec_stat=nsec_stat) self.default_pubs_dir = os.path.expanduser('~/.pubs') self.default_conf_path = os.path.expanduser('~/.pubsrc') @@ -178,7 +175,7 @@ class URLContentTestCase(DataCommandTestCase): return p3.urlparse(url).path def url_exists(self, url): - return self.fs.Exists(self._url_to_path(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) @@ -920,4 +917,4 @@ class TestCache(DataCommandTestCase): if __name__ == '__main__': - unittest.main() + unittest.main(verbosity=2)