Skip to content

Commit

Permalink
Allow dots in Bazel labels (closes #25)
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 478033638
Change-Id: I45277d03dc6c9b4c956f963ce365296c4d0199e0
GitOrigin-RevId: fc7ac7f4f1fa0162310883ba4b1e7b11b895dbe2
  • Loading branch information
dubov94 committed Sep 30, 2022
1 parent 768cc00 commit 7835fd0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 4 additions & 2 deletions xmanager/xm_local/packaging/bazel_tools.py
Expand Up @@ -147,7 +147,7 @@ def _build_multiple_targets(


# Expansions (`...`, `*`) are not allowed.
_NAME_RE = '[^:/.*]+'
_NAME_RE = r'(?:[^.*:/]|\.(?!\.\.))+'
_LABEL_LEXER = re.compile(
f'^//(?P<packages>{_NAME_RE}(/{_NAME_RE})*)?(?P<target>:{_NAME_RE})?$')
_LexedLabel = Tuple[List[str], str]
Expand All @@ -161,8 +161,10 @@ def _lex_label(label: str) -> _LexedLabel:
groups = match.groupdict()
packages: Optional[str] = groups['packages']
target: Optional[str] = groups['target']
if packages is None and target is None:
if not packages and not target:
raise ValueError(f'{label} cannot be empty')
if target == ':all':
raise ValueError('`:all` is not a valid target')
init = packages.split('/') if packages else []
last = target[1:] if target else init[-1]
return init, last
Expand Down
17 changes: 17 additions & 0 deletions xmanager/xm_local/packaging/bazel_tools_test.py
Expand Up @@ -55,6 +55,23 @@ def test_label_kind_lines_to_dict(self):
},
)

def test_absolute_label_with_extension_dot(self):
self.assertEqual(
bazel_tools._lex_label('//project/directory:image.tar'),
(['project', 'directory'], 'image.tar'))

def test_label_with_three_dots(self):
with self.assertRaisesRegex(ValueError, 'is not an absolute Bazel label'):
bazel_tools._lex_label('//project/directory/...')

def test_label_with_star_target(self):
with self.assertRaisesRegex(ValueError, 'is not an absolute Bazel label'):
bazel_tools._lex_label('//project/directory:*')

def test_label_with_all_target(self):
with self.assertRaisesRegex(ValueError, '`:all` is not a valid target'):
bazel_tools._lex_label('//project/directory:all')


if __name__ == '__main__':
unittest.main()

0 comments on commit 7835fd0

Please sign in to comment.