-
Notifications
You must be signed in to change notification settings - Fork 7
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
Allow configuration of safe API misuses #11
Comments
I was thinking a bit about how to specify members to ignore, and from my perspective, we shouldn't reinvent the wheel. I would go with the same format which is used for suppressing warnings in suppression files. This gives us quite good flexibility in terms of members specification e.g:
|
@tpodolak Sounds good to me! Some random thoughts:
|
@dtchepak the suppressions based on json file will have to be implemented manually, we will just use Roslyn ability to parse the target syntax and get the symbols based on that, the rest will be on us.
Yes, Roslyn can do both, so I think it is possible to write a quick-fix for adding suppressions to the json file. Although you probably will get this ugly pop-up window indicating that project has changed if we will add json file to the project via quick-fix (in case user didn't add it). I will try to write some PoC for quick-fix which modifies/creates the json file. |
Does it need to be added to the project? Or just dumped in the project root? (i'm assuming will be at project-level rather than solution-level?) |
@dtchepak It must be added as an additional file to the project so as I can access it from the analyzer. One way or another as for now it is impossible to properly add additional file to the project via Roslyn. We basically encountered the same bug as the one reported by StyleCops guys dotnet/roslyn#4655. So as for today the user have to create empty file on its own and set the proper build action as well. Once the file is there I will be showing the quickfixes for suppressions. |
@tpodolak Can we include the file by default with the nuget package? |
…-virtual member diagnostics - vb version
@dtchepak I've tried this already but it didn't work, same for custom build target/props file. All of the approaches seem not to copy files to the project but reference them from the nuget package location, which makes the nsubstitute.json file uneditable. I've looked at some existing packages which used to add config files to the projects (for instance NLog.Config) and those packages behave in the same weird way. So to sum up I wasn't able to implement this correctly - if you guys have time please take a look, I am out of ideas ;]. |
@tpodolak For suppressions i'm not sure if there are other safe API "misuses". Should be fine to wait until other cases come up (unless it is trivial to implement). Is it possible to ass a .cs or .vb file to the project (rather than a config file)? Or does that face the same problems? If so then we could try something like encoding with attributes as per |
I didn't check if there were issues with adding vs/cs files to the project but I would avoid do the hacks in order to make the suppressions fully automatic. I think as long as we document how to include the nsubstitute.json file in the project we should be fine. Either way the most complicated part (which is creating proper suppression entry) will be done by fix-provider so that is an acceptable solution at least in my opinion. When it comes to additional API misues I will add the one which is about to be mered: non-virtual calls in When...Do. As far as I can tell this is basically the same use case as in Returns/ReturnForAnyArgs etc |
…safe-api-misuses Gh 11 allow configuration of safe api misuses
In some cases the NSub API can be intentionally used in a questionable manner to get a specific result.
A common example I have seen is "mocking" extensions methods. For example:
We can then "mock" this extension method by cheating:
This will work because
MyExtensions.GetCount()
invokesICounter.GetCount(widget)
, soReturns
will successfully mock theICounter
call (rather than the static, unmockableMyExtensions.GetCount()
). NSubstitute.Analyzers correctly reports this as "NS001 | Member GetCount can not be intercepted. Only interface members and virtual, overriding, and abstract members can be intercepted." Which would normally be fine, but in this case we're using this to intercept the underlying call instead.@tpodolak's suggestion is to use a configuration file to allow developers to whitelist specific calls in their project. We still need to flesh out this idea, but here are some rough notes:
NS001
errors forMyProject.MyExtensions.GetCount
.NS001
forMyProject.MyExtensions.Get*
?NS1xx
series could be used for attempts to sub non-virtuals.NS101
can be for class methods,NS102
for class properties,NS111
for static class methods,NS120
for extension methods etc. Then we could specifically ignoreNS111
forMyProject.MyClass.*
in the case we know all static members delegate to an underlying virtual method, but we still want to catch any instances of trying to substitute non-virtual class methods. (This is just an idea, it is probably sufficient just to do the basic whitelist instead.)The text was updated successfully, but these errors were encountered: