def _test_appending(self, modified_input, expected_output): project = ProjectInDirectory(self.tmpdir) module_path = putfile( project.path, "module.py", read_data("appending_test_cases_module_initial.py")) test_module_path = putfile( project.path, "test_module.py", read_data("appending_test_cases_output_initial.py")) # Analyze the project with an existing test module. inspect_project(project) # Filesystem stat has resolution of 1 second, and we don't want to # sleep in a test, so we just fake the original files creation time. project["module"].created = 0 project["test_module"].created = 0 # Modify the application module and analyze it again. putfile(project.path, "module.py", read_data(modified_input)) inspect_project(project) # Regenerate the tests. add_tests_to_project(project, [module_path], 'unittest') project.save() assert_length(project.get_modules(), 2) result = read_file_contents(test_module_path) expected_result = read_data(expected_output) assert_equal_strings(expected_result, result)
def test_skips_dynamic_inspection_when_no_changes_were_made_to_the_project( self): project = ProjectInDirectory(self.tmpdir) inspect_project(project) assert_equal_strings( "INFO: No changes discovered in the source code, skipping dynamic inspection.\n", self._get_log_output())
def test_reports_each_inspected_point_of_entry(self): paths = ["one.py", "two.py"] project = ProjectInDirectory(self.tmpdir).with_points_of_entry(paths) inspect_project(project) for path in paths: assert_contains_once(self._get_log_output(), "INFO: Inspecting point of entry %s." % path)
def test_generates_test_stubs(self): expected_result = read_data("static_analysis_output.py") project = ProjectInDirectory(self.tmpdir) module_path = putfile(project.path, "module.py", read_data("static_analysis_module.py")) inspect_project(project) add_tests_to_project(project, [module_path], 'unittest') result = get_test_module_contents(project) assert_equal_strings(expected_result, result)
def test_skips_inspection_of_up_to_date_modules(self): paths = ["module.py", "something_else.py", P("module/in/directory.py")] project = ProjectInDirectory(self.tmpdir).with_modules(paths) inspect_project(project) for path in paths: assert_contains_once( self._get_log_output(), "DEBUG: %s hasn't changed since last inspection, skipping." % path)
def test_catches_exceptions_raised_by_entry_points(self): project = ProjectInDirectory(self.tmpdir).with_point_of_entry( "exc.py", "raise Exception") inspect_project(project) if sys.version_info < (2, 5): assert_contains_once( self._get_log_output(), "WARNING: Point of entry exited with error: <exceptions.Exception instance at" ) else: assert_contains_once( self._get_log_output(), "WARNING: Point of entry exited with error: Exception()")
def test_reports_each_inspected_module(self): paths = ["module.py", "something_else.py", P("module/in/directory.py")] project = ProjectInDirectory(self.tmpdir).with_modules(paths) # Force the inspection by faking files creation time. project["module"].created = 0 project["something_else"].created = 0 project["module.in.directory"].created = 0 inspect_project(project) for path in paths: assert_contains_once(self._get_log_output(), "INFO: Inspecting module %s." % path)
def execute_with_point_of_entry_and_assert(self, id): expected_result = read_data("%s_output.py" % id) project = ProjectInDirectory(self.tmpdir).with_points_of_entry( ["poe.py"]) module_path = putfile(project.path, "module.py", read_data("%s_module.py" % id)) write_content_to_file(read_data("generic_acceptance_poe.py"), project.path_for_point_of_entry("poe.py")) inspect_project(project) add_tests_to_project(project, [module_path], 'unittest') result = get_test_module_contents(project) assert_equal_strings(expected_result, result)
def test_removes_definitions_of_modules_that_dont_exist_anymore(self): project = ProjectInDirectory(self.tmpdir).with_modules( ["module.py", "other_module.py", "test_module.py"]) test_class = create(TestClass, associated_modules=[project["module"]]) add_test_case(project["test_module.py"], test_class) project.save() os.remove(os.path.join(project.path, "other_module.py")) remove_deleted_modules(project) assert_not_raises(ModuleNotFound, lambda: project["module"]) assert_raises(ModuleNotFound, lambda: project["other_module"]) assert_not_raises(ModuleNotFound, lambda: project["test_module"])
def test_warns_about_unreliable_implementation_of_util_generator_has_ended( self): if not hasattr(generator_has_ended, 'unreliable'): raise SkipTest paths = ["edgar.py", "allan.py"] project = ProjectInDirectory(self.tmpdir).with_points_of_entry(paths) inspect_project(project) assert_contains_once( self._get_log_output(), "WARNING: Pure Python implementation of " "util.generator_has_ended is not reliable on " "Python 2.4 and lower. Please compile the _util " "module or use Python 2.5 or higher.")
def test_doesnt_save_uncomplete_pickle_files(self): project = ProjectInDirectory(self.tmpdir) project.save() original_pickle = read_file_contents(project._get_pickle_path()) # Inject unpickable object into project. project._injected_attr = UNPICKABLE_OBJECT try: project.save() except PicklingError: pass # Make sure that the original file wasn't overwritten. assert_equal_strings(original_pickle, read_file_contents(project._get_pickle_path()))
def test_can_be_saved_and_restored_from_file(self): project = ProjectInDirectory(self.tmpdir).with_modules( ["good_module.py", "bad_module.py"]) project['good_module'].add_objects( [Class("AClass", [Method("amethod")]), Function("afunction")]) project['bad_module'].errors = ["Syntax error"] project.save() project = Project.from_directory(project.path) assert_equal(2, len(project.get_modules())) assert_equal(2, len(project['good_module'].objects)) assert_equal(["AClass"], get_names(project['good_module'].classes)) assert_equal(["amethod"], get_names(project['good_module'].classes[0].methods)) assert_equal(["afunction"], get_names(project['good_module'].functions)) assert_equal(["Syntax error"], project['bad_module'].errors)
def test_catches_string_exceptions_raised_by_entry_points(self): project = ProjectInDirectory(self.tmpdir).with_point_of_entry( "exc.py", "raise 'bad string'") inspect_project(project) if sys.version_info < (2, 6): assert_contains_once( self._get_log_output(), "WARNING: Point of entry exited with error: bad string") elif sys.version_info > ( 2, 6, 4): # Message changed a bit from 2.6.4 to 2.6.5 assert_contains_once( self._get_log_output(), "WARNING: Point of entry exited with error: " "TypeError('exceptions must be old-style classes or derived from BaseException, not str',)" ) else: assert_contains_once( self._get_log_output(), "WARNING: Point of entry exited with error: " "TypeError('exceptions must be classes or instances, not str',)" )
def test_changes_current_directory_to_the_projects_root(self): project = ProjectInDirectory(self.tmpdir) poe = PointOfEntryMock(project, content="import os; assert os.getcwd() == '%s'" % project.path) assert_not_raises(AssertionError, lambda: inspect_point_of_entry(poe))
def _init_project(self, module_code="", poe_content=""): self.project = ProjectInDirectory(self.tmpdir) putfile(self.project.path, "module.py", module_code) inspect_code(self.project, os.path.join(self.project.path, "module.py"), module_code) self.poe = PointOfEntryMock(self.project, content=poe_content)