def _f(self, filename=filename): print(filename) for h in ('--help', '-h'): cmd = [filename, h] if self.interpreter: cmd.insert(0, self.interpreter) try: p = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE) out, err = p.communicate(timeout=5) except OSError as e: self.fail("Error: %s" % e) except subprocess.SubprocessError as e: self.fail("Subprocess error: %s" % e) err = err.decode('utf-8') out = out.decode('utf-8') if self.check_merged_out_and_err: out = "%s\n%s" % (out, err) outl = out[:500].lower() # NOTE: # These assertions are heuristics, not policy. # If your script fails this test when it shouldn't # just add it to EXCLUDE_HELP above or change the # heuristic. # --help should produce: # * multiple lines of help on stdout (not stderr), # * including a "Usage:" string, # * not contradict itself or repeat options, # * and return success. #print(out.encode('utf8')) #print(err.encode('utf8')) if self.check_consistency: errors = check_help_consistency( out, self.options_start, self.options_end) if errors is not None: self.fail(errors) if self.check_return_code: self.assertEqual( p.returncode, 0, "%s %s\nreturncode should not be %d\n" "err:\n%s\nout:\n%s" % (filename, h, p.returncode, err, out)) if self.check_contains_usage: self.assertIn('usage', outl, 'lacks "Usage:"\n') if self.check_multiline: self.assertIn('\n', out, 'expected multi-line output')
def test_help_tree(self): # we call actual subprocesses, because we are probing the # actual help output where there is no sub-command. Don't copy # this if you have an actual command: for that use # self.runcmd() or self.runsubcmd(). known_commands = [[]] failed_commands = [] for i in range(4): new_commands = [] for c in known_commands: line = ' '.join(['samba-tool'] + c + ['--help']) try: output = self.check_output(line) except BlackboxProcessError as e: output = e.stdout failed_commands.append(c) output = get_string(output) tail = output.partition('Available subcommands:')[2] subcommands = re.findall(r'^\s*([\w-]+)\s+-', tail, re.MULTILINE) for s in subcommands: new_commands.append(c + [s]) # check that `samba-tool help X Y` == `samba-tool X Y --help` line = ' '.join(['samba-tool', 'help'] + c) try: output2 = self.check_output(line) except BlackboxProcessError as e: output2 = e.stdout failed_commands.append(c) output2 = get_string(output2) self.assertEqual(output, output2) err = check_help_consistency( output, options_start='Options:', options_end='Available subcommands:') if err is not None: self.fail("consistency error with %s:\n%s" % (line, err)) if not new_commands: break known_commands = new_commands self.assertEqual(failed_commands, [])