Example #1
0
def testfs(*args):
    """
    >>> testfs().listdir(".")
    ['f1', 'f2', 'd1']
    >>> testfs().listdir("d1")
    ['f1', 'f2']
    >>> testfs().rename("f1", "g1").listdir(".")
    ['f2', 'g1', 'd1']
    >>> testfs().open("f1").read()
    'hello world!!!'
    >>> testfs().rename("f1", "g1").open("g1").read()
    'hello world!!!'
    >>> testfs("contextopen", "f1").read()
    'hello world!!!'
    >>> testfs().glob("*f*")
    ['d1/f1', 'd1/f2', 'f1', 'f2']
    >>> testfs().isdir("d1")
    True
    >>> testfs().isdir("d1/f1")
    False
    """
    testfs = TestFS({"f1": "hello world!!!", "f2": "", "d1": "", "d1/f1": "# empty file", "d1/f2": "/* empty file */"})
    if args and args[0] == "contextopen":
        with testfs.open(args[1]) as fd:
            return fd
    else:
        return testfs
Example #2
0
    def test_rename_across_dirs(self):
        renametestfs = TestFS({
                "f1": "hello world!!!",
                "f2": "",
                "d1": "",
                "d1/f1": "# empty bash file",
                "d1/f2": "/* empty file */",
                "d1/s1/f3": "# empty python file",
                "d1/s1/bb": "# empty bash file",
                "d1/s3/f3": "# empty file",
                "d2/f1": "# another empty file",
                })

        renametestfs.rename("d1/f2", "d2/different.3")
        self.assert_(
            renametestfs.listdir("d2") == ['f1', 'different.3'],
            renametestfs.listdir("d2")
            )
        self.assert_(
            renametestfs.listdir("d1") == ["s3","f1","s1"],
            renametestfs.listdir("d1")
            )

        renametestfs.rename("d1/s1/f3", "d1/s3/aa")
        self.assert_(
            renametestfs.listdir("d1/s3") == ['aa', 'f3'],
            renametestfs.listdir("d1/s3")
            )
        self.assert_(
            renametestfs.listdir("d1/s1") == ["bb"],
            renametestfs.listdir("d1/s1")
            )
        print "\n", renametestfs.files
Example #3
0
 def setUp(self):
     self.testfs = TestFS({
             "f1": "hello world!!!",
             "f2": "",
             "d1": "",
             "d1/f1": "# empty file",
             "d1/f2": "/* empty file */",
             "d1/s1/f3": "# empty file",
             })
Example #4
0
File: tests.py Project: ipleten/md
 def setUp(self):
     from pyproxyfs import TestFS
     client = MdClient(
         "/var/maildir/testmaildir",
         filesystem=TestFS({
             "/var/maildir/testmaildir/cur/":
             "",
             "/var/maildir/testmaildir/new/1270028940.V801Ie8c95dM583793.hostname.domain.tld":
             TESTMSG1,
             "/var/maildir/testmaildir/.special/cur/":
             "",
             "/var/maildir/testmaildir/.special/new/":
             "",
             "/var/maildir/testmaildir/.special/new/1270028941.V801Ie8c95dM583793.hostname":
             TESTMSG2,
         }))
     self.client = client
Example #5
0
File: tests.py Project: ipleten/md
 def setUp(self):
     from pyproxyfs import TestFS
     folder = MdFolder(
         "testmaildir",
         base="/var/maildir",
         filesystem=TestFS({
             "/var/maildir/testmaildir/cur/":
             "",
             "/var/maildir/testmaildir/new/1270028940.V801Ie8c95dM583793.hostname":
             TESTMSG1,
             "/var/maildir/testmaildir/.special/cur/":
             "",
             "/var/maildir/testmaildir/.special/new/":
             "",
             "/var/maildir/testmaildir/.special/new/1270028940.V801Ie8c95dM583793.hostname":
             TESTMSG2,
         }))
     self.folder = folder
Example #6
0
class FilesystemTest(unittest.TestCase):
    def setUp(self):
        self.testfs = TestFS({
                "f1": "hello world!!!",
                "f2": "",
                "d1": "",
                "d1/f1": "# empty file",
                "d1/f2": "/* empty file */",
                "d1/s1/f3": "# empty file",
                })

    def test_listdir(self):
        self.assert_(
            self.testfs.listdir(".") == ['f1', 'f2', 'd1']
            )
        self.assert_(
            self.testfs.listdir("d1") == ['f1', 's1', 'f2']
            )
        self.assert_(
            self.testfs.listdir("d1/s1") == ['f3']
            )
        
    def test_rename(self):
        self.testfs.rename("f1", "g1")
        self.assert_(
            self.testfs.listdir(".") == ['f2', 'g1', 'd1']
            )

    def test_rename_into_dir(self):
        self.testfs.rename("f1", "d1/g1")
        self.assert_(
            self.testfs.listdir(".") == ['f2', 'd1']
            )
        self.assert_(
            self.testfs.listdir("d1") == ['f1', 's1', 'f2', 'g1'],
            self.testfs.listdir("d1")
            )

    def test_rename_across_dirs(self):
        renametestfs = TestFS({
                "f1": "hello world!!!",
                "f2": "",
                "d1": "",
                "d1/f1": "# empty bash file",
                "d1/f2": "/* empty file */",
                "d1/s1/f3": "# empty python file",
                "d1/s1/bb": "# empty bash file",
                "d1/s3/f3": "# empty file",
                "d2/f1": "# another empty file",
                })

        renametestfs.rename("d1/f2", "d2/different.3")
        self.assert_(
            renametestfs.listdir("d2") == ['f1', 'different.3'],
            renametestfs.listdir("d2")
            )
        self.assert_(
            renametestfs.listdir("d1") == ["s3","f1","s1"],
            renametestfs.listdir("d1")
            )

        renametestfs.rename("d1/s1/f3", "d1/s3/aa")
        self.assert_(
            renametestfs.listdir("d1/s3") == ['aa', 'f3'],
            renametestfs.listdir("d1/s3")
            )
        self.assert_(
            renametestfs.listdir("d1/s1") == ["bb"],
            renametestfs.listdir("d1/s1")
            )
        print "\n", renametestfs.files

    def test_open(self):
        self.assert_(
            self.testfs.open("f1").read() == 'hello world!!!'
            )

        self.testfs.rename("f1", "g1")
        self.assert_(
            self.testfs.open("g1").read() == 'hello world!!!'
            )

    def test_contextopen(self):
        with self.testfs.open("f1") as fd:
            self.assert_(fd.read() == 'hello world!!!')

    def test_glob(self):
        self.assert_(
            self.testfs.glob("*f*") == ['d1/f1', 'd1/f2', 'd1/s1/f3', 'f1', 'f2']
            )

    def test_status(self):
        self.assert_(self.testfs.isdir("d1"))
        self.assert_(self.testfs.isdir("d1/s1"))
        self.assert_(self.testfs.exists("d1/f1"))
        self.assert_(self.testfs.exists("d1"))
        self.assert_(self.testfs.exists("d1/s1/f3"))
        self.assert_(self.testfs.exists("d1/s1"))
        self.assert_(self.testfs.isdir("d1/s1"))

    def test_rm(self):
        self.testfs.remove("f1")
        self.assert_(not self.testfs.exists("f1"))
        
        self.testfs.remove("d1/f1")
        self.assert_(not self.testfs.exists("d1/f1"))
        self.assert_(self.testfs.exists("d1"))

    def test_show_conf_files(self):
        fs = TestFS({
            "somedir/a.cfg": "",
            "somedir/a.txt": "",
            "somedir/b.cfg": "",
            "somedir/run.py": ""
            })
        self.assert_(
            show_conf_files("somedir", filesystem=fs) == ['a.cfg', 'b.cfg']
            )