|
| 1 | +# Configuration |
| 2 | + |
| 3 | +This guide will help you to configure covered for your project's specific requirements. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +The simplest way to configure covered is through environment variables: |
| 8 | + |
| 9 | +``` bash |
| 10 | +# Basic coverage with default report |
| 11 | +COVERAGE=true rspec |
| 12 | + |
| 13 | +# Specific report types |
| 14 | +COVERAGE=PartialSummary rspec |
| 15 | +COVERAGE=BriefSummary rspec |
| 16 | +COVERAGE=MarkdownSummary rspec |
| 17 | + |
| 18 | +# Multiple reports (comma-separated) |
| 19 | +COVERAGE=PartialSummary,BriefSummary rspec |
| 20 | +``` |
| 21 | + |
| 22 | +## Configuration File |
| 23 | + |
| 24 | +For advanced configuration, create a `config/covered.rb` file in your project: |
| 25 | + |
| 26 | +~~~ ruby |
| 27 | +# config/covered.rb |
| 28 | + |
| 29 | +def ignore_paths |
| 30 | + super + ["engines/", "app/assets/", "db/migrate/"] |
| 31 | +end |
| 32 | + |
| 33 | +def include_patterns |
| 34 | + super + ["bake/**/*.rb", "engines/**/*.rb"] |
| 35 | +end |
| 36 | + |
| 37 | +def make_policy(policy) |
| 38 | + super |
| 39 | + |
| 40 | + # Custom policy configuration |
| 41 | + policy.skip(/\/generated\//) |
| 42 | + policy.include("lib/templates/**/*.erb") |
| 43 | +end |
| 44 | +~~~ |
| 45 | + |
| 46 | +## Environment Variables |
| 47 | + |
| 48 | +Covered uses several environment variables for configuration: |
| 49 | + |
| 50 | +| Variable | Description | Default | |
| 51 | +|----------------|-----------------------------------|--------------------| |
| 52 | +| `COVERAGE` | Report types to generate | `nil` (no reports) | |
| 53 | +| `COVERED_ROOT` | Project root directory | `Dir.pwd` | |
| 54 | +| `RUBYOPT` | Modified internally for autostart | Current value | |
| 55 | + |
| 56 | +### Examples |
| 57 | + |
| 58 | +``` bash |
| 59 | +# Disable coverage entirely |
| 60 | +unset COVERAGE |
| 61 | + |
| 62 | +# Enable default report (BriefSummary) |
| 63 | +COVERAGE=true |
| 64 | + |
| 65 | +# Custom project root |
| 66 | +COVERED_ROOT=/path/to/project COVERAGE=PartialSummary rspec |
| 67 | +``` |
| 68 | + |
| 69 | +## Report Types |
| 70 | + |
| 71 | +Covered provides several built-in report types: |
| 72 | + |
| 73 | +### Summary Reports |
| 74 | + |
| 75 | +- **`Summary`** - Full detailed coverage report with line-by-line analysis |
| 76 | +- **`FullSummary`** - Complete coverage without threshold filtering |
| 77 | +- **`BriefSummary`** - Shows overall statistics + top 5 files with least coverage |
| 78 | +- **`PartialSummary`** - Shows only code segments with incomplete coverage + lists 100% covered files |
| 79 | +- **`MarkdownSummary`** - Coverage report formatted as Markdown |
| 80 | +- **`Quiet`** - Suppresses all output |
| 81 | + |
| 82 | +### Usage Examples |
| 83 | + |
| 84 | +``` ruby |
| 85 | +# In config/covered.rb |
| 86 | +def make_policy(policy) |
| 87 | + super |
| 88 | + |
| 89 | + # Add multiple reports |
| 90 | + policy.reports << Covered::PartialSummary.new |
| 91 | + policy.reports << Covered::MarkdownSummary.new(threshold: 0.8) |
| 92 | +end |
| 93 | +``` |
| 94 | + |
| 95 | +## Path Configuration |
| 96 | + |
| 97 | +### Ignoring Paths |
| 98 | + |
| 99 | +Default ignored paths include `test/`, `spec/`, `fixtures/`, `vendor/`, and `config/`. Customize with: |
| 100 | + |
| 101 | +``` ruby |
| 102 | +def ignore_paths |
| 103 | + super + [ |
| 104 | + "engines/", # Engine directories |
| 105 | + "app/assets/", # Asset files |
| 106 | + "db/migrate/", # Database migrations |
| 107 | + "tmp/", # Temporary files |
| 108 | + "log/", # Log files |
| 109 | + "coverage/", # Coverage output |
| 110 | + "public/packs/" # Webpack outputs |
| 111 | + ] |
| 112 | +end |
| 113 | +``` |
| 114 | + |
| 115 | +### Including Patterns |
| 116 | + |
| 117 | +Default includes `lib/**/*.rb`. Extend for additional patterns: |
| 118 | + |
| 119 | +``` ruby |
| 120 | +def include_patterns |
| 121 | + super + [ |
| 122 | + "app/**/*.rb", # Application code |
| 123 | + "bake/**/*.rb", # Bake tasks |
| 124 | + "engines/**/*.rb", # Engine code |
| 125 | + "lib/templates/**/*.erb" # Template files (Ruby 3.2+) |
| 126 | + ] |
| 127 | +end |
| 128 | +``` |
| 129 | + |
| 130 | +## Advanced Policy Configuration |
| 131 | + |
| 132 | +The `make_policy` method provides fine-grained control: |
| 133 | + |
| 134 | +``` ruby |
| 135 | +def make_policy(policy) |
| 136 | + super |
| 137 | + |
| 138 | + # Filter by regex patterns |
| 139 | + policy.skip(/\/generated\//) # Skip generated files |
| 140 | + policy.skip(/\.generated\.rb$/) # Skip files ending in .generated.rb |
| 141 | + |
| 142 | + # Include specific files |
| 143 | + policy.include("config/application.rb") |
| 144 | + |
| 145 | + # Only track specific patterns |
| 146 | + policy.only(/^app\//) |
| 147 | + |
| 148 | + # Set custom root |
| 149 | + policy.root(File.expand_path('..', __dir__)) |
| 150 | + |
| 151 | + # Enable persistent coverage across runs |
| 152 | + policy.persist! |
| 153 | + |
| 154 | + # Configure reports programmatically |
| 155 | + if ENV['CI'] |
| 156 | + policy.reports << Covered::MarkdownSummary.new |
| 157 | + else |
| 158 | + policy.reports << Covered::PartialSummary.new |
| 159 | + end |
| 160 | +end |
| 161 | +``` |
| 162 | + |
| 163 | +## Common Configuration Patterns |
| 164 | + |
| 165 | +### Rails Applications |
| 166 | + |
| 167 | +``` ruby |
| 168 | +def ignore_paths |
| 169 | + super + [ |
| 170 | + "app/assets/", |
| 171 | + "db/migrate/", |
| 172 | + "db/seeds.rb", |
| 173 | + "config/environments/", |
| 174 | + "config/initializers/", |
| 175 | + "tmp/", |
| 176 | + "log/", |
| 177 | + "public/", |
| 178 | + "storage/" |
| 179 | + ] |
| 180 | +end |
| 181 | + |
| 182 | +def include_patterns |
| 183 | + super + [ |
| 184 | + "app/**/*.rb", |
| 185 | + "lib/**/*.rb", |
| 186 | + "config/application.rb", |
| 187 | + "config/routes.rb" |
| 188 | + ] |
| 189 | +end |
| 190 | +``` |
| 191 | + |
| 192 | +### Gem Development |
| 193 | + |
| 194 | +``` ruby |
| 195 | +def ignore_paths |
| 196 | + super + ["examples/", "benchmark/"] |
| 197 | +end |
| 198 | + |
| 199 | +def include_patterns |
| 200 | + super + ["bin/**/*.rb"] |
| 201 | +end |
| 202 | + |
| 203 | +def make_policy(policy) |
| 204 | + super |
| 205 | + |
| 206 | + # Only track the gem's main code |
| 207 | + policy.only(/^lib\//) |
| 208 | +end |
| 209 | +``` |
| 210 | + |
| 211 | +### Monorepo/Multi-Engine |
| 212 | + |
| 213 | +``` ruby |
| 214 | +def ignore_paths |
| 215 | + super + [ |
| 216 | + "engines/*/spec/", |
| 217 | + "engines/*/test/", |
| 218 | + "shared/fixtures/" |
| 219 | + ] |
| 220 | +end |
| 221 | + |
| 222 | +def include_patterns |
| 223 | + super + [ |
| 224 | + "engines/*/lib/**/*.rb", |
| 225 | + "engines/*/app/**/*.rb", |
| 226 | + "shared/lib/**/*.rb" |
| 227 | + ] |
| 228 | +end |
| 229 | +``` |
| 230 | + |
| 231 | +## Template Coverage (Ruby 3.2+) |
| 232 | + |
| 233 | +For projects using ERB, Haml, or other template engines: |
| 234 | + |
| 235 | +``` ruby |
| 236 | +def include_patterns |
| 237 | + super + [ |
| 238 | + "app/views/**/*.erb", |
| 239 | + "lib/templates/**/*.erb", |
| 240 | + "app/views/**/*.haml" |
| 241 | + ] |
| 242 | +end |
| 243 | + |
| 244 | +def make_policy(policy) |
| 245 | + super |
| 246 | + |
| 247 | + # Ensure template coverage is enabled |
| 248 | + require 'covered/erb' if defined?(ERB) |
| 249 | +end |
| 250 | +``` |
| 251 | + |
| 252 | +## CI/CD Integration |
| 253 | + |
| 254 | +### GitHub Actions |
| 255 | + |
| 256 | +``` ruby |
| 257 | +def make_policy(policy) |
| 258 | + super |
| 259 | + |
| 260 | + if ENV['GITHUB_ACTIONS'] |
| 261 | + # Use markdown format for GitHub |
| 262 | + policy.reports << Covered::MarkdownSummary.new |
| 263 | + |
| 264 | + # Fail build on low coverage |
| 265 | + policy.reports << Class.new do |
| 266 | + def call(wrapper, output = $stdout) |
| 267 | + statistics = wrapper.each.inject(Statistics.new) { |s, c| s << c } |
| 268 | + if statistics.ratio < 0.90 |
| 269 | + exit 1 |
| 270 | + end |
| 271 | + end |
| 272 | + end.new |
| 273 | + end |
| 274 | +end |
| 275 | +``` |
| 276 | + |
| 277 | +### Custom Thresholds |
| 278 | + |
| 279 | +``` ruby |
| 280 | +def make_policy(policy) |
| 281 | + super |
| 282 | + |
| 283 | + # Different thresholds for different environments |
| 284 | + threshold = case ENV['RAILS_ENV'] |
| 285 | + when 'production' then 0.95 |
| 286 | + when 'staging' then 0.90 |
| 287 | + else 0.80 |
| 288 | + end |
| 289 | + |
| 290 | + policy.reports << Covered::Summary.new(threshold: threshold) |
| 291 | +end |
| 292 | +``` |
| 293 | + |
| 294 | +## Performance Optimization |
| 295 | + |
| 296 | +For large codebases: |
| 297 | + |
| 298 | +``` ruby |
| 299 | +def make_policy(policy) |
| 300 | + super |
| 301 | + |
| 302 | + # Skip large generated directories |
| 303 | + policy.skip(/\/node_modules\//) |
| 304 | + policy.skip(/\/vendor\/bundle\//) |
| 305 | + policy.skip(/\/coverage\//) |
| 306 | + |
| 307 | + # Use more efficient reports for large projects |
| 308 | + if Dir['**/*.rb'].length > 1000 |
| 309 | + policy.reports << Covered::BriefSummary.new |
| 310 | + else |
| 311 | + policy.reports << Covered::PartialSummary.new |
| 312 | + end |
| 313 | +end |
| 314 | +``` |
| 315 | + |
| 316 | +## Troubleshooting |
| 317 | + |
| 318 | +### Common Issues |
| 319 | + |
| 320 | +**Coverage not working:** |
| 321 | +- Ensure `require 'covered/rspec'` (or similar) is at the top of your test helper |
| 322 | +- Check that `COVERAGE` environment variable is set |
| 323 | +- Verify the configuration file path is correct: `config/covered.rb` |
| 324 | + |
| 325 | +**Missing files in reports:** |
| 326 | +- Files must be required/loaded during test execution to be tracked |
| 327 | +- Use `include_patterns` to track files not loaded by tests |
| 328 | +- Check `ignore_paths` isn't excluding desired files |
| 329 | + |
| 330 | +**Performance issues:** |
| 331 | +- Use `BriefSummary` instead of `Summary` for large codebases |
| 332 | +- Add more specific patterns to `ignore_paths` |
| 333 | +- Use `policy.only()` to limit scope |
| 334 | + |
| 335 | +**Template coverage not working:** |
| 336 | +- Requires Ruby 3.2+ for full template support |
| 337 | +- Ensure template engines are loaded before coverage starts |
| 338 | +- Check that template files match `include_patterns` |
| 339 | + |
| 340 | +### Debug Configuration |
| 341 | + |
| 342 | +``` ruby |
| 343 | +def make_policy(policy) |
| 344 | + super |
| 345 | + |
| 346 | + # Debug: print current configuration |
| 347 | + if ENV['DEBUG_COVERAGE'] |
| 348 | + puts "Ignore paths: #{ignore_paths}" |
| 349 | + puts "Include patterns: #{include_patterns}" |
| 350 | + puts "Root: #{@root}" |
| 351 | + end |
| 352 | +end |
| 353 | +``` |
| 354 | + |
| 355 | +See the {ruby Covered::Config} class for complete API documentation. |
0 commit comments