Example #1
0
def test_returns_shallow(tmp_path):
    mock_path_structure(tmp_path)
    result = list(datasnap(str(tmp_path), shallow=True))

    result_name_set = set(name for name, _, _ in result)
    result_parent_set = set(parent for _, parent, _ in result)
    expected_name_set = set()
    expected_parent_set = set()
    for i in mock_data:
        if i.isdir:
            parent, name = os.path.split(i.path)
            expected_name_set.add(name)
            fullparent = str(tmp_path.joinpath(parent))
            expected_parent_set.add(fullparent)

    # Check stats output for mutation.
    # Popping certain stats keys that mutate on re-inspection.
    result_stats = [stats for _, _, stats in result]
    expected_stats = [stats for _, _, stats in shallow_walk(tmp_path)]
    for key in ['st_mtime_ns', 'st_mtime', 'st_atime', 'st_atime_ns']:
        for st in result_stats + expected_stats:
            st.pop(key)
    for count, st in enumerate(result_stats):
        result_set = set(st.items())
        expected_set = set(expected_stats[count].items())
        assert result_set == expected_set

    # Check that names and parents are as expected.
    assert expected_name_set == result_name_set
    assert expected_parent_set == result_parent_set
Example #2
0
def test_shallow_walk_return_value(tmp_path, monkeypatch):
    mock_path_structure(tmp_path)

    # Build expected return sets.
    expected_path_set = set(
        os.path.join(str(tmp_path), i.path) for i in mock_data if i.isdir)
    expected_name_set = set(
        os.path.split(i.path)[1] for i in mock_data if i.isdir)

    results = list(shallow_walk(tmp_path))
    results_path_set = set(os.path.join(par, name) for name, par, _ in results)
    results_name_set = set(name for name, _p, _d in results)

    # Check all returns are directories.
    assert all(isdir for n, s, isdir in results)
    # Check all expected paths and names are present.
    assert expected_path_set == results_path_set
    assert expected_name_set == results_name_set
    # Check to make sure stats are not mutated.
    # Popping out certain keys that may change value upon re-inspection.
    for result in results:
        name, parent, stats = result
        compare = buildresult(parent, name)
        compare_name, compare_parent, compare_stats = compare
        assert compare_name == name
        assert compare_parent == parent
        assert os.path.exists(os.path.join(parent, name))
        for key in ['st_mtime_ns', 'st_mtime', 'st_atime', 'st_atime_ns']:
            compare_stats.pop(key)
            stats.pop(key)
        assert set(compare_stats.items()) == set(stats.items())
Example #3
0
def test_deep_walk_callback(tmp_path):
    mock_path_structure(tmp_path)
    collector = []
    def callback(increment):
        collector.append(increment)
    results = list(deep_walk(tmp_path, callback=callback))
    assert sum(collector) == len(mock_data)
Example #4
0
def test_md5_return_value(tmp_path):
    mock_path_structure(tmp_path)
    for i in mock_data:
        if i.isdir:
            continue
        fullpath = str(tmp_path.joinpath(Path(i.path)))
        result = md5(fullpath)
        assert isinstance(result, str)
        assert len(result) == 32
        assert result.lower() == result
Example #5
0
def test_returns_deep(tmp_path):
    mock_path_structure(tmp_path)
    result = list(datasnap(str(tmp_path)))

    result_name_set = set(name for name, _, isdir in result)
    result_parent_set = set(parent for _, parent, _ in result)
    expected_name_set = set()
    expected_parent_set = set()
    for i in mock_data:
        parent, name = os.path.split(i.path)
        expected_name_set.add(name)
        fullparent = str(tmp_path.joinpath(parent))
        expected_parent_set.add(fullparent)

    # Check that names and parents are as expected.
    assert expected_name_set == result_name_set
    assert expected_parent_set == result_parent_set
Example #6
0
def test_deep_walk_return_value(tmp_path):
    mock_path_structure(tmp_path)

    expected_name_set = set(Path(i.path).name for i in mock_data)
    expected_parent_set = set(
        str(tmp_path.joinpath(Path(i.path).parent)) for i in mock_data)
    
    results = list(deep_walk(tmp_path))
    result_name_set = set(r.name for r in results)
    result_parent_set = set(r.parent for r in results)
    assert result_name_set == expected_name_set
    assert result_parent_set == expected_parent_set

    results = [(name, par, frozenset(stats.keys())) for name, par, stats in results]
    expected = []
    for i in mock_data:
        name = Path(i.path).name
        parent = str(tmp_path.joinpath(Path(i.path).parent)) 
        build = buildresult(parent, name)
        expected.append((build.name, build.parent, frozenset(build.stats.keys())))
    assert set(results) == set(expected)
Example #7
0
def test_deep_walk_hash(tmp_path):
    mock_path_structure(tmp_path)
    results = list(deep_walk(tmp_path, hash=True))
    assert all([r.stats.get('md5') for r in results if not r.stats['isdir']])