Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
If you move the example ./composer.json
out of the webroot, Unit Tests fail:
Drupal\Tests\ComposerIntegrationTest::testComposerJson
file_get_contents(docroot/composer.json): failed to open stream: No such file or directorydocroot/core/tests/Drupal/Tests/ComposerIntegrationTest.php:62
Proposed resolution
Since ./composer.json
is an example file (like example.gitignore
), then it should be excluded from testing since it has no function to Drupal what-so-ever.
Remaining tasks
- Wirte Patch
User interface changes
None
API changes
None
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#43 | 2545344-nr-bot.txt | 132 bytes | needs-review-queue-bot |
#32 | interdiff-2545344-30-31.txt | 580 bytes | kyvour |
#32 | 2545344-31.patch | 8.29 KB | kyvour |
#17 | 2545344_17-docs-only.patch | 905 bytes | Mile23 |
#17 | 2545344_17-code-changes.patch | 3.31 KB | Mile23 |
Comments
Comment #1
timmillwoodHere's an initial patch, it just does a simple file_exists, then fails if the file is missing, unless it's ./composer.json.
I was thinking we should return some sort of message if ./composer.json is missing, but not fail the test, not sure how useful that would be though.
Comment #2
martin107 CreditAttribution: martin107 commentedI am not sure how to do this ... maybe someone else could comment I would be really gratefulIf the thrust of this issue is expanded to :-"Make sure the file exists AND it has no errors in it" then we would solve the testing required for this issue #2545430: Remove "UnexpectedValueException" from composer.jsoncurrently the only way I know to add this is through the"drush composer diagnose" commandComment #3
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedComment #4
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedI think we should not introduce the file_exists for all composer.json because this fails silent if we remove one of files accidentally. I think it makes more sense to add a special case for root composer.json because this files may does not exists in a project managed with composer.
Comment #5
dawehnerIt would be great to document this file_exists() call. The reasons given by webflo make total sense.
Comment #6
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedComment #7
tstoecklerAwesome, thanks!
Comment #8
alexpottI'm not entirely convinced by this change since this test is for testing core so that file should be there - there are many tests that would fail if tested not on a clean checkout of drupal core. Also there is an issue suggesting making this the main composer json.
Comment #9
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedThanks correct. Lets focus our efforts on #2380389: Use a single vendor directory in the root. We have to get it in before RC1.
Comment #10
davidwbarratt CreditAttribution: davidwbarratt at Golf Channel commentedThis is still a problem if you choose to move your composer.json file out of the webroot. :(
Comment #11
timmillwoodAgree with @davidwbarratt.
Queued the patch in #6 for retesting. Think it's still valid.
Comment #12
davidwbarratt CreditAttribution: davidwbarratt at Golf Channel commentedWorks for me!
Comment #13
alexpottI think this is wrong thinking - tests are designed to run against the checkout - if you move things around they break - that is how it is supposed to be. There is no response to my point in #8.
Comment #14
davidwbarratt CreditAttribution: davidwbarratt at Golf Channel commented#13,
I think the test is faulty to begin with. It's like testing to see if .htaccess or web.config is there, when technically it's not needed and depending on your configuration (i.e. if you have Drupal in a subfolder, etc.) it might mess things up. If
./composer.json
is intended to be modified, I don't think we should have a test ensuring it's in a proper format, or even if it's there at all.Currently this is the only test that fails in a live environment, so the choice is either to fix it, or skip the entire file.
Comment #15
Mile23The root
composer.json
file isn't an example file.If you need to run all the unit tests against an installation where you've moved the
composer.json
file around, then you can use./vendor/bin/phpunit -c core --exclude-group Composer
We might also make a feature request forrun-tests.sh
to be able to exclude groups.Alternately, if there were some way to determine whether the test were being run to test core or test something else, then it might be fair to mark the test as skipped under that circumstance. But that seems a long way to go for an edge case. The
file_exists()
call negates part of the purpose of the test otherwise.Comment #16
bradjones1Agreed with the point in #14... there's nothing to say Drupal couldn't be included inside another project, with a top-level composer.json.
Regardless, though, I've come to include a composer.json file in the webroot with a simple contents of
{}
to appease this test, but also composer_manager, which last time I checked also expected to find this file as valid JSON. Just in case you're looking for a quick, non-patch fix.Comment #17
Mile23I thought: Hey, let's add the collected wisdom of this issue to the documentation of
ComposerIntegrationTest
.Then I saw
getPaths()
and thought: That should be a data provider.Lo the yak was shaved, and I ended up adding a
getRoot()
static method toUnitTestCase
.Presented here: The full code-changes patch, and also the docs-only patch.
Comment #18
dawehnerEven is the provider is executed before setup() we still don't need a static method, IMHO
Comment #19
Mile23Well, then it won't be useful for
setupBeforeClass()
either.But we can just do the docs change in patch
2545344_17-docs-only.patch
if that's intractable or out of scope.Comment #21
bradjones1Since this was last working, Composer use has basically become required for installing Drupal core from source, and there's also a new test method to compute the hash of the lock file. See http://cgit.drupalcode.org/drupal/commit/core/tests/Drupal/Tests/Compose...
So, including an empty composer.json file is no longer enough to stave off this failure.
I think it would be worthwhile at this point to see if a core maintainer thinks it's worth supporting non-out-of-the-box installation methods that cause this to be an issue, or to simply document this and require the dev to exclude this test suite?
Comment #22
dawehnerHere is a general question. Why does one run the core tests in their custom projects? It is not the job of each individual site to prove that core works. Maybe though I misunderstand the question. Beside of that the patch here certainly improves the quality of the patch though.
Comment #23
Mile23@dawehner: If you run tests using the core tools, the core tests will be discovered and possibly run.
The solution to this problem is to mark the test as incomplete if the file is not present, probably in setUp().
Comment #24
dawehnerI still think its not the responsibility for you to run those tests and its not the responsibility for Drupal to support arbitrary paths of stuff in its test.
Its also not that our tests are smoke tests for your environment.
Just an idea: We could maybe separate unit from unit-core in our phpunit.xml file and so exclude all those tests easily.
Comment #25
webflo CreditAttribution: webflo at UEBERBIT GmbH commentedI run all core unit tests on https://github.com/drupal-composer/drupal-project. We caught a few bugs around autoloading that way, because the project changes the location of the vendor folder.
Comment #26
Mile23Looks like drupal-project does this in travis-ci:
@group Composer
only applies to this test.So that's either a workaround for a problem in core or it's a solution to the policy in core. Which do we think it is?
Comment #30
kyvour CreditAttribution: kyvour as a volunteer commentedWhy do not use drupal-finder package?
It's already used in the drush and drupal console for the searching of the composer and drupal root, so seems like it's a stable package which works fine. I don't see any reasons why we can't use it here.
There is a patch for ComposerIntegrationTest. The idea was taken from #17 but code was rewritten to use DrupalFinder class.
Comment #31
kyvour CreditAttribution: kyvour as a volunteer commentedComment #32
kyvour CreditAttribution: kyvour as a volunteer commentedJust realized that dirname() accept second parameter only in php7
Patch is updated.
Comment #34
borisson_I agree with this, it's not for every site to run all tests.
I'm pretty sure that this project is not just another project, excluding this test-group is a good idea for that.
The docs-only patch in #17 is a good idea, otherwise I think we should just close this issue.
Comment #35
bradjones1I'd +1 a docs-only change... if you are sophisticated enough to get WTF'd by this you will be able to do some investigating and the proposed docs patch is clear enough to give you some sensible options, while not changing anything functionally on core's end.
Comment #43
needs-review-queue-bot CreditAttribution: needs-review-queue-bot as a volunteer commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #45
quietone CreditAttribution: quietone at PreviousNext commented