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


##########
src/traffic_ctl/CtrlPrinters.cc:
##########
@@ -18,12 +18,14 @@
   limitations under the License.
 */
 
+#include <iomanip>
 #include <iostream>
 #include <unordered_map>
 #include <string_view>

Review Comment:
   CtrlPrinters.cc now calls snprintf() in PluginListPrinter::write_output(), 
but the file doesn't include <cstdio>/<stdio.h>. Relying on transitive includes 
is brittle and can break builds depending on header order / standard library 
implementation.



##########
tests/gold_tests/traffic_ctl/traffic_ctl_test_utils.py:
##########
@@ -139,6 +141,43 @@ def validate_result_with_text(self, text: str):
         self._finish()
         return self
 
+    def validate_json_data_matches(self, expected: dict):
+        """
+        Validate the JSON-RPC result data equals expected, exactly.
+
+        Descends into result.data, which is where every handler puts its
+        payload, and compares the whole structure rather than a few fields, so
+        an added, removed or renamed key fails the assertion. This is the
+        machine-readable contract; prefer it over asserting the human-readable
+        text output, whose column widths are a presentation detail.
+
+        Write every scalar in expected as a string: the emitter double-quotes
+        all of them, so booleans arrive as 'true' and integers as '1'.
+
+        On mismatch both structures are printed to stderr, key-sorted.
+
+        Example:
+            traffic_ctl.plugin().list().as_json().validate_json_data_matches(
+                {'source': 'plugin.yaml', 'plugins': []})
+        """
+        # The script is single quoted and interpolates nothing. Expected values
+        # travel as one shlex.quote'd json argument, so a value containing a
+        # quote, a $, or a backtick is compared literally instead of being
+        # expanded by the shell or breaking the script it is embedded in.
+        script = (
+            "import sys, json\n"
+            "actual = json.load(sys.stdin)['result']['data']\n"
+            "expected = json.loads(sys.argv[1])\n"
+            "if actual != expected:\n"
+            "    print('FAIL: result.data does not match', file=sys.stderr)\n"
+            "    print('  actual  :', json.dumps(actual, sort_keys=True), 
file=sys.stderr)\n"
+            "    print('  expected:', json.dumps(expected, sort_keys=True), 
file=sys.stderr)\n"
+            "    sys.exit(1)\n")
+        payload = shlex.quote(json.dumps(expected))
+        self._cmd = f"{self._cmd} | python3 -c {shlex.quote(script)} {payload}"
+        self._finish()

Review Comment:
   The new validate_json_data_matches helper hardcodes `python3`. Autests in 
this repo commonly use `{sys.executable}` so they run under the same 
interpreter as the harness (virtualenvs, non-system Python, etc.); using 
`python3` can make the test depend on environment PATH / system packages.



-- 
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