/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_script.py

Merge shell-like-tests into description

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 Canonical Ltd
 
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
 
 
17
 
 
18
from bzrlib import (
 
19
    osutils,
 
20
    tests,
 
21
    )
 
22
from bzrlib.tests import script
 
23
 
 
24
 
 
25
class TestScriptSyntax(tests.TestCase):
 
26
 
 
27
    def test_comment_is_ignored(self):
 
28
        self.assertEquals([], script._script_to_commands('#comment\n'))
 
29
 
 
30
    def test_empty_line_is_ignored(self):
 
31
        self.assertEquals([], script._script_to_commands('\n'))
 
32
 
 
33
    def test_simple_command(self):
 
34
        self.assertEquals([(['cd', 'trunk'], None, None, None)],
 
35
                           script._script_to_commands('cd trunk'))
 
36
 
 
37
    def test_command_with_single_quoted_param(self):
 
38
        story = """bzr commit -m 'two words'"""
 
39
        self.assertEquals([(['bzr', 'commit', '-m', 'two words'],
 
40
                            None, None, None)],
 
41
                           script._script_to_commands(story))
 
42
 
 
43
    def test_command_with_double_quoted_param(self):
 
44
        story = """bzr commit -m "two words" """
 
45
        self.assertEquals([(['bzr', 'commit', '-m', 'two words'],
 
46
                            None, None, None)],
 
47
                           script._script_to_commands(story))
 
48
 
 
49
    def test_command_with_input(self):
 
50
        self.assertEquals([(['cat', '>file'], 'content\n', None, None)],
 
51
                           script._script_to_commands('cat >file\n<content\n'))
 
52
 
 
53
    def test_command_with_output(self):
 
54
        story = """
 
55
bzr add
 
56
>adding file
 
57
>adding file2
 
58
"""
 
59
        self.assertEquals([(['bzr', 'add'], None,
 
60
                            'adding file\nadding file2\n', None)],
 
61
                          script._script_to_commands(story))
 
62
 
 
63
    def test_command_with_error(self):
 
64
        story = """
 
65
bzr branch foo
 
66
2>bzr: ERROR: Not a branch: "foo"
 
67
"""
 
68
        self.assertEquals([(['bzr', 'branch', 'foo'],
 
69
                            None, None, 'bzr: ERROR: Not a branch: "foo"\n')],
 
70
                          script._script_to_commands(story))
 
71
    def test_input_without_command(self):
 
72
        self.assertRaises(SyntaxError, script._script_to_commands, '<input')
 
73
 
 
74
    def test_output_without_command(self):
 
75
        self.assertRaises(SyntaxError, script._script_to_commands, '>input')
 
76
 
 
77
    def test_command_with_backquotes(self):
 
78
        story = """
 
79
foo = `bzr file-id toto`
 
80
"""
 
81
        self.assertEquals([(['foo', '=', '`bzr file-id toto`'],
 
82
                            None, None, None)],
 
83
                          script._script_to_commands(story))
 
84
 
 
85
 
 
86
class TestScriptExecution(script.TestCaseWithScript):
 
87
 
 
88
    def test_unknown_command(self):
 
89
        self.assertRaises(SyntaxError, self.run_script, 'foo')
 
90
 
 
91
    def test_unexpected_output(self):
 
92
        story = """
 
93
mkdir dir
 
94
cd dir
 
95
>Hello, I have just cd into dir !
 
96
"""
 
97
        self.assertRaises(AssertionError, self.run_script, story)
 
98
 
 
99
 
 
100
class TestCat(script.TestCaseWithScript):
 
101
 
 
102
    def test_cat_usage(self):
 
103
        self.assertRaises(SyntaxError, self.run_script, 'cat foo bar baz')
 
104
        self.assertRaises(SyntaxError, self.run_script, 'cat foo <bar')
 
105
 
 
106
    def test_cat_input_to_output(self):
 
107
        out, err = self.run_command(['cat'], 'content\n', 'content\n', None)
 
108
        self.assertEquals('content\n', out)
 
109
        self.assertEquals(None, err)
 
110
 
 
111
    def test_cat_file_to_output(self):
 
112
        self.build_tree_contents([('file', 'content\n')])
 
113
        out, err = self.run_command(['cat', 'file'], None, 'content\n', None)
 
114
        self.assertEquals('content\n', out)
 
115
        self.assertEquals(None, err)
 
116
 
 
117
    def test_cat_input_to_file(self):
 
118
        out, err = self.run_command(['cat', '>file'], 'content\n', None, None)
 
119
        self.assertFileEqual('content\n', 'file')
 
120
        self.assertEquals(None, out)
 
121
        self.assertEquals(None, err)
 
122
        out, err = self.run_command(['cat', '>>file'], 'more\n', None, None)
 
123
        self.assertFileEqual('content\nmore\n', 'file')
 
124
        self.assertEquals(None, out)
 
125
        self.assertEquals(None, err)
 
126
 
 
127
    def test_cat_file_to_file(self):
 
128
        self.build_tree_contents([('file', 'content\n')])
 
129
        out, err = self.run_command(['cat', 'file', '>file2'], None, None, None)
 
130
        self.assertFileEqual('content\n', 'file2')
 
131
 
 
132
 
 
133
class TestMkdir(script.TestCaseWithScript):
 
134
 
 
135
    def test_mkdir_usage(self):
 
136
        self.assertRaises(SyntaxError, self.run_script, 'mkdir')
 
137
        self.assertRaises(SyntaxError, self.run_script, 'mkdir foo bar')
 
138
 
 
139
    def test_mkdir_jailed(self):
 
140
        self.assertRaises(ValueError, self.run_script, 'mkdir /out-of-jail')
 
141
        self.assertRaises(ValueError, self.run_script, 'mkdir ../out-of-jail')
 
142
 
 
143
    def test_mkdir_in_jail(self):
 
144
        self.run_script("""
 
145
mkdir dir
 
146
cd dir
 
147
mkdir ../dir2
 
148
cd ..
 
149
""")
 
150
        self.failUnlessExists('dir')
 
151
        self.failUnlessExists('dir2')
 
152
 
 
153
 
 
154
class TestCd(script.TestCaseWithScript):
 
155
 
 
156
    def test_cd_usage(self):
 
157
        self.assertRaises(SyntaxError, self.run_script, 'cd foo bar')
 
158
 
 
159
    def test_cd_out_of_jail(self):
 
160
        self.assertRaises(ValueError, self.run_script, 'cd /out-of-jail')
 
161
        self.assertRaises(ValueError, self.run_script, 'cd ..')
 
162
 
 
163
    def test_cd_dir_and_back_home(self):
 
164
        self.assertEquals(self.test_dir, osutils.getcwd())
 
165
        self.run_script("""
 
166
mkdir dir
 
167
cd dir
 
168
""")
 
169
        self.assertEquals(osutils.pathjoin(self.test_dir, 'dir'),
 
170
                          osutils.getcwd())
 
171
 
 
172
        self.run_script('cd')
 
173
        self.assertEquals(self.test_dir, osutils.getcwd())
 
174
 
 
175
 
 
176
class TestBzr(script.TestCaseWithScript):
 
177
 
 
178
    def test_bzr_smoke(self):
 
179
        self.run_script('bzr init branch')
 
180
        self.failUnlessExists('branch')
 
181
 
 
182
 
 
183
class TestEcho(script.TestCaseWithScript):
 
184
 
 
185
    def test_echo_usage(self):
 
186
        story = """
 
187
echo foo
 
188
<bar
 
189
"""
 
190
        self.assertRaises(SyntaxError, self.run_script, story)
 
191
 
 
192
    def test_echo_to_output(self):
 
193
        out, err = self.run_command(['echo'], None, '\n', None)
 
194
        self.assertEquals('\n', out)
 
195
        self.assertEquals(None, err)
 
196
 
 
197
    def test_echo_some_to_output(self):
 
198
        out, err = self.run_command(['echo', 'hello'], None, 'hello\n', None)
 
199
        self.assertEquals('hello\n', out)
 
200
        self.assertEquals(None, err)
 
201
 
 
202
    def test_echo_more_output(self):
 
203
        out, err = self.run_command(['echo', 'hello', 'happy', 'world'],
 
204
                                    None, 'hellohappyworld\n', None)
 
205
        self.assertEquals('hellohappyworld\n', out)
 
206
        self.assertEquals(None, err)
 
207
 
 
208
    def test_echo_appended(self):
 
209
        out, err = self.run_command(['echo', 'hello', '>file'],
 
210
                                    None, None, None)
 
211
        self.assertEquals(None, out)
 
212
        self.assertEquals(None, err)
 
213
        self.assertFileEqual('hello\n', 'file')
 
214
        out, err = self.run_command(['echo', 'happy', '>>file'],
 
215
                                    None, None, None)
 
216
        self.assertEquals(None, out)
 
217
        self.assertEquals(None, err)
 
218
        self.assertFileEqual('hello\nhappy\n', 'file')