def readmegen(all_stories, project_dir, story_dir, build_dir, project_name, check=False): docfolder = build_dir / "readme" if docfolder.exists(): docfolder.rmtree(ignore_errors=True) docfolder.mkdir() directory_template(all_stories, project_dir, story_dir, docfolder, readme=True).ensure_built() import re text_with_absolute_links = re.sub( r"(\[.*?\])\(((?!http).*?)\)", r"\g<1>(https://hitchdev.com/{0}/\g<2>)".format(project_name), docfolder.joinpath("index.md").text(), ).replace("\r\n", "\n") if check: Templex(changelog(project_dir)).assert_match( project_dir.joinpath("CHANGELOG.md").read_text()) Templex(text_with_absolute_links).assert_match( project_dir.joinpath("README.md").read_text()) print("README and CHANGELOG are correct.") else: project_dir.joinpath("CHANGELOG.md").write_text(changelog(project_dir)) project_dir.joinpath("README.md").write_text(text_with_absolute_links) print("README and CHANGELOG generated")
def run(self, code, will_output=None, raises=None): to_run = self.example_py_code.with_code(code) if self._cprofile: to_run = to_run.with_cprofile( self.path.profile.joinpath("{0}.dat".format(self.story.slug))) result = (to_run.expect_exceptions().run() if raises is not None else to_run.run()) if will_output is not None: actual_output = "\n".join( [line.rstrip() for line in result.output.split("\n")]) try: Templex(will_output).assert_match(actual_output) except AssertionError: if self._rewrite: self.current_step.update(**{"will output": actual_output}) else: raise if raises is not None: differential = False # Difference between python 2 and python 3 output? exception_type = raises.get("type") message = raises.get("message") if exception_type is not None: if not isinstance(exception_type, str): differential = True exception_type = ( exception_type["in python 2"] if self.given["python version"].startswith("2") else exception_type["in python 3"]) if message is not None: if not isinstance(message, str): differential = True message = (message["in python 2"] if self.given["python version"].startswith("2") else message["in python 3"]) try: result = self.example_py_code.expect_exceptions().run() result.exception_was_raised(exception_type) exception_message = self._story_friendly_output( result.exception.message) Templex(exception_message).assert_match(message) except AssertionError: if self._rewrite and not differential: new_raises = raises.copy() new_raises["message"] = self._story_friendly_output( result.exception.message) self.current_step.update(raises=new_raises) else: raise
def run(self, code, will_output=None, yaml_output=True, raises=None, in_interpreter=False): if in_interpreter: code = '{0}\nprint(repr({1}))'.format( '\n'.join(code.strip().split('\n')[:-1]), code.strip().split('\n')[-1]) to_run = self.example_py_code.with_code(code) if self.settings.get("cprofile"): to_run = to_run.with_cprofile( self.path.profile.joinpath("{0}.dat".format(self.story.slug))) result = to_run.expect_exceptions().run( ) if raises is not None else to_run.run() if will_output is not None: actual_output = '\n'.join( [line.rstrip() for line in result.output.split("\n")]) try: Templex(will_output).assert_match(actual_output) except NonMatching: if self.settings.get("rewrite"): self.current_step.update(**{"will output": actual_output}) else: raise if raises is not None: differential = False # Difference between python 2 and python 3 output? exception_type = raises.get('type') message = raises.get('message') if exception_type is not None: if not isinstance(exception_type, str): differential = True exception_type = exception_type['in python 2']\ if self.given['python version'].startswith("2")\ else exception_type['in python 3'] if message is not None: if not isinstance(message, str): differential = True message = message['in python 2']\ if self.given['python version'].startswith("2")\ else message['in python 3'] try: result = self.example_py_code.expect_exceptions().run() result.exception_was_raised(exception_type, message) except ExpectedExceptionMessageWasDifferent as error: if self.settings.get("rewrite") and not differential: new_raises = raises.copy() new_raises['message'] = result.exception.message self.current_step.update(raises=new_raises) else: raise
def parsed_as(self, example_json): try: Templex(example_json).assert_match( self._parser.parse_to_json(self.given['string'])) except AssertionError: if self._rewrite: self.current_step.update(example_json=self._parser. parse_to_json(self.given['string'])) else: raise
def run(self, code, will_output=None): result = self.example_py_code.with_code(code).run() if will_output is not None: try: Templex(will_output).assert_match(result.output) except AssertionError: if self._rewrite: self.current_step.update(**{"will output": result.output}) else: raise
def run(self, code, will_output=None, raises=None): self.example_py_code = ExamplePythonCode(self.python, self.path.state)\ .with_terminal_size(160, 100)\ .with_setup_code(self.given['setup'])\ .in_dir(self.path.state) to_run = self.example_py_code.with_code(code) if self.settings.get("cprofile"): to_run = to_run.with_cprofile( self.path.profile.joinpath("{0}.dat".format(self.story.slug)) ) result = to_run.expect_exceptions().run() if raises is not None else to_run.run() actual_output = result.output if will_output is not None: try: Templex(will_output).assert_match(actual_output) except AssertionError: if self.settings.get("overwrite artefacts"): self.current_step.update(**{"will output": actual_output}) else: raise if raises is not None: exception_type = raises.get('type') message = raises.get('message') try: result.exception_was_raised(exception_type) exception_message = self._process_exception(result.exception.message) Templex(message).assert_match(exception_message) except AssertionError: if self.settings.get("overwrite artefacts"): new_raises = raises.copy() new_raises['message'] = exception_message self.current_step.update(raises=new_raises) else: raise
def sql(self, expected_result): exercise_file = self._path.project / self.given['exercise'] assert exercise_file.exists(), "{} doesn't exist.".format( self.given['exercise']) actual_result = self._psql("-f", self._path.project / self.given['exercise']).output() if actual_result != expected_result: try: Templex(expected_result).assert_match(actual_result) except AssertionError: if self._rewrite: self.current_step.update(expected_result=actual_result) else: raise
def build_output_is(self, files): for filename, content in files.items(): filepath = self.path.state.joinpath(filename) assert filepath.exists(), "{0} does not exist".format(filename) if content != "": try: Templex(content).assert_match(filepath.text()) except AssertionError as error: raise AssertionError("{0} is nonmatching:\n\n{1}".format(filename, error)) actual_files = [ path.replace(self.path.state + "/", "") for path in pathquery(self.path.state.joinpath("example")).is_not_dir() ] assert len(actual_files) == len(files.keys()), \ "Should be:\n\n{0}\n\nAre actually:\n\n{1}\n".format( '\n'.join(files.keys()), '\n'.join(actual_files), )
def run(self, cmd=None, will_output=None, timeout=240, exit_code=0): process = self.snapshot.cmd(cmd.replace("\n", " ; ")).interact().screensize( 160, 100).run() process.wait_for_finish(timeout=timeout) actual_output = '\n'.join(process.stripshot().split("\n")[:-1]) if will_output is not None: try: Templex(will_output).assert_match(actual_output) except AssertionError: if self._rewrite: self.current_step.update(**{"will output": actual_output}) else: raise assert process.exit_code == exit_code, "Exit code should be {} was {}, output:\n{}".format( exit_code, process.exit_code, actual_output, )
def _run(self, command, args, will_output=None, exit_code=0, timeout=5): process = command(*shlex.split(args)).interact().screensize(160, 80).run() process.wait_for_finish() actual_output = process.stripshot()\ .replace(self.path.state, "/path/to") # Replace 35.1 SECONDS with n.n SECONDS actual_output = re.sub("[0-9]+\.[0-9]", "n.n", actual_output) if will_output is not None: try: Templex(will_output).assert_match(actual_output) except AssertionError: if self._rewrite: self.current_step.update(**{"will output": actual_output}) else: raise assert process.exit_code == exit_code, "Exit code should be {} was {}, output:\n{}".format( exit_code, process.exit_code, actual_output, )
def run( self, code, will_output=None, yaml_output=True, raises=None, in_interpreter=False, ): if in_interpreter: if self.given["python version"].startswith("3"): code = "{0}\nprint(repr({1}))".format( "\n".join(code.strip().split("\n")[:-1]), code.strip().split("\n")[-1] ) else: code = "{0}\nprint repr({1})".format( "\n".join(code.strip().split("\n")[:-1]), code.strip().split("\n")[-1] ) to_run = self.example_py_code.with_code(code) if self._cprofile: to_run = to_run.with_cprofile( self.path.profile.joinpath("{0}.dat".format(self.story.slug)) ) if raises is None: result = ( to_run.expect_exceptions().run() if raises is not None else to_run.run() ) if will_output is not None: actual_output = "\n".join( [line.rstrip() for line in result.output.split("\n")] ) try: Templex(will_output).assert_match(actual_output) except AssertionError: if self._rewrite: self.current_step.update(**{"will output": actual_output}) else: raise elif raises is not None: differential = False # Difference between python 2 and python 3 output? exception_type = raises.get("type") message = raises.get("message") if exception_type is not None: if not isinstance(exception_type, str): differential = True exception_type = ( exception_type["in python 2"] if self.given["python version"].startswith("2") else exception_type["in python 3"] ) if message is not None: if not isinstance(message, str): differential = True message = ( message["in python 2"] if self.given["python version"].startswith("2") else message["in python 3"] ) try: result = to_run.expect_exceptions().run() result.exception_was_raised(exception_type, message) except ExpectedExceptionMessageWasDifferent as error: if self._rewrite and not differential: new_raises = raises.copy() new_raises["message"] = result.exception.message self.current_step.update(raises=new_raises) else: raise
def assert_text(self, will_output, actual_output): Templex(will_output).assert_match(actual_output)
def output_ends_with(self, contents): Templex(contents).assert_match(self.result.output.split("\n")[-1])