Exemplo n.º 1
0
    def test_import_module_with_invalid_cache_magic_number(
            self, module_dir, cached_module_ns, cached_module_file,
            load_namespace):
        cache_filename = importer._cache_from_source(
            os.path.join(module_dir, cached_module_file))
        with open(cache_filename, mode="r+b") as f:
            f.seek(0)
            f.write(b"1999")

        using_cache = load_namespace(cached_module_ns)
        assert cached_module_ns == using_cache.find(sym.symbol("val")).value
Exemplo n.º 2
0
    def test_import_module_with_invalid_rawsize(self, module_dir,
                                                cached_module_ns,
                                                cached_module_file,
                                                load_namespace):
        cache_filename = importer._cache_from_source(
            os.path.join(module_dir, cached_module_file))
        with open(cache_filename, mode="r+b") as f:
            f.seek(8)
            f.write(importer._w_long(7733))

        using_cache = load_namespace(cached_module_ns)
        assert cached_module_ns == using_cache.find(sym.symbol("val")).value
Exemplo n.º 3
0
    def test_import_module_with_truncated_timestamp(self, module_dir,
                                                    cached_module_ns,
                                                    cached_module_file,
                                                    load_namespace):
        cache_filename = importer._cache_from_source(
            os.path.join(module_dir, cached_module_file))
        with open(cache_filename, mode="w+b") as f:
            f.write(importer.MAGIC_NUMBER)
            f.write(b"abc")

        using_cache = load_namespace(cached_module_ns)
        assert cached_module_ns == using_cache.find(sym.symbol("val")).value
Exemplo n.º 4
0
    def make_new_module(self, module_dir):
        """Fixture returning a function which creates a new module and then
        removes it after the test run."""
        filenames = []

        def _make_new_module(*ns_path: str,
                             ns_name: str = "",
                             module_text: Optional[str] = None) -> None:
            """Generate a new module. If ns_name is not the empty string, use that
            name as the name of a Basilisp namespace with a single Var named `val`
            containing the name of the namespace. Otherwise, if module_text is not
            None, emit that module text directly to the generated module.

            You may specify only either the ns_name or module_text.

            This method always cleans up the module and any cached modules that it
            creates."""
            assert ns_name == "" or module_text is None

            if ns_path[:-1]:
                os.makedirs(os.path.join(module_dir, *ns_path[:-1]),
                            exist_ok=True)

            filename = os.path.join(module_dir, *ns_path)
            filenames.append(filename)

            with open(filename, mode="w") as mod:
                if ns_name != "" and module_text is None:
                    mod.write(f"(ns {ns_name}) (def val (name *ns*))")
                else:
                    mod.write(module_text)

        try:
            yield _make_new_module
        finally:
            for filename in filenames:
                os.unlink(filename)

                try:
                    os.unlink(importer._cache_from_source(filename))
                except FileNotFoundError:
                    pass