File: test_snapshot.py

package info (click to toggle)
borgmatic 2.0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,752 kB
  • sloc: python: 58,506; sh: 150; makefile: 8; javascript: 5
file content (108 lines) | stat: -rw-r--r-- 4,203 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from flexmock import flexmock

from borgmatic.borg.pattern import Pattern
from borgmatic.hooks.data_source import snapshot as module


def test_get_contained_patterns_without_candidates_returns_empty():
    flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=flexmock()))
    flexmock(module.os.path).should_receive('exists').and_return(True)

    assert module.get_contained_patterns('/mnt', {}) == ()


def test_get_contained_patterns_with_self_candidate_returns_self():
    device = flexmock()
    flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=device))
    flexmock(module.os.path).should_receive('exists').and_return(True)
    candidates = {
        Pattern('/foo', device=device),
        Pattern('/mnt', device=device),
        Pattern('/bar', device=device),
    }

    assert module.get_contained_patterns('/mnt', candidates) == (Pattern('/mnt', device=device),)
    assert candidates == {Pattern('/foo', device=device), Pattern('/bar', device=device)}


def test_get_contained_patterns_with_self_candidate_and_caret_prefix_returns_self():
    device = flexmock()
    flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=device))
    flexmock(module.os.path).should_receive('exists').and_return(True)
    candidates = {
        Pattern('^/foo', device=device),
        Pattern('^/mnt', device=device),
        Pattern('^/bar', device=device),
    }

    assert module.get_contained_patterns('/mnt', candidates) == (Pattern('^/mnt', device=device),)
    assert candidates == {Pattern('^/foo', device=device), Pattern('^/bar', device=device)}


def test_get_contained_patterns_with_child_candidate_returns_child():
    device = flexmock()
    flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=device))
    flexmock(module.os.path).should_receive('exists').and_return(True)
    candidates = {
        Pattern('/foo', device=device),
        Pattern('/mnt/subdir', device=device),
        Pattern('/bar', device=device),
    }

    assert module.get_contained_patterns('/mnt', candidates) == (
        Pattern('/mnt/subdir', device=device),
    )
    assert candidates == {Pattern('/foo', device=device), Pattern('/bar', device=device)}


def test_get_contained_patterns_with_grandchild_candidate_returns_child():
    device = flexmock()
    flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=device))
    flexmock(module.os.path).should_receive('exists').and_return(True)
    candidates = {
        Pattern('/foo', device=device),
        Pattern('/mnt/sub/dir', device=device),
        Pattern('/bar', device=device),
    }

    assert module.get_contained_patterns('/mnt', candidates) == (
        Pattern('/mnt/sub/dir', device=device),
    )
    assert candidates == {Pattern('/foo', device=device), Pattern('/bar', device=device)}


def test_get_contained_patterns_ignores_child_candidate_on_another_device():
    one_device = flexmock()
    another_device = flexmock()
    flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=one_device))
    flexmock(module.os.path).should_receive('exists').and_return(True)
    candidates = {
        Pattern('/foo', device=one_device),
        Pattern('/mnt/subdir', device=another_device),
        Pattern('/bar', device=one_device),
    }

    assert module.get_contained_patterns('/mnt', candidates) == ()
    assert candidates == {
        Pattern('/foo', device=one_device),
        Pattern('/mnt/subdir', device=another_device),
        Pattern('/bar', device=one_device),
    }


def test_get_contained_patterns_with_non_existent_parent_directory_ignores_child_candidate():
    device = flexmock()
    flexmock(module.os).should_receive('stat').and_return(flexmock(st_dev=device))
    flexmock(module.os.path).should_receive('exists').and_return(False)
    candidates = {
        Pattern('/foo', device=device),
        Pattern('/mnt/subdir', device=device),
        Pattern('/bar', device=device),
    }

    assert module.get_contained_patterns('/mnt', candidates) == ()
    assert candidates == {
        Pattern('/foo', device=device),
        Pattern('/mnt/subdir', device=device),
        Pattern('/bar', device=device),
    }