示例#1
0
def test_hide_file():
    """
    Dumb checks, need to improve tests here
    We need to verifiy that attrib has been run on windows and that file begins with dot on unixes
    """
    filename = "ofunctions.test_hide_file." + random_string(16) + ".file"
    test_directory = os.path.abspath(os.path.dirname(__file__))
    path = os.path.join(test_directory, filename)
    remove_file(path)

    with open(path, "w") as fp:
        fp.write("TEST")

    assert hide_file(path), "File is now hidden"
    assert hide_file(path, False), "File is now visible"

    remove_file(path)
示例#2
0
def test_remove_bom():
    utf8_with_bom_data = b"\xef\xbb\xbf\x13\x37\x00\x12\x05\x01\x12\x01\x05"
    utf8_without_bom_data = b"\x13\x37\x00\x12\x05\x01\x12\x01\x05"

    filename = "ofunctions.test_remove_bom." + random_string(16) + ".file"
    remove_file(filename)

    with open(filename, "wb") as fp:
        fp.write(utf8_with_bom_data)

    remove_bom(filename)

    with open(filename, "rb") as fp:
        file_data = fp.read()
    remove_file(filename)

    assert file_data == utf8_without_bom_data, "Test file does not look like it should"
示例#3
0
def get_writable_random_file(
    ident_str="tmp_file_utils",  # type: str
):
    # type: (...) -> Optional[str]
    """
    Try to return a path to a not yet existing random file
    """

    timestamp_format = "%Y-%m-%d.%H-%M-%S.%f"

    tmp_dir = get_writable_temp_dir()
    if tmp_dir:
        return os.path.join(
            tmp_dir,
            "{}.{}.{}.tmp".format(
                ident_str,
                datetime.utcnow().strftime(timestamp_format),
                random.random_string(16),
            ),
        )
    return None
示例#4
0
def test_check_file_timestamp_delta():
    """
    Windows file creation dates are VERY wrong when requested by python
    The following code will keep earlier file creation dates, even if file is removed
    Hence we'll add some random string to the filename to make sure the tests will not fail
    """
    filename = ("ofunctions.test_check_file_timestamp_delta." +
                random_string(16) + ".file")
    test_directory = os.path.abspath(os.path.dirname(__file__))
    path = os.path.join(test_directory, filename)
    remove_file(path)

    with open(path, "w") as file_handle:
        file_handle.write("test")
    result = check_file_timestamp_delta(path,
                                        years=0,
                                        days=0,
                                        hours=0,
                                        minutes=0,
                                        seconds=-2)
    assert result is False, "Just created file should not be older than 2 seconds"
    sleep(3)
    result = check_file_timestamp_delta(path,
                                        years=0,
                                        days=0,
                                        hours=0,
                                        minutes=0,
                                        seconds=-2)
    assert result is True, "Just created file should now be older than 2 seconds"
    remove_file(path)

    result = check_file_timestamp_delta(sys.argv[0],
                                        years=-200,
                                        days=0,
                                        hours=0,
                                        minutes=0,
                                        seconds=0)
    assert (
        result is False
    ), "Ahh see... A file older than 200 years ? Is my code still running in the year 2221 ?"
示例#5
0
def prepare_temp_file():
    filename = "ofunctions.checksum_file." + random_string(16) + ".file"
    test_directory = os.path.abspath(os.path.dirname(__file__))
    path = os.path.join(test_directory, filename)
    remove_file(path)
    return path