-
-
Notifications
You must be signed in to change notification settings - Fork 607
/
test_wrapped_globs.py
218 lines (183 loc) · 8.03 KB
/
test_wrapped_globs.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
import unittest
from textwrap import dedent
from pants.base.payload import Payload
from pants.build_graph.address_lookup_error import AddressLookupError
from pants.build_graph.build_file_aliases import BuildFileAliases
from pants.build_graph.files import Files
from pants.build_graph.target import Target
from pants.engine.fs import EMPTY_SNAPSHOT
from pants.source.wrapped_globs import EagerFilesetWithSpec, Filespec, LazyFilesetWithSpec
from pants.testutil.test_base import TestBase
class DummyTarget(Target):
def __init__(self, address=None, payload=None, sources=None, **kwargs):
payload = payload or Payload()
payload.add_fields(
{"sources": self.create_sources_field(sources, address.spec_path, key_arg="sources")}
)
super().__init__(address=address, payload=payload, **kwargs)
class FilesetRelPathWrapperTest(TestBase):
@classmethod
def alias_groups(cls):
return BuildFileAliases(targets={"dummy_target": DummyTarget})
def setUp(self) -> None:
super().setUp()
self.create_file("y/morx.java")
self.create_file("y/fleem.java")
self.create_file("z/w/foo.java")
self.create_link("y", "z/w/y")
def test_no_dir_glob(self) -> None:
self.create_file("y/f.txt")
self.add_to_build_file("y/BUILD", 'dummy_target(name="y", sources=["*"])')
self.context().scan()
def test_no_dir_glob_question(self) -> None:
self.create_file("y/?")
self.add_to_build_file("y/BUILD", 'dummy_target(name="y", sources=["?"])')
self.context().scan()
def _spec_test(self, spec: str, expected: Filespec) -> None:
self.add_to_build_file("y/BUILD", f'dummy_target(name="y", sources={spec})')
graph = self.context().scan()
globs = graph.get_target_from_spec("y").globs_relative_to_buildroot()
self.assertEqual(expected, globs)
def test_glob_to_spec(self) -> None:
self._spec_test('["*.java"]', {"globs": ["y/*.java"]})
def test_glob_to_spec_exclude(self) -> None:
self._spec_test(
'["*.java", "!fleem.java"]',
{"globs": ["y/*.java"], "exclude": [{"globs": ["y/fleem.java"]}]},
)
def test_glob_mid_single(self) -> None:
self.create_file("y/a/dir/Fleem.java")
self._spec_test('["a/*/Fleem.java"]', {"globs": ["y/a/*/Fleem.java"]})
def test_glob_to_spec_list(self) -> None:
self._spec_test('["fleem.java", "morx.java"]', {"globs": ["y/fleem.java", "y/morx.java"]})
def test_glob_exclude(self) -> None:
self.add_to_build_file(
"y/BUILD",
dedent(
"""
dummy_target(name="y", sources=["*.java", "!fleem.java"])
"""
),
)
graph = self.context().scan()
assert ["morx.java"] == list(
graph.get_target_from_spec("y").sources_relative_to_source_root()
)
def test_glob_with_folder_with_only_folders(self) -> None:
self.add_to_build_file(
"z/BUILD",
dedent(
"""
dummy_target(name="z", sources=["*", "!BUILD"])
"""
),
)
graph = self.context().scan()
self.assertEqual(
[], list(graph.get_target_from_spec("z").sources_relative_to_source_root())
)
def test_glob_exclude_doesnt_modify_exclude_array(self) -> None:
self.add_to_build_file(
"y/BUILD",
dedent(
"""
list_of_files = ["fleem.java"]
dummy_target(name="y", sources=["*.java", *(f"!{fp}" for fp in list_of_files)])
dummy_target(name="z", sources=list_of_files)
"""
),
)
graph = self.context().scan()
self.assertEqual(
["fleem.java"],
list(graph.get_target_from_spec("y:z").sources_relative_to_source_root()),
)
def test_subdir_glob(self) -> None:
self.create_file("y/dir/f.scala")
self.add_to_build_file("y/BUILD", 'dummy_target(name="y", sources=["dir/*.scala"])')
self.context().scan()
def test_subdir_glob_question(self) -> None:
self.create_file("y/dir/?.scala")
self.add_to_build_file("y/BUILD", 'dummy_target(name="y", sources=["dir/?.scala"])')
self.context().scan()
def test_subdir_with_dir_glob(self) -> None:
self.create_file("y/dir/test/f.scala")
self.add_to_build_file("y/BUILD", 'dummy_target(name="y", sources=["dir/**/*.scala"])')
self.context().scan()
@unittest.skip(reason="TODO: #4760")
def test_parent_dir_glob(self) -> None:
self.add_to_build_file("y/BUILD", 'dummy_target(name="y", sources=["../*.scala"])')
with self.assertRaises(AddressLookupError):
self.context().scan()
@unittest.skip(reason="TODO: #4760")
def test_parent_dir_glob_question(self) -> None:
self.add_to_build_file("y/BUILD", 'dummy_target(name="y", sources=["../?.scala"])')
with self.assertRaises(AddressLookupError):
self.context().scan()
@unittest.skip(reason="TODO: #4760")
def test_parent_dir_bracket_glob_question(self) -> None:
self.add_to_build_file(
"y/BUILD",
dedent(
"""
dummy_target(name="y", sources=["../[dir1, dir2]/?.scala"])
"""
),
)
with self.assertRaises(AddressLookupError):
self.context().scan()
@unittest.skip(reason="TODO: #4760")
def test_parent_dir_bracket(self) -> None:
self.add_to_build_file(
"y/BUILD",
dedent(
"""
dummy_target(name="y", sources=["../[dir1, dir2]/File.scala"])
"""
),
)
with self.assertRaises(AddressLookupError):
self.context().scan()
def test_absolute_dir_glob(self) -> None:
self.add_to_build_file("y/BUILD", 'dummy_target(name="y", sources=["/root/*.scala"])')
with self.assertRaises(AddressLookupError):
self.context().scan()
def test_absolute_dir_glob_question(self) -> None:
self.add_to_build_file("y/BUILD", 'dummy_target(name="y", sources=["/root/?.scala"])')
with self.assertRaises(AddressLookupError):
self.context().scan()
class FilesetWithSpecTest(TestBase):
@classmethod
def alias_groups(cls):
return BuildFileAliases(targets={"files": Files})
def test_lazy_fileset_with_spec_fails_if_filespec_not_prefixed_by_relroot(self) -> None:
with self.assertRaises(ValueError):
LazyFilesetWithSpec("foo", {"globs": ["notfoo/a.txt"]}, lambda: ["foo/a.txt"])
def test_eager_fileset_with_spec_fails_if_filespec_not_prefixed_by_relroot(self) -> None:
with self.assertRaises(ValueError):
EagerFilesetWithSpec("foo", {"globs": ["notfoo/a.txt"]}, EMPTY_SNAPSHOT)
def test_lazy_fileset_with_spec_fails_if_exclude_filespec_not_prefixed_by_relroot(self) -> None:
with self.assertRaises(ValueError):
LazyFilesetWithSpec(
"foo",
{"globs": [], "exclude": [{"globs": ["notfoo/a.txt"]}]},
lambda: ["foo/a.txt"],
)
def test_eager_fileset_with_spec_fails_if_exclude_filespec_not_prefixed_by_relroot(
self,
) -> None:
with self.assertRaises(ValueError):
EagerFilesetWithSpec(
"foo", {"globs": [], "exclude": [{"globs": ["notfoo/a.txt"]}]}, EMPTY_SNAPSHOT
)
def test_iter_relative_paths(self) -> None:
self.create_files("test_root", ["a", "b", "c"])
efws = self.sources_for(["a", "b", "c"], "test_root")
self.assertEqual(
efws.files_hash, b"cb11a7f0b5a1e22b93c36783608ba531ea831c2f68a5c9f9498417b211bcfea4",
)
self.assertEqual(
list(efws.paths_from_buildroot_iter()), ["test_root/a", "test_root/b", "test_root/c"],
)