summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozfile/tests/stubs.py
blob: 3c1bd472079ab85650042981b87f99c5ab20cc8f (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
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_empty_stub():
    tempdir = tempfile.mkdtemp()
    return tempdir


def create_stub(tempdir=None):
    """create a stub directory"""

    tempdir = tempdir or 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 = open(fullpath, "w")
            f.write(contents)
            f.close()
        return tempdir
    except Exception:
        try:
            shutil.rmtree(tempdir)
        except Exception:
            pass
        raise