예제 #1
0
def test_longname_windows(tmp_path):
    r"""
    longname() is a helper to deal with long Windows paths. It returns a Path
    object which, when os.fspath() is called on it, absolutifies its location
    and adds the magic \\?\ prefix.
    """
    deep = ['1234567890'] * 50

    nonexists = Path(*deep, 'nonexists.txt')
    assert not os.fspath(nonexists).startswith('\\\\?\\')
    assert len(os.fspath(nonexists)) > 500
    assert os.fspath(file.longname(nonexists)).startswith('\\\\?\\')
    assert Path.cwd().drive not in os.fspath(nonexists)
    assert Path.cwd().drive in os.fspath(file.longname(nonexists))

    exists = tmp_path.joinpath(*deep, 'exists.txt')
    file.longname(exists).parent.mkdir(parents=True)
    file.longname(exists).touch()
    assert not os.fspath(exists).startswith('\\\\?\\')
    assert len(os.fspath(exists)) > 500
    assert os.fspath(file.longname(exists)).startswith('\\\\?\\')
    assert exists.drive in os.fspath(exists)
    assert exists.drive in os.fspath(file.longname(exists))

    prefixed = '\\\\?\\' + os.fspath(exists)
    assert os.fspath(prefixed).count('?') == 1
    assert os.fspath(file.longname(prefixed)).count('?') == 1

    prefixed = '\\\\?\\' + os.fspath(nonexists)
    assert os.fspath(prefixed).count('?') == 1
    assert os.fspath(file.longname(prefixed)).count('?') == 1
예제 #2
0
def test_hash_long_path(tmp_path):
    """
    Bugfix regression test: must cope with long file paths.
    """
    dir = '0123456789' * 2
    tmp_file = tmp_path.joinpath(dir)
    while len(os.fspath(tmp_file)) <= 512:
        tmp_file = tmp_file.joinpath(dir)
    file.longname(tmp_file).parent.mkdir(parents=True)
    file.longname(tmp_file).write_bytes(b'123')
    expected = hashlib.sha256(b'123').digest()
    requests, responses = file.hasher(1)
    requests.put_many([tmp_file]).end()
    assert next(iter(responses)) == (tmp_file, expected)
예제 #3
0
def test_recurse_files(tmp_path):
    """
    recurse_files() reads the files in a directory and its subdirectories.
    """
    with tempfiles(tmp_path) as path:
        chnl = file.recurse_files(path.parent)
        assert isinstance(chnl, Channel)
        files = set(chnl)
        assert path in files
        # Starting point is not included.
        assert path.parent not in files
        assert set(file.recurse_files(path)) == set()
        all_files = set(file.recurse_files(tmp_path))
        # Test that it really is just files, not directories
        assert path.parent not in all_files
        assert all(f.is_file() for f in all_files if f.name != 'deep')
        assert all(file.longname(f).is_file() for f in all_files)
        # Test that filenames starting . are included.
        assert tmp_path.joinpath('.hidden') in all_files
        # Test that long paths are traversed
        assert any(f.name == 'deep' for f in all_files), all_files  # pragma: no branch
        # A containment check without set() could lead to abandoning the
        # iterator. We'd need more files to confirm that really has caused an
        # early exit in the worker thread, and anyway the Channel tests look into
        # that, so let's just make sure the proper code works.
        with file.recurse_files(tmp_path).cancel_context() as chnl:
            assert path in chnl
예제 #4
0
def tempfiles(tmpdir, file_size=0):
    (tmpdir / 'sub' / 'dir').mkdir(parents=True)
    (tmpdir / '.hidden').write_bytes(b'x' * file_size)
    deepfile = file.longname(Path(tmpdir, *(['1234567890'] * 50), 'deep'))
    deepfile.parent.mkdir(parents=True)
    deepfile.write_bytes(b'x' * file_size)
    filename = tmpdir / 'sub' / 'dir' / 'unhidden'
    filename.write_bytes(b'x' * file_size)
    yield filename
예제 #5
0
def test_longname_posix(tmp_path):
    """
    longname() is a helper to deal with long Windows paths. The POSIX version
    just does enough to allow for portable code.
    """
    deep = ['1234567890'] * 50

    nonexists = Path(*deep, 'nonexists.txt')
    assert not os.fspath(nonexists).startswith('\\\\?\\')
    assert len(os.fspath(nonexists)) > 500
    assert not os.fspath(file.longname(nonexists)).startswith('\\\\?\\')
    assert not os.fspath(nonexists).startswith('/')
    assert os.fspath(file.longname(nonexists)).startswith('/')

    exists = tmp_path.joinpath(*deep, 'exists.txt')
    file.longname(exists).parent.mkdir(parents=True)
    file.longname(exists).touch()
    assert not os.fspath(exists).startswith('\\\\?\\')
    assert len(os.fspath(exists)) > 500
    assert not os.fspath(file.longname(exists)).startswith('\\\\?\\')
    assert os.fspath(exists).startswith('/')
    assert os.fspath(file.longname(exists)).startswith('/')
예제 #6
0
def test_longname_portable(tmp_path):
    deep = ['1234567890'] * 50

    nonexists = Path(*deep, 'nonexists.txt')
    assert not os.fspath(nonexists).startswith('\\\\?\\')
    assert len(os.fspath(nonexists)) > 500
    assert not os.fspath(nonexists).startswith(os.path.sep)
    assert os.fspath(file.longname(nonexists)).startswith(os.path.sep)

    exists = tmp_path.joinpath(*deep, 'exists.txt')
    file.longname(exists).parent.mkdir(parents=True)
    file.longname(exists).touch()
    assert not os.fspath(exists).startswith('\\\\?\\')
    assert len(os.fspath(exists)) > 500
    assert os.fspath(file.longname(exists)).startswith(os.path.sep)