Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/rapids_pre_commit_hooks/utils/dependencies_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from .yaml import (
Anchor,
AnchorPreservingLoader,
check_and_mark_anchor,
load_with_anchors,
node_has_type,
)

Expand Down Expand Up @@ -914,13 +914,8 @@ def traverse_root(


def traverse_dependencies_yaml(handler: Handler, content: str) -> None:
loader = AnchorPreservingLoader(content)
try:
root = loader.get_single_node()
assert root is not None
finally:
loader.dispose()
traverse_root(handler, loader.document_anchors[0], set(), root)
root, anchors = load_with_anchors(content)
traverse_root(handler, anchors, set(), root)


def is_python_output_type(output_type: str) -> bool:
Expand Down
10 changes: 10 additions & 0 deletions src/rapids_pre_commit_hooks/utils/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ def check_and_mark_anchor(

def is_reference_anchor(anchor: "Optional[Anchor]") -> bool:
return anchor is not None and anchor.anchor_type == AnchorType.REFERENCE


def load_with_anchors(stream) -> "tuple[yaml.Node, dict[str, yaml.Node]]":
loader = AnchorPreservingLoader(stream)
try:
root = loader.get_single_node()
assert root is not None
return root, dict(loader.document_anchors[0])
finally:
loader.dispose()
36 changes: 13 additions & 23 deletions tests/rapids_pre_commit_hooks/dependencies/test_cuda_suffixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@
CUDASuffixedHandler,
)
from rapids_pre_commit_hooks.utils import dependencies_yaml
from rapids_pre_commit_hooks.utils.yaml import Anchor, AnchorType
from rapids_pre_commit_hooks.utils.yaml import (
Anchor,
AnchorType,
load_with_anchors,
)
from rapids_pre_commit_hooks_test_utils import (
find_yaml_node_for_span,
parse_named_spans,
zip_expected_warnings,
)


def _compose(content):
loader = dependencies_yaml.AnchorPreservingLoader(content)
try:
return loader.get_single_node()
finally:
loader.dispose()


class TestCUDASuffixedHandler:
@pytest.mark.parametrize(
["output_type", "expected"],
Expand All @@ -49,7 +45,7 @@ def test_handle_output_type(self, output_type, expected):
assert context.has_python_output_type is True

def test_handle_common(self):
composed = _compose(
composed, _ = load_with_anchors(
"""\
common:
packages: []
Expand Down Expand Up @@ -143,7 +139,7 @@ def test_handle_common_item(
warnings,
):
content, spans = parse_named_spans(content, dict)
composed = _compose(content)
composed, _ = load_with_anchors(content)
common_key, common = composed.value[0]
linter = lint.Linter(
"dependencies.yaml", content, "verify-dependencies"
Expand Down Expand Up @@ -616,7 +612,7 @@ def test_handle_specific_item(
warnings,
):
content, spans = parse_named_spans(content, dict)
composed = _compose(content)
composed, _ = load_with_anchors(content)
linter = lint.Linter(
"dependencies.yaml", content, "verify-dependencies"
)
Expand Down Expand Up @@ -682,7 +678,7 @@ def test_handle_matrix(self):
+ cuda_suffixed: "true"
"""
)
composed = _compose(content)
composed, _ = load_with_anchors(content)
matrix_key, matrix = composed.value[0]
context = CUDASuffixedHandler.MatricesItemContext()
handler = CUDASuffixedHandler(Mock(), Mock())
Expand Down Expand Up @@ -770,7 +766,7 @@ def test_handle_matrix_item(
expected_cuda_major,
has_cuda_node,
):
composed = _compose(content)
composed, _ = load_with_anchors(content)
key, value = composed.value[0]
context = CUDASuffixedHandler.MatricesItemContext()
handler = CUDASuffixedHandler(Mock(), Mock())
Expand Down Expand Up @@ -907,7 +903,7 @@ def test_handle_package(
suffixed_names,
unsuffixed_names,
):
package_node = _compose(requirement)
package_node, _ = load_with_anchors(requirement)
rapids_version = SimpleNamespace(cuda_suffixed_packages={"package"})
context = CUDASuffixedHandler.PackagesContext(
CUDASuffixedHandler.MatricesItemContext(),
Expand Down Expand Up @@ -1191,11 +1187,7 @@ def test_handle_package(
def test_check_cuda_suffixed_integration(content, warnings):
content, spans = parse_named_spans(content, dict)

loader = dependencies_yaml.AnchorPreservingLoader(content)
try:
composed = loader.get_single_node()
finally:
loader.dispose()
composed, anchors = load_with_anchors(content)

args = Mock()
linter = lint.Linter("dependencies.yaml", content, "verify-dependencies")
Expand All @@ -1208,9 +1200,7 @@ def test_check_cuda_suffixed_integration(content, warnings):
"get_rapids_version",
return_value=rapids_version,
):
dependencies_yaml.traverse_root(
handler, loader.document_anchors[0], set(), composed
)
dependencies_yaml.traverse_root(handler, anchors, set(), composed)

assert linter.warnings == zip_expected_warnings(
spans.get("warnings", []), warnings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,13 @@
NamingConventionsHandler,
)
from rapids_pre_commit_hooks.utils import dependencies_yaml
from rapids_pre_commit_hooks.utils.yaml import load_with_anchors
from rapids_pre_commit_hooks_test_utils import (
parse_named_spans,
zip_expected_warnings,
)


def _compose(content):
loader = dependencies_yaml.AnchorPreservingLoader(content)
try:
return loader.get_single_node()
finally:
loader.dispose()


class TestNamingConventionsHandler:
@pytest.mark.parametrize(
["content", "expected_warnings"],
Expand Down Expand Up @@ -186,7 +179,7 @@ class TestNamingConventionsHandler:
)
def test_handle_files_item(self, content, expected_warnings):
content, spans = parse_named_spans(content, dict)
root = _compose(content)
root, _ = load_with_anchors(content)
files = root.value[0][1]
file_key, file_value = files.value[0]
file_fields = {key.value: value for key, value in file_value.value}
Expand Down Expand Up @@ -348,19 +341,13 @@ def test_handle_extras_key(self):
def test_check_naming_conventions_integration(content, warnings):
content, spans = parse_named_spans(content, dict)

loader = dependencies_yaml.AnchorPreservingLoader(content)
try:
composed = loader.get_single_node()
finally:
loader.dispose()
composed, anchors = load_with_anchors(content)

args = Mock()
linter = lint.Linter("dependencies.yaml", content, "verify-dependencies")
handler = NamingConventionsHandler(linter, args)

dependencies_yaml.traverse_root(
handler, loader.document_anchors[0], set(), composed
)
dependencies_yaml.traverse_root(handler, anchors, set(), composed)

assert linter.warnings == zip_expected_warnings(
spans.get("warnings", []), warnings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
is_nvidia_library_package,
)
from rapids_pre_commit_hooks.utils import dependencies_yaml
from rapids_pre_commit_hooks.utils.yaml import Anchor, AnchorType
from rapids_pre_commit_hooks.utils.yaml import (
Anchor,
AnchorType,
load_with_anchors,
)
from rapids_pre_commit_hooks_test_utils import (
find_yaml_node_for_span,
parse_named_spans,
Expand Down Expand Up @@ -142,11 +146,7 @@ def test_handle_common(
linter = lint.Linter(
"dependencies.yaml", content, "verify-dependencies"
)
loader = dependencies_yaml.AnchorPreservingLoader(content)
try:
composed = loader.get_single_node()
finally:
loader.dispose()
composed, _ = load_with_anchors(content)
common_key, common = composed.value[0]

handler = UseCUDAWheelsHandler(linter, args)
Expand Down Expand Up @@ -303,11 +303,7 @@ def test_handle_matrices_item(
linter = lint.Linter(
"dependencies.yaml", content, "verify-dependencies"
)
loader = dependencies_yaml.AnchorPreservingLoader(content)
try:
composed = loader.get_single_node()
finally:
loader.dispose()
composed, _ = load_with_anchors(content)

handler = UseCUDAWheelsHandler(linter, args)
with handler.handle_specific_item(Mock(), composed) as item_context:
Expand Down Expand Up @@ -355,11 +351,7 @@ def test_handle_matrix(self):
linter = lint.Linter(
"dependencies.yaml", content, "verify-dependencies"
)
loader = dependencies_yaml.AnchorPreservingLoader(content)
try:
composed = loader.get_single_node()
finally:
loader.dispose()
composed, _ = load_with_anchors(content)
matrix_key = find_yaml_node_for_span(composed, spans["matrix_key"])
matrix = find_yaml_node_for_span(composed, spans["matrix"])

Expand Down Expand Up @@ -408,11 +400,7 @@ def test_handle_matrix_item(self, content, expected_has_use_cuda_wheels):
linter = lint.Linter(
"dependencies.yaml", content, "verify-dependencies"
)
loader = dependencies_yaml.AnchorPreservingLoader(content)
try:
composed = loader.get_single_node()
finally:
loader.dispose()
composed, _ = load_with_anchors(content)
matrix_item_key, matrix_item = composed.value[0]

handler = UseCUDAWheelsHandler(linter, args)
Expand Down Expand Up @@ -498,11 +486,7 @@ def test_handle_packages(
linter = lint.Linter(
"dependencies.yaml", content, "verify-dependencies"
)
loader = dependencies_yaml.AnchorPreservingLoader(content)
try:
composed = loader.get_single_node()
finally:
loader.dispose()
composed, _ = load_with_anchors(content)
packages_key = find_yaml_node_for_span(composed, spans["packages_key"])
packages = find_yaml_node_for_span(composed, spans["packages"])

Expand Down Expand Up @@ -621,11 +605,7 @@ def test_handle_package(
linter = lint.Linter(
"dependencies.yaml", content, "verify-dependencies"
)
loader = dependencies_yaml.AnchorPreservingLoader(content)
try:
package_node = loader.get_single_node()
finally:
loader.dispose()
package_node, _ = load_with_anchors(content)

handler = UseCUDAWheelsHandler(linter, args)
packages_context = Mock(
Expand Down Expand Up @@ -991,19 +971,13 @@ def test_handle_package(
def test_check_use_cuda_wheels_integration(content, warnings):
content, spans = parse_named_spans(content, dict)

loader = dependencies_yaml.AnchorPreservingLoader(content)
try:
composed = loader.get_single_node()
finally:
loader.dispose()
composed, anchors = load_with_anchors(content)

args = Mock()
linter = lint.Linter("dependencies.yaml", content, "verify-dependencies")
handler = UseCUDAWheelsHandler(linter, args)

dependencies_yaml.traverse_root(
handler, loader.document_anchors[0], set(), composed
)
dependencies_yaml.traverse_root(handler, anchors, set(), composed)
assert linter.warnings == zip_expected_warnings(
spans.get("warnings", []), warnings
)
8 changes: 2 additions & 6 deletions tests/rapids_pre_commit_hooks/test_alpha_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from rapids_pre_commit_hooks import alpha_spec, lint
from rapids_pre_commit_hooks.utils.yaml import (
Anchor,
AnchorPreservingLoader,
AnchorType,
load_with_anchors,
)
from rapids_pre_commit_hooks_test_utils import (
parse_named_spans,
Expand Down Expand Up @@ -421,11 +421,7 @@ def test_handle_package(
):
args = Mock(mode=mode)
linter = lint.Linter("dependencies.yaml", content, "verify-alpha-spec")
loader = AnchorPreservingLoader(content)
try:
composed = loader.get_single_node()
finally:
loader.dispose()
composed, _ = load_with_anchors(content)
handler = alpha_spec.AlphaSpecHandler(linter, args)
item_context = alpha_spec.AlphaSpecHandler.ItemContext(
has_python_output_type=True
Expand Down
Loading
Loading