Skip to content

Commit 6065eb3

Browse files
committed
fix(tests): combine rebase/resolve tests to check idempotence
1 parent e3a6fd3 commit 6065eb3

File tree

1 file changed

+118
-114
lines changed

1 file changed

+118
-114
lines changed
Lines changed: 118 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# -*- coding: utf-8 -*-
21
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
32
# vi: set ft=python sts=4 ts=4 sw=4 et:
3+
"""Check the resolving/rebasing feature of ``BasePath``s."""
44
from __future__ import print_function, unicode_literals
55

66
from ... import base as nib
@@ -22,157 +22,161 @@ class _test_spec(nib.TraitedSpec):
2222
nib.traits.Dict(nib.Str, nib.File()))
2323

2424

25-
def test_rebase_path_traits():
26-
"""Check rebase_path_traits."""
25+
def test_rebase_resolve_path_traits():
26+
"""Check rebase_path_traits and resolve_path_traits and idempotence."""
2727
spec = _test_spec()
2828

29-
a = rebase_path_traits(
30-
spec.trait('a'), '/some/path/f1.txt', '/some/path')
29+
v = '/some/path/f1.txt'
30+
a = rebase_path_traits(spec.trait('a'), v, '/some/path')
3131
assert a == Path('f1.txt')
3232

33-
a = rebase_path_traits(
34-
spec.trait('a'), '/some/path/f1.txt', '/some/other/path')
35-
assert a == Path('/some/path/f1.txt')
33+
a = resolve_path_traits(spec.trait('a'), a, '/some/path')
34+
assert a == Path(v)
3635

37-
b = rebase_path_traits(
38-
spec.trait('b'), ('/some/path/f1.txt', '/some/path/f2.txt'), '/some/path')
36+
a = rebase_path_traits(spec.trait('a'), v, '/some/other/path')
37+
assert a == Path(v)
38+
39+
a = resolve_path_traits(spec.trait('a'), a, '/some/path')
40+
assert a == Path(v)
41+
42+
v = ('/some/path/f1.txt', '/some/path/f2.txt')
43+
b = rebase_path_traits(spec.trait('b'), v, '/some/path')
3944
assert b == (Path('f1.txt'), Path('f2.txt'))
4045

41-
c = rebase_path_traits(
42-
spec.trait('c'), ['/some/path/f1.txt', '/some/path/f2.txt', '/some/path/f3.txt'],
43-
'/some/path')
46+
b = resolve_path_traits(spec.trait('b'), b, '/some/path')
47+
assert b == (Path(v[0]), Path(v[1]))
48+
49+
v = ['/some/path/f1.txt', '/some/path/f2.txt', '/some/path/f3.txt']
50+
c = rebase_path_traits(spec.trait('c'), v, '/some/path')
4451
assert c == [Path('f1.txt'), Path('f2.txt'), Path('f3.txt')]
4552

46-
d = rebase_path_traits(
47-
spec.trait('d'), 2.0, '/some/path')
48-
assert d == 2.0
53+
c = resolve_path_traits(spec.trait('c'), c, '/some/path')
54+
assert c == [Path(vp) for vp in v]
4955

50-
d = rebase_path_traits(
51-
spec.trait('d'), '/some/path/either.txt', '/some/path')
52-
assert '%s' % d == 'either.txt'
56+
v = 2.0
57+
d = rebase_path_traits(spec.trait('d'), v, '/some/path')
58+
assert d == v
5359

54-
e = rebase_path_traits(
55-
spec.trait('e'), ['/some/path/f1.txt', '/some/path/f2.txt', '/some/path/f3.txt'],
56-
'/some/path')
57-
assert e == [Path('f1.txt'), Path('f2.txt'), Path('f3.txt')]
60+
d = resolve_path_traits(spec.trait('d'), d, '/some/path')
61+
assert d == v
5862

59-
e = rebase_path_traits(
60-
spec.trait('e'), [['/some/path/f1.txt', '/some/path/f2.txt'], [['/some/path/f3.txt']]],
61-
'/some/path')
62-
assert e == [[Path('f1.txt'), Path('f2.txt')], [[Path('f3.txt')]]]
63+
v = '/some/path/either.txt'
64+
d = rebase_path_traits(spec.trait('d'), v, '/some/path')
65+
assert d == Path('either.txt')
6366

64-
ee = rebase_path_traits(
65-
spec.trait('ee'), [['/some/path/f1.txt', '/some/path/f2.txt'], [['/some/path/f3.txt']]],
66-
'/some/path')
67-
assert ee == [['/some/path/f1.txt', '/some/path/f2.txt'], [['/some/path/f3.txt']]]
67+
d = resolve_path_traits(spec.trait('d'), d, '/some/path')
68+
assert d == Path(v)
6869

69-
f = rebase_path_traits(
70-
spec.trait('f'), {'1': '/some/path/f1.txt'}, '/some/path')
71-
assert f == {'1': Path('f1.txt')}
70+
v = ['/some/path/f1.txt', '/some/path/f2.txt', '/some/path/f3.txt']
71+
e = rebase_path_traits(spec.trait('e'), v, '/some/path')
72+
assert e == [Path('f1.txt'), Path('f2.txt'), Path('f3.txt')]
7273

73-
g = rebase_path_traits(
74-
spec.trait('g'), 'some/path/either.txt', '/some/path')
75-
assert '%s' % g == 'some/path/either.txt'
74+
e = resolve_path_traits(spec.trait('e'), e, '/some/path')
75+
assert e == [Path(vp) for vp in v]
7676

77-
g = rebase_path_traits(
78-
spec.trait('g'), '/some/path/either.txt', '/some')
79-
assert '%s' % g == 'path/either.txt'
77+
v = [['/some/path/f1.txt', '/some/path/f2.txt'], [['/some/path/f3.txt']]]
78+
e = rebase_path_traits(spec.trait('e'), v, '/some/path')
79+
assert e == [[Path('f1.txt'), Path('f2.txt')], [[Path('f3.txt')]]]
8080

81-
g = rebase_path_traits(spec.trait('g'), 'string', '/some')
82-
assert '%s' % g == 'string'
81+
e = resolve_path_traits(spec.trait('e'), e, '/some/path')
82+
assert e == [[[Path(vpp) for vpp in vp] if isinstance(vp, list) else Path(vp) for vp in inner]
83+
for inner in v]
8384

84-
g = rebase_path_traits(spec.trait('g'), '2', '/some/path')
85-
assert g == '2' # You dont want this one to be a Path
85+
# These are Str - no rebasing/resolving should happen
86+
v = [['/some/path/f1.txt', '/some/path/f2.txt'], [['/some/path/f3.txt']]]
87+
ee = rebase_path_traits(spec.trait('ee'), v, '/some/path')
88+
assert ee == v
8689

87-
h = rebase_path_traits(spec.trait('h'), '2', '/some/path')
88-
assert h == '2'
90+
ee = resolve_path_traits(spec.trait('ee'), [['f1.txt', 'f2.txt'], [['f3.txt']]], '/some/path')
91+
assert ee == [['f1.txt', 'f2.txt'], [['f3.txt']]]
8992

90-
i = rebase_path_traits(spec.trait('i'), '/some/path/either/file.txt', '/some/path')
91-
assert '%s' % i == 'either/file.txt'
93+
v = {'1': '/some/path/f1.txt'}
94+
f = rebase_path_traits(spec.trait('f'), v, '/some')
95+
assert f == {'1': Path('path/f1.txt')}
9296

93-
i = rebase_path_traits(spec.trait('i'), ('/some/path/either/tuple/file.txt', 2), '/some/path')
94-
assert ('%s' % i[0], i[1]) == ('either/tuple/file.txt', 2)
97+
f = resolve_path_traits(spec.trait('f'), f, '/some')
98+
assert f == {k: Path(val) for k, val in v.items()}
9599

96-
j = rebase_path_traits(spec.trait('j'), '/some/path/either/file.txt', '/some/path')
97-
assert '%s' % j == 'either/file.txt'
100+
# Either(Str, File): passing in path-like apply manipulation
101+
v = '/some/path/either.txt'
102+
g = rebase_path_traits(spec.trait('g'), v, '/some/path')
103+
assert g == Path('either.txt')
98104

99-
j = rebase_path_traits(spec.trait('j'), ('/some/path/either/tuple/file.txt', 2), '/some/path')
100-
assert ('%s' % j[0], j[1]) == ('either/tuple/file.txt', 2)
105+
g = resolve_path_traits(spec.trait('g'), g, '/some/path')
106+
assert g == Path(v)
101107

102-
j = rebase_path_traits(spec.trait('j'), {'a': '/some/path/either/dict/file.txt'},
103-
'/some/path')
104-
assert j == {'a': Path('either/dict/file.txt')}
108+
g = rebase_path_traits(spec.trait('g'), v, '/some')
109+
assert g == Path('path/either.txt')
105110

111+
g = resolve_path_traits(spec.trait('g'), g, '/some')
112+
assert g == Path(v)
106113

107-
def test_resolve_path_traits():
108-
"""Check resolve_path_traits."""
109-
spec = _test_spec()
114+
# Either(Str, File): passing str discards File
115+
v = 'either.txt'
116+
g = rebase_path_traits(spec.trait('g'), v, '/some/path')
117+
assert g == v
118+
119+
# This is a problematic case, it is impossible to know whether this
120+
# was meant to be a string or a file.
121+
# In this implementation, strings take precedence
122+
g = resolve_path_traits(spec.trait('g'), g, '/some/path')
123+
assert g == v
110124

111-
a = resolve_path_traits(
112-
spec.trait('a'), 'f1.txt', '/some/path')
113-
assert a == Path('/some/path/f1.txt')
125+
v = 'string'
126+
g = rebase_path_traits(spec.trait('g'), v, '/some')
127+
assert g == v
114128

115-
a = resolve_path_traits(
116-
spec.trait('a'), '/already/absolute/f1.txt', '/some/path')
117-
assert a == Path('/already/absolute/f1.txt')
129+
# This is a problematic case, it is impossible to know whether this
130+
# was meant to be a string or a file.
131+
g = resolve_path_traits(spec.trait('g'), v, '/some')
132+
assert g == v
118133

119-
b = resolve_path_traits(
120-
spec.trait('b'), ('f1.txt', 'f2.txt'), '/some/path')
121-
assert b == (Path('/some/path/f1.txt'), Path('/some/path/f2.txt'))
134+
v = v
135+
g = rebase_path_traits(spec.trait('g'), v, '/some/path')
136+
assert g == v # You dont want this one to be a Path
122137

123-
c = resolve_path_traits(
124-
spec.trait('c'), ['f1.txt', 'f2.txt', 'f3.txt'],
125-
'/some/path')
126-
assert c == [Path('/some/path/f1.txt'), Path('/some/path/f2.txt'), Path('/some/path/f3.txt')]
138+
# This is a problematic case, it is impossible to know whether this
139+
# was meant to be a string or a file.
140+
g = resolve_path_traits(spec.trait('g'), g, '/some/path')
141+
assert g == v # You dont want this one to be a Path
127142

128-
d = resolve_path_traits(
129-
spec.trait('d'), 2.0, '/some/path')
130-
assert d == 2.0
143+
h = rebase_path_traits(spec.trait('h'), v, '/some/path')
144+
assert h == v
131145

132-
d = resolve_path_traits(
133-
spec.trait('d'), 'either.txt', '/some/path')
134-
assert '%s' % d == '/some/path/either.txt'
146+
h = resolve_path_traits(spec.trait('h'), h, '/some/path')
147+
assert h == v
135148

136-
e = resolve_path_traits(
137-
spec.trait('e'), ['f1.txt', 'f2.txt', 'f3.txt'],
138-
'/some/path')
139-
assert e == [Path('/some/path/f1.txt'), Path('/some/path/f2.txt'), Path('/some/path/f3.txt')]
149+
v = '/some/path/either/file.txt'
150+
i = rebase_path_traits(spec.trait('i'), v, '/some/path')
151+
assert i == Path('either/file.txt')
140152

141-
e = resolve_path_traits(
142-
spec.trait('e'), [['f1.txt', 'f2.txt'], [['f3.txt']]],
143-
'/some/path')
144-
assert e == [[Path('/some/path/f1.txt'), Path('/some/path/f2.txt')],
145-
[[Path('/some/path/f3.txt')]]]
153+
i = resolve_path_traits(spec.trait('i'), i, '/some/path')
154+
assert i == Path(v)
146155

147-
f = resolve_path_traits(
148-
spec.trait('f'), {'1': 'path/f1.txt'}, '/some')
149-
assert f == {'1': Path('/some/path/f1.txt')}
156+
v = ('/some/path/either/tuple/file.txt', 2)
157+
i = rebase_path_traits(spec.trait('i'), v, '/some/path')
158+
assert i == (Path('either/tuple/file.txt'), 2)
150159

151-
g = resolve_path_traits(
152-
spec.trait('g'), '/either.txt', '/some/path')
153-
assert g == Path('/either.txt')
160+
i = resolve_path_traits(spec.trait('i'), i, '/some/path')
161+
assert i == (Path(v[0]), v[1])
154162

155-
# This is a problematic case, it is impossible to know whether this
156-
# was meant to be a string or a file.
157-
# In this implementation, strings take precedence
158-
g = resolve_path_traits(
159-
spec.trait('g'), 'path/either.txt', '/some')
160-
assert g == 'path/either.txt'
163+
v = '/some/path/either/file.txt'
164+
j = rebase_path_traits(spec.trait('j'), v, '/some/path')
165+
assert j == Path('either/file.txt')
161166

162-
# This is a problematic case, it is impossible to know whether this
163-
# was meant to be a string or a file.
164-
g = resolve_path_traits(spec.trait('g'), 'string', '/some')
165-
assert g == 'string'
167+
j = resolve_path_traits(spec.trait('j'), j, '/some/path')
168+
assert j == Path(v)
166169

167-
# This is a problematic case, it is impossible to know whether this
168-
# was meant to be a string or a file.
169-
g = resolve_path_traits(spec.trait('g'), '2', '/some/path')
170-
assert g == '2' # You dont want this one to be a Path
170+
v = ('/some/path/either/tuple/file.txt', 2)
171+
j = rebase_path_traits(spec.trait('j'), ('/some/path/either/tuple/file.txt', 2), '/some/path')
172+
assert j == (Path('either/tuple/file.txt'), 2)
171173

172-
h = resolve_path_traits(spec.trait('h'), '2', '/some/path')
173-
assert h == '2'
174+
j = resolve_path_traits(spec.trait('j'), j, '/some/path')
175+
assert j == (Path(v[0]), v[1])
174176

175-
ee = resolve_path_traits(
176-
spec.trait('ee'), [['f1.txt', 'f2.txt'], [['f3.txt']]],
177-
'/some/path')
178-
assert ee == [['f1.txt', 'f2.txt'], [['f3.txt']]]
177+
v = {'a': '/some/path/either/dict/file.txt'}
178+
j = rebase_path_traits(spec.trait('j'), v, '/some/path')
179+
assert j == {'a': Path('either/dict/file.txt')}
180+
181+
j = resolve_path_traits(spec.trait('j'), j, '/some/path')
182+
assert j == {k: Path(val) for k, val in v.items()}

0 commit comments

Comments
 (0)