Copilot commented on code in PR #13058:
URL: https://github.com/apache/trafficserver/pull/13058#discussion_r3035722804


##########
tools/hrw4u/tests/test_autest_rules_reverse.py:
##########
@@ -0,0 +1,65 @@
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+"""Test conf -> hrw4u conversion matches the autest .hrw4u files."""
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+from antlr4 import CommonTokenStream, InputStream
+
+from u4wrh.u4wrhLexer import u4wrhLexer
+from u4wrh.u4wrhParser import u4wrhParser
+from u4wrh.hrw_visitor import HRWInverseVisitor
+
+AUTEST_RULES_DIR = Path(
+    __file__).resolve().parent.parent.parent.parent / "tests" / "gold_tests" / 
"pluginTest" / "header_rewrite" / "rules"
+
+
+def _collect_autest_pairs() -> list[pytest.param]:
+    """Collect .conf/.hrw4u pairs from the autest rules directory."""
+    if not AUTEST_RULES_DIR.is_dir():
+        return []
+
+    pairs = []
+    for conf in sorted(AUTEST_RULES_DIR.glob("*.conf")):
+        hrw4u = conf.with_suffix(".hrw4u")
+        if hrw4u.exists():
+            pairs.append(pytest.param(conf, hrw4u, id=conf.stem))
+
+    return pairs
+
+
[email protected]("conf_file,hrw4u_file", _collect_autest_pairs())
+def test_conf_to_hrw4u(conf_file: Path, hrw4u_file: Path) -> None:
+    """Test that conf -> hrw4u output matches the .hrw4u file."""
+    text = conf_file.read_text()
+    lexer = u4wrhLexer(InputStream(text))
+    stream = CommonTokenStream(lexer)
+    parser = u4wrhParser(stream)
+    tree = parser.program()
+
+    visitor = HRWInverseVisitor(filename=str(conf_file), merge_sections=False)
+    result = visitor.visit(tree)
+
+    assert result is not None, f"u4wrh produced no output for {conf_file.name}"
+    assert len(result) > 0, f"u4wrh produced empty output for {conf_file.name}"

Review Comment:
   This reverse test doesn't pass an ErrorCollector into HRWInverseVisitor, so 
any semantic issues won't be reported in a structured way (and warnings can't 
be asserted) compared to other tests that use ErrorCollector. Consider 
constructing an ErrorCollector here, passing it via error_collector=..., and 
asserting it has no errors before comparing output.



##########
tools/hrw4u/src/hrw_visitor.py:
##########
@@ -84,12 +87,23 @@ def _reset_condition_state(self) -> None:
         self._in_elif_mode = False
         self._in_group = False
         self._group_terms.clear()
+        self._expecting_if_cond = False
+
+    def _close_if_chain_for_new_rule(self) -> None:
+        """Close if-else chain when a new rule starts without elif/else."""
+        expecting_nested_if = self._expecting_if_cond
+        self._expecting_if_cond = False
+
+        if (self._if_depth > 0 and not self._in_elif_mode and not 
self._pending_terms and not expecting_nested_if and
+                not self._in_group):
+            self.debug("new rule detected - closing if chain")
+            self._start_new_section(SectionType.REMAP)

Review Comment:
   _close_if_chain_for_new_rule() hard-codes SectionType.REMAP when it decides 
to start a new section. This ignores the visitor's configured default 
section_label and can produce incorrect output when the inverse visitor is used 
with a non-REMAP default hook (e.g., global header_rewrite configs default to 
READ_RESPONSE). Consider starting the new section using the configured default 
(e.g., a stored _default_section_label captured from the ctor arg), rather than 
always REMAP.



##########
tools/hrw4u/tests/test_autest_rules.py:
##########
@@ -0,0 +1,67 @@
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+"""Test hrw4u -> conf conversion matches the autest .conf files."""
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+from antlr4 import CommonTokenStream, InputStream
+
+from hrw4u.hrw4uLexer import hrw4uLexer
+from hrw4u.hrw4uParser import hrw4uParser
+from hrw4u.visitor import HRW4UVisitor
+from hrw4u.errors import ErrorCollector
+
+AUTEST_RULES_DIR = Path(
+    __file__).resolve().parent.parent.parent.parent / "tests" / "gold_tests" / 
"pluginTest" / "header_rewrite" / "rules"
+
+
+def _collect_autest_pairs() -> list[pytest.param]:
+    """Collect .conf/.hrw4u pairs from the autest rules directory."""
+    if not AUTEST_RULES_DIR.is_dir():
+        return []
+
+    pairs = []
+    for conf in sorted(AUTEST_RULES_DIR.glob("*.conf")):
+        hrw4u = conf.with_suffix(".hrw4u")
+        if hrw4u.exists():
+            pairs.append(pytest.param(conf, hrw4u, id=conf.stem))
+
+    return pairs
+
+
[email protected]("conf_file,hrw4u_file", _collect_autest_pairs())
+def test_hrw4u_to_conf(conf_file: Path, hrw4u_file: Path) -> None:
+    """Test that hrw4u -> conf output matches the .conf file."""

Review Comment:
   pyproject.toml adds an `autest` pytest marker, but this test isn't marked 
with it. Adding `@pytest.mark.autest` (alongside the parametrize) would let 
CI/devs include/exclude these integration-style tests explicitly (they depend 
on the gold_tests rule corpus).



##########
tools/hrw4u/tests/test_autest_rules_reverse.py:
##########
@@ -0,0 +1,65 @@
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+"""Test conf -> hrw4u conversion matches the autest .hrw4u files."""
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+from antlr4 import CommonTokenStream, InputStream
+
+from u4wrh.u4wrhLexer import u4wrhLexer
+from u4wrh.u4wrhParser import u4wrhParser
+from u4wrh.hrw_visitor import HRWInverseVisitor
+
+AUTEST_RULES_DIR = Path(
+    __file__).resolve().parent.parent.parent.parent / "tests" / "gold_tests" / 
"pluginTest" / "header_rewrite" / "rules"
+
+
+def _collect_autest_pairs() -> list[pytest.param]:
+    """Collect .conf/.hrw4u pairs from the autest rules directory."""
+    if not AUTEST_RULES_DIR.is_dir():
+        return []
+
+    pairs = []
+    for conf in sorted(AUTEST_RULES_DIR.glob("*.conf")):
+        hrw4u = conf.with_suffix(".hrw4u")
+        if hrw4u.exists():
+            pairs.append(pytest.param(conf, hrw4u, id=conf.stem))
+
+    return pairs
+
+
[email protected]("conf_file,hrw4u_file", _collect_autest_pairs())
+def test_conf_to_hrw4u(conf_file: Path, hrw4u_file: Path) -> None:
+    """Test that conf -> hrw4u output matches the .hrw4u file."""

Review Comment:
   pyproject.toml adds an `autest` pytest marker, but this test isn't marked 
with it. Adding `@pytest.mark.autest` would make it easier to run these 
roundtrip tests selectively (they depend on the gold_tests rule corpus).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to