test-pmd-rule 0.1.0

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.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Your Organization
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.
package/README.md ADDED
@@ -0,0 +1,232 @@
1
+ # test-pmd-rule
2
+
3
+ A standalone PMD Rule Tester for Apex rules, with XPath analysis and coverage validation.
4
+
5
+ [![Node Version](https://img.shields.io/badge/node-%3E%3D25.0.0-brightgreen)](https://nodejs.org/)
6
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.x-blue)](https://www.typescriptlang.org/)
7
+ [![License](https://img.shields.io/badge/license-MIT-green)](LICENSE.md)
8
+
9
+ ## Overview
10
+
11
+ This tool validates PMD Apex rules by testing them against examples embedded in the rule XML files. It ensures rules work correctly, provide adequate test coverage, and don't contain hardcoded values that should be parameterized.
12
+
13
+ ## Features
14
+
15
+ - **Rule Validation**: Tests PMD rules against their documented examples with detailed test results
16
+ - **XPath Analysis**: Analyzes XPath expressions for node types, operators, attributes, and conditionals
17
+ - **Coverage Checking**: Validates that XPath expressions are properly tested with line number references
18
+ - **Hardcoded Value Detection**: Identifies values that should be parameterized
19
+ - **Line Number Tracking**: Shows exact line numbers in XML files for missing coverage items
20
+ - **TypeScript**: Written in TypeScript for better maintainability
21
+ - **100% Test Coverage**: All code is thoroughly tested (lines, functions, branches, statements)
22
+ - **Modular Architecture**: Clean separation of concerns with low complexity functions
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ # Clone the repository
28
+ git clone https://github.com/your-org/test-pmd-rule.git
29
+ cd test-pmd-rule
30
+
31
+ # Install dependencies
32
+ pnpm install
33
+
34
+ # Build the project
35
+ pnpm build
36
+ ```
37
+
38
+ ## Usage
39
+
40
+ ```bash
41
+ # Test a single rule
42
+ node dist/test-pmd-rule.js path/to/rule.xml
43
+
44
+ # Or after building
45
+ pnpm build
46
+ node dist/test-pmd-rule.js rulesets/code-style/AvoidMagicNumbers.xml
47
+ ```
48
+
49
+ The tool will:
50
+
51
+ 1. Extract examples from the PMD rule XML file
52
+ 2. Parse violation and valid markers from example code
53
+ 3. Create temporary Apex test files
54
+ 4. Run PMD against the test files
55
+ 5. Validate that violations occur for violation examples and don't occur for valid examples
56
+ 6. Analyze XPath coverage and show missing items with line numbers
57
+ 7. Report comprehensive test results
58
+
59
+ ## Requirements
60
+
61
+ - **Node.js**: ≥25.0.0
62
+ - **PMD CLI**: Available in PATH (see [PMD Installation](https://pmd.github.io/pmd/pmd_userdocs_installation.html))
63
+ - **Package Manager**: pnpm ≥10.0.0 (recommended)
64
+
65
+ ## Development
66
+
67
+ ### Setup
68
+
69
+ ```bash
70
+ # Clone the repository
71
+ git clone https://github.com/your-org/test-pmd-rule.git
72
+ cd test-pmd-rule
73
+
74
+ # Install dependencies
75
+ pnpm install
76
+
77
+ # Set up git hooks
78
+ pnpm prepare
79
+ ```
80
+
81
+ ### Available Scripts
82
+
83
+ ```bash
84
+ # Build the project
85
+ pnpm build
86
+
87
+ # Run tests
88
+ pnpm test
89
+
90
+ # Run tests with coverage (100% required)
91
+ pnpm test:coverage
92
+
93
+ # Run tests in watch mode
94
+ pnpm test:watch
95
+
96
+ # Type checking
97
+ pnpm typecheck
98
+
99
+ # Linting
100
+ pnpm lint
101
+
102
+ # Linting with auto-fix
103
+ pnpm lint:fix
104
+
105
+ # Formatting
106
+ pnpm format
107
+
108
+ # Check formatting
109
+ pnpm format:check
110
+
111
+ # Pre-commit checks (format, lint, test coverage)
112
+ pnpm pre-commit
113
+ ```
114
+
115
+ ### Project Structure
116
+
117
+ ```
118
+ src/
119
+ ├── cli/ # Command-line interface
120
+ │ └── main.ts # CLI entry point
121
+ ├── pmd/ # PMD execution utilities
122
+ │ ├── runPMD.ts # PMD CLI execution
123
+ │ └── parseViolations.ts # XML violation parsing
124
+ ├── parser/ # Example parsing and processing
125
+ │ ├── parseExample.ts # Example code parsing
126
+ │ ├── extractMarkers.ts # Violation/valid marker extraction
127
+ │ └── createTestFile.ts # Test file generation
128
+ ├── xpath/ # XPath analysis and validation
129
+ │ ├── extractXPath.ts # XPath extraction from XML
130
+ │ ├── analyzeXPath.ts # XPath analysis orchestration
131
+ │ ├── checkCoverage.ts # Coverage checking
132
+ │ └── extractors/ # XPath component extractors
133
+ │ ├── extractNodeTypes.ts
134
+ │ ├── extractOperators.ts
135
+ │ ├── extractAttributes.ts
136
+ │ └── extractConditionals.ts
137
+ ├── tester/ # Main testing logic
138
+ │ ├── RuleTester.ts # Main tester class
139
+ │ ├── qualityChecks.ts # Quality validation
140
+ │ └── quality/ # Quality check modules
141
+ │ ├── checkRuleMetadata.ts
142
+ │ ├── checkExamples.ts
143
+ │ └── checkDuplicates.ts
144
+ └── types/ # TypeScript type definitions
145
+ └── index.ts
146
+
147
+ tests/ # Unit and integration tests
148
+ ├── unit/ # Unit tests
149
+ └── integration/ # End-to-end tests
150
+
151
+ docs/ # Documentation (symlinked from agent-docs)
152
+ ```
153
+
154
+ ## Output Format
155
+
156
+ The tool provides detailed output including:
157
+
158
+ - **Test Details**: Individual test results for each example (violation/valid tests)
159
+ - **Test Summary**: Overall statistics (examples tested, passed, total violations)
160
+ - **XPath Coverage**: Detailed coverage analysis showing:
161
+ - Node types coverage with line numbers for missing items
162
+ - Conditionals coverage (only missing items shown)
163
+ - Attributes coverage with line numbers for missing items
164
+ - Operators coverage
165
+ - **Final Status**: Overall pass/fail status
166
+
167
+ Example output:
168
+
169
+ ```
170
+ 🧪 Testing rule: rulesets/code-style/MyRule.xml
171
+
172
+ 📋 Test Details:
173
+ - Example 1 Test: Violation ✅
174
+ - Example 1 Test: Valid ✅
175
+ - Example 2 Test: Violation ✅
176
+
177
+ 📊 Test Summary:
178
+ Examples tested: 2
179
+ Examples passed: 2
180
+ Total violations: 3
181
+ Rule triggers violations: ✅ Yes
182
+
183
+ 🔍 XPath Coverage:
184
+ Status: ⚠️ Incomplete
185
+ Coverage items: 2
186
+ 1. ⚠️ Node types: 4/6 covered
187
+ Missing:
188
+ - Line 43: VariableDeclaration
189
+ - Line 50: VariableExpression
190
+ 2. ⚠️ Attributes: 2/3 covered
191
+ Missing:
192
+ - Line 52: BeginLine
193
+
194
+ ✅ All tests passed!
195
+ ```
196
+
197
+ ## Documentation
198
+
199
+ Comprehensive documentation is available in the [`docs/`](docs/) directory (symlinked from [agent-docs](https://github.com/starch-uk/agent-docs/)):
200
+
201
+ - PMD Quick Reference
202
+ - Code Analyzer Configuration
203
+ - XPath 3.1 Reference
204
+ - PMD Apex AST Reference
205
+
206
+ ## Contributing
207
+
208
+ We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
209
+
210
+ ### Development Guidelines
211
+
212
+ - **Cyclomatic Complexity**: All functions must maintain <10 complexity (target: <7)
213
+ - **Test Coverage**: 100% coverage required (lines, functions, branches, statements)
214
+ - **Code Style**: Follow the established Prettier and ESLint configurations
215
+ - **Function Length**: Maximum 39 lines per function
216
+ - **Module Size**: Maximum 500 lines per module
217
+ - **Commits**: Use conventional commit messages
218
+ - **Pre-commit**: All commits must pass format check, linting, and 100% test coverage
219
+
220
+ ## License
221
+
222
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details.
223
+
224
+ ## Security
225
+
226
+ Please report security vulnerabilities to [security@starch.uk](mailto:security@starch.uk). See our [Security Policy](SECURITY.md) for more information.
227
+
228
+ ## Related Projects
229
+
230
+ - [sca-extra](https://github.com/starch-uk/sca-extra) - Salesforce Code Analyzer extras
231
+ - [PMD](https://pmd.github.io/) - PMD static analysis tool
232
+ - [agent-docs](https://github.com/starch-uk/agent-docs) - Documentation for AI agents