summaryrefslogtreecommitdiff
path: root/fs/tests/test_wrapfs.py
blob: 153026afbaae1c2e0ae9da08075bfe94a512701a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""

  fs.tests.test_wrapfs:  testcases for FS wrapper implementations

"""

import unittest
from fs.tests import FSTestCases, ThreadingTestCases

import os
import sys
import shutil
import tempfile

from fs import osfs
from fs.errors import * 
from fs.path import *
from fs.utils import remove_all
from fs import wrapfs

import six
from six import PY3, b

class TestWrapFS(unittest.TestCase, FSTestCases, ThreadingTestCases):
    
    #__test__ = False
    
    def setUp(self):
        self.temp_dir = tempfile.mkdtemp(u"fstest")
        self.fs = wrapfs.WrapFS(osfs.OSFS(self.temp_dir))

    def tearDown(self):
        shutil.rmtree(self.temp_dir)
        self.fs.close()

    def check(self, p):
        return os.path.exists(os.path.join(self.temp_dir, relpath(p)))


from fs.wrapfs.lazyfs import LazyFS
class TestLazyFS(unittest.TestCase, FSTestCases, ThreadingTestCases):
    
    def setUp(self):
        self.temp_dir = tempfile.mkdtemp(u"fstest")
        self.fs = LazyFS((osfs.OSFS,(self.temp_dir,)))

    def tearDown(self):
        shutil.rmtree(self.temp_dir)
        self.fs.close()

    def check(self, p):
        return os.path.exists(os.path.join(self.temp_dir, relpath(p)))


from fs.wrapfs.limitsizefs import LimitSizeFS
class TestLimitSizeFS(TestWrapFS):

    _dont_retest = TestWrapFS._dont_retest + ("test_big_file",)
    
    def setUp(self):
        super(TestLimitSizeFS,self).setUp()
        self.fs = LimitSizeFS(self.fs,1024*1024*2)  # 2MB limit

    def tearDown(self):
        remove_all(self.fs, "/")
        self.assertEquals(self.fs.cur_size,0)
        super(TestLimitSizeFS,self).tearDown()
        self.fs.close()

    def test_storage_error(self):
        total_written = 0
        for i in xrange(1024*2):
            try:
                total_written += 1030
                self.fs.setcontents("file %i" % i, b("C")*1030)
            except StorageSpaceError:
                self.assertTrue(total_written > 1024*1024*2)
                self.assertTrue(total_written < 1024*1024*2 + 1030)
                break
        else:
            self.assertTrue(False,"StorageSpaceError not raised")           


from fs.wrapfs.hidedotfilesfs import HideDotFilesFS
class TestHideDotFilesFS(unittest.TestCase):

    def setUp(self):
        self.temp_dir = tempfile.mkdtemp(u"fstest")
        open(os.path.join(self.temp_dir, u".dotfile"), 'w').close()
        open(os.path.join(self.temp_dir, u"regularfile"), 'w').close()
        os.mkdir(os.path.join(self.temp_dir, u".dotdir"))
        os.mkdir(os.path.join(self.temp_dir, u"regulardir"))
        self.fs = HideDotFilesFS(osfs.OSFS(self.temp_dir))

    def tearDown(self):
        shutil.rmtree(self.temp_dir)
        self.fs.close()

    def test_hidden(self):
        self.assertEquals(len(self.fs.listdir(hidden=False)), 2)
        self.assertEquals(len(list(self.fs.ilistdir(hidden=False))), 2)

    def test_nonhidden(self):
        self.assertEquals(len(self.fs.listdir(hidden=True)), 4)
        self.assertEquals(len(list(self.fs.ilistdir(hidden=True))), 4)

    def test_default(self):
        self.assertEquals(len(self.fs.listdir()), 2)
        self.assertEquals(len(list(self.fs.ilistdir())), 2)