Skip to content

Commit 734f96f

Browse files
committed
Refactor
1 parent ccc73a9 commit 734f96f

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

exec_test.py

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,65 @@
66
import re
77
from distutils.dir_util import copy_tree
88

9-
# テスト事前準備
9+
TEST_DIR = 'tests'
10+
TEST_TMP_DIR = './tmp_tests'
1011

11-
# テスト実行のためのtmpディレクトリを作成し、testsをコピーする
12-
os.mkdir('./tmp_tests')
13-
copy_tree('./tests/', './tmp_tests')
14-
execute_dir = []
15-
exit_status = 0
1612

17-
# 各テストファイル毎に、テスト対象ソースと共通ライブラリをテストファイルと同一のディレクトリに複製する。
18-
for name in glob.iglob('tmp_tests/**/test_*.py', recursive=True):
13+
def exec_test(target_dir):
14+
exit_status = 0
15+
try:
16+
subprocess.check_call(['green', '-vv', '--processes', '1', target_dir])
17+
except subprocess.CalledProcessError:
18+
exit_status = 1
19+
20+
return exit_status
21+
22+
23+
def copy_required_files(path):
1924
# テストの実行ディレクトリパスを取得
20-
test_dir = './' + name[:name.rfind('/')]
21-
# テストの実行ディレクトリを追加
22-
execute_dir.append(test_dir)
25+
test_dir = './' + path[:path.rfind('/')]
26+
2327
# テスト対象ソースを複製(対象ソースは tests 配下と同一構造の src ディレクトリ配下が対象)
2428
copy_tree(re.sub('^\./tmp_tests', './src', test_dir), test_dir)
29+
2530
# 共通ライブラリを複製
2631
copy_tree('./src/common', test_dir)
32+
2733
# 共通ライブラリを複製
2834
copy_tree('./tests/tests_common', test_dir)
2935

30-
for name in execute_dir:
31-
try:
32-
subprocess.check_call(['green', name])
33-
except subprocess.CalledProcessError:
34-
exit_status = 1
3536

36-
# tmpディレクトリは削除
37-
shutil.rmtree('./tmp_tests')
37+
def main():
38+
# テスト事前準備
39+
if os.path.isdir(TEST_TMP_DIR):
40+
shutil.rmtree(TEST_TMP_DIR)
41+
42+
# テスト実行のためのtmpディレクトリを作成し、testsをコピーする
43+
os.mkdir('./tmp_tests')
44+
copy_tree('./tests/', './tmp_tests')
45+
46+
# 引数でファイル名を受け取っている場合は変数にセットする
47+
target_file_path = sys.argv[1] if len(sys.argv) == 2 else None
48+
49+
exec_dir = ''
50+
51+
if target_file_path:
52+
# tmpフォルダ上の指定されたファイルのパスを取得
53+
exec_file = TEST_TMP_DIR + target_file_path[(target_file_path.find(TEST_DIR) + len(TEST_DIR)):]
54+
exec_dir = exec_file[:exec_file.rfind('/')]
55+
copy_required_files(exec_file)
56+
else:
57+
exec_dir = TEST_TMP_DIR
58+
59+
for name in glob.iglob('tmp_tests/**/test_*.py', recursive=True):
60+
copy_required_files(name)
61+
62+
result = exec_test(exec_dir)
63+
64+
# tmpディレクトリは削除
65+
66+
shutil.rmtree(TEST_TMP_DIR)
67+
sys.exit(result)
68+
3869

39-
sys.exit(exit_status)
70+
main()

0 commit comments

Comments
 (0)