summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozfile/tests/stubs.py
blob: 06d79e7af13c891b521d8566a0c796b546fbc372 (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
import os
import shutil
import tempfile


# stub file paths
files = [('foo.txt',),
         ('foo', 'bar.txt',),
         ('foo', 'bar', 'fleem.txt',),
         ('foobar', 'fleem.txt',),
         ('bar.txt',),
         ('nested_tree', 'bar', 'fleem.txt',),
         ('readonly.txt',),
         ]


def create_stub():
    """create a stub directory"""

    tempdir = tempfile.mkdtemp()
    try:
        for path in files:
            fullpath = os.path.join(tempdir, *path)
            dirname = os.path.dirname(fullpath)
            if not os.path.exists(dirname):
                os.makedirs(dirname)
            contents = path[-1]
            f = file(fullpath, 'w')
            f.write(contents)
            f.close()
        return tempdir
    except Exception:
        try:
            shutil.rmtree(tempdir)
        except:
            pass
        raise