|
| 1 | +# Copyright 2023 The StackStorm Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from textwrap import dedent |
| 17 | + |
| 18 | +from pants.backend.python.target_types import ( |
| 19 | + PythonSourceTarget, |
| 20 | + PythonSourcesGeneratorTarget, |
| 21 | +) |
| 22 | +from pants.backend.python.target_types_rules import rules as python_target_types_rules |
| 23 | +from pants.engine.addresses import Address |
| 24 | +from pants.engine.target import InferredDependencies |
| 25 | +from pants.testutil.rule_runner import QueryRule, RuleRunner |
| 26 | + |
| 27 | +from .target_types_rules import ( |
| 28 | + InferPacksGlobDependencies, |
| 29 | + PacksGlobInferenceFieldSet, |
| 30 | + rules as pack_metadata_target_types_rules, |
| 31 | +) |
| 32 | +from .target_types import PacksGlob |
| 33 | + |
| 34 | + |
| 35 | +def test_infer_packs_globs_dependencies() -> None: |
| 36 | + rule_runner = RuleRunner( |
| 37 | + rules=[ |
| 38 | + *python_target_types_rules(), |
| 39 | + *pack_metadata_target_types_rules(), |
| 40 | + QueryRule(InferredDependencies, (InferPacksGlobDependencies,)), |
| 41 | + ], |
| 42 | + target_types=[ |
| 43 | + PythonSourceTarget, |
| 44 | + PythonSourcesGeneratorTarget, |
| 45 | + PacksGlob, |
| 46 | + ], |
| 47 | + ) |
| 48 | + rule_runner.write_files( |
| 49 | + { |
| 50 | + "packs/BUILD": dedent( |
| 51 | + """\ |
| 52 | + python_sources( |
| 53 | + name="git_submodule", |
| 54 | + sources=["./git_submodule/*.py"], |
| 55 | + ) |
| 56 | +
|
| 57 | + packs_glob( |
| 58 | + name="all_packs_glob", |
| 59 | + dependencies=[ |
| 60 | + "!./configs", # explicit ignore |
| 61 | + "./a", # explicit include |
| 62 | + ], |
| 63 | + ) |
| 64 | + """ |
| 65 | + ), |
| 66 | + "packs/a/BUILD": "python_sources()", |
| 67 | + "packs/a/__init__.py": "", |
| 68 | + "packs/a/fixture.py": "", |
| 69 | + "packs/b/BUILD": dedent( |
| 70 | + """\ |
| 71 | + python_sources( |
| 72 | + dependencies=["packs/configs/b.yaml"], |
| 73 | + ) |
| 74 | + """ |
| 75 | + ), |
| 76 | + "packs/b/__init__.py": "", |
| 77 | + "packs/b/fixture.py": "", |
| 78 | + "packs/c/BUILD": "python_sources()", |
| 79 | + "packs/c/__init__.py": "", |
| 80 | + "packs/c/fixture.py": "", |
| 81 | + "packs/d/BUILD": "python_sources()", |
| 82 | + "packs/d/__init__.py": "", |
| 83 | + "packs/d/fixture.py": "", |
| 84 | + # imitate a pack in a git submodule (should NOT have a BUILD file) |
| 85 | + "packs/git_submodule/__init__.py": "", |
| 86 | + "packs/git_submodule/fixture.py": "", |
| 87 | + "packs/configs/BUILD": dedent( |
| 88 | + """\ |
| 89 | + resources( |
| 90 | + sources=["*.yaml"], |
| 91 | + ) |
| 92 | + """ |
| 93 | + ), |
| 94 | + "packs/configs/b.yaml": dedent( |
| 95 | + """\ |
| 96 | + --- |
| 97 | + # pack config for pack b |
| 98 | + """ |
| 99 | + ), |
| 100 | + } |
| 101 | + ) |
| 102 | + |
| 103 | + def run_dep_inference(address: Address) -> InferredDependencies: |
| 104 | + args = [ |
| 105 | + "--source-root-patterns=/packs", |
| 106 | + ] |
| 107 | + rule_runner.set_options(args, env_inherit={"PATH", "PYENV_ROOT", "HOME"}) |
| 108 | + target = rule_runner.get_target(address) |
| 109 | + return rule_runner.request( |
| 110 | + InferredDependencies, |
| 111 | + [InferPacksGlobDependencies(PacksGlobInferenceFieldSet.create(target))], |
| 112 | + ) |
| 113 | + |
| 114 | + assert run_dep_inference( |
| 115 | + Address("packs", target_name="all_packs_glob") |
| 116 | + ) == InferredDependencies( |
| 117 | + [ |
| 118 | + # should not have packs/a (explicit dep does not need to be inferred) |
| 119 | + # should not have packs/configs (explicitly ignored) |
| 120 | + # should not have packs/git_submodule (no BUILD file = no targets to add) |
| 121 | + Address("packs/b"), |
| 122 | + Address("packs/c"), |
| 123 | + Address("packs/d"), |
| 124 | + ], |
| 125 | + ) |
0 commit comments