def test_dispatch_when_filesystem_has_been_modified():
    # Create an empty www_root
    www = FilesystemTree()
    # Help the dispatchers that rely on `mtime`
    st = os.stat(www.root)
    os.utime(www.root, (st.st_atime - 10, st.st_mtime - 10))
    # Initialize the dispatchers
    dispatchers = []
    for dispatcher_class in DISPATCHER_CLASSES:
        dispatchers.append(dispatcher_class(
            www_root    = www.root,
            is_dynamic  = lambda n: n.endswith('.spt'),
            indices     = ['index.html'],
            typecasters = {},
        ))
        dispatchers[-1].build_dispatch_tree()
    # Now add an index file and try to dispatch
    www.mk(('index.html', 'Greetings, program!'))
    for dispatcher in dispatchers:
        print("Attempting dispatch with", dispatcher.__class__.__name__)
        result = dispatcher.dispatch('/', [''])
        if dispatcher.__class__.__name__ == 'UserlandDispatcher':
            assert result.status == DispatchStatus.unindexed
            assert result.match == www.root + os.path.sep
        else:
            assert result.status == DispatchStatus.okay
            assert result.match == www.resolve('index.html')
        assert result.wildcards is None
        assert result.extension is None
        assert result.canonical is None
def test_dispatcher_returns_a_result(dispatcher_class):
    www = FilesystemTree()
    www.mk(('index.html', 'Greetings, program!'),)
    dispatcher = dispatcher_class(
        www_root    = www.root,
        is_dynamic  = lambda n: n.endswith('.spt'),
        indices     = ['index.html'],
        typecasters = {},
    )
    dispatcher.build_dispatch_tree()
    result = dispatcher.dispatch('/', [''])
    assert result.status == DispatchStatus.okay
    assert result.match == os.path.join(www.root, 'index.html')
    assert result.wildcards == None
Exemple #3
0
def test_context_manager_removes_on_clean_context_exit():
    ftname = None
    with FilesystemTree() as ft:
        ft.mk(('file.txt', 'some text'))
        ftname = ft.resolve('file.txt')
        assert os.path.exists(ftname)
    assert not os.path.exists(ftname)
Exemple #4
0
def test_encoding_utf8_via_instance_attribute():
    try:
        fs = FilesystemTree(encoding='cp1140')
        fs.encoding = 'utf8'
        fs.mk(('file.txt', A_UNICODE))
        contents = open(fs.resolve('file.txt'), 'rb').read()
        assert contents == AS_UTF8
    finally:
        fs.remove()
Exemple #5
0
def test_sd_true_via_instance_attribute():
    try:
        fs = FilesystemTree(should_dedent=False)
        fs.should_dedent = True
        fs.mk(('some/dir/file.txt', '    Greetings, program!'))
        contents = open(fs.resolve('some/dir/file.txt')).read()
        assert contents == 'Greetings, program!'
    finally:
        fs.remove()
Exemple #6
0
class RunDeployHooks(DeployHooksHarness):
    def setUp(self):
        self.ft = FilesystemTree()
        self.db.run('DROP TABLE IF EXISTS foo;')

    def tearDown(self):
        self.ft.remove()
        self.db.run('DROP TABLE IF EXISTS foo;')

    def test_runs_deploy_hooks(self):
        self.ft.mk(('before.sql', 'CREATE TABLE foo (bar int);'),
                   ('after.py', AFTER_PY),
                   ('after.sql', 'ALTER TABLE foo RENAME COLUMN bar TO baz;'))

        raises(ProgrammingError, self.db.all, 'SELECT name FROM foo')
        self.run_deploy_hooks(_deploy_dir=self.ft.root)
        assert self.db.all('SELECT baz FROM foo ORDER BY baz') == [42, 537]
Exemple #7
0
def test_sd_false_via_constructor():
    try:
        fs = FilesystemTree(should_dedent=False)
        fs.mk(('some/dir/file.txt', '    Greetings, program!'))
        contents = open(fs.resolve('some/dir/file.txt')).read()
        assert contents == '    Greetings, program!'
    finally:
        fs.remove()
Exemple #8
0
def test_encoding_cp1140_via_constructor():
    try:
        fs = FilesystemTree(encoding='cp1140')
        fs.mk(('file.txt', A_UNICODE))
        contents = open(fs.resolve('file.txt'), 'rb').read()
        assert contents == AS_CP1140
    finally:
        fs.remove()
Exemple #9
0
def test_encoding_utf8_via_instance_attribute():
    try:
        fs = FilesystemTree(encoding='cp1140')
        fs.encoding = 'utf8'
        fs.mk(('file.txt', A_UNICODE))
        contents = open(fs.resolve('file.txt'), 'rb').read()
        assert contents == AS_UTF8
    finally:
        fs.remove()
Exemple #10
0
def test_sd_true_via_instance_attribute():
    try:
        fs = FilesystemTree(should_dedent=False)
        fs.should_dedent = True
        fs.mk(('some/dir/file.txt', '    Greetings, program!'))
        contents = open(fs.resolve('some/dir/file.txt')).read()
        assert contents == 'Greetings, program!'
    finally:
        fs.remove()
Exemple #11
0
def test_context_manager_doesnt_remove_on_exception_context_exit():
    excepted = False
    try:
        with FilesystemTree() as ft:
            ft.mk(('file.txt', 'some text'))
            ftname = ft.resolve('file.txt')
            assert os.path.exists(ftname)
            raise ValueError
    except ValueError:
        excepted = True
    assert excepted
    assert os.path.exists(ftname)
def test_dispatcher_returns_unindexed_for_unindexed_directory(dispatcher_class):
    www = FilesystemTree()
    dispatcher = dispatcher_class(
        www_root    = www.root,
        is_dynamic  = lambda n: n.endswith('.spt'),
        indices     = [],
        typecasters = {},
    )
    dispatcher.build_dispatch_tree()
    r = dispatcher.dispatch('/', [''])
    assert r.status == DispatchStatus.unindexed
    assert r.match == www.root + os.path.sep
Exemple #13
0
def test_sd_false_via_class_attribute():
    _reset = FilesystemTree.should_dedent
    try:
        FilesystemTree.should_dedent = False
        fs = FilesystemTree()
        fs.mk(('some/dir/file.txt', '    Greetings, program!'))
        contents = open(fs.resolve('some/dir/file.txt')).read()
        assert contents == '    Greetings, program!'
    finally:
        FilesystemTree.should_dedent = _reset
        fs.remove()
Exemple #14
0
def test_encoding_utf8_via_constructor():
    _reset = FilesystemTree.encoding
    try:
        FilesystemTree.encoding = 'cp1140'
        fs = FilesystemTree(encoding='utf8')
        fs.mk(('file.txt', A_UNICODE))
        contents = open(fs.resolve('file.txt'), 'rb').read()
        assert contents == AS_UTF8
    finally:
        FilesystemTree.encoding = _reset
        fs.remove()
Exemple #15
0
def test_encoding_cp1140_via_class_attribute():
    _reset = FilesystemTree.encoding
    try:
        FilesystemTree.encoding = 'cp1140'
        fs = FilesystemTree()
        fs.mk(('file.txt', A_UNICODE))
        contents = open(fs.resolve('file.txt'), 'rb').read()
        assert contents == AS_CP1140
    finally:
        FilesystemTree.encoding = _reset
        fs.remove()
Exemple #16
0
def test_encoding_cp1140_via_constructor():
    try:
        fs = FilesystemTree(encoding='cp1140')
        fs.mk(('file.txt', A_UNICODE))
        contents = open(fs.resolve('file.txt'), 'rb').read()
        assert contents == AS_CP1140
    finally:
        fs.remove()
Exemple #17
0
def test_sd_false_via_constructor():
    try:
        fs = FilesystemTree(should_dedent=False)
        fs.mk(('some/dir/file.txt', '    Greetings, program!'))
        contents = open(fs.resolve('some/dir/file.txt')).read()
        assert contents == '    Greetings, program!'
    finally:
        fs.remove()
Exemple #18
0
def test_encoding_utf8_via_constructor():
    _reset = FilesystemTree.encoding
    try:
        FilesystemTree.encoding = 'cp1140'
        fs = FilesystemTree(encoding='utf8')
        fs.mk(('file.txt', A_UNICODE))
        contents = open(fs.resolve('file.txt'), 'rb').read()
        assert contents == AS_UTF8
    finally:
        FilesystemTree.encoding = _reset
        fs.remove()
Exemple #19
0
def test_encoding_cp1140_via_class_attribute():
    _reset = FilesystemTree.encoding
    try:
        FilesystemTree.encoding = 'cp1140'
        fs = FilesystemTree()
        fs.mk(('file.txt', A_UNICODE))
        contents = open(fs.resolve('file.txt'), 'rb').read()
        assert contents == AS_CP1140
    finally:
        FilesystemTree.encoding = _reset
        fs.remove()
Exemple #20
0
def test_sd_false_via_class_attribute():
    _reset = FilesystemTree.should_dedent
    try:
        FilesystemTree.should_dedent = False
        fs = FilesystemTree()
        fs.mk(('some/dir/file.txt', '    Greetings, program!'))
        contents = open(fs.resolve('some/dir/file.txt')).read()
        assert contents == '    Greetings, program!'
    finally:
        FilesystemTree.should_dedent = _reset
        fs.remove()
Exemple #21
0
 def setUp(self):
     self.ft = FilesystemTree()
     self.db.run('DROP TABLE IF EXISTS foo;')
Exemple #22
0
 def __init__(self):
     self.fs = namedtuple('fs', 'www project')
     self.fs.www = FilesystemTree()
     self.fs.project = FilesystemTree()
     self._request_processor = None
Exemple #23
0
def fs():
    fs = FilesystemTree()
    yield fs
    fs.remove()
Exemple #24
0
    '/style.css',
    '/username',
    '/username/',
    '/nonexistent/file.php',
    '/foo/',
    '/foo/bar',
    '/foo/bar.txt',
    '/foo/bar/',
    '/foo/bar/baz',
]

times = {}
for dispatcher_class in dispatcher_classes:
    print("Timing", dispatcher_class.__name__)
    total_time = 0
    with FilesystemTree() as ft:
        ft.mk(*FILES)
        dispatcher = dispatcher_class(
            ft.root, is_dynamic, aspen.request_processor.default_indices,
            aspen.request_processor.typecasting.defaults)
        for url in URLS:
            dispatch = lambda: dispatcher.dispatch(url, url.split('/'))
            time = timeit(dispatch, number=1000)
            print(url, time)
            total_time += time
    times[dispatcher_class.__name__] = total_time
    print()

print("Totals:", json.dumps(times, indent=4, sort_keys=True), end='\n\n')

ordered = sorted(times.items(), key=lambda t: t[1])
Exemple #25
0
 def __init__(self):
     self.project = FilesystemTree()
     self.www = FilesystemTree()
Exemple #26
0
 def __init__(self):
     self.fs = namedtuple('fs', 'www project')
     self.fs.www = FilesystemTree()
     self.fs.project = FilesystemTree()
     self.client = Client(self.fs.www.root, self.fs.project.root)
Exemple #27
0
def test_it_can_be_instantiated():
    assert FilesystemTree().__class__.__name__ == 'FilesystemTree'
Exemple #28
0
def test_args_go_to_mk_not_root():
    fs = FilesystemTree('foo', 'bar')
    assert fs.root != 'foo'