1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from __future__ import print_function |
3
|
|
|
import os |
4
|
|
|
from hashlib import md5 |
5
|
|
|
from yamldirs import create_files |
6
|
|
|
from dkfileutils import changed, path |
7
|
|
|
from dkfileutils.changed import Directory |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def test_empty_digest(): |
11
|
|
|
files = """ |
12
|
|
|
emptydir: [] |
13
|
|
|
""" |
14
|
|
|
with create_files(files) as directory: |
15
|
|
|
assert changed.digest('emptydir') == md5(b"").hexdigest() |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def test_changed(): |
19
|
|
|
files = """ |
20
|
|
|
emptydir: [] |
21
|
|
|
""" |
22
|
|
|
with create_files(files) as directory: |
23
|
|
|
assert changed.changed('emptydir') |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def test_changed_glob(): |
27
|
|
|
files = """ |
28
|
|
|
a: |
29
|
|
|
b: |
30
|
|
|
c: |
31
|
|
|
d.txt: hello world |
32
|
|
|
e: |
33
|
|
|
f.rst: thanks for all the fish |
34
|
|
|
""" |
35
|
|
|
with create_files(files) as _root: |
36
|
|
|
adir = changed.Directory('a') |
37
|
|
|
print('digest:', changed.digest(adir)) |
38
|
|
|
|
39
|
|
|
# should be changed first time |
40
|
|
|
assert adir.changed(glob='**/*') |
41
|
|
|
assert adir.changed(glob='**/*.txt') |
42
|
|
|
assert adir.changed(glob='**/*.rst') |
43
|
|
|
|
44
|
|
|
print('digest:', changed.digest(adir)) |
45
|
|
|
|
46
|
|
|
# not changed second time |
47
|
|
|
assert not adir.changed(glob='**/*') |
48
|
|
|
assert not adir.changed(glob='**/*.txt') |
49
|
|
|
assert not adir.changed(glob='**/*.rst') |
50
|
|
|
|
51
|
|
|
dtxt = adir / 'b' / 'c' / 'd.txt' |
52
|
|
|
with dtxt.open('a') as fp: |
53
|
|
|
print('appended', file=fp) |
54
|
|
|
|
55
|
|
|
print("DTXT:", dtxt.read()) |
56
|
|
|
os.chdir(_root) |
57
|
|
|
|
58
|
|
|
print('digest:', changed.digest(adir)) |
59
|
|
|
print('cwd:', os.getcwd()) |
60
|
|
|
adir = changed.Directory('a') |
61
|
|
|
assert adir.changed(glob='**/*') |
62
|
|
|
assert adir.changed(glob='**/*.txt') |
63
|
|
|
assert not adir.changed(glob='**/*.rst') |
64
|
|
|
|
65
|
|
|
frst = adir / 'e' / 'f.rst' |
66
|
|
|
with frst.open('a') as fp: |
67
|
|
|
print('appended', file=fp) |
68
|
|
|
|
69
|
|
|
assert adir.changed(glob='**/*') |
70
|
|
|
assert not adir.changed(glob='**/*.txt') |
71
|
|
|
assert adir.changed(glob='**/*.rst') |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def test_missing(): |
75
|
|
|
assert changed.changed("this-directory-doesnt-exist") |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
def test_multifiles(): |
79
|
|
|
files = """ |
80
|
|
|
a: |
81
|
|
|
- b: | |
82
|
|
|
hello |
83
|
|
|
- c: | |
84
|
|
|
world |
85
|
|
|
""" |
86
|
|
|
with create_files(files) as directory: |
87
|
|
|
assert changed.changed('a') |
88
|
|
|
assert not Directory('a').changed() |
89
|
|
|
|