more robust fake_fs testing; added papers add test
This commit is contained in:
parent
13bd18cda9
commit
b4504278f2
@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# -*- coding:utf-8 -*-
|
# -*- coding:utf-8 -*-
|
||||||
|
|
||||||
import papers_cmd
|
from papers import papers_cmd
|
||||||
papers_cmd.execute()
|
papers_cmd.execute()
|
@ -26,24 +26,25 @@ cmds = collections.OrderedDict([
|
|||||||
('update', commands.update_cmd),
|
('update', commands.update_cmd),
|
||||||
])
|
])
|
||||||
|
|
||||||
config = configs.read_config()
|
|
||||||
ui = UI(config)
|
|
||||||
|
|
||||||
# Extend with plugin commands
|
|
||||||
plugin.load_plugins(config, ui, configs.get_plugins(config))
|
|
||||||
for p in plugin.get_plugins().values():
|
|
||||||
cmds.update(collections.OrderedDict([(p.name, p)]))
|
|
||||||
#
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="research papers repository")
|
|
||||||
subparsers = parser.add_subparsers(title="valid commands", dest="command")
|
|
||||||
|
|
||||||
for cmd_mod in cmds.values():
|
|
||||||
subparser = cmd_mod.parser(subparsers, config) # why do we return the subparser ?
|
|
||||||
|
|
||||||
def execute(raw_args = sys.argv):
|
def execute(raw_args = sys.argv):
|
||||||
|
config = configs.read_config()
|
||||||
|
ui = UI(config)
|
||||||
|
|
||||||
|
# Extend with plugin commands
|
||||||
|
plugin.load_plugins(config, ui, configs.get_plugins(config))
|
||||||
|
for p in plugin.get_plugins().values():
|
||||||
|
cmds.update(collections.OrderedDict([(p.name, p)]))
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description="research papers repository")
|
||||||
|
subparsers = parser.add_subparsers(title="valid commands", dest="command")
|
||||||
|
|
||||||
|
for cmd_mod in cmds.values():
|
||||||
|
subparser = cmd_mod.parser(subparsers, config) # why do we return the subparser ?
|
||||||
|
|
||||||
args = parser.parse_args(raw_args[1:])
|
args = parser.parse_args(raw_args[1:])
|
||||||
args.config = config
|
args.config = config
|
||||||
|
|
||||||
args.ui = ui
|
args.ui = ui
|
||||||
cmd = args.command
|
cmd = args.command
|
||||||
del args.command
|
del args.command
|
||||||
|
@ -4,33 +4,75 @@ import pkgutil
|
|||||||
|
|
||||||
import testenv
|
import testenv
|
||||||
import fake_filesystem
|
import fake_filesystem
|
||||||
|
import fake_filesystem_shutil
|
||||||
|
|
||||||
import papers
|
real_os = os
|
||||||
from papers import papers_cmd
|
real_open = open
|
||||||
|
|
||||||
|
fake_os, fake_open, fake_shutil = None, None
|
||||||
|
|
||||||
|
def _create_fake_fs():
|
||||||
|
global fake_os, fake_open, fake_shutil
|
||||||
|
|
||||||
def create_fake_fs():
|
|
||||||
fake_fs = fake_filesystem.FakeFilesystem()
|
fake_fs = fake_filesystem.FakeFilesystem()
|
||||||
fake_os = fake_filesystem.FakeOsModule(fake_fs)
|
fake_os = fake_filesystem.FakeOsModule(fake_fs)
|
||||||
fake_open = fake_filesystem.FakeFileOpen(fake_fs)
|
fake_open = fake_filesystem.FakeFileOpen(fake_fs)
|
||||||
fake_fs.CreateFile('/Users/fabien/bla')
|
fake_shutil = fake_filesystem_shutil.FakeShutilModule(fake_fs)
|
||||||
|
|
||||||
|
fake_fs.CreateDirectory(fake_os.path.expanduser('~'))
|
||||||
__builtins__['open'] = fake_open
|
__builtins__['open'] = fake_open
|
||||||
__builtins__['file'] = fake_open
|
__builtins__['file'] = fake_open
|
||||||
|
|
||||||
for importer, modname, ispkg in pkgutil.walk_packages(path=papers.__path__,
|
sys.modules['os'] = fake_os
|
||||||
prefix=papers.__name__+'.',
|
sys.modules['shutil'] = fake_shutil
|
||||||
onerror=lambda x: None):
|
|
||||||
|
import papers
|
||||||
|
for importer, modname, ispkg in pkgutil.walk_packages(
|
||||||
|
path=papers.__path__,
|
||||||
|
prefix=papers.__name__+'.',
|
||||||
|
onerror=lambda x: None):
|
||||||
md = __import__(modname, fromlist = 'dummy')
|
md = __import__(modname, fromlist = 'dummy')
|
||||||
md.os = fake_os
|
md.os = fake_os
|
||||||
|
md.shutil = fake_shutil
|
||||||
|
|
||||||
|
return fake_fs
|
||||||
|
|
||||||
class TestUseCases(unittest.TestCase):
|
class TestInit(unittest.TestCase):
|
||||||
|
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
create_fake_fs()
|
fs = _create_fake_fs()
|
||||||
papers_md.execute_papers('papers init -p paper_test2'.split())
|
from papers import papers_cmd
|
||||||
|
|
||||||
def test_init(self):
|
|
||||||
create_fake_fs()
|
|
||||||
papers_cmd.execute('papers init -p paper_test2'.split())
|
papers_cmd.execute('papers init -p paper_test2'.split())
|
||||||
|
self.assertEqual(set(fake_os.listdir('/paper_test2/')), {'bibdata', 'doc', 'meta', 'papers.yaml'})
|
||||||
|
|
||||||
|
class TestAdd(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_add(self):
|
||||||
|
|
||||||
|
fs = _create_fake_fs()
|
||||||
|
from papers import papers_cmd
|
||||||
|
papers_cmd.execute('papers init'.split())
|
||||||
|
|
||||||
|
with real_open('data/pagerank.bib', 'r') as f:
|
||||||
|
fs.CreateFile('/data/page.bib', contents = f.read())
|
||||||
|
with real_open('data/pagerank.pdf', 'r') as f:
|
||||||
|
fs.CreateFile('/data/page.pdf', contents = f.read())
|
||||||
|
|
||||||
|
papers_cmd.execute('papers add -b /data/page.bib -d /data/page.pdf'.split())
|
||||||
|
|
||||||
|
|
||||||
|
class TestAdd2(unittest.TestCase):
|
||||||
|
def test_add2(self):
|
||||||
|
|
||||||
|
fs = _create_fake_fs()
|
||||||
|
from papers import papers_cmd
|
||||||
|
papers_cmd.execute('papers init -p /not_default'.split())
|
||||||
|
|
||||||
|
with real_open('data/pagerank.bib', 'r') as f:
|
||||||
|
fs.CreateFile('/data/page.bib', contents = f.read())
|
||||||
|
with real_open('data/pagerank.pdf', 'r') as f:
|
||||||
|
fs.CreateFile('/data/page.pdf', contents = f.read())
|
||||||
|
|
||||||
|
papers_cmd.execute('papers add -b /data/page.bib -d /data/page.pdf'.split())
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user