1
# Copyright (C) 2009 Canonical Ltd
1
# Copyright (C) 2009, 2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
75
76
input, output, error = None, None, None
77
text = textwrap.dedent(text)
76
78
for line in text.split('\n'):
78
80
# Keep a copy for error reporting
108
110
error.append(line[2:] + '\n')
112
# can happen if the first line is not recognized as a command, eg
113
# if the prompt has leading whitespace
110
114
if output is None:
111
115
if cmd_cur is None:
112
raise SyntaxError('No command for that output',
116
raise SyntaxError('No command for line %r' % (line,),
113
117
(file_name, lineno, 1, orig))
115
119
output.append(line + '\n')
217
221
# Specifying None means: any output is accepted
219
223
if actual is None:
220
test_case.fail('Unexpected: %s' % actual)
224
test_case.fail('We expected output: %r, but found None'
221
226
matching = self.output_checker.check_output(
222
227
expected, actual, self.check_options)
290
295
inputs.append(self._read_input(None, in_name))
291
296
except IOError, e:
292
if e.errno == errno.ENOENT:
297
# Some filenames are illegal on Windows and generate EINVAL
298
# rather than just saying the filename doesn't exist
299
if e.errno in (errno.ENOENT, errno.EINVAL):
294
301
'%s: No such file or directory\n' % (in_name,))
295
303
# Basically cat copy input to output
296
304
output = ''.join(inputs)
297
305
# Handle output redirections
299
307
output = self._write_output(output, out_name, out_mode)
300
308
except IOError, e:
301
if e.errno == errno.ENOENT:
309
# If out_name cannot be created, we may get 'ENOENT', however if
310
# out_name is something like '', we can get EINVAL
311
if e.errno in (errno.ENOENT, errno.EINVAL):
302
312
return 1, None, '%s: No such file or directory\n' % (out_name,)
303
314
return 0, output, None
305
316
def do_echo(self, test_case, input, args):
306
317
(in_name, out_name, out_mode, args) = _scan_redirection_options(args)
308
raise SyntaxError('Specify parameters OR use redirection')
319
raise SyntaxError('echo doesn\'t read from stdin')
310
321
input = ' '.join(args)
312
input = self._read_input(input, in_name)
314
if e.errno == errno.ENOENT:
315
return 1, None, '%s: No such file or directory\n' % (in_name,)
316
322
# Always append a \n'
322
328
output = self._write_output(output, out_name, out_mode)
323
329
except IOError, e:
324
if e.errno == errno.ENOENT:
330
if e.errno in (errno.ENOENT, errno.EINVAL):
325
331
return 1, None, '%s: No such file or directory\n' % (out_name,)
326
333
return 0, output, None
328
335
def _get_jail_root(self, test_case):
398
405
return retcode, None, err
407
def do_mv(self, test_case, input, args):
409
def error(msg, src, dst):
410
return "mv: cannot move %s to %s: %s\n" % (src, dst, msg)
412
if not args or len(args) != 2:
413
raise SyntaxError("Usage: mv path1 path2")
417
if os.path.isdir(dst):
418
real_dst = os.path.join(dst, os.path.basename(src))
419
os.rename(src, real_dst)
421
if e.errno == errno.ENOENT:
422
err = error('No such file or directory', src, dst)
429
return retcode, None, err
401
433
class TestCaseWithMemoryTransportAndScript(tests.TestCaseWithMemoryTransport):
402
434
"""Helper class to experiment shell-like test and memory fs.
445
477
def run_command(self, cmd, input, output, error):
446
478
return self.script_runner.run_command(self, cmd, input, output, error)
481
def run_script(test_case, script_string):
482
"""Run the given script within a testcase"""
483
return ScriptRunner().run_script(test_case, script_string)