sapling/eden/integration/lib/temporary_directory.py
Matt Glazar 790947a4c1 Consolidate test temp dir creation and cleanup
Summary: Several tests create a temporary directory and destroy it. Consolidate the temporary directories and their cleanup code to reduce code duplication.

Reviewed By: simpkins

Differential Revision: D10251110

fbshipit-source-id: caa5b5ad49fcb7925f63094594af4a20009d790d
2018-10-10 17:29:39 -07:00

38 lines
1.1 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 abc
import os
import pathlib
import tempfile
import typing
from .util import cleanup_tmp_dir
class TemporaryDirectoryMixin(metaclass=abc.ABCMeta):
def make_temporary_directory(self) -> str:
def clean_up(path_str: str) -> None:
if os.environ.get("EDEN_TEST_NO_CLEANUP"):
print("Leaving behind eden test directory %r" % path_str)
else:
cleanup_tmp_dir(pathlib.Path(path_str))
path_str = tempfile.mkdtemp(prefix="eden_test.")
self.addCleanup(lambda: clean_up(path_str))
return path_str
def addCleanup(
self,
function: typing.Callable[..., typing.Any],
*args: typing.Any,
**kwargs: typing.Any
) -> None:
raise NotImplementedError()