Generic/ScopeIndent: remove check for test constant from src code #1036
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.
Description
The
Generic.WhiteSpace.ScopeIndent
sniff includes aprivate
boolean$debug
property which can be toggled to enable/disable showing sniff specific debug information. By default, debug information is not shown.Showing of debug information can be enabled in the following manner:
true
in the sniff file (without committing as the default should not change in the committed code).--runtime-set scope_indent_debug 1
flag set.scope_indent_debug
config setting totrue
from a custom ruleset.scope_indent_debug
config setting by running--config-set scope_indent_debug 1
.The
Generic.WhiteSpace.ScopeIndent
sniff includes a condition in theregister()
method to prevent debug information from being generated during a test run, which would lead to "unexpected output", which would mark the test as risky and potentially fail the build.Now, generally speaking, having conditions specific to running the tests in the source should be consider bad practice as it makes testing code affected by those type of conditions impossible. Basically, if a test needs a specific environment, this should be handled in the test setup/teardown.
As things were, this condition was also not very effective. Of the above ways of changing the value of the
$debug
property, only method [1] and [4] could come into play in automated test run situations and the debug output would only be prevented for [1] (the hard-coded default value being changed in the sniff code itself).In case of [4], the debug info would (originally) still show.
Now, the debug info showing when the system-wide default was set for the
scope_indent_debug
config setting [4], was (incidentally) fixed via PR #275 which introduced theConfigDouble
, which prevents the user specificCodeSniffer.conf
file from being read out for those tests using theConfigDouble
, which, since #275, includes all tests extending theAbstractSniffUnitTest
class.So, now we still have situation [1] to deal with - which basically comes down to a maintainer/contributor enabling the debug option while working on the sniff, but not wanting the tests to fail when running them to check the changing they are making.
Well, for the sniff tests, as they currently, we cannot use the PHPUnit
setUp()
method as the sniff will not have been initialized yet, preventing us from using reflection on the object. Additionally, depending on the order of tests being run, theConfig
may also not yet have been setup. That is inherit to the current test setup of theAbstractSniffUnitTest
class and fixing that is outside the scope of this ticket.However, the
AbstractSniffUnitTest
offers asetCliValues()
method, which is called before the test case files are being tokenized and processed, so we can use that method to set thescope_indent_debug
config tofalse
for the test run, solving the problem and allowing for removing the test specific condition from theScopeIndent
sniff.Additional notes:
Generic
as well as thePEAR
ScopeIndent
sniffs as thePEAR
sniff extends theGeneric
sniff and we cannot rely on the tests being run in a specific order, so we need to make sure the config is set either way.Config::getConfigData('scope_indent_debug')
at the start of theprocess()
method. This should be moved up into theregister()
method for efficiency, as Config settings are not changable during the run, so there should be no need to check the value for each and every file. However, this would break with how the currentAbstractSniffUnitTest
base class works as it does everything within the test method. Fixing that is outside the scope of this PR, but something to keep in mind for a later time.Suggested changelog entry
N/A
Related issues/external references
Related to #966