forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_repro.py
122 lines (94 loc) · 4.95 KB
/
test_repro.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
from dvc.command.remove import CmdRemove
from dvc.command.repro import CmdRepro
from dvc.command.run import CmdRun
from dvc.executor import Executor
from tests.test_cmd_run import RunBasicTest
class ReproBasicEnv(RunBasicTest):
def setUp(self):
super(ReproBasicEnv, self).setUp()
self.file_name1 = os.path.join('data', 'file1')
self.file1_code_file = 'file1.py'
self.create_file_and_commit(self.file1_code_file, 'print("Hello")' + os.linesep + 'print("Mary")')
self.settings.parse_args(['run',
'--stdout', self.file_name1,
'--code', self.file1_code_file,
'python', self.file1_code_file, '--not-repro'])
cmd_file1 = CmdRun(self.settings)
self.assertEqual(cmd_file1.code_dependencies, [self.file1_code_file])
cmd_file1.run()
self.file_name11 = os.path.join('data', 'file11')
self.file11_code_file = 'file11.py'
self.create_file_and_commit(self.file11_code_file,
'import sys' + os.linesep + 'print(open(sys.argv[1]).readline().strip())')
self.settings.parse_args(['run',
'--stdout', self.file_name11,
'--code', self.file11_code_file,
'python', self.file11_code_file, self.file_name1])
CmdRun(self.settings).run()
self.file_name2 = os.path.join('data', 'file2')
self.file2_code_file = 'file2.py'
self.create_file_and_commit(self.file2_code_file,
'print("Bobby")')
self.settings.parse_args(['run',
'--stdout', self.file_name2,
'python', self.file2_code_file, '--not-repro'])
CmdRun(self.settings).run()
self.file_res_code_file = 'code_res.py'
self.create_file_and_commit(self.file_res_code_file,
'import sys' + os.linesep +
'text1 = open(sys.argv[1]).read()' + os.linesep +
'text2 = open(sys.argv[2]).read()' + os.linesep +
'print(text1 + text2)')
self.file_name_res = os.path.join('data', 'file_res')
self.settings.parse_args(['run',
'--stdout', self.file_name_res,
'--code', self.file_res_code_file,
'python', self.file_res_code_file,
self.file_name11,
self.file_name2])
cmd_res = CmdRun(self.settings)
self.assertEqual(cmd_res.code_dependencies, [self.file_res_code_file])
cmd_res.run()
lines = list(filter(None, map(str.strip, open(self.file_name_res).readlines())))
self.assertEqual(lines, ['Hello', 'Bobby'])
def create_file_and_commit(self, file_name, content='Any', message='Just a commit'):
self.create_file(file_name, content)
self.commit_file(file_name, message)
@staticmethod
def commit_file(file_name, message='Just a commit'):
Executor.exec_cmd_only_success(['git', 'add', file_name])
Executor.exec_cmd_only_success(['git', 'commit', '-m', message])
def modify_file_and_commit(self, filename, content_to_add=' '):
fd = open(filename, 'a')
fd.write(content_to_add)
fd.close()
self.commit_file(filename)
class ReproCodeDependencyTest(ReproBasicEnv):
def test(self):
self.modify_file_and_commit(self.file_res_code_file)
self.settings.parse_args('repro {}'.format(self.file_name_res))
CmdRepro(self.settings).run()
self.assertEqual(open(self.file_name_res).read().strip(), 'Hello\nBobby')
class ReproChangedDependency(ReproBasicEnv):
def test(self):
self.recreate_file1()
self.settings.parse_args('repro {}'.format(self.file_name11))
CmdRepro(self.settings).run()
self.assertEqual(open(self.file_name11).read(), 'Goodbye\n')
def recreate_file1(self):
self.settings.parse_args('remove {} --keep-in-cloud'.format(self.file_name1))
CmdRemove(self.settings).run()
file1_code_file = 'file1_2.py'
self.create_file_and_commit(file1_code_file, 'print("Goodbye")' + os.linesep + 'print("Jack")')
self.settings.parse_args(['run',
'--stdout', self.file_name1,
'--code', file1_code_file,
'python', file1_code_file, '--not-repro'])
CmdRun(self.settings).run()
class ReproChangedDeepDependency(ReproChangedDependency):
def test(self):
self.recreate_file1()
self.settings.parse_args('repro {}'.format(self.file_name_res))
CmdRepro(self.settings).run()
self.assertEqual(open(self.file_name_res).read().strip(), 'Goodbye\nBobby')