143
139
:param args: The command line arguments
145
:return: A tuple containing:
141
:return: A tuple containing:
146
142
- The file name redirected from or None
147
143
- The file name redirected to or None
148
144
- The mode to open the output file or None
167
163
in_name = redirected_file_name('<', arg[1:], args)
168
164
elif arg.startswith('>>'):
169
165
out_name = redirected_file_name('>>', arg[2:], args)
171
167
elif arg.startswith('>',):
172
168
out_name = redirected_file_name('>', arg[1:], args)
175
171
remaining.append(arg)
176
172
return in_name, out_name, out_mode, remaining
179
175
class ScriptRunner(object):
180
176
"""Run a shell-like script from a test.
184
from breezy.tests import script
180
from brzlib.tests import script
188
184
def test_bug_nnnnn(self):
189
185
sr = script.ScriptRunner()
190
186
sr.run_script(self, '''
233
229
self._check_output(output, actual_output, test_case)
234
except AssertionError as e:
230
except AssertionError, e:
235
231
raise AssertionError(str(e) + " in stdout of command %s" % cmd)
237
233
self._check_output(error, actual_error, test_case)
238
except AssertionError as e:
239
raise AssertionError(str(e)
240
+ " in stderr of running command %s" % cmd)
234
except AssertionError, e:
235
raise AssertionError(str(e) +
236
" in stderr of running command %s" % cmd)
241
237
if retcode and not error and actual_error:
242
238
test_case.fail('In \n\t%s\nUnexpected error: %s'
243
239
% (' '.join(cmd), actual_error))
253
249
test_case.fail('expected output: %r, but found nothing'
256
252
null_output_matches_anything = getattr(
257
253
self, 'null_output_matches_anything', False)
284
280
# Strip the simple and double quotes since we don't care about
285
281
# them. We leave the backquotes in place though since they have a
286
282
# different semantic.
287
if arg[0] in ('"', "'") and arg[0] == arg[-1]:
283
if arg[0] in ('"', "'") and arg[0] == arg[-1]:
290
286
if glob.has_magic(arg):
301
297
def _read_input(self, input, in_name):
302
298
if in_name is not None:
303
infile = open(in_name, 'r')
299
infile = open(in_name, 'rb')
305
301
# Command redirection takes precedence over provided input
306
302
input = infile.read()
321
def do_brz(self, test_case, input, args):
322
encoding = osutils.get_user_encoding()
323
stdout = ui_testing.StringIOWithEncoding()
324
stderr = ui_testing.StringIOWithEncoding()
325
stdout.encoding = stderr.encoding = encoding
326
handler = logging.StreamHandler(stderr)
327
handler.setLevel(logging.INFO)
329
logger = logging.getLogger('')
330
logger.addHandler(handler)
332
retcode = test_case._run_bzr_core(
333
args, encoding=encoding, stdin=input, stdout=stdout,
334
stderr=stderr, working_dir=None)
336
logger.removeHandler(handler)
338
return retcode, stdout.getvalue(), stderr.getvalue()
317
def do_bzr(self, test_case, input, args):
318
retcode, out, err = test_case._run_bzr_core(
319
args, retcode=None, encoding=None, stdin=input, working_dir=None)
320
return retcode, out, err
340
322
def do_cat(self, test_case, input, args):
341
323
(in_name, out_name, out_mode, args) = _scan_redirection_options(args)
351
333
for in_name in input_names:
353
335
inputs.append(self._read_input(None, in_name))
355
337
# Some filenames are illegal on Windows and generate EINVAL
356
338
# rather than just saying the filename doesn't exist
357
339
if e.errno in (errno.ENOENT, errno.EINVAL):
363
345
# Handle output redirections
365
347
output = self._write_output(output, out_name, out_mode)
367
349
# If out_name cannot be created, we may get 'ENOENT', however if
368
350
# out_name is something like '', we can get EINVAL
369
351
if e.errno in (errno.ENOENT, errno.EINVAL):
384
366
# Handle output redirections
386
368
output = self._write_output(output, out_name, out_mode)
388
370
if e.errno in (errno.ENOENT, errno.EINVAL):
389
371
return 1, None, '%s: No such file or directory\n' % (out_name,)
424
406
def error(msg, path):
425
return "rm: cannot remove '%s': %s\n" % (path, msg)
407
return "rm: cannot remove '%s': %s\n" % (path, msg)
427
409
force, recursive = False, False
441
423
# FIXME: Should we put that in osutils ?
445
427
# Various OSes raises different exceptions (linux: EISDIR,
446
428
# win32: EACCES, OSX: EPERM) when invoked on a directory
447
429
if e.errno in (errno.EISDIR, errno.EPERM, errno.EACCES):
465
447
def do_mv(self, test_case, input, args):
468
449
def error(msg, src, dst):
469
450
return "mv: cannot move %s to %s: %s\n" % (src, dst, msg)
476
457
if os.path.isdir(dst):
477
458
real_dst = os.path.join(dst, os.path.basename(src))
478
459
os.rename(src, real_dst)
480
461
if e.errno == errno.ENOENT:
481
462
err = error('No such file or directory', src, dst)
505
487
self.overrideEnv('INSIDE_EMACS', '1')
507
489
def run_script(self, script, null_output_matches_anything=False):
508
return self.script_runner.run_script(self, script,
509
null_output_matches_anything=null_output_matches_anything)
490
return self.script_runner.run_script(self, script,
491
null_output_matches_anything=null_output_matches_anything)
511
493
def run_command(self, cmd, input, output, error):
512
494
return self.script_runner.run_command(self, cmd, input, output, error)
541
523
def run_script(self, script, null_output_matches_anything=False):
542
524
return self.script_runner.run_script(self, script,
543
null_output_matches_anything=null_output_matches_anything)
525
null_output_matches_anything=null_output_matches_anything)
545
527
def run_command(self, cmd, input, output, error):
546
528
return self.script_runner.run_command(self, cmd, input, output, error)
549
531
def run_script(test_case, script_string, null_output_matches_anything=False):
550
532
"""Run the given script within a testcase"""
551
533
return ScriptRunner().run_script(test_case, script_string,
552
null_output_matches_anything=null_output_matches_anything)
534
null_output_matches_anything=null_output_matches_anything)