Skip to content
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

[Question] Is there any way to specify wildcard match expression for the subjects? #1272

Closed
omaik opened this issue Oct 27, 2021 · 9 comments

Comments

@omaik
Copy link

omaik commented Oct 27, 2021

** Summary:

I have a rails project, on which I want to configure the CI job, to run mutations for all changed classes in incremental mode.

My .mutant.yml

integration: rspec
includes:
  - config
requires:
  - environment.rb

** Commands that work fine:

RAILS_ENV=test bundle exec mutant run -- 'ModuleName*'

RAILS_ENV=test bundle exec mutant run --since master -- 'ModuleName*'

** Problem :

I would like to run mutations across ALL classes. Since this is a Rails project, there are many top-level classes(Models, Controllers, etc). It will be painful to specify all of them in the config file, and someone may forget to add there a new class.

Tried something like these, but mutant does not match any subject.

RAILS_ENV=test bundle exec mutant run -- '*'
RAILS_ENV=test bundle exec mutant run 
RAILS_ENV=test bundle exec mutant run -- 'Object*' 

# RAILS_ENV=test bundle exec mutant run -- '*' 
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.8-compliant syntax, but you are running 2.6.6.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
Module#name from: REXML::Functions raised an error: #<NoMethodError: undefined method `[]' for nil:NilClass>. Fix your lib to follow normal ruby semantics!
{Module,Class}#name should return resolvable constant name as String or nil
Mutant environment:
Matcher:         #<Mutant::Matcher::Config subjects: [*]>
Integration:     rspec
Jobs:            16
Includes:        ["config"]
Requires:        ["environment.rb"]
Subjects:        0
Total-Tests:     330
Selected-Tests:  0
Tests/Subject:   0.00 avg
Mutations:       0
Mutant environment:
Matcher:         #<Mutant::Matcher::Config subjects: [*]>
Integration:     rspec
Jobs:            16
Includes:        ["config"]
Requires:        ["environment.rb"]
Subjects:        0
Total-Tests:     330
Selected-Tests:  0
Tests/Subject:   0.00 avg
Mutations:       0
Results:         0
Kills:           0
Alive:           0
Timeouts:        0
Runtime:         0.09s
Killtime:        0.00s
Overhead:        Inf%
Mutations/s:     0.00
Coverage:        100.00%

Probably I just doing something wrong and missing obvious stuff, please forgive me for that.

Thanks in advance!

@mbj
Copy link
Owner

mbj commented Oct 27, 2021

There is an unreleased version that makes mutant rails aware allowing you to do rails:models, rails:controllers etc. Happy to get that officially released this weekend.

@mbj
Copy link
Owner

mbj commented Nov 7, 2021

First iteration for primary rails support is adding env variables pre app boot, which is done in: #1274.

@mbj
Copy link
Owner

mbj commented Nov 8, 2021

I'm actively working on this in: #1129. Sorry @omaik it takes its time.

@omaik
Copy link
Author

omaik commented Nov 8, 2021

@mbj NP, nothing critical here, just wanted to know if I miss something.

I checked the source code and seems that now I understand that if no module is specified, mutant has no way to separate application classes from gems/built-in Ruby classes.

@mbj
Copy link
Owner

mbj commented Nov 8, 2021

@omaik Yes its overall a signalling issue in the wider ruby ecosystem.

We have a "flat global mutable object namespace". And unless you use sorbet: "Everything can add classes/modules everywhere". Mutant hence has to be given a "root" to expand from, to not have to read the source locations of every method available to eventually fall within your repository (Which would be very slow at scale).

The rails integration I'm working on gives mutant some defaults like a new match expression rails:* that does the "right thing" in expanding the root to Application{Controller,Record,Whatever}.

An alternative for the "owned" namespace detection is to use the local sorbet setup. This is something being worked on in a currently private branch I'll be able to push soon.

Mutant is in a funny chicken egg situation, where these features prevent adoption. But until I get more subscribers, I need to keep working hard outside of mutant. So thanks for your patience ;).

@mbj
Copy link
Owner

mbj commented Nov 15, 2021

@omaik making progress in smaller steps, I just extracted a descendants matcher primitive that will be part of tonights 0.11.2 release:

#1283

It allows to match subjects by inheritance tree that does NOT form a namespace topology. As typical rails does not:

ApplicationControlelr
SomeResourceController < ApplicationController

You can now match all descendants of application controller via the new match expression: descendants:ApplicationController.

Note this is not rails or controller specific syntax. It works with any inheritance tree. Together with eager loading you should now be able to get much nicer mutant matches.

Please be aware that mutant supports multiple match expressions, so you can do:

descendants:ApplicationController
descendants:ApplicationRecord
MyNamespace*

in the same execution.

@omaik
Copy link
Author

omaik commented Nov 15, 2021

@mbj This is awesome, will check this out. Thank you so much!

@omaik
Copy link
Author

omaik commented Nov 23, 2021

@mbj This feature works as expected and covers 99% of our codebase because almost all classes are inherited from a few base classes. Thanks again!

@omaik omaik closed this as completed Nov 23, 2021
@mbj
Copy link
Owner

mbj commented Nov 23, 2021

@omaik Thanks for the feedback. Much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants