-
Notifications
You must be signed in to change notification settings - Fork 134
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
How to prevent depending on tests and vendor directories #1220
Comments
Let me see what I can glean from the data provided. I will start from the provided output and work backward. ----------- ---------------------------------------------------------------------------------------------
Reason Tests
----------- ---------------------------------------------------------------------------------------------
Violation Tests\EndToEnd\EndToEndTest must not depend on Domain\Example (Domain)
Tests\EndToEndTest::57 ->
Domain\Example::5
tests/EndToEndTest.php:5
Violation Tests\EndToEnd\EndToEndTest must not depend on PHPUnit\Util\Test (Vendor)
Tests\TestCase::13 ->
PHPUnit\Framework\TestCase::26 ->
PHPUnit\Util\Test::1044
----------- --------------------------------------------------------------------------------------------- implies that you do not allow dependencies from ----------- ---------------------------------------------------------------------------------------------
Reason Vendor
----------- ---------------------------------------------------------------------------------------------
Violation Tests\EndToEnd\EndToEndTest must not depend on Infrastructure\Example (Infrastructure)
Tests\EndToEnd\EndToEndFunctionalTestCase::57 ->
Infrastructure\Example::12
tests/EndToEnd/EndToEndFunctionalTestCase.php:12
----------- --------------------------------------------------------------------------------------------- implies that class-like (class, interface, trait, enum) Both of those things seem like you did not expect them to be the case. Looking at the relevant parts of the provided configuration : parameters:
layers:
-
name: Vendor
collectors:
- type: directory
regex: ./vendor/.*
ruleset:
Tests:
- Domain I would not expect this behavior either. For the second problem ( For the first problem, I am stumped. It should not be possible given the information you've provided on this issue. Is it possible that you have mistyped something when transcribing your configuration to this issue post? Also from the output, it seems that ----------- ---------------------------------------------------------------------------------------------
Reason Tests
----------- ---------------------------------------------------------------------------------------------
Violation Tests\EndToEnd\EndToEndTest must not depend on Domain\Example (Domain)
Tests\EndToEndTest::57 ->
Domain\Example::5
tests/EndToEndTest.php:5
----------- ---------------------------------------------------------------------------------------------
----------- ---------------------------------------------------------------------------------------------
Reason Vendor
----------- ---------------------------------------------------------------------------------------------
Violation Tests\EndToEnd\EndToEndTest must not depend on Infrastructure\Example (Infrastructure)
Tests\EndToEnd\EndToEndFunctionalTestCase::57 ->
Infrastructure\Example::12
tests/EndToEnd/EndToEndFunctionalTestCase.php:12
----------- --------------------------------------------------------------------------------------------- Did you get a warning about that? |
Thank you, @patrickkusebauch! I decided to try to reproduce the problem from scratch at https://github.com/TravisCarden/qossmic-deptrac-1220, but I failed to do so. You can see in that repo that exactly the expected results are produced, so something else is apparently going on with the implementation in my actual project (https://github.com/php-tuf/composer-stager). I'll leave this issue open while I do a little more investigation. Update: I found one problem. It appears that my "Tests" layer regex was matching test directories in the |
@patrickkusebauch how does Deptrac deal with Related issue: Found also the documentation that explains the current behaviour: https://qossmic.github.io/deptrac/concepts/#internal-and-deptrac-internal-annotation |
#867 mentions the current solution for |
That's a good catch. That could be it. Can you try to run the dev-main branch of deptrac? It has better output for those sort of situations. |
That's also what deptrac says on the main branch -> Your - name: Domain
collectors:
- type: directory
value: ./src/Domain/* |
Okay, great. I updated to
Is there a way to disable that behavior ( |
There is, but it is IMHO stupid. We should do better. What you can do is to have a custom rule like this: use Qossmic\Deptrac\Core\Ast\AstMap\ClassLike\ClassLikeReference;
use Qossmic\Deptrac\Contract\Analyser\ProcessEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DependsOnInternalTokenSkipper implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
ProcessEvent::class => ['invoke', -1],
];
}
public function invoke(ProcessEvent $event): void
{
foreach ($event->dependentLayers as $dependentLayer => $_) {
if ($event->dependerLayer !== $dependentLayer
&& $event->dependentReference instanceof ClassLikeReference
&& $event->dependentReference->isInternal
) {
$event->stopPropagation();
}
}
} And then register it in your config file according to the documentation. By the subscriber precedence, it will run right before the rule that causes the violations and will prevent it from running. |
Thanks! That takes care of my I don't think I could do that with Also, I still get some other errors I don't fully understand:
And of course, I don't want to actually scan the @patrickkusebauch Re: "There is, but it is IMHO stupid. We should do better.", I've created a feature request to be "helpful". 😉 #1223 |
@TravisCarden do you have this current config somewhere for me to checkout? Than I could have look. |
Looks to me like your vendor layer includes some stubs for core PHP classes. Common culprits are static analysis tools, IDE support tools and poly fills for future PHP versions. |
I ended up solving this for myself with a custom PHPCS sniff that just looks for |
Hi! I'm having trouble getting my layer configuration to work. I'm trying to do two things: 1) keep my production code from accidentally using test classes, and 2) keep my domain layer from depending on vendor code.
And I'm having two problems: 1) Deptrac incorrectly counts it as a violation when a test class depends on a production class, and 2) it prints something like 600,000 violations in the vendor directory.
Here are the relevant portions of my directory structure:
Here's my configuration file:
deptrac.yaml
I've inserted three violations into
src/Domain/Example.php
: one dependency on each of the Infrastructure layer, the tests directory, and a vendor library. It correctly finds these violations, but it reports a ton of violations intests
andvendor
that I don't want it to.Here's a representative sample from the command output:
Output
I feel like I must misunderstand something fundamental. Thank you so much for any help!
The text was updated successfully, but these errors were encountered: