forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_executor.py
49 lines (37 loc) · 1.65 KB
/
test_executor.py
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
import os
import shutil
import tempfile
from unittest import TestCase
from dvc.executor import Executor, ExecutorError
from dvc.system import System
class TestExecutor(TestCase):
def setUp(self):
self.test_dir = System.get_long_path(tempfile.mkdtemp())
self.code_file = 'myfile.py'
fd = open(self.code_file, 'w')
fd.write('import sys\nsys.stdout.write("111")\nsys.stderr.write("222")\n')
fd.close()
def tearDown(self):
shutil.rmtree(self.test_dir)
if os.path.exists('myfile.py'):
os.remove('myfile.py')
def test_suppress_output(self):
output = Executor.exec_cmd_only_success(['python', self.code_file])
self.assertEqual(output, '111')
def test_output_to_std_console(self):
# This output "111" in stdout and "222" are not suppressible.
# You will see the outputs when the unit-test runs.
output = Executor.exec_cmd_only_success(['python', self.code_file], '-', '-')
self.assertEqual(output, '')
def test_custom_file_outputs(self):
stdout_file = os.path.join(self.test_dir, 'stdout.txt')
stderr_file = os.path.join(self.test_dir, 'stderr.txt')
output = Executor.exec_cmd_only_success(['python', self.code_file], stdout_file, stderr_file)
self.assertEqual(output, '')
self.assertEqual(open(stdout_file).read(), "111")
self.assertEqual(open(stderr_file).read(), "222")
os.remove(stdout_file)
os.remove(stderr_file)
def test_wrong_command(self):
with self.assertRaises(ExecutorError):
Executor.exec_cmd_only_success(['--wrong-command--', self.test_dir])