diff --git a/tests/fake_env.py b/tests/fake_env.py index 1e7c3bf..71ef8f5 100644 --- a/tests/fake_env.py +++ b/tests/fake_env.py @@ -5,8 +5,11 @@ import shutil import glob import dotdot -from pyfakefs import (fake_filesystem, fake_filesystem_shutil, - fake_filesystem_glob, fake_filesystem_unittest) + +from pyfakefs import fake_filesystem, fake_filesystem_unittest + +# from pyfakefs import (fake_filesystem, fake_filesystem_shutil, +# fake_filesystem_glob, fake_filesystem_unittest) from pubs.p3 import input, _fake_stdio, _get_fake_stdio_ucontent from pubs import content, filebroker @@ -20,27 +23,26 @@ real_shutil = shutil real_glob = glob real_io = io - -def copy_dir(fs, real_dir, fake_dir=None): - """Copy all the data directory into the fake fs""" - if fake_dir is None: - fake_dir = real_dir - - for filename in real_os.listdir(real_dir): - real_path = real_os.path.join(real_dir, filename) - fake_path = os.path.join(fake_dir, filename) - if real_os.path.isfile(real_path): - _, ext = real_os.path.splitext(filename) - if ext in ['.yaml', '.bib', '.txt', '.md']: - with real_io.open(real_path, 'r', encoding='utf-8') as f: - fs.CreateFile(os.path.abspath(fake_path), contents=f.read()) - else: - with real_io.open(real_path, 'rb') as f: - fs.CreateFile(fake_path, contents=f.read()) - - if real_os.path.isdir(real_path): - fs.CreateDirectory(fake_path) - copy_dir(fs, real_path, fake_path) +# def copy_dir(fs, real_dir, fake_dir=None): +# """Copy all the data directory into the fake fs""" +# if fake_dir is None: +# fake_dir = real_dir +# +# for filename in real_os.listdir(real_dir): +# real_path = real_os.path.join(real_dir, filename) +# fake_path = os.path.join(fake_dir, filename) +# if real_os.path.isfile(real_path): +# _, ext = real_os.path.splitext(filename) +# if ext in ['.yaml', '.bib', '.txt', '.md']: +# with real_io.open(real_path, 'r', encoding='utf-8') as f: +# fs.CreateFile(os.path.abspath(fake_path), contents=f.read()) +# else: +# with real_io.open(real_path, 'rb') as f: +# fs.CreateFile(fake_path, contents=f.read()) +# +# if real_os.path.isdir(real_path): +# fs.CreateDirectory(fake_path) +# copy_dir(fs, real_path, fake_path) # redirecting output @@ -108,8 +110,11 @@ class FakeInput(): class TestFakeFs(fake_filesystem_unittest.TestCase): def setUp(self): + self.rootpath = os.path.dirname(__file__) self.setUpPyfakefs() - self.fs.CreateDirectory(os.path.expanduser('~')) + self.fs.CreateDirectory(self.rootpath) + os.chdir(self.rootpath) + def reset_fs(self): self._stubber.tearDown() # renew the filesystem diff --git a/tests/requirements.txt b/tests/requirements.txt index d8d89cf..d8ab57e 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,4 +1,4 @@ # those are the additional packages required to run the tests six --e git+http://github.com/humm/pyfakefs@two_fixes#egg=pyfakefs +git+http://github.com/jmcgeheeiv/pyfakefs#egg=pyfakefs ddt diff --git a/tests/test_databroker.py b/tests/test_databroker.py index 7cb54d3..0723421 100644 --- a/tests/test_databroker.py +++ b/tests/test_databroker.py @@ -16,6 +16,7 @@ class TestDataBroker(fake_env.TestFakeFs): def test_databroker(self): + ende = endecoder.EnDecoder() page99_metadata = ende.decode_metadata(str_fixtures.metadata_raw0) page99_bibentry = ende.decode_bibdata(str_fixtures.bibtex_raw0) @@ -48,9 +49,9 @@ class TestDataBroker(fake_env.TestFakeFs): for db_class in [databroker.DataBroker, datacache.DataCache]: self.reset_fs() - fake_env.copy_dir(self.fs, os.path.join(os.path.dirname(__file__), 'testrepo'), 'repo') + self.fs.add_real_directory(os.path.join(self.rootpath, 'testrepo'), read_only=False) - db = db_class('repo', 'repo/doc', create=False) + db = db_class('testrepo', 'testrepo/doc', create=False) self.assertEqual(db.pull_bibentry('Page99'), page99_bibentry) @@ -66,8 +67,8 @@ class TestDataBroker(fake_env.TestFakeFs): db.pull_metadata('citekey') db.add_doc('Larry99', 'docsdir://Page99.pdf') - self.assertTrue(content.check_file('repo/doc/Page99.pdf', fail=False)) - self.assertTrue(content.check_file('repo/doc/Larry99.pdf', fail=False)) + self.assertTrue(content.check_file('testrepo/doc/Page99.pdf', fail=False)) + self.assertTrue(content.check_file('testrepo/doc/Larry99.pdf', fail=False)) db.remove_doc('docsdir://Page99.pdf') diff --git a/tests/test_datacache.py b/tests/test_datacache.py index 40641d6..7e95f81 100644 --- a/tests/test_datacache.py +++ b/tests/test_datacache.py @@ -105,10 +105,10 @@ class TestCacheEntrySet(unittest.TestCase): self.databroker_meta.filebroker.mtime = time.time() - 1.1 value = self.metacache.pull('a') self.assertEqual(value, 'c') - self.databroker_meta.filebroker.mtime = time.time() self.databroker_bib.bib = 'b' - self.databroker_bib.filebroker.mtime = time.time() - 1.1 + self.databroker_bib.filebroker.mtime = time.time() self.bibcache.push_to_cache('a', 'c') + self.databroker_bib.filebroker.mtime = time.time() - 1.1 value = self.bibcache.pull('a') self.assertEqual(value, 'c') @@ -142,3 +142,7 @@ class TestCacheEntrySet(unittest.TestCase): self.metacache.push_to_cache('a', 'b') self.databroker_meta.filebroker.mtime = time.time() - 1.1 self.assertFalse(self.metacache._is_outdated('a')) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_filebroker.py b/tests/test_filebroker.py index 26846d4..b4b4d9d 100644 --- a/tests/test_filebroker.py +++ b/tests/test_filebroker.py @@ -26,7 +26,7 @@ class TestFileBroker(fake_env.TestFakeFs): def test_existing_data(self): - fake_env.copy_dir(self.fs, os.path.join(os.path.dirname(__file__), 'testrepo'), 'testrepo') + self.fs.add_real_directory(os.path.join(self.rootpath, 'testrepo'), read_only=False) fb = filebroker.FileBroker('testrepo', create = True) bib_content = content.read_text_file('testrepo/bib/Page99.bib') @@ -84,7 +84,7 @@ class TestDocBroker(fake_env.TestFakeFs): def test_doccopy(self): - fake_env.copy_dir(self.fs, os.path.join(os.path.dirname(__file__), 'data'), 'data') + self.fs.add_real_directory(os.path.join(self.rootpath, 'data'), read_only=False) fb = filebroker.FileBroker('testrepo', create = True) docb = filebroker.DocBroker('testrepo') diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 4c275d5..d1a2841 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -145,8 +145,11 @@ class DataCommandTestCase(CommandTestCase): def setUp(self, nsec_stat=True): super(DataCommandTestCase, self).setUp(nsec_stat=nsec_stat) - 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') + 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) + + # 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') # Actual tests @@ -181,7 +184,7 @@ class TestAdd(DataCommandTestCase): def test_add(self): cmds = ['pubs init', - 'pubs add /data/pagerank.bib -d /data/pagerank.pdf', + 'pubs add data/pagerank.bib -d data/pagerank.pdf', ] self.execute_cmds(cmds) bib_dir = os.path.join(self.default_pubs_dir, 'bib') @@ -193,7 +196,7 @@ class TestAdd(DataCommandTestCase): def test_add_bibutils(self): cmds = ['pubs init', - 'pubs add /bibexamples/bibutils.bib', + 'pubs add bibexamples/bibutils.bib', ] self.execute_cmds(cmds) bib_dir = os.path.join(self.default_pubs_dir, 'bib') @@ -201,14 +204,14 @@ class TestAdd(DataCommandTestCase): def test_add2(self): cmds = ['pubs init -p /not_default', - 'pubs add /data/pagerank.bib -d /data/pagerank.pdf', + 'pubs add data/pagerank.bib -d data/pagerank.pdf', ] self.execute_cmds(cmds) self.assertEqual(set(os.listdir('/not_default/doc')), {'Page99.pdf'}) def test_add_citekey(self): cmds = ['pubs init', - 'pubs add -k CustomCitekey /data/pagerank.bib', + 'pubs add -k CustomCitekey data/pagerank.bib', ] self.execute_cmds(cmds) bib_dir = os.path.join(self.default_pubs_dir, 'bib') @@ -219,14 +222,14 @@ class TestAdd(DataCommandTestCase): "utf-8 citekeys are not supported yet.\n" "See https://github.com/pubs/pubs/issues/28 for details.") # actually not checked cmds = ['pubs init', - ('pubs add /bibexamples/utf8.bib', [], '', err), + ('pubs add bibexamples/utf8.bib', [], '', err), ] with self.assertRaises(FakeSystemExit): self.execute_cmds(cmds) def test_add_doc_nocopy_does_not_copy(self): cmds = ['pubs init', - 'pubs add /data/pagerank.bib --link -d /data/pagerank.pdf', + 'pubs add data/pagerank.bib --link -d data/pagerank.pdf', ] self.execute_cmds(cmds) self.assertEqual(os.listdir( @@ -235,15 +238,15 @@ class TestAdd(DataCommandTestCase): def test_add_move_removes_doc(self): cmds = ['pubs init', - 'pubs add /data/pagerank.bib --move -d /data/pagerank.pdf', + 'pubs add data/pagerank.bib --move -d data/pagerank.pdf', ] self.execute_cmds(cmds) - self.assertFalse(os.path.exists('/data/pagerank.pdf')) + self.assertFalse(os.path.exists('data/pagerank.pdf')) def test_add_twice_fails(self): cmds = ['pubs init', - 'pubs add /data/pagerank.bib', - 'pubs add -k Page99 /data/turing1950.bib', + 'pubs add data/pagerank.bib', + 'pubs add -k Page99 data/turing1950.bib', ] with self.assertRaises(FakeSystemExit): self.execute_cmds(cmds) @@ -252,7 +255,7 @@ class TestAdd(DataCommandTestCase): @unittest.expectedFailure def test_leading_citekey_space(self): cmds = ['pubs init', - 'pubs add /bibexamples/leadingspace.bib', + 'pubs add bibexamples/leadingspace.bib', 'pubs rename LeadingSpace NoLeadingSpace', ] self.execute_cmds(cmds) @@ -263,22 +266,24 @@ class TestList(DataCommandTestCase): def test_list(self): cmds = ['pubs init -p /not_default2', 'pubs list', - 'pubs add /data/pagerank.bib -d /data/pagerank.pdf', + 'pubs add data/pagerank.bib -d data/pagerank.pdf', 'pubs list', ] outs = self.execute_cmds(cmds) self.assertEqual(0, len(outs[1].splitlines())) self.assertEqual(1, len(outs[3].splitlines())) + @unittest.expectedFailure #FIXME pyfakefs's shutil.rmtree seems to have problems: submit an issue. def test_list_several_no_date(self): - self.execute_cmds(['pubs init -p /testrepo']) + self.execute_cmds(['pubs init -p testrepo']) shutil.rmtree('testrepo') - testrepo = os.path.join(os.path.dirname(__file__), 'testrepo') - fake_env.copy_dir(self.fs, testrepo, 'testrepo') + self.fs.add_real_directory(os.path.join(self.rootpath, 'testrepo'), read_only=False) + + #fake_env.copy_dir(self.fs, testrepo, 'testrepo') cmds = ['pubs list', 'pubs remove -f Page99', 'pubs list', - 'pubs add /data/pagerank.bib -d /data/pagerank.pdf', + 'pubs add data/pagerank.bib -d data/pagerank.pdf', 'pubs list', ] outs = self.execute_cmds(cmds) @@ -400,7 +405,7 @@ class TestUsecase(DataCommandTestCase): '[Page99] Page, Lawrence et al. "The PageRank Citation Ranking: Bringing Order to the Web." (1999) | network,search\n', ] - cmds = ['pubs init -p paper_first/', + cmds = ['pubs init -p /paper_first', 'pubs add -d data/pagerank.pdf data/pagerank.bib', 'pubs list', 'pubs tag', @@ -566,7 +571,7 @@ class TestUsecase(DataCommandTestCase): 'doc', 'Page99.pdf'))) # Also test that do not remove original - self.assertTrue(os.path.exists('/data/pagerank.pdf')) + self.assertTrue(os.path.exists('data/pagerank.pdf')) def test_doc_add_with_move(self): cmds = ['pubs init -p paper_second/', @@ -574,7 +579,7 @@ class TestUsecase(DataCommandTestCase): 'pubs doc add --move data/pagerank.pdf Page99' ] self.execute_cmds(cmds) - self.assertFalse(os.path.exists('/data/pagerank.pdf')) + self.assertFalse(os.path.exists('data/pagerank.pdf')) def test_doc_remove(self): cmds = ['pubs init',