Exemple #1
0
    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.file_path_completer)
            self.assertIn(os.path.normcase(
                os.path.join(os.getcwd(), "src", "file.c")), res)
            self.assertIn(os.path.normcase(
                os.path.join(os.getcwd(), "root.c")), res)
            self.assertNotIn(os.path.normcase(
                os.path.join(os.getcwd(), "ignore_dir/src.c")), res)
            self.assertNotIn(os.path.normcase(
                os.path.join(os.getcwd(), "ignore_dir/src.js")), res)

        os.chdir(orig_cwd)
Exemple #2
0
    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.normcase(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(),
                                         self.file_path_completer)[0]),
                sorted(files))

        os.remove(".gitignore")
        os.chdir(orig_cwd)
Exemple #3
0
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 test_get_project_files_ci_mode(self):
        orig_cwd = os.getcwd()
        os.chdir(os.path.dirname(os.path.realpath(__file__)) + 
            os.sep + "file_globs_ci_testfiles")

        with suppress_stdout():
            res, _ = get_project_files(self.log_printer, self.printer, os.getcwd()
                , True)
            self.assertIn(os.path.join(os.getcwd(), "src", "file.c"), res)
            self.assertIn(os.path.join(os.getcwd(), "root.c"), res)
            self.assertIn(os.path.join(os.getcwd(), "ignore_dir", "src.c"), res)
            self.assertIn(os.path.join(os.getcwd(), "ignore_dir", "src.js"), res)

        os.chdir(orig_cwd)
    def test_get_project_files_ci_mode(self):
        orig_cwd = os.getcwd()
        os.chdir(os.path.dirname(os.path.realpath(__file__)) +
                 os.sep + "file_globs_ci_testfiles")

        with suppress_stdout():
            res, _ = get_project_files(self.log_printer,
                                       self.printer,
                                       os.getcwd(),
                                       self.file_path_completer,
                                       True)

            paths = [
                os.path.join(os.getcwd(), "src", "file.c"),
                os.path.join(os.getcwd(), "root.c"),
                os.path.join(os.getcwd(), "ignore_dir", "src.c"),
                os.path.join(os.getcwd(), "ignore_dir", "src.js"),
            ]

            for path in paths:
                self.assertIn(os.path.normcase(path), res)

        os.chdir(orig_cwd)
Exemple #6
0
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 no_print_func():
     with suppress_stdout():
         print("func")
         raise NotImplementedError
Exemple #8
0
 def test_question_typecast(self):
     with simulate_console_inputs("apples, mangoes"), suppress_stdout():
         response = ask_question(self.question_text, typecast=list)
         self.assertEqual(response, ["apples", "mangoes"])
 def no_print_func():
     with suppress_stdout():
         print('func')
         raise NotImplementedError
    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""")

        os.makedirs("another_folder", exist_ok=True)
        os.chdir("another_folder")
        with open('.gitignore', 'w') as f:
            f.write("""
# Start of gitignore
*.js
# End of gitignore""")
        os.chdir(os.path.dirname(os.path.realpath(__file__)))
        os.chdir("file_globs_gitignore_testfiles")
        os.makedirs("data", exist_ok=True)
        os.chdir("data")
        os.makedirs("sample", exist_ok=True)
        os.chdir("sample")
        with open('.gitignore', 'w') as f:
            f.write("""
# Start of gitignore
*.html
# End of gitignore""")
        os.chdir(os.path.dirname(os.path.realpath(__file__)))
        os.chdir("file_globs_gitignore_testfiles")
        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"),
                 os.path.join("another_folder", "another_file.c"),
                 os.path.join("data", "sample", "index.css"),
                 os.path.join("data", "example.py"),
                 ".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"),
                         os.path.join("another_folder", "script.js"),
                         os.path.join("data", "sample", "index.html"),
                         "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"]
        files += [os.path.join("another_folder", ".gitignore")]
        files += [os.path.join("data", "sample", ".gitignore")]

        gitignore_dir_list = [os.getcwd(),
                              os.path.join(os.getcwd(), "another_folder"),
                              os.path.join(os.getcwd(), "data", "sample")]
        globs = list(get_gitignore_glob(os.getcwd(), gitignore_dir_list))
        returned_files = collect_files(
            [os.path.join(os.getcwd(), "**")],
            self.log_printer,
            ignored_file_paths=globs)
        files = [os.path.normcase(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(),
                                         self.file_path_completer)[0]),
                sorted(files))

        os.remove(os.path.join("another_folder", ".gitignore"))
        os.remove(os.path.join("data", "sample", ".gitignore"))
        os.remove(".gitignore")
        os.chdir(orig_cwd)