sapling/eden/integration/lib/temporary_directory.py
Matt Glazar 506cf4ebf8 Use TemporaryDirectoryMixin in CLI tests
Summary: CLI tests use shutil.rmtree to clean up temporary directories. Reuse TemporaryDirectoryMixin which is more robust against errors and supports EDEN_TEST_NO_CLEANUP.

Reviewed By: chadaustin

Differential Revision: D13268108

fbshipit-source-id: d77e95a2def0dceb34cf14e19c0c0c0e3aeef3f2
2018-12-03 14:42:11 -08:00

35 lines
1.0 KiB
Python

#!/usr/bin/env python3
#
# Copyright (c) 2016-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
import contextlib
import pathlib
import tempfile
import typing
from eden.test_support.temporary_directory import cleanup_tmp_dir
# TODO(strager): Merge create_tmp_dir with
# eden.test_support.temporary_directory.TemporaryDirectoryMixin.make_temporary_directory.
@contextlib.contextmanager
def create_tmp_dir() -> typing.Iterator[pathlib.Path]:
"""A helper class to manage temporary directories for snapshots.
This is similar to the standard tempdir.TemporaryDirectory code,
but does a better job of cleaning up the directory if some of its contents are
read-only.
"""
tmpdir = pathlib.Path(tempfile.mkdtemp(prefix="eden_data."))
try:
yield tmpdir
finally:
cleanup_tmp_dir(tmpdir)