rbtr-lang-hcl 2026.9.0.dev1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Alejandro Giacometti
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,59 @@
1
+ Metadata-Version: 2.4
2
+ Name: rbtr-lang-hcl
3
+ Version: 2026.9.0.dev1
4
+ Summary: rbtr — HCL / Terraform language plugin
5
+ Keywords: code-search,code-index,tree-sitter,static-analysis,semantic-search,developer-tools,hcl,terraform
6
+ Author: Alejandro Giacometti
7
+ Author-email: Alejandro Giacometti <alejandro.giacometti@gmail.com>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Programming Language :: Python :: 3 :: Only
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Topic :: Software Development :: Libraries
15
+ Classifier: Topic :: Text Processing :: Indexing
16
+ Classifier: Typing :: Typed
17
+ Requires-Dist: rbtr==2026.9.0.dev1
18
+ Requires-Dist: tree-sitter-hcl
19
+ Requires-Python: >=3.13
20
+ Project-URL: Homepage, https://github.com/janrito/rbtr
21
+ Project-URL: Repository, https://github.com/janrito/rbtr
22
+ Project-URL: Documentation, https://github.com/janrito/rbtr/tree/main/packages/rbtr-lang-hcl#readme
23
+ Project-URL: Issues, https://github.com/janrito/rbtr/issues
24
+ Project-URL: Changelog, https://github.com/janrito/rbtr/releases
25
+ Description-Content-Type: text/markdown
26
+
27
+ # rbtr-lang-hcl
28
+
29
+ HCL / Terraform support for [rbtr]. Optional plugin — install with
30
+ `pip install rbtr[hcl]`.
31
+
32
+ [rbtr]: https://github.com/janrito/rbtr/tree/main/packages/rbtr#readme
33
+
34
+ ## What it ingests
35
+
36
+ Top-level blocks become **config-key** chunks (HCL is configuration, not
37
+ code). A block is named by its type and labels combined.
38
+
39
+ A `module` block's `source` becomes an **import**. A Terraform module is a
40
+ directory rather than a file, so a `source` naming one inside the repository
41
+ reaches every `.tf` file in it; a registry or git source reaches nothing,
42
+ having nothing here to reach.
43
+
44
+ ## Chunks produced
45
+
46
+ ```hcl
47
+ resource "aws_instance" "web" {} # config_key "resource aws_instance web"
48
+ variable "region" {} # config_key "variable region"
49
+ terraform {} # config_key "terraform"
50
+ ```
51
+
52
+ ## Embedded / injected chunks
53
+
54
+ None. HCL does not embed other languages.
55
+
56
+ ## Grammar & dependencies
57
+
58
+ Uses the `tree-sitter-hcl` grammar (`.hcl`, `.tf`). No dependency on other
59
+ language plugins.
@@ -0,0 +1,33 @@
1
+ # rbtr-lang-hcl
2
+
3
+ HCL / Terraform support for [rbtr]. Optional plugin — install with
4
+ `pip install rbtr[hcl]`.
5
+
6
+ [rbtr]: https://github.com/janrito/rbtr/tree/main/packages/rbtr#readme
7
+
8
+ ## What it ingests
9
+
10
+ Top-level blocks become **config-key** chunks (HCL is configuration, not
11
+ code). A block is named by its type and labels combined.
12
+
13
+ A `module` block's `source` becomes an **import**. A Terraform module is a
14
+ directory rather than a file, so a `source` naming one inside the repository
15
+ reaches every `.tf` file in it; a registry or git source reaches nothing,
16
+ having nothing here to reach.
17
+
18
+ ## Chunks produced
19
+
20
+ ```hcl
21
+ resource "aws_instance" "web" {} # config_key "resource aws_instance web"
22
+ variable "region" {} # config_key "variable region"
23
+ terraform {} # config_key "terraform"
24
+ ```
25
+
26
+ ## Embedded / injected chunks
27
+
28
+ None. HCL does not embed other languages.
29
+
30
+ ## Grammar & dependencies
31
+
32
+ Uses the `tree-sitter-hcl` grammar (`.hcl`, `.tf`). No dependency on other
33
+ language plugins.
@@ -0,0 +1,58 @@
1
+ [build-system]
2
+ requires = ["uv_build>=0.11.26,<1"]
3
+ build-backend = "uv_build"
4
+
5
+ [tool.uv.build-backend]
6
+ module-name = "rbtr_lang_hcl"
7
+
8
+ [tool.uv.sources.rbtr]
9
+ workspace = true
10
+
11
+ [project]
12
+ name = "rbtr-lang-hcl"
13
+ version = "2026.9.0-dev1"
14
+ description = "rbtr — HCL / Terraform language plugin"
15
+ readme = "README.md"
16
+ license = "MIT"
17
+ license-files = ["LICENSE"]
18
+ keywords = [
19
+ "code-search",
20
+ "code-index",
21
+ "tree-sitter",
22
+ "static-analysis",
23
+ "semantic-search",
24
+ "developer-tools",
25
+ "hcl",
26
+ "terraform",
27
+ ]
28
+ classifiers = [
29
+ "Development Status :: 4 - Beta",
30
+ "Intended Audience :: Developers",
31
+ "Programming Language :: Python :: 3 :: Only",
32
+ "Programming Language :: Python :: 3.13",
33
+ "Topic :: Software Development :: Libraries",
34
+ "Topic :: Text Processing :: Indexing",
35
+ "Typing :: Typed",
36
+ ]
37
+ requires-python = ">=3.13"
38
+ dependencies = [
39
+ "rbtr==2026.9.0-dev1",
40
+ "tree-sitter-hcl",
41
+ ]
42
+
43
+ [[project.authors]]
44
+ name = "Alejandro Giacometti"
45
+ email = "alejandro.giacometti@gmail.com"
46
+
47
+ [project.urls]
48
+ Homepage = "https://github.com/janrito/rbtr"
49
+ Repository = "https://github.com/janrito/rbtr"
50
+ Documentation = "https://github.com/janrito/rbtr/tree/main/packages/rbtr-lang-hcl#readme"
51
+ Issues = "https://github.com/janrito/rbtr/issues"
52
+ Changelog = "https://github.com/janrito/rbtr/releases"
53
+
54
+ [project.entry-points."rbtr.languages"]
55
+ hcl = "rbtr_lang_hcl.plugin:hcl"
56
+
57
+ [dependency-groups]
58
+ dev = ["rbtr[test]"]
@@ -0,0 +1,62 @@
1
+ [build-system]
2
+ requires = ["uv_build>=0.11.26,<1"]
3
+ build-backend = "uv_build"
4
+
5
+ [tool.uv.build-backend]
6
+ module-name = "rbtr_lang_hcl"
7
+ # No source-exclude: the plugin's tests, samples, and snapshots ship with
8
+ # the wheel (they are the language's golden record), and hcl.scm ships as
9
+ # package data.
10
+
11
+ [project]
12
+ name = "rbtr-lang-hcl"
13
+ version = "2026.9.0-dev1"
14
+ description = "rbtr — HCL / Terraform language plugin"
15
+ readme = "README.md"
16
+ license = "MIT"
17
+ license-files = ["LICENSE"]
18
+ authors = [
19
+ { name = "Alejandro Giacometti", email = "alejandro.giacometti@gmail.com" },
20
+ ]
21
+ keywords = [
22
+ "code-search",
23
+ "code-index",
24
+ "tree-sitter",
25
+ "static-analysis",
26
+ "semantic-search",
27
+ "developer-tools",
28
+ "hcl",
29
+ "terraform",
30
+ ]
31
+ classifiers = [
32
+ "Development Status :: 4 - Beta",
33
+ "Intended Audience :: Developers",
34
+ "Programming Language :: Python :: 3 :: Only",
35
+ "Programming Language :: Python :: 3.13",
36
+ "Topic :: Software Development :: Libraries",
37
+ "Topic :: Text Processing :: Indexing",
38
+ "Typing :: Typed",
39
+ ]
40
+ requires-python = ">=3.13"
41
+ dependencies = [
42
+ "rbtr==2026.9.0-dev1",
43
+ "tree-sitter-hcl",
44
+ ]
45
+
46
+ [project.urls]
47
+ Homepage = "https://github.com/janrito/rbtr"
48
+ Repository = "https://github.com/janrito/rbtr"
49
+ Documentation = "https://github.com/janrito/rbtr/tree/main/packages/rbtr-lang-hcl#readme"
50
+ Issues = "https://github.com/janrito/rbtr/issues"
51
+ Changelog = "https://github.com/janrito/rbtr/releases"
52
+
53
+ [project.entry-points."rbtr.languages"]
54
+ # The value resolves to the language's `LanguageRegistration` (named by its
55
+ # id) — see the rbtr ARCHITECTURE "External plugins" note.
56
+ hcl = "rbtr_lang_hcl.plugin:hcl"
57
+
58
+ [tool.uv.sources]
59
+ rbtr = { workspace = true }
60
+
61
+ [dependency-groups]
62
+ dev = ["rbtr[test]"]
@@ -0,0 +1 @@
1
+ """HCL language plugin package."""
@@ -0,0 +1,20 @@
1
+ ; Comments (HCL: `#`, `//`, `/* */` — all `comment`).
2
+ (comment) @comment
3
+
4
+ (config_file
5
+ (body
6
+ (block) @config_key))
7
+
8
+ ; A `module` block's `source` names the directory the module is read from,
9
+ ; so it is an import: `module "x" { source = "./b" }` reaches `./b`.
10
+ (block
11
+ (identifier) @_block_type
12
+ (#eq? @_block_type "module")
13
+ (body
14
+ (attribute
15
+ (identifier) @_attr
16
+ (#eq? @_attr "source")
17
+ (expression
18
+ (literal_value
19
+ (string_lit
20
+ (template_literal) @_import_module)))))) @import
@@ -0,0 +1,61 @@
1
+ """HCL language plugin.
2
+
3
+ Extracts each top-level block as a config-key chunk, via a
4
+ tree-sitter query. The block name combines its type and labels.
5
+
6
+ Extracted chunks::
7
+
8
+ resource "aws_instance" "web" {} → config_key "resource aws_instance web"
9
+ variable "region" {} → config_key "variable region"
10
+ terraform {} → config_key "terraform"
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ from typing import TYPE_CHECKING
16
+
17
+ from rbtr.languages.registration import (
18
+ LanguageRegistration,
19
+ NameResolver,
20
+ QueryExtraction,
21
+ load_query,
22
+ )
23
+
24
+ if TYPE_CHECKING:
25
+ from tree_sitter import Node
26
+
27
+ # Top-level blocks only: a `block` whose body is the file's own body,
28
+ # not one nested inside another block.
29
+
30
+
31
+ hcl = LanguageRegistration(
32
+ id="hcl",
33
+ extensions=frozenset({".hcl", ".tf"}),
34
+ grammar_module="tree_sitter_hcl",
35
+ extraction=QueryExtraction(
36
+ query=load_query(__package__, "hcl"),
37
+ ),
38
+ package_directory=True,
39
+ extraction_serial=4,
40
+ )
41
+
42
+
43
+ @hcl.name_extractor
44
+ def hcl_block_name(
45
+ resolver: NameResolver, capture_name: str, node: Node, captures: dict[str, list[Node]]
46
+ ) -> str:
47
+ """Name a top-level HCL block by its type and labels.
48
+
49
+ `resource "aws_instance" "web"` → `"resource aws_instance web"`;
50
+ a bare `terraform {}` block → `"terraform"`. Non-block captures
51
+ fall back to the default resolver.
52
+ """
53
+ if capture_name != "config_key":
54
+ return resolver(capture_name, node, captures)
55
+ labels: list[str] = []
56
+ for child in node.children:
57
+ if child.type == "identifier" and child.text:
58
+ labels.append(child.text.decode("utf-8", errors="replace"))
59
+ elif child.type == "string_lit" and child.text:
60
+ labels.append(child.text.decode("utf-8", errors="replace").strip('"'))
61
+ return " ".join(labels)
File without changes
@@ -0,0 +1,5 @@
1
+ [
2
+ "main.tf::module \"greeter\" {\n source = \"./modules/greeter\"\n} -> modules/greeter/main.tf::output greeting [imports]",
3
+ "main.tf::module \"greeter\" {\n source = \"./modules/greeter\"\n} -> modules/greeter/main.tf::variable name [imports]",
4
+ "main.tf::module \"greeter\" {\n source = \"./modules/greeter\"\n} -> modules/greeter/versions.tf::terraform [imports]"
5
+ ]
@@ -0,0 +1,192 @@
1
+ [
2
+ {
3
+ "blob_sha": "sha1",
4
+ "file_path": "main.tf",
5
+ "kind": "comment",
6
+ "name": "",
7
+ "scope": "",
8
+ "language": "hcl",
9
+ "file_language": "hcl",
10
+ "content": "# Greeter service infrastructure.\n#\n# The HCL plugin extracts each top-level block as a doc section, named by its\n# type and labels: a bare block by its type (terraform), a variable/output by\n# its name, a resource by its type and name.",
11
+ "line_start": 1,
12
+ "line_end": 5,
13
+ "metadata": {
14
+ "module": "",
15
+ "names": "",
16
+ "dots": "",
17
+ "language_hint": ""
18
+ },
19
+ "id": "7d2924582868db42"
20
+ },
21
+ {
22
+ "blob_sha": "sha1",
23
+ "file_path": "main.tf",
24
+ "kind": "config_key",
25
+ "name": "terraform",
26
+ "scope": "",
27
+ "language": "hcl",
28
+ "file_language": "hcl",
29
+ "content": "terraform {\n required_version = \">= 1.5\"\n}",
30
+ "line_start": 7,
31
+ "line_end": 9,
32
+ "metadata": {
33
+ "module": "",
34
+ "names": "",
35
+ "dots": "",
36
+ "language_hint": ""
37
+ },
38
+ "id": "f58842c77d1a871a"
39
+ },
40
+ {
41
+ "blob_sha": "sha1",
42
+ "file_path": "main.tf",
43
+ "kind": "config_key",
44
+ "name": "variable region",
45
+ "scope": "",
46
+ "language": "hcl",
47
+ "file_language": "hcl",
48
+ "content": "variable \"region\" {\n description = \"Deployment region for the greeter service.\"\n type = string\n default = \"us-east-1\"\n}",
49
+ "line_start": 11,
50
+ "line_end": 15,
51
+ "metadata": {
52
+ "module": "",
53
+ "names": "",
54
+ "dots": "",
55
+ "language_hint": ""
56
+ },
57
+ "id": "3e495ada0ca309cb"
58
+ },
59
+ {
60
+ "blob_sha": "sha1",
61
+ "file_path": "main.tf",
62
+ "kind": "config_key",
63
+ "name": "resource aws_instance greeter",
64
+ "scope": "",
65
+ "language": "hcl",
66
+ "file_language": "hcl",
67
+ "content": "resource \"aws_instance\" \"greeter\" {\n ami = \"ami-0abc123\"\n instance_type = \"t3.micro\"\n\n tags = {\n Name = \"greeter\"\n }\n}",
68
+ "line_start": 17,
69
+ "line_end": 24,
70
+ "metadata": {
71
+ "module": "",
72
+ "names": "",
73
+ "dots": "",
74
+ "language_hint": ""
75
+ },
76
+ "id": "0bb9553077557d9d"
77
+ },
78
+ {
79
+ "blob_sha": "sha1",
80
+ "file_path": "main.tf",
81
+ "kind": "config_key",
82
+ "name": "output greeter_ip",
83
+ "scope": "",
84
+ "language": "hcl",
85
+ "file_language": "hcl",
86
+ "content": "output \"greeter_ip\" {\n description = \"Public IP of the greeter instance.\"\n value = aws_instance.greeter.public_ip\n}",
87
+ "line_start": 26,
88
+ "line_end": 29,
89
+ "metadata": {
90
+ "module": "",
91
+ "names": "",
92
+ "dots": "",
93
+ "language_hint": ""
94
+ },
95
+ "id": "52d00073c71d16e6"
96
+ },
97
+ {
98
+ "blob_sha": "sha1",
99
+ "file_path": "main.tf",
100
+ "kind": "config_key",
101
+ "name": "module greeter",
102
+ "scope": "",
103
+ "language": "hcl",
104
+ "file_language": "hcl",
105
+ "content": "module \"greeter\" {\n source = \"./modules/greeter\"\n}",
106
+ "line_start": 31,
107
+ "line_end": 33,
108
+ "metadata": {
109
+ "module": "",
110
+ "names": "",
111
+ "dots": "",
112
+ "language_hint": ""
113
+ },
114
+ "id": "d08d3f48a7c705df"
115
+ },
116
+ {
117
+ "blob_sha": "sha1",
118
+ "file_path": "main.tf",
119
+ "kind": "import",
120
+ "name": "module \"greeter\" {\n source = \"./modules/greeter\"\n}",
121
+ "scope": "",
122
+ "language": "hcl",
123
+ "file_language": "hcl",
124
+ "content": "module \"greeter\" {\n source = \"./modules/greeter\"\n}",
125
+ "line_start": 31,
126
+ "line_end": 33,
127
+ "metadata": {
128
+ "module": "./modules/greeter",
129
+ "names": "",
130
+ "dots": "",
131
+ "language_hint": ""
132
+ },
133
+ "id": "04b63f6a4402d449"
134
+ },
135
+ {
136
+ "blob_sha": "sha1",
137
+ "file_path": "modules/greeter/main.tf",
138
+ "kind": "config_key",
139
+ "name": "variable name",
140
+ "scope": "",
141
+ "language": "hcl",
142
+ "file_language": "hcl",
143
+ "content": "# The module main.tf reads via `source = \"./modules/greeter\"`. A Terraform\n# module is a directory, so the reference reaches every .tf file in it.\nvariable \"name\" {\n type = string\n default = \"world\"\n}",
144
+ "line_start": 1,
145
+ "line_end": 6,
146
+ "metadata": {
147
+ "module": "",
148
+ "names": "",
149
+ "dots": "",
150
+ "language_hint": ""
151
+ },
152
+ "id": "6473410d3fce61e5"
153
+ },
154
+ {
155
+ "blob_sha": "sha1",
156
+ "file_path": "modules/greeter/main.tf",
157
+ "kind": "config_key",
158
+ "name": "output greeting",
159
+ "scope": "",
160
+ "language": "hcl",
161
+ "file_language": "hcl",
162
+ "content": "output \"greeting\" {\n value = \"Hello, ${var.name}\"\n}",
163
+ "line_start": 8,
164
+ "line_end": 10,
165
+ "metadata": {
166
+ "module": "",
167
+ "names": "",
168
+ "dots": "",
169
+ "language_hint": ""
170
+ },
171
+ "id": "7d290195d36951c4"
172
+ },
173
+ {
174
+ "blob_sha": "sha1",
175
+ "file_path": "modules/greeter/versions.tf",
176
+ "kind": "config_key",
177
+ "name": "terraform",
178
+ "scope": "",
179
+ "language": "hcl",
180
+ "file_language": "hcl",
181
+ "content": "# A second file of the same module, reached by the same reference.\nterraform {\n required_version = \">= 1.6\"\n}",
182
+ "line_start": 1,
183
+ "line_end": 4,
184
+ "metadata": {
185
+ "module": "",
186
+ "names": "",
187
+ "dots": "",
188
+ "language_hint": ""
189
+ },
190
+ "id": "b86fbf09f6da8746"
191
+ }
192
+ ]
@@ -0,0 +1,29 @@
1
+ """HCL extraction test cases (top-level blocks -> config_key)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pytest_cases import case
6
+
7
+ type SymbolCase = tuple[str, str, list[tuple[str, str, str]]]
8
+
9
+
10
+ @case(tags=["symbol"])
11
+ def case_hcl_splits_by_blocks() -> SymbolCase:
12
+ """HCL splits by top-level blocks."""
13
+ src = """\
14
+ resource "aws_instance" "web" {
15
+ ami = "ami-12345"
16
+ }
17
+
18
+ variable "region" {
19
+ default = "us-east-1"
20
+ }
21
+ """
22
+ return (
23
+ "hcl",
24
+ src,
25
+ [
26
+ ("config_key", "resource aws_instance web", ""),
27
+ ("config_key", "variable region", ""),
28
+ ],
29
+ )
@@ -0,0 +1,33 @@
1
+ # Greeter service infrastructure.
2
+ #
3
+ # The HCL plugin extracts each top-level block as a doc section, named by its
4
+ # type and labels: a bare block by its type (terraform), a variable/output by
5
+ # its name, a resource by its type and name.
6
+
7
+ terraform {
8
+ required_version = ">= 1.5"
9
+ }
10
+
11
+ variable "region" {
12
+ description = "Deployment region for the greeter service."
13
+ type = string
14
+ default = "us-east-1"
15
+ }
16
+
17
+ resource "aws_instance" "greeter" {
18
+ ami = "ami-0abc123"
19
+ instance_type = "t3.micro"
20
+
21
+ tags = {
22
+ Name = "greeter"
23
+ }
24
+ }
25
+
26
+ output "greeter_ip" {
27
+ description = "Public IP of the greeter instance."
28
+ value = aws_instance.greeter.public_ip
29
+ }
30
+
31
+ module "greeter" {
32
+ source = "./modules/greeter"
33
+ }
@@ -0,0 +1,10 @@
1
+ # The module main.tf reads via `source = "./modules/greeter"`. A Terraform
2
+ # module is a directory, so the reference reaches every .tf file in it.
3
+ variable "name" {
4
+ type = string
5
+ default = "world"
6
+ }
7
+
8
+ output "greeting" {
9
+ value = "Hello, ${var.name}"
10
+ }
@@ -0,0 +1,4 @@
1
+ # A second file of the same module, reached by the same reference.
2
+ terraform {
3
+ required_version = ">= 1.6"
4
+ }
@@ -0,0 +1,17 @@
1
+ """HCL extraction tests (cases in `cases_extraction.py`)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pytest_cases import parametrize_with_cases
6
+
7
+ from rbtr.git import FileEntry
8
+ from rbtr.languages.extract import extract_file
9
+
10
+
11
+ @parametrize_with_cases("lang, source, expected", cases=".cases_extraction", has_tag="symbol")
12
+ def test_extracts_expected_symbols(lang: str, source: str, expected: list) -> None:
13
+ """Each expected (kind, name, scope) tuple appears in the output."""
14
+ chunks = extract_file(FileEntry("input", "sha1", source.encode()), lang)
15
+ symbols = [(c.kind, c.name, c.scope) for c in chunks]
16
+ for exp in expected:
17
+ assert exp in symbols, f"expected {exp} not found in {symbols}"
@@ -0,0 +1,68 @@
1
+ """HCL sample extraction: the `samples/hcl/` project through the real pipeline."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+ from typing import TYPE_CHECKING
7
+
8
+ import pytest
9
+ from tree_sitter import Parser
10
+
11
+ from rbtr.domain.models import Chunk, ChunkKind, Edge
12
+ from rbtr.git import FileEntry
13
+ from rbtr.languages.edges import build_resolution_map, infer_import_edges
14
+ from rbtr.languages.extract import extract_file
15
+ from rbtr.languages.manager import get_manager
16
+ from rbtr.testing import render_edges
17
+
18
+ if TYPE_CHECKING:
19
+ from syrupy.assertion import SnapshotAssertion
20
+
21
+
22
+ @pytest.fixture
23
+ def project() -> list[tuple[str, str]]:
24
+ root = Path(__file__).parent / "samples" / "hcl"
25
+ return [
26
+ (str(p.relative_to(root)), p.read_text()) for p in sorted(root.rglob("*")) if p.is_file()
27
+ ]
28
+
29
+
30
+ @pytest.fixture
31
+ def chunks(project: list[tuple[str, str]]) -> list[Chunk]:
32
+ manager = get_manager()
33
+ out: list[Chunk] = []
34
+ for path, text in project:
35
+ lang = manager.detect_language(path) or "hcl"
36
+ out.extend(extract_file(FileEntry(path, "sha1", text.encode()), lang))
37
+ return out
38
+
39
+
40
+ @pytest.fixture
41
+ def edges(project: list[tuple[str, str]], chunks: list[Chunk]) -> list[Edge]:
42
+ manager = get_manager()
43
+ repo_files = {path for path, _ in project}
44
+ return infer_import_edges(chunks, repo_files, build_resolution_map(manager))
45
+
46
+
47
+ def test_emits_expected_kinds(chunks: list[Chunk]) -> None:
48
+ """The sample exercises HCL's config-key, comment and import chunks."""
49
+ kinds = {c.kind for c in chunks}
50
+ assert {ChunkKind.CONFIG_KEY, ChunkKind.COMMENT, ChunkKind.IMPORT} <= kinds
51
+
52
+
53
+ def test_parses_cleanly(project: list[tuple[str, str]]) -> None:
54
+ manager = get_manager()
55
+ for path, text in project:
56
+ grammar = manager.grammar(manager.detect_language(path) or "hcl")
57
+ assert grammar is not None
58
+ assert not Parser(grammar).parse(text.encode()).root_node.has_error, path
59
+
60
+
61
+ def test_extraction_matches_snapshot(chunks: list[Chunk], snapshot_json: SnapshotAssertion) -> None:
62
+ assert chunks == snapshot_json
63
+
64
+
65
+ def test_edges_match_snapshot(
66
+ chunks: list[Chunk], edges: list[Edge], snapshot_json: SnapshotAssertion
67
+ ) -> None:
68
+ assert render_edges(edges, chunks) == snapshot_json