summaryrefslogtreecommitdiff
path: root/fs/tests/test_utils.py
blob: d2e435f4aba166ba5a39747b5479968107eada38 (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
112
113
114
115
import unittest

from fs.tempfs import TempFS
from fs.memoryfs import MemoryFS
from fs import utils

from six import b

class TestUtils(unittest.TestCase):
    
    def _make_fs(self, fs):
        fs.setcontents("f1", b("file 1"))
        fs.setcontents("f2", b("file 2"))
        fs.setcontents("f3", b("file 3"))
        fs.makedir("foo/bar", recursive=True)
        fs.setcontents("foo/bar/fruit", b("apple"))
        
    def _check_fs(self, fs):
        self.assert_(fs.isfile("f1"))
        self.assert_(fs.isfile("f2"))
        self.assert_(fs.isfile("f3"))
        self.assert_(fs.isdir("foo/bar"))
        self.assert_(fs.isfile("foo/bar/fruit"))
        self.assertEqual(fs.getcontents("f1", "rb"), b("file 1"))
        self.assertEqual(fs.getcontents("f2", "rb"), b("file 2"))
        self.assertEqual(fs.getcontents("f3", "rb"), b("file 3"))
        self.assertEqual(fs.getcontents("foo/bar/fruit", "rb"), b("apple"))        
        
    def test_copydir_root(self):
        """Test copydir from root"""
        fs1 = MemoryFS()
        self._make_fs(fs1)        
        fs2 = MemoryFS()
        utils.copydir(fs1, fs2)        
        self._check_fs(fs2)
                
        fs1 = TempFS()
        self._make_fs(fs1)        
        fs2 = TempFS()
        utils.copydir(fs1, fs2)        
        self._check_fs(fs2)
    
    def test_copydir_indir(self):
        """Test copydir in a directory"""        
        fs1 = MemoryFS()
        fs2 = MemoryFS()
        self._make_fs(fs1)        
        utils.copydir(fs1, (fs2, "copy"))        
        self._check_fs(fs2.opendir("copy"))

        fs1 = TempFS()
        fs2 = TempFS()
        self._make_fs(fs1)        
        utils.copydir(fs1, (fs2, "copy"))        
        self._check_fs(fs2.opendir("copy"))
    
    def test_movedir_indir(self):
        """Test movedir in a directory"""        
        fs1 = MemoryFS()
        fs2 = MemoryFS()
        fs1sub = fs1.makeopendir("from")
        self._make_fs(fs1sub)            
        utils.movedir((fs1, "from"), (fs2, "copy"))        
        self.assert_(not fs1.exists("from"))     
        self._check_fs(fs2.opendir("copy"))

        fs1 = TempFS()
        fs2 = TempFS()
        fs1sub = fs1.makeopendir("from")
        self._make_fs(fs1sub)            
        utils.movedir((fs1, "from"), (fs2, "copy"))
        self.assert_(not fs1.exists("from"))      
        self._check_fs(fs2.opendir("copy"))
        
    def test_movedir_root(self):
        """Test movedir to root dir"""        
        fs1 = MemoryFS()
        fs2 = MemoryFS()
        fs1sub = fs1.makeopendir("from")
        self._make_fs(fs1sub)            
        utils.movedir((fs1, "from"), fs2)
        self.assert_(not fs1.exists("from"))     
        self._check_fs(fs2)

        fs1 = TempFS()
        fs2 = TempFS()
        fs1sub = fs1.makeopendir("from")
        self._make_fs(fs1sub)            
        utils.movedir((fs1, "from"), fs2)
        self.assert_(not fs1.exists("from"))        
        self._check_fs(fs2)
        
    def test_remove_all(self):
        """Test remove_all function"""
        fs = TempFS()
        fs.setcontents("f1", b("file 1"))
        fs.setcontents("f2", b("file 2"))
        fs.setcontents("f3", b("file 3"))
        fs.makedir("foo/bar", recursive=True)
        fs.setcontents("foo/bar/fruit", b("apple"))        
        fs.setcontents("foo/baz", b("baz"))
        
        utils.remove_all(fs, "foo/bar")
        self.assert_(not fs.exists("foo/bar/fruit"))
        self.assert_(fs.exists("foo/bar"))
        self.assert_(fs.exists("foo/baz"))
        utils.remove_all(fs,  "")
        self.assert_(not fs.exists("foo/bar/fruit"))
        self.assert_(not fs.exists("foo/bar/baz"))
        self.assert_(not fs.exists("foo/baz"))
        self.assert_(not fs.exists("foo"))
        self.assert_(not fs.exists("f1"))
        self.assert_(fs.isdirempty('/'))