Пример #1
0
def dir_contents_and_preprocessor_source(
        dir_contents__given_preprocessor_file_path: Callable[[pathlib.Path],
                                                             DirContents],
        preprocessor_py_source: str):
    """
    :param dir_contents__given_preprocessor_file_path: pathlib.Path -> DirContents
    A function that given the path of the file that contains the preprocessor, gives
    a DirContents.  This DirContents is then written by this method to
    a new tmp directory.
    A contextmanager that gives a pair of pathlib.Path:s:
    The first one is the root directory that contains the structure given
    by dir_contents.
    The other is the file that contains preprocessor_py_source.

    The preprocessor source file is guarantied to not be located inside
      the first directory.
    (test-case-file-path, preprocessor-source-file).
   """
    prefix = strftime(program_info.PROGRAM_NAME + '-test-', localtime())
    with tempfile.TemporaryDirectory(prefix=prefix +
                                     '-preprocessor-') as pre_proc_dir:
        preprocessor_file_path = resolved_path(
            pre_proc_dir) / 'preprocessor.py'
        with preprocessor_file_path.open('w') as f:
            f.write(preprocessor_py_source)
        dir_contents = dir_contents__given_preprocessor_file_path(
            preprocessor_file_path)
        with tempfile.TemporaryDirectory(
                prefix=prefix + '-dir-contents-') as dir_contents_root:
            dir_contents_dir_path = resolved_path(dir_contents_root)
            dir_contents.write_to(dir_contents_dir_path)
            yield (dir_contents_dir_path, preprocessor_file_path)
Пример #2
0
def home_directory_structure(contents: HdsPopulator = hds_populators.empty(),
                             prefix: str = program_info.PROGRAM_NAME + '-test-hds-') \
        -> HomeDs:
    with tempfile.TemporaryDirectory(prefix=prefix + '-case') as case_dir_name:
        with tempfile.TemporaryDirectory(prefix=prefix +
                                         '-act') as act_dir_name:
            hds = HomeDs(case_dir=resolved_path(case_dir_name),
                         act_dir=resolved_path(act_dir_name))
            contents.populate_hds(hds)
            yield hds
Пример #3
0
def tmp_file_containing(contents: str,
                        suffix: str = '',
                        directory=None) -> pathlib.Path:
    """
    Returns a context manager (used by with tmp_file(...) as file_path) ...
    The context manager takes care of deleting the file.

    :param contents: The contents of the returned file.
    :param suffix: A possible suffix of the file name (a dot does not automatically separate stem, suffix).
    :return: File path of a closed text file with the given contents.
    """
    path = None
    try:
        fd, absolute_file_path = tempfile.mkstemp(prefix=program_info.PROGRAM_NAME + '-test-',
                                                  suffix=suffix,
                                                  dir=directory,
                                                  text=True)
        fo = os.fdopen(fd, 'w+')
        fo.write(contents)
        fo.close()
        path = resolved_path(absolute_file_path)
        yield path
    finally:
        if path:
            path.unlink()
Пример #4
0
    def test_construct_sandbox_directory_structure(self):
        with tempfile.TemporaryDirectory(prefix=program_info.PROGRAM_NAME + '-test-') as tmp_dir_name:
            root = resolved_path(tmp_dir_name)
            tmp_dir_name = str(root)
            sds = sut.construct_at(tmp_dir_name)

            self._assert_is_existing_dir_with_given_number_of_files_in_it(sds.root_dir,
                                                                          4)

            self._assert_is_existing_empty_dir_with_name(root / 'tmp',
                                                         sds.user_tmp_dir)

            self._assert_is_existing_empty_dir_with_name(root / 'act',
                                                         sds.act_dir)

            self._assert_is_existing_empty_dir_with_name(root / 'result',
                                                         sds.result_dir)

            self._assert_is_existing_dir_with_given_number_of_files_in_it(root / 'internal',
                                                                          2)

            self._assert_is_existing_empty_dir_with_name(root / 'internal' / 'tmp',
                                                         sds.internal_tmp_dir)

            self._assert_is_existing_empty_dir_with_name(root / 'internal' / 'log',
                                                         sds.log_dir)
Пример #5
0
def single_dir_setup(contents: DirContents = empty_dir_contents()
                     ) -> SingleDirSetup:
    action_dir = Dir('act', contents.file_system_elements)
    tmp_dir_contents = DirContents([action_dir])
    with tempfile.TemporaryDirectory() as dir_name:
        dir_path = resolved_path(dir_name)
        tmp_dir_contents.write_to(dir_path)
        yield SingleDirSetup(dir_path / action_dir.name, )
Пример #6
0
def check_with_pre_proc(additional_arguments: List[str],
                        setup: SetupWithPreprocessor,
                        put: unittest.TestCase,
                        runner: Callable[[unittest.TestCase, List[str]], SubProcessResult]):
    with tempfile.TemporaryDirectory(prefix=program_info.PROGRAM_NAME + '-suite-test-preprocessor-') as pre_proc_dir:
        preprocessor_file_path = resolved_path(pre_proc_dir) / 'preprocessor.py'
        with preprocessor_file_path.open('w') as f:
            f.write(setup.preprocessor_source())
        with tempfile.TemporaryDirectory(prefix=program_info.PROGRAM_NAME + '-suite-test-dir-contents-') as tmp_dir:
            tmp_dir_path = resolved_path(tmp_dir)
            file_structure = setup.file_structure(tmp_dir_path,
                                                  sys.executable,
                                                  str(preprocessor_file_path))
            file_structure.write_to(tmp_dir_path)
            file_argument = str(setup.file_argument_based_at(tmp_dir_path))
            first_arguments = setup.first_arguments(tmp_dir_path, sys.executable, str(preprocessor_file_path))
            arguments = first_arguments + ARGUMENTS_FOR_TEST_INTERPRETER + additional_arguments + [file_argument]
            sub_process_result = runner(put, arguments)
            setup.check(put,
                        tmp_dir_path,
                        sub_process_result)
Пример #7
0
def check(additional_arguments: List[str],
          setup: SetupWithoutPreprocessor,
          put: unittest.TestCase,
          runner: Callable[[unittest.TestCase, List[str]], SubProcessResult]):
    with tempfile.TemporaryDirectory(prefix=program_info.PROGRAM_NAME + '-suite-test-') as tmp_dir:
        tmp_dir_path = resolved_path(tmp_dir)
        setup.file_structure(tmp_dir_path).write_to(tmp_dir_path)
        file_argument = str(setup.file_argument_based_at(tmp_dir_path))
        first_arguments = setup.first_arguments(tmp_dir_path)
        arguments_for_interpreter = setup.arguments_for_interpreter()
        arguments = first_arguments + arguments_for_interpreter + additional_arguments + [file_argument]
        sub_process_result = runner(put, arguments)
        setup.check(put,
                    tmp_dir_path,
                    sub_process_result)
Пример #8
0
def check(setup: Setup, put: unittest.TestCase):
    # ARRANGE #
    with tempfile.TemporaryDirectory(prefix=program_info.PROGRAM_NAME +
                                     '-test-') as tmp_dir:
        with preserved_cwd():
            tmp_dir_path = resolved_path(tmp_dir)
            os.chdir(str(tmp_dir_path))

            setup.file_structure_to_read().write_to(tmp_dir_path)
            # ACT & ASSERT #
            with put.assertRaises(Exception) as cm:
                # ACT #
                Reader(default_environment()).apply(
                    setup.root_suite_based_at(tmp_dir_path))
            # ASSERT #
            setup.check_exception(tmp_dir_path, cm.exception, put)
Пример #9
0
def check(setup: Setup, put: unittest.TestCase):
    with tempfile.TemporaryDirectory(prefix=program_info.PROGRAM_NAME +
                                     '-test-') as tmp_dir:
        tmp_dir_path = resolved_path(tmp_dir)
        setup.file_structure_to_read(tmp_dir_path).write_to(tmp_dir_path)
        test_case_handling_setup = setup.test_case_handling_setup()
        suite_reading_environment = Environment(
            setup.suite_configuration_section_parser(),
            _TEST_CASE_PARSING_SETUP, test_case_handling_setup)
        hierarchy_reader = Reader(suite_reading_environment)
        reporter = ExecutionTracingProcessingReporter()
        processor = Processor(
            _default_case_configuration(test_case_handling_setup),
            hierarchy_reader, reporter, DepthFirstEnumerator(),
            case_processing.
            new_processor_that_is_allowed_to_pollute_current_process)
        exit_code = processor.process(setup.root_suite_based_at(tmp_dir_path),
                                      null_output_reporting_environment())
        setup.assertions(put, reporter.complete_suite_reporter, exit_code)
Пример #10
0
def temp_dir(
        prefix: str = program_info.PROGRAM_NAME
) -> ContextManager[pathlib.Path]:
    dir_str = tempfile.mkdtemp(prefix=prefix)
    yield resolved_path(dir_str)
    shutil.rmtree(dir_str, onerror=_onerror_mk_read_write_and_retry)
Пример #11
0
def run_subprocess(cmd_and_args: List[str],
                   stdin_contents: str = '') -> SubProcessResult:
    with tempfile.TemporaryDirectory(prefix=program_info.PROGRAM_NAME + '-test-') as tmp_dir:
        return capture_subprocess(cmd_and_args,
                                  resolved_path(tmp_dir),
                                  stdin_contents=stdin_contents)