Example #1
0
def parse_text_fakefs(
        text: Text, name: Text, print_on_error: bool, *,
        f_import: Optional[Callable],
        filename: Text) -> Tuple[ast.Module, type_info_mod.TypeInfo]:
    """Wraps parse_text with a *fake filesystem* holding "text" in "filename".

  This primarily exists for use from testing infrastructure! For binaries and
  libraries one would expect things to be in runfiles instead of text.

  Args:
    text: Text to put in the fake file.
    name: Name to use for the module.
    print_on_error: Whether to print to stderr if an error is encountered
      parsing/typechecking the DSLX text.
    f_import: Hook used when a module needs to import another module it depends
      upon.
    filename: Path to use in the fake filesystem for the contexts of the fake
      file (with DSLX text).

  Returns:
    The DSLX module and the type information.
  """
    fs = fakefs.FakeFilesystem()
    fakefs_util.create_file(fs, filename, contents=text)
    fake_open = fakefs.FakeFileOpen(fs)
    return parse_text(text,
                      name,
                      print_on_error=print_on_error,
                      f_import=f_import,
                      filename=filename,
                      fs_open=fake_open)
Example #2
0
def parse_text_fakefs(
        text: Text, name: Text, print_on_error: bool, *,
        import_cache: ImportCache, additional_search_paths: Tuple[str, ...],
        filename: Text) -> Tuple[ast.Module, type_info_mod.TypeInfo]:
    """Wraps parse_text with a *fake filesystem* holding "text" in "filename".

  This primarily exists for use from testing infrastructure! For binaries and
  libraries one would expect things to be in runfiles instead of text.

  Args:
    text: Text to put in the fake file.
    name: Name to use for the module.
    print_on_error: Whether to print to stderr if an error is encountered
      parsing/typechecking the DSLX text.
    import_cache: Import cache to use for any module dependencies.
    additional_search_paths: Additional search paths to use on import.
    filename: Path to use in the fake filesystem for the contexts of the fake
      file (with DSLX text).

  Returns:
    The DSLX module and the type information.
  """
    fs = fakefs.FakeFilesystem()
    fakefs_util.create_file(fs, filename, contents=text)
    fake_open = fakefs.FakeFileOpen(fs)
    return parse_text(text,
                      name,
                      print_on_error=print_on_error,
                      import_cache=import_cache,
                      additional_search_paths=additional_search_paths,
                      filename=filename,
                      fs_open=fake_open)
Example #3
0
def scoped_fakefs(path: str, contents: str):
    with ffu.Patcher() as patcher:
        yield create_file(patcher.fs, path, contents=contents)