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