def test_clean_directory_NO_py_file(set_up_current_2):
    starting_number_of_subfolder, current_size, _ = set_up_current_2

    kept_nb = 1

    assert current_size == len(os.listdir(CLEANING_TEST_DIR + CURRENT))
    assert starting_number_of_subfolder - current_size == len(
        os.listdir(CLEANING_TEST_DIR + PAST))

    past_dir = os.listdir(CLEANING_TEST_DIR + PAST)
    exp_dir = past_dir.pop()

    print("\nthe directory where is the python file: ", exp_dir)
    the_test_file = CLEANING_TEST_DIR + PAST + "/" + exp_dir + "/python_file_dont_delete_test.py"

    with open(the_test_file, 'w+') as f:
        pass

    assert os.path.isfile(the_test_file)

    with pytest.raises(AssertionError) as excinfo:
        experiment_directory_tools.clean_result_directory(CLEANING_TEST_DIR,
                                                          keep=kept_nb)
        raise " --> Abort directory cleaning. There is a python file present in the directory" in str(
            excinfo.value)

    assert os.path.isfile(
        the_test_file), "FAIL --> the python file was deleted!!!"

    assert current_size == len(os.listdir(CLEANING_TEST_DIR + CURRENT))
    assert starting_number_of_subfolder - current_size == len(
        os.listdir(CLEANING_TEST_DIR + PAST))
def test_clean_directory_2_in_current(set_up_current_2):
    starting_number_of_subfolder, current_size, kept_nb = set_up_current_2

    assert current_size == len(os.listdir(CLEANING_TEST_DIR + CURRENT))
    assert starting_number_of_subfolder - current_size == len(
        os.listdir(CLEANING_TEST_DIR + PAST))

    experiment_directory_tools.clean_result_directory(CLEANING_TEST_DIR,
                                                      keep=kept_nb)

    assert 0 == len(os.listdir(CLEANING_TEST_DIR + CURRENT))
    assert kept_nb == len(os.listdir(CLEANING_TEST_DIR + PAST))
def test_create_more_than_kept_number(set_up_current_2):
    starting_number_of_subfolder, current_size, kept_nb = set_up_current_2

    assert current_size == len(os.listdir(CLEANING_TEST_DIR + CURRENT))
    assert starting_number_of_subfolder - current_size == len(
        os.listdir(CLEANING_TEST_DIR + PAST))

    experiment_directory_tools.clean_result_directory(CLEANING_TEST_DIR,
                                                      keep=kept_nb)
    experiment_directory_tools.create_run_directory(CLEANING_TEST_DIR,
                                                    "new run", 111)
    experiment_directory_tools.create_run_directory(CLEANING_TEST_DIR,
                                                    "new run", 222)
    experiment_directory_tools.create_run_directory(CLEANING_TEST_DIR,
                                                    "new run", 333)
    experiment_directory_tools.create_run_directory(CLEANING_TEST_DIR,
                                                    "new run", 444)
    experiment_directory_tools.create_run_directory(CLEANING_TEST_DIR,
                                                    "new run", 555)
    experiment_directory_tools.create_run_directory(CLEANING_TEST_DIR,
                                                    "new run", 666)

    assert 6 == len(os.listdir(CLEANING_TEST_DIR + CURRENT))
    assert kept_nb == len(os.listdir(CLEANING_TEST_DIR + PAST))

    experiment_directory_tools.clean_result_directory(CLEANING_TEST_DIR,
                                                      keep=kept_nb)

    assert 0 == len(os.listdir(CLEANING_TEST_DIR + CURRENT))
    assert kept_nb + 1 == len(os.listdir(CLEANING_TEST_DIR + PAST))

    list_dir_substring = []
    for dir_str in os.listdir(CLEANING_TEST_DIR + PAST):
        list_dir_substring.extend(dir_str.split('-'))

    assert '111' in list_dir_substring
    assert '222' in list_dir_substring
    assert '333' in list_dir_substring
    assert '444' in list_dir_substring
    assert '555' in list_dir_substring
    assert '666' in list_dir_substring
Beispiel #4
0
def prep_test():
    clean_result_directory(MOCK_DIR)
    run_dir = create_run_directory(MOCK_DIR, "unit_test", UUID=uuid.uuid4())
    yield set_up(run_dir)