testing/mozbase/mozfile/tests/stubs.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mozbase/mozfile/tests/stubs.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,37 @@
     1.4 +import os
     1.5 +import shutil
     1.6 +import tempfile
     1.7 +
     1.8 +
     1.9 +# stub file paths
    1.10 +files = [('foo.txt',),
    1.11 +         ('foo', 'bar.txt',),
    1.12 +         ('foo', 'bar', 'fleem.txt',),
    1.13 +         ('foobar', 'fleem.txt',),
    1.14 +         ('bar.txt',),
    1.15 +         ('nested_tree', 'bar', 'fleem.txt',),
    1.16 +         ('readonly.txt',),
    1.17 +         ]
    1.18 +
    1.19 +
    1.20 +def create_stub():
    1.21 +    """create a stub directory"""
    1.22 +
    1.23 +    tempdir = tempfile.mkdtemp()
    1.24 +    try:
    1.25 +        for path in files:
    1.26 +            fullpath = os.path.join(tempdir, *path)
    1.27 +            dirname = os.path.dirname(fullpath)
    1.28 +            if not os.path.exists(dirname):
    1.29 +                os.makedirs(dirname)
    1.30 +            contents = path[-1]
    1.31 +            f = file(fullpath, 'w')
    1.32 +            f.write(contents)
    1.33 +            f.close()
    1.34 +        return tempdir
    1.35 +    except Exception, e:
    1.36 +        try:
    1.37 +            shutil.rmtree(tempdir)
    1.38 +        except:
    1.39 +            pass
    1.40 +        raise e

mercurial