Fix run_test_server Keep blob filename handling.
[arvados.git] / sdk / python / tests / arvados_testutil.py
1 #!/usr/bin/env python
2
3 import errno
4 import os
5 import shutil
6 import tempfile
7 import unittest
8
9 class ArvadosBaseTestCase(unittest.TestCase):
10     # This class provides common utility functions for our tests.
11
12     def setUp(self):
13         self._tempdirs = []
14
15     def tearDown(self):
16         for workdir in self._tempdirs:
17             shutil.rmtree(workdir, ignore_errors=True)
18
19     def make_tmpdir(self):
20         self._tempdirs.append(tempfile.mkdtemp())
21         return self._tempdirs[-1]
22
23     def data_file(self, filename):
24         try:
25             basedir = os.path.dirname(__file__)
26         except NameError:
27             basedir = '.'
28         return open(os.path.join(basedir, 'data', filename))
29
30     def build_directory_tree(self, tree):
31         tree_root = self.make_tmpdir()
32         for leaf in tree:
33             path = os.path.join(tree_root, leaf)
34             try:
35                 os.makedirs(os.path.dirname(path))
36             except OSError as error:
37                 if error.errno != errno.EEXIST:
38                     raise
39             with open(path, 'w') as tmpfile:
40                 tmpfile.write(leaf)
41         return tree_root
42
43     def make_test_file(self, text="test"):
44         testfile = tempfile.NamedTemporaryFile()
45         testfile.write(text)
46         testfile.flush()
47         return testfile