1
# Copyright (C) 2009 Canonical Ltd
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.
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.
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
22
from bzrlib.tests import script
25
class TestScriptSyntax(tests.TestCase):
27
def test_comment_is_ignored(self):
28
self.assertEquals([], script._script_to_commands('#comment\n'))
30
def test_empty_line_is_ignored(self):
31
self.assertEquals([], script._script_to_commands('\n'))
33
def test_simple_command(self):
34
self.assertEquals([(['cd', 'trunk'], None, None, None)],
35
script._script_to_commands('cd trunk'))
37
def test_command_with_single_quoted_param(self):
38
story = """bzr commit -m 'two words'"""
39
self.assertEquals([(['bzr', 'commit', '-m', 'two words'],
41
script._script_to_commands(story))
43
def test_command_with_double_quoted_param(self):
44
story = """bzr commit -m "two words" """
45
self.assertEquals([(['bzr', 'commit', '-m', 'two words'],
47
script._script_to_commands(story))
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'))
53
def test_command_with_output(self):
59
self.assertEquals([(['bzr', 'add'], None,
60
'adding file\nadding file2\n', None)],
61
script._script_to_commands(story))
63
def test_command_with_error(self):
66
2>bzr: ERROR: Not a branch: "foo"
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')
74
def test_output_without_command(self):
75
self.assertRaises(SyntaxError, script._script_to_commands, '>input')
77
def test_command_with_backquotes(self):
79
foo = `bzr file-id toto`
81
self.assertEquals([(['foo', '=', '`bzr file-id toto`'],
83
script._script_to_commands(story))
86
class TestScriptExecution(script.TestCaseWithScript):
88
def test_unknown_command(self):
89
self.assertRaises(SyntaxError, self.run_script, 'foo')
91
def test_unexpected_output(self):
95
>Hello, I have just cd into dir !
97
self.assertRaises(AssertionError, self.run_script, story)
100
class TestCat(script.TestCaseWithScript):
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')
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)
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)
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)
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')
133
class TestMkdir(script.TestCaseWithScript):
135
def test_mkdir_usage(self):
136
self.assertRaises(SyntaxError, self.run_script, 'mkdir')
137
self.assertRaises(SyntaxError, self.run_script, 'mkdir foo bar')
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')
143
def test_mkdir_in_jail(self):
150
self.failUnlessExists('dir')
151
self.failUnlessExists('dir2')
154
class TestCd(script.TestCaseWithScript):
156
def test_cd_usage(self):
157
self.assertRaises(SyntaxError, self.run_script, 'cd foo bar')
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 ..')
163
def test_cd_dir_and_back_home(self):
164
self.assertEquals(self.test_dir, osutils.getcwd())
169
self.assertEquals(osutils.pathjoin(self.test_dir, 'dir'),
172
self.run_script('cd')
173
self.assertEquals(self.test_dir, osutils.getcwd())
176
class TestBzr(script.TestCaseWithScript):
178
def test_bzr_smoke(self):
179
self.run_script('bzr init branch')
180
self.failUnlessExists('branch')
183
class TestEcho(script.TestCaseWithScript):
185
def test_echo_usage(self):
190
self.assertRaises(SyntaxError, self.run_script, story)
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)
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)
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)
208
def test_echo_appended(self):
209
out, err = self.run_command(['echo', 'hello', '>file'],
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'],
216
self.assertEquals(None, out)
217
self.assertEquals(None, err)
218
self.assertFileEqual('hello\nhappy\n', 'file')