Debian Codegraph @

tests/test_doit_cmd.py

raw symbols SCIP
1 import os
2 from unittest.mock import Mock
3 import tempfile
4 import shutil
5
6 import pytest
7
8 from doit.exceptions import InvalidCommand
9 from doit.cmd_run import Run
10 from doit.cmd_list import List
11 from doit import doit_cmd
12
13
14
15 def cmd_main(args):
16 main = doit_cmd.DoitMain()
17 main.BIN_NAME = 'doit'
18 return main.run(args)
19
20
21 class TestRun(object):
22 def test_version(self, capsys):
23 cmd_main(["--version"])
24 out, err = capsys.readouterr()
25 assert "lib" in out
26
27 def test_usage(self, capsys):
28 cmd_main(["--help"])
29 out, err = capsys.readouterr()
30 assert "doit list" in out
31
32 def test_run_is_default(self, monkeypatch):
33 mock_run = Mock()
34 monkeypatch.setattr(Run, "execute", mock_run)
35 cmd_main([])
36 assert 1 == mock_run.call_count
37
38 def test_run_other_subcommand(self, monkeypatch):
39 mock_list = Mock()
40 monkeypatch.setattr(List, "execute", mock_list)
41 cmd_main(["list"])
42 assert 1 == mock_list.call_count
43
44 def test_cmdline_vars(self, monkeypatch):
45 mock_run = Mock()
46 monkeypatch.setattr(Run, "execute", mock_run)
47 cmd_main(['x=1', 'y=abc'])
48 assert '1' == doit_cmd.get_var('x')
49 assert 'abc' == doit_cmd.get_var('y')
50 assert None is doit_cmd.get_var('z')
51
52 def test_cmdline_novars(self, monkeypatch):
53 mock_run = Mock()
54 monkeypatch.setattr(Run, "execute", mock_run)
55 cmd_main(['x=1'])
56
57 # Simulate the variable below not being initialized by a subprocess on
58 # Windows. See https://github.com/pydoit/doit/issues/164.
59 doit_cmd._CMDLINE_VARS = None
60 assert None is doit_cmd.get_var('x')
61
62 def test_cmdline_vars_not_opts(self, monkeypatch):
63 mock_run = Mock()
64 monkeypatch.setattr(Run, "execute", mock_run)
65 cmd_main(['--z=5'])
66 assert None == doit_cmd.get_var('--z')
67
68 def test_cmdline_loader_option_before_cmd_name(self, monkeypatch):
69 mock_list = Mock()
70 monkeypatch.setattr(List, "execute", mock_list)
71 cmd_main(['-k', 'list', '--all'])
72 assert mock_list.called
73 params, args = mock_list.call_args[0]
74 assert params['subtasks'] == True
75 assert params['seek_file'] == True
76 assert args == []
77
78 def test_cmdline_loader_option_mixed(self, monkeypatch):
79 mock_run = Mock()
80 monkeypatch.setattr(Run, "execute", mock_run)
81 cmd_main(['-c', '-k', 'lala'])
82 assert mock_run.called
83 params, args = mock_run.call_args[0]
84 assert params['continue'] == True
85 assert params['seek_file'] == True
86 assert args == ['lala']
87
88 def test_task_loader_has_cmd_list(self, monkeypatch):
89 cmd_names = []
90 def save_cmd_names(self, params, args):
91 cmd_names.extend(self.loader.cmd_names)
92 monkeypatch.setattr(Run, "execute", save_cmd_names)
93 cmd_main([])
94 assert 'list' in cmd_names
95
96 def test_extra_config(self, monkeypatch, depfile_name):
97 outfile_val = []
98 def monkey_run(self, opt_values, pos_args):
99 outfile_val.append(opt_values['outfile'])
100 monkeypatch.setattr(Run, "execute", monkey_run)
101 extra_config = {
102 'outfile': 'foo.txt',
103 'dep_file': depfile_name,
104 }
105 doit_cmd.DoitMain(extra_config={'GLOBAL': extra_config}).run([])
106 assert outfile_val[0] == 'foo.txt'
107
108
109
110 class TestErrors(object):
111 def test_interrupt(self, monkeypatch):
112 def my_raise(*args):
113 raise KeyboardInterrupt()
114 mock_cmd = Mock(side_effect=my_raise)
115 monkeypatch.setattr(Run, "execute", mock_cmd)
116 pytest.raises(KeyboardInterrupt, cmd_main, [])
117
118 def test_user_error(self, capsys, monkeypatch):
119 mock_cmd = Mock(side_effect=InvalidCommand)
120 monkeypatch.setattr(Run, "execute", mock_cmd)
121 got = cmd_main([])
122 assert 3 == got
123 out, err = capsys.readouterr()
124 assert "ERROR" in err
125
126 def test_internal_error(self, capsys, monkeypatch):
127 mock_cmd = Mock(side_effect=Exception)
128 monkeypatch.setattr(Run, "execute", mock_cmd)
129 got = cmd_main([])
130 assert 3 == got
131 out, err = capsys.readouterr()
132 # traceback from Exception (this case code from mock lib)
133 assert "mock.py" in err
134
135
136 class TestConfig(object):
137 def test_no_ini_config_file(self):
138 main = doit_cmd.DoitMain(config_filenames=())
139 main.run(['--version'])
140
141 def test_load_plugins_command(self):
142 config_filename = os.path.join(os.path.dirname(__file__), 'sample.cfg')
143 main = doit_cmd.DoitMain(config_filenames=config_filename)
144 assert 1 == len(main.config['COMMAND'])
145 # test loaded plugin command is actually used with plugin name
146 assert 'foo' in main.get_cmds()
147
148 def test_merge_api_ini_config(self):
149 config_filename = os.path.join(os.path.dirname(__file__), 'sample.cfg')
150 api_config = {'GLOBAL': {'opty':'10', 'optz':'10'}}
151 main = doit_cmd.DoitMain(config_filenames=config_filename,
152 extra_config=api_config)
153 assert 1 == len(main.config['COMMAND'])
154 # test loaded plugin command is actually used with plugin name
155 assert 'foo' in main.get_cmds()
156 # INI has higher preference the api_config
157 assert main.config['GLOBAL'] == {'optx':'6', 'opty':'7', 'optz':'10'}
158
159 def test_execute_command_plugin(self, capsys):
160 config_filename = os.path.join(os.path.dirname(__file__), 'sample.cfg')
161 main = doit_cmd.DoitMain(config_filenames=config_filename)
162 main.run(['foo'])
163 got = capsys.readouterr()[0]
164 assert got == 'this command does nothing!\n'
165
166 # TOML
167 def test_merge_api_toml_config(self):
168 config_filename = os.path.join(os.path.dirname(__file__), 'sample.toml')
169 api_config = {'GLOBAL': {'opty':'10', 'optz':'10'}}
170 main = doit_cmd.DoitMain(config_filenames=config_filename,
171 extra_config=api_config)
172 assert 1 == len(main.config['COMMAND'])
173 # test loaded plugin command is actually used with plugin name
174 assert 'foo' in main.get_cmds()
175 # INI has higher preference the api_config
176 assert main.config['GLOBAL'] == {'optx':'6', 'opty':'7', 'optz':'10'}
177
178 assert main.config['foo'] == {'nval': 33}
179 assert main.config['task:bar'] == {'opt': "baz"}
180
181
182 def test_find_pyproject_toml_config(self):
183 config_filename = os.path.join(os.path.dirname(__file__), 'pyproject.toml')
184 api_config = {'GLOBAL': {'opty':'10', 'optz':'10'}}
185
186 with tempfile.TemporaryDirectory() as td:
187 shutil.copy(config_filename, os.path.join(td, 'pyproject.toml'))
188 old_cwd = os.getcwd()
189
190 try:
191 os.chdir(td)
192 main = doit_cmd.DoitMain(extra_config=api_config)
193 finally:
194 os.chdir(old_cwd)
195
196 assert 1 == len(main.config['COMMAND']), main.config
197 # test loaded plugin command is actually used with plugin name
198 assert 'bar' in main.get_cmds()
199 # INI has higher preference the api_config
200 assert main.config['GLOBAL'] == {'optx':'2', 'opty':'3', 'optz':'10'}