Пример #1
0
def test_ntf_simple_persistance():
    with volatile.file() as tmp:
        assert os.path.exists(tmp.name)
        tmp.close()
        assert os.path.exists(tmp.name)

    assert not os.path.exists(tmp.name)
Пример #2
0
def test_file_cleanup_after_exception():
    try:
        with volatile.file() as tmp:
            name = tmp.name
            assert os.path.exists(name)
            raise RuntimeError()
            pass
    except RuntimeError:
        pass

    assert not os.path.exists(name)
Пример #3
0
def xz_dir(target):
    """Creates a `.tar.xz`-formatted archive from a path, with all archive paths being relative to
    the `target` dir.

    Returns the archive in-memory."""
    with volatile.file() as archive:
        with tarfile.open(archive.name, "w:xz") as tar:
            for dirpath, _dirnames, filenames in os.walk(target):
                for filename in filenames:
                    full_path = os.path.join(dirpath, filename)
                    relpath = os.path.relpath(full_path, target)
                    tar.add(full_path, arcname=relpath)

        archive.close()
        return open(archive.name, "rb").read()
Пример #4
0
def edit(remote_path, create=True):
    with volatile.file() as tmp:
        created = False
        if create and not remote.lstat(remote_path):
            tmp.write('')
            created = True
        else:
            tmp.write(remote.file(remote_path, 'rb').read())
        tmp.close()

        try:
            ef = EditableFile(tmp.name)
            yield ef
        except Exception:
            raise
        else:
            if created or ef.modified:
                upload_file(ef.name, remote_path).changed
                ef.changed = True
            else:
                ef.changed = False
Пример #5
0
def edit(remote_path, create=True):
    with volatile.file() as tmp:
        created = False
        if create and not remote.lstat(remote_path):
            tmp.write('')
            created = True
        else:
            tmp.write(remote.file(remote_path, 'rb').read())
        tmp.close()

        try:
            ef = EditableFile(tmp.name)
            yield ef
        except Exception:
            raise
        else:
            if created or ef.modified:
                upload_file(ef.name, remote_path).changed
                ef.changed = True
            else:
                ef.changed = False
Пример #6
0
    def run_bowler_modifier(
        self,
        input_text,
        selector=None,
        modifier=None,
        selector_func=None,
        modifier_func=None,
        in_process=True,
        query_func=None,
    ):
        """Returns the modified text."""

        if not (selector or selector_func or query_func):
            raise ValueError("Pass selector")
        if not (modifier or modifier_func or query_func):
            raise ValueError("Pass modifier")

        exception_queue = multiprocessing.Queue()

        def store_exceptions_on(func):
            @functools.wraps(func)
            def inner(node, capture, filename):
                # When in_process=False, this runs in another process.  See notes below.
                try:
                    return func(node, capture, filename)
                except Exception as e:
                    exception_queue.put(e)

            return inner

        def default_query_func(files):
            if selector_func:
                q = selector_func(files)
            else:
                q = Query(files).select(selector)

            if modifier_func:
                q = modifier_func(q)
            else:
                q = q.modify(modifier)

            return q

        if query_func is None:
            query_func = default_query_func

        with volatile.file(mode="w", suffix=".py") as f:
            # TODO: I'm almost certain this will not work on Windows, since
            # NamedTemporaryFile has it already open for writing.  Consider
            # using mktemp directly?
            f.write(input_text + "\n")
            f.close()

            query = query_func([f.name])
            assert query is not None, "Remember to return the Query"
            assert query.retcode is None, "Return before calling .execute"
            assert len(query.transforms) == 1, "TODO: Support multiple"

            for i in range(len(query.current.callbacks)):
                query.current.callbacks[i] = store_exceptions_on(
                    query.current.callbacks[i]
                )

            # We require the in_process parameter in order to record coverage properly,
            # but it also helps in bubbling exceptions and letting tests read state set
            # by modifiers.
            query.execute(
                interactive=False, write=True, silent=False, in_process=in_process
            )

            # In the case of in_process=False (mirroring normal use of the tool) we use
            # the queue to ship back exceptions from local_process, which can actually
            # fail the test.  Normally exceptions in modifiers are not printed
            # at all unless you pass --debug, and even then you don't get the
            # traceback.
            # See https://github.com/facebookincubator/Bowler/issues/63
            if not exception_queue.empty():
                raise AssertionError from exception_queue.get()

            with open(f.name, "r") as fr:
                return fr.read().rstrip()
Пример #7
0
def mdv_print(md):
    with volatile.file(suffix='.md') as tmp:
        tmp.write(md.encode('utf8'))
        tmp.close()

        subprocess.check_call(['mdv', tmp.name])
Пример #8
0
def test_can_ignore_missing():
    with volatile.file(ignore_missing=True) as tmp:
        os.unlink(tmp.name)
Пример #9
0
def test_unlink_raises():
    with pytest.raises(OSError):
        with volatile.file() as tmp:
            os.unlink(tmp.name)
Пример #10
0
def test_keeps_content():
    with volatile.file() as tmp:
        tmp.write(b'foo')
        tmp.close()

        assert b'foo' == open(tmp.name, 'rb').read()