/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2009, 2010, 2011, 2016 Canonical Ltd
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
17
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
18
from breezy import (
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
19
    commands,
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
20
    osutils,
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
21
    tests,
5463.1.2 by Martin Pool
Make run_argv_aliases reset the global verbosity after running
22
    trace,
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
23
    ui,
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
24
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
25
from breezy.tests import script
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
26
27
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
28
class TestSyntax(tests.TestCase):
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
29
30
    def test_comment_is_ignored(self):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
31
        self.assertEqual([], script._script_to_commands('#comment\n'))
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
32
5417.1.6 by Martin Pool
Add passing test for multi-line comment
33
    def test_comment_multiple_lines(self):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
34
        self.assertEqual([
5417.1.6 by Martin Pool
Add passing test for multi-line comment
35
            (['bar'], None, None, None),
36
            ],
37
            script._script_to_commands("""
38
            # this comment is ignored
39
            # so is this
40
            # no we run bar
41
            $ bar
42
            """))
43
5417.1.5 by Martin Pool
Better handling of blank lines in test scripts
44
    def test_trim_blank_lines(self):
45
        """Blank lines are respected, but trimmed at the start and end.
46
7195.5.1 by Martin
Fix remaining whitespace lint in codebase
47
        Python triple-quoted syntax is going to give stubby/empty blank lines
48
        right at the start and the end.  These are cut off so that callers don't
5417.1.5 by Martin Pool
Better handling of blank lines in test scripts
49
        need special syntax to avoid them.
50
51
        However we do want to be able to match commands that emit blank lines.
52
        """
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
53
        self.assertEqual([
5417.1.5 by Martin Pool
Better handling of blank lines in test scripts
54
            (['bar'], None, '\n', None),
55
            ],
56
            script._script_to_commands("""
57
            $bar
58
59
            """))
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
60
61
    def test_simple_command(self):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
62
        self.assertEqual([(['cd', 'trunk'], None, None, None)],
7143.15.2 by Jelmer Vernooij
Run autopep8.
63
                         script._script_to_commands('$ cd trunk'))
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
64
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
65
    def test_command_with_single_quoted_param(self):
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
66
        story = """$ brz commit -m 'two words'"""
67
        self.assertEqual([(['brz', 'commit', '-m', "'two words'"],
7143.15.2 by Jelmer Vernooij
Run autopep8.
68
                           None, None, None)],
69
                         script._script_to_commands(story))
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
70
71
    def test_command_with_double_quoted_param(self):
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
72
        story = """$ brz commit -m "two words" """
73
        self.assertEqual([(['brz', 'commit', '-m', '"two words"'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
74
                           None, None, None)],
75
                         script._script_to_commands(story))
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
76
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
77
    def test_command_with_input(self):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
78
        self.assertEqual(
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
79
            [(['cat', '>file'], 'content\n', None, None)],
80
            script._script_to_commands('$ cat >file\n<content\n'))
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
81
5283.1.2 by Martin Pool
ScriptRunner strips consistent leading indentation from scripts
82
    def test_indented(self):
83
        # scripts are commonly given indented within the test source code, and
84
        # common indentation is stripped off
85
        story = """
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
86
            $ brz add
5283.1.2 by Martin Pool
ScriptRunner strips consistent leading indentation from scripts
87
            adding file
88
            adding file2
89
            """
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
90
        self.assertEqual([(['brz', 'add'], None,
7143.15.2 by Jelmer Vernooij
Run autopep8.
91
                           'adding file\nadding file2\n', None)],
92
                         script._script_to_commands(story))
5283.1.2 by Martin Pool
ScriptRunner strips consistent leading indentation from scripts
93
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
94
    def test_command_with_output(self):
95
        story = """
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
96
$ brz add
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
97
adding file
98
adding file2
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
99
"""
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
100
        self.assertEqual([(['brz', 'add'], None,
7143.15.2 by Jelmer Vernooij
Run autopep8.
101
                           'adding file\nadding file2\n', None)],
102
                         script._script_to_commands(story))
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
103
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
104
    def test_command_with_error(self):
105
        story = """
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
106
$ brz branch foo
107
2>brz: ERROR: Not a branch: "foo"
4665.5.3 by Vincent Ladeuil
Separate error from normal output.
108
"""
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
109
        self.assertEqual([(['brz', 'branch', 'foo'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
110
                           None, None, 'brz: ERROR: Not a branch: "foo"\n')],
111
                         script._script_to_commands(story))
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
112
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
113
    def test_input_without_command(self):
114
        self.assertRaises(SyntaxError, script._script_to_commands, '<input')
115
116
    def test_output_without_command(self):
117
        self.assertRaises(SyntaxError, script._script_to_commands, '>input')
118
4665.5.2 by Vincent Ladeuil
Handle simple, double and back quotes.
119
    def test_command_with_backquotes(self):
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
120
        story = """
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
121
$ foo = `brz file-id toto`
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
122
"""
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
123
        self.assertEqual([(['foo', '=', '`brz file-id toto`'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
124
                           None, None, None)],
125
                         script._script_to_commands(story))
4665.5.1 by Vincent Ladeuil
Start some shell-like capability to write tests.
126
127
4665.5.18 by Vincent Ladeuil
Better redirection handling.
128
class TestRedirections(tests.TestCase):
129
130
    def _check(self, in_name, out_name, out_mode, remaining, args):
131
        self.assertEqual(script._scan_redirection_options(args),
132
                         (in_name, out_name, out_mode, remaining))
133
134
    def test_no_redirection(self):
135
        self._check(None, None, None, [], [])
136
        self._check(None, None, None, ['foo', 'bar'], ['foo', 'bar'])
137
138
    def test_input_redirection(self):
139
        self._check('foo', None, None, [], ['<foo'])
140
        self._check('foo', None, None, ['bar'], ['bar', '<foo'])
141
        self._check('foo', None, None, ['bar'], ['bar', '<', 'foo'])
142
        self._check('foo', None, None, ['bar'], ['<foo', 'bar'])
143
        self._check('foo', None, None, ['bar', 'baz'], ['bar', '<foo', 'baz'])
144
145
    def test_output_redirection(self):
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
146
        self._check(None, 'foo', 'w+', [], ['>foo'])
147
        self._check(None, 'foo', 'w+', ['bar'], ['bar', '>foo'])
148
        self._check(None, 'foo', 'w+', ['bar'], ['bar', '>', 'foo'])
149
        self._check(None, 'foo', 'a+', [], ['>>foo'])
150
        self._check(None, 'foo', 'a+', ['bar'], ['bar', '>>foo'])
151
        self._check(None, 'foo', 'a+', ['bar'], ['bar', '>>', 'foo'])
4665.5.18 by Vincent Ladeuil
Better redirection handling.
152
153
    def test_redirection_syntax_errors(self):
154
        self._check('', None, None, [], ['<'])
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
155
        self._check(None, '', 'w+', [], ['>'])
156
        self._check(None, '', 'a+', [], ['>>'])
157
        self._check('>', '', 'a+', [], ['<', '>', '>>'])
4665.5.18 by Vincent Ladeuil
Better redirection handling.
158
159
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
160
class TestExecution(script.TestCaseWithTransportAndScript):
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
161
162
    def test_unknown_command(self):
5508.2.2 by Martin
Document test to make it clearer what exactly is being asserted
163
        """A clear error is reported for commands that aren't recognised
164
165
        Testing the attributes of the SyntaxError instance is equivalent to
166
        using traceback.format_exception_only and comparing with:
167
          File "<string>", line 1
168
            foo --frob
169
            ^
170
        SyntaxError: Command not found "foo"
171
        """
5508.2.1 by Martin
In shell-like tests raise a valid SyntaxError if a command is not found
172
        e = self.assertRaises(SyntaxError, self.run_script, "$ foo --frob")
173
        self.assertContainsRe(e.msg, "not found.*foo")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
174
        self.assertEqual(e.text, "foo --frob")
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
175
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
176
    def test_blank_output_mismatches_output(self):
177
        """If you give output, the output must actually be blank.
7143.15.2 by Jelmer Vernooij
Run autopep8.
178
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
179
        See <https://bugs.launchpad.net/bzr/+bug/637830>: previously blank
180
        output was a wildcard.  Now you must say ... if you want that.
181
        """
182
        self.assertRaises(AssertionError,
7143.15.2 by Jelmer Vernooij
Run autopep8.
183
                          self.run_script,
184
                          """
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
185
            $ echo foo
186
            """)
187
5531.1.2 by Vincent Ladeuil
s/blank_output/null_output/
188
    def test_null_output_matches_option(self):
7195.5.1 by Martin
Fix remaining whitespace lint in codebase
189
        """If you want null output to be a wild card, you can pass
5531.1.2 by Vincent Ladeuil
s/blank_output/null_output/
190
        null_output_matches_anything to run_script"""
5509.1.3 by Neil Martinsen-Burrell
change option name and mention the effect on standard error
191
        self.run_script(
192
            """
193
            $ echo foo
5531.1.2 by Vincent Ladeuil
s/blank_output/null_output/
194
            """, null_output_matches_anything=True)
5509.1.3 by Neil Martinsen-Burrell
change option name and mention the effect on standard error
195
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
196
    def test_ellipsis_everything(self):
197
        """A simple ellipsis matches everything."""
198
        self.run_script("""
199
        $ echo foo
200
        ...
201
        """)
202
203
    def test_ellipsis_matches_empty(self):
204
        self.run_script("""
205
        $ cd .
206
        ...
207
        """)
208
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
209
    def test_stops_on_unexpected_output(self):
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
210
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
211
$ mkdir dir
212
$ cd dir
213
The cd command ouputs nothing
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
214
"""
215
        self.assertRaises(AssertionError, self.run_script, story)
216
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
217
    def test_stops_on_unexpected_error(self):
218
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
219
$ cat
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
220
<Hello
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
221
$ brz not-a-command
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
222
"""
223
        self.assertRaises(AssertionError, self.run_script, story)
224
225
    def test_continue_on_expected_error(self):
226
        story = """
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
227
$ brz not-a-command
4665.5.13 by Vincent Ladeuil
Script execution must stop on unexpected errors.
228
2>..."not-a-command"
229
"""
230
        self.run_script(story)
231
4665.5.16 by Vincent Ladeuil
Continue script execution based on status not error output.
232
    def test_continue_on_error_output(self):
233
        # The status matters, not the output
234
        story = """
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
235
$ brz init
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
236
...
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
237
$ cat >file
4665.5.16 by Vincent Ladeuil
Continue script execution based on status not error output.
238
<Hello
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
239
$ brz add file
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
240
...
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
241
$ brz commit -m 'adding file'
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
242
2>...
4665.5.16 by Vincent Ladeuil
Continue script execution based on status not error output.
243
"""
244
        self.run_script(story)
245
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
246
    def test_ellipsis_output(self):
247
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
248
$ cat
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
249
<first line
250
<second line
251
<last line
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
252
first line
253
...
254
last line
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
255
"""
256
        self.run_script(story)
257
        story = """
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
258
$ brz not-a-command
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
259
2>..."not-a-command"
260
"""
261
        self.run_script(story)
262
263
        story = """
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
264
$ brz branch not-a-branch
265
2>brz: ERROR: Not a branch...not-a-branch/".
4665.5.12 by Vincent Ladeuil
Support '...' in expected strings.
266
"""
267
        self.run_script(story)
268
269
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
270
class TestArgumentProcessing(script.TestCaseWithTransportAndScript):
271
272
    def test_globing(self):
273
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
274
$ echo cat >cat
275
$ echo dog >dog
276
$ cat *
277
cat
278
dog
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
279
""")
280
281
    def test_quoted_globbing(self):
282
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
283
$ echo cat >cat
284
$ cat '*'
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
285
2>*: No such file or directory
286
""")
287
288
    def test_quotes_removal(self):
289
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
290
$ echo 'cat' "dog" '"chicken"' "'dragon'"
291
cat dog "chicken" 'dragon'
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
292
""")
293
5463.1.2 by Martin Pool
Make run_argv_aliases reset the global verbosity after running
294
    def test_verbosity_isolated(self):
295
        """Global verbosity is isolated from commands run in scripts.
296
        """
297
        # see also 656694; we should get rid of global verbosity
298
        self.run_script("""
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
299
        $ brz init --quiet a
5463.1.2 by Martin Pool
Make run_argv_aliases reset the global verbosity after running
300
        """)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
301
        self.assertEqual(trace.is_quiet(), False)
5463.1.2 by Martin Pool
Make run_argv_aliases reset the global verbosity after running
302
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
303
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
304
class TestCat(script.TestCaseWithTransportAndScript):
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
305
306
    def test_cat_usage(self):
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
307
        self.assertRaises(SyntaxError, self.run_script, 'cat foo <bar')
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
308
309
    def test_cat_input_to_output(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
310
        retcode, out, err = self.run_command(['cat'],
311
                                             'content\n', 'content\n', None)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
312
        self.assertEqual('content\n', out)
313
        self.assertEqual(None, err)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
314
315
    def test_cat_file_to_output(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
316
        self.build_tree_contents([('file', b'content\n')])
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
317
        retcode, out, err = self.run_command(['cat', 'file'],
318
                                             None, 'content\n', None)
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
319
        self.assertEqual('content\n', out)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
320
        self.assertEqual(None, err)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
321
322
    def test_cat_input_to_file(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
323
        retcode, out, err = self.run_command(['cat', '>file'],
324
                                             'content\n', None, None)
7290.18.6 by Jelmer Vernooij
Fix some more tests.
325
        self.assertFileEqual('content\n', 'file')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
326
        self.assertEqual(None, out)
327
        self.assertEqual(None, err)
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
328
        retcode, out, err = self.run_command(['cat', '>>file'],
329
                                             'more\n', None, None)
7290.18.6 by Jelmer Vernooij
Fix some more tests.
330
        self.assertFileEqual('content\nmore\n', 'file')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
331
        self.assertEqual(None, out)
332
        self.assertEqual(None, err)
4665.5.4 by Vincent Ladeuil
Implement a 'cat' command.
333
334
    def test_cat_file_to_file(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
335
        self.build_tree_contents([('file', b'content\n')])
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
336
        retcode, out, err = self.run_command(['cat', 'file', '>file2'],
337
                                             None, None, None)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
338
        self.assertFileEqual(b'content\n', 'file2')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
339
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
340
    def test_cat_files_to_file(self):
6855.4.1 by Jelmer Vernooij
Yet more bees.
341
        self.build_tree_contents([('cat', b'cat\n')])
342
        self.build_tree_contents([('dog', b'dog\n')])
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
343
        retcode, out, err = self.run_command(['cat', 'cat', 'dog', '>file'],
344
                                             None, None, None)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
345
        self.assertFileEqual(b'cat\ndog\n', 'file')
4665.5.19 by Vincent Ladeuil
Implement globbing and enhance cat to accept multiple files.
346
4665.5.18 by Vincent Ladeuil
Better redirection handling.
347
    def test_cat_bogus_input_file(self):
348
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
349
$ cat <file
4665.5.18 by Vincent Ladeuil
Better redirection handling.
350
2>file: No such file or directory
351
""")
352
353
    def test_cat_bogus_output_file(self):
354
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
355
$ cat >
4665.5.18 by Vincent Ladeuil
Better redirection handling.
356
2>: No such file or directory
357
""")
358
359
    def test_echo_bogus_output_file(self):
360
        # We need a backing file sysytem for that test so it can't be in
361
        # TestEcho
362
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
363
$ echo >
4665.5.18 by Vincent Ladeuil
Better redirection handling.
364
2>: No such file or directory
365
""")
366
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
367
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
368
class TestMkdir(script.TestCaseWithTransportAndScript):
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
369
370
    def test_mkdir_usage(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
371
        self.assertRaises(SyntaxError, self.run_script, '$ mkdir')
372
        self.assertRaises(SyntaxError, self.run_script, '$ mkdir foo bar')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
373
374
    def test_mkdir_jailed(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
375
        self.assertRaises(ValueError, self.run_script, '$ mkdir /out-of-jail')
7143.15.2 by Jelmer Vernooij
Run autopep8.
376
        self.assertRaises(ValueError, self.run_script,
377
                          '$ mkdir ../out-of-jail')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
378
379
    def test_mkdir_in_jail(self):
380
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
381
$ mkdir dir
382
$ cd dir
383
$ mkdir ../dir2
384
$ cd ..
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
385
""")
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
386
        self.assertPathExists('dir')
387
        self.assertPathExists('dir2')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
388
389
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
390
class TestCd(script.TestCaseWithTransportAndScript):
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
391
392
    def test_cd_usage(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
393
        self.assertRaises(SyntaxError, self.run_script, '$ cd foo bar')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
394
395
    def test_cd_out_of_jail(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
396
        self.assertRaises(ValueError, self.run_script, '$ cd /out-of-jail')
397
        self.assertRaises(ValueError, self.run_script, '$ cd ..')
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
398
399
    def test_cd_dir_and_back_home(self):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
400
        self.assertEqual(self.test_dir, osutils.getcwd())
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
401
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
402
$ mkdir dir
403
$ cd dir
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
404
""")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
405
        self.assertEqual(osutils.pathjoin(self.test_dir, 'dir'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
406
                         osutils.getcwd())
4665.5.5 by Vincent Ladeuil
Implement 'cd' and 'mkdir'.
407
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
408
        self.run_script('$ cd')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
409
        self.assertEqual(self.test_dir, osutils.getcwd())
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
410
411
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
412
class TestBrz(script.TestCaseWithTransportAndScript):
4665.5.6 by Vincent Ladeuil
Implement 'bzr' command.
413
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
414
    def test_brz_smoke(self):
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
415
        self.run_script("""
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
416
            $ brz init branch
5422.3.1 by Martin Pool
Blank scriptrunner output only matches blank
417
            Created a standalone tree (format: ...)
418
            """)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
419
        self.assertPathExists('branch')
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
420
421
4665.5.11 by Vincent Ladeuil
Create a new test case based on TestCaseWithMemoryTransport.
422
class TestEcho(script.TestCaseWithMemoryTransportAndScript):
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
423
424
    def test_echo_usage(self):
425
        story = """
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
426
$ echo foo
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
427
<bar
428
"""
429
        self.assertRaises(SyntaxError, self.run_script, story)
430
4789.18.1 by John Arbash Meinel
Rework test_script a little bit.
431
    def test_echo_input(self):
432
        self.assertRaises(SyntaxError, self.run_script, """
433
            $ echo <foo
434
            """)
435
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
436
    def test_echo_to_output(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
437
        retcode, out, err = self.run_command(['echo'], None, '\n', None)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
438
        self.assertEqual('\n', out)
439
        self.assertEqual(None, err)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
440
441
    def test_echo_some_to_output(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
442
        retcode, out, err = self.run_command(['echo', 'hello'],
443
                                             None, 'hello\n', None)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
444
        self.assertEqual('hello\n', out)
445
        self.assertEqual(None, err)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
446
447
    def test_echo_more_output(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
448
        retcode, out, err = self.run_command(
449
            ['echo', 'hello', 'happy', 'world'],
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
450
            None, 'hello happy world\n', None)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
451
        self.assertEqual('hello happy world\n', out)
452
        self.assertEqual(None, err)
4665.5.8 by Vincent Ladeuil
Implement 'echo' command.
453
454
    def test_echo_appended(self):
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
455
        retcode, out, err = self.run_command(['echo', 'hello', '>file'],
456
                                             None, None, None)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
457
        self.assertEqual(None, out)
458
        self.assertEqual(None, err)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
459
        self.assertFileEqual(b'hello\n', 'file')
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
460
        retcode, out, err = self.run_command(['echo', 'happy', '>>file'],
461
                                             None, None, None)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
462
        self.assertEqual(None, out)
463
        self.assertEqual(None, err)
6973.13.2 by Jelmer Vernooij
Fix some more tests.
464
        self.assertFileEqual(b'hello\nhappy\n', 'file')
4665.5.15 by Vincent Ladeuil
Catch the retcode for all commands.
465
5417.1.5 by Martin Pool
Better handling of blank lines in test scripts
466
    def test_empty_line_in_output_is_respected(self):
467
        self.run_script("""
468
            $ echo
469
470
            $ echo bar
471
            bar
472
            """)
473
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
474
475
class TestRm(script.TestCaseWithTransportAndScript):
476
477
    def test_rm_usage(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
478
        self.assertRaises(SyntaxError, self.run_script, '$ rm')
479
        self.assertRaises(SyntaxError, self.run_script, '$ rm -ff foo')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
480
481
    def test_rm_file(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
482
        self.run_script('$ echo content >file')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
483
        self.assertPathExists('file')
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
484
        self.run_script('$ rm file')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
485
        self.assertPathDoesNotExist('file')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
486
487
    def test_rm_file_force(self):
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
488
        self.assertPathDoesNotExist('file')
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
489
        self.run_script('$ rm -f file')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
490
        self.assertPathDoesNotExist('file')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
491
492
    def test_rm_files(self):
493
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
494
$ echo content >file
495
$ echo content >file2
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
496
""")
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
497
        self.assertPathExists('file2')
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
498
        self.run_script('$ rm file file2')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
499
        self.assertPathDoesNotExist('file2')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
500
501
    def test_rm_dir(self):
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
502
        self.run_script('$ mkdir dir')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
503
        self.assertPathExists('dir')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
504
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
505
$ rm dir
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
506
2>rm: cannot remove 'dir': Is a directory
507
""")
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
508
        self.assertPathExists('dir')
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
509
510
    def test_rm_dir_recursive(self):
511
        self.run_script("""
4665.5.20 by Vincent Ladeuil
Fixed as per Martin's review.
512
$ mkdir dir
513
$ rm -r dir
4665.5.17 by Vincent Ladeuil
Implement 'rm' command.
514
""")
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
515
        self.assertPathDoesNotExist('dir')
4953.2.1 by Neil Martinsen-Burrell
add an mv command to shell-like tests
516
517
518
class TestMv(script.TestCaseWithTransportAndScript):
519
520
    def test_usage(self):
521
        self.assertRaises(SyntaxError, self.run_script, '$ mv')
522
        self.assertRaises(SyntaxError, self.run_script, '$ mv f')
523
        self.assertRaises(SyntaxError, self.run_script, '$ mv f1 f2 f3')
524
525
    def test_move_file(self):
526
        self.run_script('$ echo content >file')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
527
        self.assertPathExists('file')
4953.2.1 by Neil Martinsen-Burrell
add an mv command to shell-like tests
528
        self.run_script('$ mv file new_name')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
529
        self.assertPathDoesNotExist('file')
530
        self.assertPathExists('new_name')
4953.2.1 by Neil Martinsen-Burrell
add an mv command to shell-like tests
531
4953.2.3 by Vincent Ladeuil
Use os.rename() and fix some typos.
532
    def test_move_unknown_file(self):
533
        self.assertRaises(AssertionError,
534
                          self.run_script, '$ mv unknown does-not-exist')
535
4953.2.1 by Neil Martinsen-Burrell
add an mv command to shell-like tests
536
    def test_move_dir(self):
537
        self.run_script("""
538
$ mkdir dir
539
$ echo content >dir/file
540
""")
541
        self.run_script('$ mv dir new_name')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
542
        self.assertPathDoesNotExist('dir')
543
        self.assertPathExists('new_name')
544
        self.assertPathExists('new_name/file')
4953.2.3 by Vincent Ladeuil
Use os.rename() and fix some typos.
545
4953.2.2 by Neil Martinsen-Burrell
added a test for "mv file dir" and a NEWS entry
546
    def test_move_file_into_dir(self):
547
        self.run_script("""
548
$ mkdir dir
549
$ echo content > file
550
""")
551
        self.run_script('$ mv file dir')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
552
        self.assertPathExists('dir')
553
        self.assertPathDoesNotExist('file')
554
        self.assertPathExists('dir/file')
4953.2.2 by Neil Martinsen-Burrell
added a test for "mv file dir" and a NEWS entry
555
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
556
557
class cmd_test_confirm(commands.Command):
558
559
    def run(self):
560
        if ui.ui_factory.get_boolean(
7143.15.2 by Jelmer Vernooij
Run autopep8.
561
                u'Really do it',
562
                # 'breezy.tests.test_script.confirm',
563
                # {}
564
                ):
5417.1.3 by Martin Pool
Additional script tests
565
            self.outf.write('Do it!\n')
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
566
        else:
6619.3.3 by Jelmer Vernooij
Apply 2to3 print fix.
567
            print('ok, no')
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
568
569
570
class TestUserInteraction(script.TestCaseWithMemoryTransportAndScript):
571
572
    def test_confirm_action(self):
5417.1.2 by Martin Pool
Additional docs; yay testdoc
573
        """You can write tests that demonstrate user confirmation.
7143.15.2 by Jelmer Vernooij
Run autopep8.
574
6155.5.4 by Vincent Ladeuil
Address gz's review comments.
575
        Specifically, ScriptRunner does't care if the output line for the
576
        prompt isn't terminated by a newline from the program; it's implicitly
577
        terminated by the input.
5417.1.2 by Martin Pool
Additional docs; yay testdoc
578
        """
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
579
        commands.builtin_command_registry.register(cmd_test_confirm)
7143.15.2 by Jelmer Vernooij
Run autopep8.
580
        self.addCleanup(
581
            commands.builtin_command_registry.remove, 'test-confirm')
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
582
        self.run_script("""
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
583
            $ brz test-confirm
6182.2.16 by Benoît Pierre
Fix script tests.
584
            2>Really do it? ([y]es, [n]o): yes
6182.2.5 by Benoît Pierre
Fix script tests.
585
            <y
5417.1.3 by Martin Pool
Additional script tests
586
            Do it!
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
587
            $ brz test-confirm
6182.2.16 by Benoît Pierre
Fix script tests.
588
            2>Really do it? ([y]es, [n]o): no
6182.2.5 by Benoît Pierre
Fix script tests.
589
            <n
5417.1.3 by Martin Pool
Additional script tests
590
            ok, no
5417.1.1 by Martin Pool
ScriptRunner can now cope with commands that prompt for input.
591
            """)
592
7143.15.2 by Jelmer Vernooij
Run autopep8.
593
6155.5.4 by Vincent Ladeuil
Address gz's review comments.
594
class TestShelve(script.TestCaseWithTransportAndScript):
595
596
    def setUp(self):
597
        super(TestShelve, self).setUp()
598
        self.run_script("""
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
599
            $ brz init test
6155.5.4 by Vincent Ladeuil
Address gz's review comments.
600
            Created a standalone tree (format: 2a)
601
            $ cd test
602
            $ echo foo > file
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
603
            $ brz add
6155.5.4 by Vincent Ladeuil
Address gz's review comments.
604
            adding file
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
605
            $ brz commit -m 'file added'
6155.5.4 by Vincent Ladeuil
Address gz's review comments.
606
            2>Committing to:...test/
607
            2>added file
608
            2>Committed revision 1.
609
            $ echo bar > file
610
            """)
611
612
    def test_shelve(self):
613
        self.run_script("""
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
614
            $ brz shelve -m 'shelve bar'
6182.2.16 by Benoît Pierre
Fix script tests.
615
            2>Shelve? ([y]es, [N]o, [f]inish, [q]uit): yes
6155.5.4 by Vincent Ladeuil
Address gz's review comments.
616
            <y
617
            2>Selected changes:
618
            2> M  file
6182.2.16 by Benoît Pierre
Fix script tests.
619
            2>Shelve 1 change(s)? ([y]es, [N]o, [f]inish, [q]uit): yes
6182.2.9 by Benoît Pierre
Fix script tests.
620
            <y
6155.5.4 by Vincent Ladeuil
Address gz's review comments.
621
            2>Changes shelved with id "1".
622
            """,
623
                        null_output_matches_anything=True)
624
        self.run_script("""
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
625
            $ brz shelve --list
6155.5.4 by Vincent Ladeuil
Address gz's review comments.
626
              1: shelve bar
627
            """)
628
629
    def test_dont_shelve(self):
630
        # We intentionally provide no input here to test EOF
7195.5.2 by Martin
Fix bt.test_script error that needs trailing space
631
        self.run_script((
632
            "$ brz shelve -m 'shelve bar'\n"
633
            "2>Shelve? ([y]es, [N]o, [f]inish, [q]uit): \n"
634
            "2>No changes to shelve.\n"
635
            ), null_output_matches_anything=True)
6155.5.4 by Vincent Ladeuil
Address gz's review comments.
636
        self.run_script("""
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
637
            $ brz st
6155.5.4 by Vincent Ladeuil
Address gz's review comments.
638
            modified:
639
              file
640
            """)