def iimport_objects(file_paths, names=None, types=None, supers=None, attributes=None, local=False, suppress_output=False): """ Import all objects from the given modules that fulfill the requirements :param file_paths: File path(s) from which objects will be imported. :param names: Name(s) an objects need to have one of. :param types: Type(s) an objects need to be out of. :param supers: Class(es) objects need to be a subclass of. :param attributes: Attribute(s) an object needs to (all) have. :param local: If True: Objects need to be defined in the file they appear in to be collected. :param suppress_output: Whether console output from stdout shall be suppressed or not. :return: An iterator that yields all matching python objects. :raises Exception: Any exception that is thrown in module code or an ImportError if paths are erroneous. """ with ExitStack() as stack: if not suppress_output: stack.enter_context(suppress_stdout()) yield from _iimport_objects(file_paths, names, types, supers, attributes, local)
def iimport_objects(file_paths, names=None, types=None, supers=None, attributes=None, local=False, verbose=False): """ Import all objects from the given modules that fulfill the requirements :param file_paths: File path(s) from which objects will be imported :param names: Name(s) an objects need to have one of :param types: Type(s) an objects need to be out of :param supers: Class(es) objects need to be a subclass of :param attributes: Attribute(s) an object needs to (all) have :param local: Objects need to be defined in the file they appear in if True :return: iterator that yields all matching python objects """ if not verbose: with suppress_stdout(): for obj in _iimport_objects(file_paths, names, types, supers, attributes, local): yield obj else: for obj in _iimport_objects(file_paths, names, types, supers, attributes, local): yield obj
def iimport_objects(file_paths, names=None, types=None, supers=None, attributes=None, local=False, verbose=False): """ Import all objects from the given modules that fulfill the requirements :param file_paths: File path(s) from which objects will be imported :param names: Name(s) an objects need to have one of :param types: Type(s) an objects need to be out of :param supers: Class(es) objects need to be a subclass of :param attributes: Attribute(s) an object needs to (all) have :param local: if True: Objects need to be defined in the file they appear in to be collected :return: iterator that yields all matching python objects :raises Exception: Any exception that is thrown in module code or an ImportError if paths are erroneous. """ if not verbose: with suppress_stdout(): for obj in _iimport_objects(file_paths, names, types, supers, attributes, local): yield obj else: for obj in _iimport_objects(file_paths, names, types, supers, attributes, local): yield obj
def test_get_project_files_gitignore(self): orig_cwd = os.getcwd() os.chdir(os.path.dirname(os.path.realpath(__file__))) os.makedirs("file_globs_gitignore_testfiles", exist_ok=True) os.chdir("file_globs_gitignore_testfiles") with open(".gitignore", "w") as f: f.write(""" # Start of gitignore build ignore.c /tests /upload.c /*.py *.pyc __pycache__ # End of gitignore""") files = [ os.path.join("src", "main.c"), os.path.join("src", "main.h"), os.path.join("src", "lib", "ssl.c"), os.path.join("src", "tests", "main.c"), os.path.join("src", "main.py"), os.path.join("src", "upload.c"), ".coafile" ] ignored_files = [ os.path.join("build", "main.c"), os.path.join("tests", "run.c"), os.path.join("src", "build", "main.c"), "ignore.c", os.path.join("src", "ignore.c"), "globexp.py", "upload.c", os.path.join("src", "main.pyc"), "run.pyc" ] for file in files + ignored_files: os.makedirs(os.path.dirname(os.path.abspath(file)), exist_ok=True) open(file, "w").close() files += [".gitignore"] globs = list(get_gitignore_glob(os.getcwd())) returned_files = collect_files([os.path.join(os.getcwd(), "**")], self.log_printer, ignored_file_paths=globs) files = [os.path.abspath(file) for file in files] ignored_files = [os.path.abspath(file) for file in ignored_files] self.maxDiff = None self.assertEqual(sorted(files), sorted(returned_files)) with suppress_stdout(): self.assertEqual( sorted( get_project_files(self.log_printer, self.printer, os.getcwd())[0]), sorted(files)) os.remove(".gitignore") os.chdir(orig_cwd)
def __check_module_skip(filename): module_dir = os.path.dirname(filename) if module_dir not in sys.path: sys.path.insert(0, module_dir) # Don't allow module code printing with suppress_stdout(): module = importlib.import_module( os.path.basename(os.path.splitext(filename)[0])) for name, object in inspect.getmembers(module): if inspect.isfunction(object) and name == "skip_test": return object() return False
def check_module_skip(filename): with preserve_sys_path(), suppress_stdout(): module_dir = os.path.dirname(filename) if module_dir not in sys.path: sys.path.insert(0, module_dir) try: module = importlib.import_module(os.path.basename(os.path.splitext(filename)[0])) for name, obj in inspect.getmembers(module): if inspect.isfunction(obj) and name == "skip_test": return obj() except ImportError as exception: return str(exception) return False
def delete_coverage(silent=False): """ Deletes previous coverage data. :return: False if coverage3 cannot be executed. """ coverage_available = False with suppress_stdout(): coverage_available = (execute_coverage_command("combine") == 0 and execute_coverage_command("erase") == 0) if not coverage_available and not silent: print("Coverage failed. Falling back to standard unit tests." "Install code coverage measurement for python3. Package" "name should be something like: python-coverage3/coverage") return coverage_available
def check_module_skip(filename): with preserve_sys_path(), suppress_stdout(): module_dir = os.path.dirname(filename) if module_dir not in sys.path: sys.path.insert(0, module_dir) try: module = importlib.import_module( os.path.basename(os.path.splitext(filename)[0])) for name, obj in inspect.getmembers(module): if inspect.isfunction(obj) and name == "skip_test": return obj() except ImportError as exception: return str(exception) return False
def test_get_project_files(self): orig_cwd = os.getcwd() os.chdir(os.path.dirname(os.path.realpath(__file__))) os.makedirs("file_globs_testfiles", exist_ok=True) os.chdir("file_globs_testfiles") os.makedirs("src", exist_ok=True) os.makedirs("ignore_dir", exist_ok=True) open(os.path.join("src", "file.c"), "w").close() open("root.c", "w").close() open(os.path.join("ignore_dir", "src.c"), "w").close() open(os.path.join("ignore_dir", "src.js"), "w").close() with suppress_stdout(), simulate_console_inputs("**.c", "ignore_dir/**"): res = get_project_files(self.log_printer, self.printer, os.getcwd()) self.assertIn(os.path.join(os.getcwd(), "src", "file.c"), res) self.assertIn(os.path.join(os.getcwd(), "root.c"), res) self.assertNotIn(os.path.join(os.getcwd(), "ignore_dir/src.c"), res) self.assertNotIn(os.path.join(os.getcwd(), "ignore_dir/src.js"), res) os.chdir(orig_cwd)
def test_get_project_files(self): orig_cwd = os.getcwd() os.chdir(os.path.dirname(os.path.realpath(__file__))) os.makedirs("file_globs_testfiles", exist_ok=True) os.chdir("file_globs_testfiles") os.makedirs("src", exist_ok=True) os.makedirs("ignore_dir", exist_ok=True) open(os.path.join("src", "file.c"), "w").close() open("root.c", "w").close() open(os.path.join("ignore_dir", "src.c"), "w").close() open(os.path.join("ignore_dir", "src.js"), "w").close() with suppress_stdout(), simulate_console_inputs("ignore_dir/**"): res, _ = get_project_files(self.log_printer, self.printer, os.getcwd()) self.assertIn(os.path.join(os.getcwd(), "src", "file.c"), res) self.assertIn(os.path.join(os.getcwd(), "root.c"), res) self.assertNotIn(os.path.join(os.getcwd(), "ignore_dir/src.c"), res) self.assertNotIn(os.path.join(os.getcwd(), "ignore_dir/src.js"), res) os.chdir(orig_cwd)
def no_print_func(): with suppress_stdout(): print("func") raise NotImplementedError