]> git.phdru.name Git - ppu.git/blob - tests/ppu_tu.py
Refactor(tests): Exit with "name not found in PATH" in find_in_path
[ppu.git] / tests / ppu_tu.py
1 """PPU test utilities"""
2
3
4 import os
5 import shutil
6 import sys
7 from tempfile import mkdtemp
8
9
10 def setup():
11     global tmp_dir
12     tmp_dir = mkdtemp()
13     os.chdir(tmp_dir)
14
15
16 def teardown():
17     os.chdir(os.sep)  # To the root of the FS
18     shutil.rmtree(tmp_dir)
19
20
21 def find_in_path(name):
22     for path in os.environ["PATH"].split(os.pathsep):
23         path = path.strip('"')
24         test_prog_path = os.path.join(path, name)
25         if os.path.exists(test_prog_path):
26             return test_prog_path
27     sys.exit("Cannot find %s in %s" % (name, os.environ["PATH"]))
28
29
30 def create_files(files, subdirectory=None):
31     if subdirectory:
32         os.makedirs(subdirectory)
33     else:
34         subdirectory = ''
35     for fname in files:
36         with open(os.path.join(subdirectory, fname), 'w'):
37             pass
38
39
40 def assert_files_exist(files):
41     if isinstance(files, str):
42         files = [files]
43     for fname in files:
44         assert os.path.exists(fname)
45
46
47 def assert_files_not_exist(files):
48     if isinstance(files, str):
49         files = [files]
50     for fname in files:
51         assert not os.path.exists(fname)