|
1
|
|
|
from __future__ import print_function |
|
2
|
|
|
import unittest |
|
3
|
|
|
from mock import Mock |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class RecordingTests(unittest.TestCase): |
|
8
|
|
|
|
|
9
|
|
|
def setUp(self): |
|
10
|
|
|
import niprov.recording as recording |
|
11
|
|
|
self.log = Mock() |
|
12
|
|
|
self.opts = Mock() |
|
13
|
|
|
self.opts.dryrun = False |
|
14
|
|
|
self.sub = Mock() |
|
15
|
|
|
self.listener = Mock() |
|
16
|
|
|
recording.log = self.log |
|
17
|
|
|
self.recording = recording |
|
18
|
|
|
self.dependencies = Mock() |
|
19
|
|
|
self.dependencies.reconfigureOrGetConfiguration.return_value = self.opts |
|
20
|
|
|
self.dependencies.getExternals.return_value = self.sub |
|
21
|
|
|
self.dependencies.getListener.return_value = self.listener |
|
22
|
|
|
|
|
23
|
|
|
def record(self, cmd, **kwargs): |
|
24
|
|
|
self.recording.record(cmd, dependencies=self.dependencies, |
|
25
|
|
|
opts=self.opts, **kwargs) |
|
26
|
|
|
|
|
27
|
|
|
def test_Executes_commands(self): |
|
28
|
|
|
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f'] |
|
29
|
|
|
self.record(cmd) |
|
30
|
|
|
self.sub.run.assert_any_call(cmd) |
|
31
|
|
|
|
|
32
|
|
|
def test_Extracts_provenance_from_commands(self): |
|
33
|
|
|
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f'] |
|
34
|
|
|
self.record(cmd) |
|
35
|
|
|
self.log.assert_called_with(['newfile.f'],'mytransform',['oldfile.f'], |
|
36
|
|
|
transient=False, code=' '.join(cmd), logtext=self.sub.run().output, |
|
37
|
|
|
script=None, opts=self.opts, provenance={}, user=None) |
|
38
|
|
|
|
|
39
|
|
|
def test_If_parent_or_new_provided_override_parsed(self): |
|
40
|
|
|
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f'] |
|
41
|
|
|
self.record(cmd, parents=['customParent'], new='customNew') |
|
42
|
|
|
self.log.assert_called_with(['customNew'],'mytransform',['customParent'], |
|
43
|
|
|
transient=False, code=' '.join(cmd), logtext=self.sub.run().output, |
|
44
|
|
|
script=None, opts=self.opts, provenance={}, user=None) |
|
45
|
|
|
|
|
46
|
|
|
def test_Passes_transient_flag(self): |
|
47
|
|
|
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f'] |
|
48
|
|
|
self.record(cmd, transient=True) |
|
49
|
|
|
self.log.assert_called_with(['newfile.f'],'mytransform',['oldfile.f'], |
|
50
|
|
|
transient=True, code=' '.join(cmd), logtext=self.sub.run().output, |
|
51
|
|
|
script=None, opts=self.opts, provenance={}, user=None) |
|
52
|
|
|
|
|
53
|
|
|
def test_Informs_listener_about_interpretation(self): |
|
54
|
|
|
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f'] |
|
55
|
|
|
self.record(cmd) |
|
56
|
|
|
self.listener.interpretedRecording.assert_called_with( |
|
57
|
|
|
['newfile.f'],'mytransform',['oldfile.f']) |
|
58
|
|
|
|
|
59
|
|
|
def test_Works_on_single_string_too(self): |
|
60
|
|
|
cmd = 'mytransform -out newfile.f -in oldfile.f' |
|
61
|
|
|
self.record(cmd) |
|
62
|
|
|
self.sub.run.assert_called_with(cmd.split()) |
|
63
|
|
|
self.log.assert_called_with(['newfile.f'],'mytransform',['oldfile.f'], |
|
64
|
|
|
transient=False, code=cmd, logtext=self.sub.run().output, |
|
65
|
|
|
script=None, opts=self.opts, provenance={}, user=None) |
|
66
|
|
|
|
|
67
|
|
|
def test_Python_code(self): |
|
68
|
|
|
myfunc = Mock() |
|
69
|
|
|
myfunc.side_effect = lambda a,b,one=None,two=None: None |
|
70
|
|
|
myfunc.func_name = 'myfunc' |
|
71
|
|
|
args = ['foo','bar'] |
|
72
|
|
|
kwargs = {'one':'foz','two':'baz'} |
|
73
|
|
|
self.record(myfunc, args=args, kwargs=kwargs, new='new.f', parents=['old.f']) |
|
74
|
|
|
myfunc.assert_called_with(*args, **kwargs) |
|
75
|
|
|
self.log.assert_called_with(['new.f'],myfunc.func_name,['old.f'], |
|
76
|
|
|
transient=False, code=None, logtext='', |
|
77
|
|
|
script=myfunc.func_code.co_filename, |
|
78
|
|
|
opts=self.opts, provenance={'args':args, 'kwargs':kwargs}, user=None) |
|
79
|
|
|
|
|
80
|
|
|
def test_Python_code_output_captured(self): |
|
81
|
|
|
def myfunc(): |
|
82
|
|
|
print('Hello MyFunc') |
|
83
|
|
|
args = [] |
|
84
|
|
|
kwargs = {} |
|
85
|
|
|
self.record(myfunc, args=args, kwargs=kwargs, new='new.f', parents=['old.f']) |
|
86
|
|
|
self.log.assert_called_with(['new.f'],myfunc.func_name,['old.f'], |
|
87
|
|
|
transient=False, code=None, logtext='Hello MyFunc\n', |
|
88
|
|
|
script=myfunc.func_code.co_filename, user=None, |
|
89
|
|
|
opts=self.opts, provenance={'args':args, 'kwargs':kwargs}) |
|
90
|
|
|
|
|
91
|
|
|
def test_On_dryrun_doesnt_execute_python_code(self): |
|
92
|
|
|
myfunc = Mock() |
|
93
|
|
|
myfunc.side_effect = lambda a,b,one=None,two=None: None |
|
94
|
|
|
myfunc.func_name = 'myfunc' |
|
95
|
|
|
args = ['foo','bar'] |
|
96
|
|
|
kwargs = {'one':'foz','two':'baz'} |
|
97
|
|
|
self.opts.dryrun = True |
|
98
|
|
|
self.record(myfunc, args=args, kwargs=kwargs) |
|
99
|
|
|
assert not myfunc.called, "Passed function was called on dry run." |
|
100
|
|
|
|
|
101
|
|
|
def test_On_dryrun_doesnt_execute_commands(self): |
|
102
|
|
|
self.opts.dryrun = True |
|
103
|
|
|
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f'] |
|
104
|
|
|
self.record(cmd) |
|
105
|
|
|
assert not self.sub.run.called, "Command was executed on dry run." |
|
106
|
|
|
|
|
107
|
|
|
def test_Passes_opts_to_log(self): |
|
108
|
|
|
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f'] |
|
109
|
|
|
self.record(cmd, transient=True) |
|
110
|
|
|
self.log.assert_called_with(['newfile.f'],'mytransform',['oldfile.f'], |
|
111
|
|
|
transient=True, code=' '.join(cmd), logtext=self.sub.run().output, |
|
112
|
|
|
script=None, provenance={}, opts=self.opts, user=None) |
|
113
|
|
|
|
|
114
|
|
|
def test_Abbreviated_versions_of_out_and_in(self): |
|
115
|
|
|
cmd = ['mytransform','-o','newfile.f','-i','oldfile.f'] |
|
116
|
|
|
self.record(cmd) |
|
117
|
|
|
self.log.assert_called_with(['newfile.f'],'mytransform',['oldfile.f'], |
|
118
|
|
|
transient=False, code=' '.join(cmd), logtext=self.sub.run().output, |
|
119
|
|
|
script=None, opts=self.opts, provenance={}, user=None) |
|
120
|
|
|
|
|
121
|
|
|
def test_Passes_listener_bash_command(self): |
|
122
|
|
|
def myfunc(): |
|
123
|
|
|
print('Hello MyFunc') |
|
124
|
|
|
args = [] |
|
125
|
|
|
kwargs = {} |
|
126
|
|
|
self.record(myfunc, args=args, kwargs=kwargs, new='new.f', parents=['old.f']) |
|
127
|
|
|
assert not self.listener.receivedBashCommand.called |
|
128
|
|
|
cmd = ['mytransform','-o','newfile.f','-i','oldfile.f'] |
|
129
|
|
|
self.record(cmd) |
|
130
|
|
|
self.listener.receivedBashCommand.assert_called_with(cmd) |
|
131
|
|
|
|
|
132
|
|
|
def test_Calls_reconfigureOrGetConfiguration_on_dependencies(self): |
|
133
|
|
|
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f'] |
|
134
|
|
|
outOpts = Mock() |
|
135
|
|
|
outOpts.dryrun = True |
|
136
|
|
|
self.dependencies.reconfigureOrGetConfiguration.return_value = outOpts |
|
137
|
|
|
self.record(cmd, transient=True) |
|
138
|
|
|
self.dependencies.reconfigureOrGetConfiguration.assert_called_with( |
|
139
|
|
|
self.opts) |
|
140
|
|
|
assert not self.sub.run.called |
|
141
|
|
|
|
|
142
|
|
|
def test_Passes_user_arg(self): |
|
143
|
|
|
cmd = ['mytransform','-out','newfile.f','-in','oldfile.f'] |
|
144
|
|
|
self.record(cmd, user='someone') |
|
145
|
|
|
self.log.assert_called_with(['newfile.f'],'mytransform',['oldfile.f'], |
|
146
|
|
|
transient=False, user='someone', code=' '.join(cmd), |
|
147
|
|
|
logtext=self.sub.run().output, |
|
148
|
|
|
script=None, opts=self.opts, provenance={}) |
|
149
|
|
|
|
|
150
|
|
|
|