def test_help(self): with captured_output() as (out, err): result = main(["-h"]) self.assertEquals(result, 0) out = out.getvalue() err = err.getvalue() self.assertIn("Usage:", out)
def test_no_args(self): with LogCapture() as l: with captured_output() as (out, err): result = main([]) self.assertEquals(result, 2) l = str(l) err = err.getvalue() self.assertIn("Missing template filename", l) self.assertIn("Usage:", err)
def test_bad_format(self): with LogCapture() as l: with captured_output() as (out, err): result = main(["-f", "qwerty"]) self.assertEquals(result, 2) l = str(l) err = err.getvalue() self.assertIn("Invalid output format 'qwerty': valid types are", l) self.assertIn("Usage:", err)
def test_bad_args(self): with LogCapture() as l: with captured_output() as (out, err): result = main(["-x"]) self.assertEquals(result, 2) l = str(l) err = err.getvalue() self.assertIn("option -x not recognized", l) self.assertIn("Usage:", err)
def run_docs(self, template_filename, resource_filenames=(), expected_returncode=0, expected_filename=None, expected_errors=None, template_arg=False, local_tags=True, output_filename=None, long_parameters=True, format=None): args = [] if not local_tags: args += ["--no-local-tag" if long_parameters else "-n"] if output_filename is not None: args += ["--output" if long_parameters else "-o", output_filename] if format is not None: args += ["--format" if long_parameters else "-f", format] if template_arg: args += ["--template" if long_parameters else "-t", self.testdir + template_filename] else: args += [self.testdir + template_filename] args += [self.testdir + r for r in resource_filenames] log.info("Starting CLI with args=%s", args) with LogCapture() as log_capture: with captured_output() as (out, err): result = main(args) log.info("CLI returned with code %d" % result) out = out.getvalue() err = err.getvalue() if out: log.debug("stdout: %s", out) if err: log.debug("stderr: %s", err) # Redisplay the log records for record in log_capture.records: getLogger(record.name).handle(record) self.assertEquals(result, expected_returncode) if expected_filename is not None: if format == "json": load = json_load load_str = json_loads else: load = load_str = yaml_compose_all if output_filename: with open(output_filename, "r") as fd: actual = list(load(fd)) else: actual = list(load_str(out)) with open(self.testdir + expected_filename, "r") as fd: expected = list(load(fd)) for a_el, e_el in zip(actual, expected): if format == "json": self.assertEqual(a_el, e_el) else: try: self.assertTrue(nodes_equal(a_el, e_el)) except: log.error("actual=%s", a_el) log.error("expect=%s", e_el) raise log_capture = str(log_capture) if expected_errors is not None: if isinstance(expected_errors, string_types): self.assertIn(expected_errors, log_capture) else: for error in expected_errors: self.assertIn(error, log_capture) return