-
Notifications
You must be signed in to change notification settings - Fork 4
Extraction hardening: tcl #995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
80d312e
Reapply "Extraction hardening: tcl (#990)"
squid-protocol 00b54be
fix: resolve ruff formatting and unused imports
squid-protocol 17b3e18
fix: remove duplicate dictionary key
squid-protocol c1cd01b
fix: remove impossible negative test cases for Tcl
squid-protocol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| """ | ||
| Tcl extraction hardening (epic #848). See | ||
| tests/extraction/how_to_harden_extraction.md for the methodology. | ||
|
|
||
| Covers all four extraction gauntlets for tcl in one file: func_start, | ||
| args, class_start, _dependency_capture. | ||
| """ | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| from gitgalaxy.standards.language_standards import LANGUAGE_DEFINITIONS | ||
|
|
||
| _EXTRACTION_DIR = str(Path(__file__).resolve().parent.parent) | ||
| if _EXTRACTION_DIR not in sys.path: | ||
| sys.path.insert(0, _EXTRACTION_DIR) | ||
|
|
||
| from _extraction_harness import ( # noqa: E402 | ||
| assert_invalid_no_match, | ||
| assert_pathological_dependency_match, | ||
| assert_pathological_match, | ||
| assert_valid_dependency_match, | ||
| assert_valid_match, | ||
| ) | ||
|
|
||
| TCL_RULES = LANGUAGE_DEFINITIONS["tcl"]["rules"] | ||
|
|
||
| FUNCTION_CASES: dict[str, Any] = { | ||
| "valid": [ | ||
| ("proc TargetFunc {", "TargetFunc"), | ||
| ("proc TargetFunc {a b} {", "TargetFunc"), | ||
| ("proc ::namespace::TargetFunc {args} {", "TargetFunc"), | ||
| ], | ||
| "invalid": [ | ||
| "set TargetFunc", | ||
| "if {$TargetFunc}", | ||
| "TargetFunc a b", | ||
| "if {$a == $b} {", | ||
| "set proc TargetFunc", | ||
| 'puts "proc TargetFunc {"', | ||
| ], | ||
| "pathological": [ | ||
| ("proc \t TargetFunc \t {", "TargetFunc"), | ||
| ("proc TargetFunc {", "TargetFunc"), | ||
| ("proc \n ::namespace::TargetFunc \n { \n a \n b \n } \n {", "TargetFunc"), | ||
| ], | ||
| } | ||
|
|
||
| ARGS_CASES: dict[str, Any] = { | ||
| "valid": [ | ||
| ("proc TargetFunc {a b} {", "a b"), | ||
| ("proc ::namespace::TargetFunc {args} {", "args"), | ||
| ("proc TargetFunc {{a 1} b} {", "{a 1} b"), | ||
| ], | ||
| "invalid": [ | ||
| "TargetFunc a b", | ||
| "if {$a == $b} {", | ||
| ], | ||
| "pathological": [ | ||
| ("proc \n ::namespace::TargetFunc \n { \n a \n b \n } \n {", " \n a \n b \n "), | ||
| ("proc TargetFunc \n { \n {a 1} \n {b 2} \n } \n {", " \n {a 1} \n {b 2} \n "), | ||
| ("proc TargetFunc { a b } {", " a b "), | ||
| ], | ||
| } | ||
|
|
||
| CLASS_CASES: dict[str, Any] = { | ||
| "valid": [ | ||
| ("oo::class create TargetClass {", "TargetClass"), | ||
| ("snit::type TargetClass {", "TargetClass"), | ||
| ("itcl::class TargetClass {", "TargetClass"), | ||
| ], | ||
| "invalid": ["set TargetClass", "if {$TargetClass}", "TargetClass create foo"], | ||
| "pathological": [ | ||
| ("oo::class \t create \t TargetClass \t {", "TargetClass"), | ||
| ("oo::class \n create \n TargetClass \n {", "TargetClass"), | ||
| ("snit::type \n TargetClass \n {", "TargetClass"), | ||
| ], | ||
| } | ||
|
|
||
| DEPENDENCY_CASES: dict[str, Any] = { | ||
| "valid": [ | ||
| ("package require TargetPkg", "TargetPkg"), | ||
| ("source TargetPkg.tcl", "TargetPkg.tcl"), | ||
| ("load TargetPkg.so", "TargetPkg.so"), | ||
| ("package require -exact TargetPkg 1.0", "TargetPkg"), | ||
| ('source "TargetPkg.tcl"', "TargetPkg.tcl"), | ||
| ], | ||
| "invalid": [ | ||
| "puts TargetPkg", | ||
| "set package TargetPkg", | ||
| 'puts "package require TargetPkg"', | ||
| ], | ||
| "pathological": [ | ||
| ("package require TargetPkg", "TargetPkg"), | ||
| ("source TargetPkg.tcl", "TargetPkg.tcl"), | ||
| ("package \t require \t TargetPkg", "TargetPkg"), | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| class TestTclExtraction: | ||
| # ------------------------------------------------------------------------- | ||
| # func_start | ||
| # ------------------------------------------------------------------------- | ||
| @pytest.mark.parametrize("payload,expected", FUNCTION_CASES["valid"]) | ||
| def test_func_start_valid(self, payload, expected): | ||
| assert_valid_match(TCL_RULES["func_start"], payload, expected, "tcl") | ||
|
|
||
| @pytest.mark.parametrize("payload", FUNCTION_CASES["invalid"]) | ||
| def test_func_start_invalid(self, payload): | ||
| assert_invalid_no_match(TCL_RULES["func_start"], payload, "tcl") | ||
|
|
||
| @pytest.mark.parametrize("payload,expected", FUNCTION_CASES["pathological"]) | ||
| def test_func_start_pathological(self, payload, expected): | ||
| assert_pathological_match(TCL_RULES["func_start"], payload, expected, "tcl") | ||
|
|
||
| # ------------------------------------------------------------------------- | ||
| # args | ||
| # ------------------------------------------------------------------------- | ||
| @pytest.mark.parametrize("payload,expected", ARGS_CASES["valid"]) | ||
| def test_args_valid(self, payload, expected): | ||
| assert_valid_match(TCL_RULES["args"], payload, expected, "tcl") | ||
|
|
||
| @pytest.mark.parametrize("payload", ARGS_CASES["invalid"]) | ||
| def test_args_invalid(self, payload): | ||
| assert_invalid_no_match(TCL_RULES["args"], payload, "tcl") | ||
|
|
||
| @pytest.mark.parametrize("payload,expected", ARGS_CASES["pathological"]) | ||
| def test_args_pathological(self, payload, expected): | ||
| assert_pathological_match(TCL_RULES["args"], payload, expected, "tcl") | ||
|
|
||
| # ------------------------------------------------------------------------- | ||
| # class_start | ||
| # ------------------------------------------------------------------------- | ||
| @pytest.mark.parametrize("payload,expected", CLASS_CASES["valid"]) | ||
| def test_class_start_valid(self, payload, expected): | ||
| assert_valid_match(TCL_RULES["class_start"], payload, expected, "tcl") | ||
|
|
||
| @pytest.mark.parametrize("payload", CLASS_CASES["invalid"]) | ||
| def test_class_start_invalid(self, payload): | ||
| assert_invalid_no_match(TCL_RULES["class_start"], payload, "tcl") | ||
|
|
||
| @pytest.mark.parametrize("payload,expected", CLASS_CASES["pathological"]) | ||
| def test_class_start_pathological(self, payload, expected): | ||
| assert_pathological_match(TCL_RULES["class_start"], payload, expected, "tcl") | ||
|
|
||
| # ------------------------------------------------------------------------- | ||
| # _dependency_capture | ||
| # ------------------------------------------------------------------------- | ||
| @pytest.mark.parametrize("payload,expected", DEPENDENCY_CASES["valid"]) | ||
| def test_dependency_valid(self, payload, expected): | ||
| assert_valid_dependency_match(TCL_RULES["_dependency_capture"], payload, expected, "tcl") | ||
|
|
||
| @pytest.mark.parametrize("payload", DEPENDENCY_CASES["invalid"]) | ||
| def test_dependency_invalid(self, payload): | ||
| assert_invalid_no_match(TCL_RULES["_dependency_capture"], payload, "tcl") | ||
|
|
||
| @pytest.mark.parametrize("payload,expected", DEPENDENCY_CASES["pathological"]) | ||
| def test_dependency_pathological(self, payload, expected): | ||
| assert_pathological_dependency_match(TCL_RULES["_dependency_capture"], payload, expected, "tcl") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.