1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-06 20:17:15 +00:00

[PR #11689/a4bba992 backport][stable-12] composer - make create-project idempotent, add force parameter (#11700)

composer - make `create-project` idempotent, add `force` parameter (#11689)

* composer - make create-project idempotent, add force parameter

Adds a check for an existing composer.json in working_dir before running
create-project, so the task is skipped rather than failing on second run.
A new force parameter allows bypassing this check when needed.

Fixes #725.



* changelog fragment: rename to PR number, add PR URL



---------


(cherry picked from commit a4bba99203)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
patchback[bot] 2026-03-27 21:34:35 +01:00 committed by GitHub
parent 1a1056099c
commit de180d01e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 97 additions and 7 deletions

View file

@ -9,6 +9,17 @@ from __future__ import annotations
from ansible_collections.community.general.plugins.modules import composer
from .uthelper import RunCommandMock, UTHelper
from .uthelper import RunCommandMock, TestCaseMock, UTHelper
UTHelper.from_module(composer, __name__, mocks=[RunCommandMock])
class OsPathExistsMock(TestCaseMock):
name = "os_path_exists"
def setup(self, mocker):
mocker.patch("os.path.exists", return_value=self.mock_specs.get("return_value", False))
def check(self, test_case, results):
pass
UTHelper.from_module(composer, __name__, mocks=[RunCommandMock, OsPathExistsMock])

View file

@ -3,6 +3,25 @@
# SPDX-License-Identifier: GPL-3.0-or-later
---
anchors:
help_install: &help_install
command: ["/testbin/php", "/testbin/composer", "help", "install", "--working-dir", "/var/www/foo", "--no-interaction", "--format=json"]
rc: 0
out: |
{"definition": {"options": ["a", "b", "c"]}}
err: ''
help_create_project: &help_create_project
command: ["/testbin/php", "/testbin/composer", "help", "create-project", "--working-dir", "/var/www/foo", "--no-interaction", "--format=json"]
rc: 0
out: |
{"definition": {"options": ["a", "b", "c"]}}
err: ''
run_create_project: &run_create_project
command: ["/testbin/php", "/testbin/composer", "create-project", "--working-dir", "/var/www/foo", "vendor/package"]
rc: 0
out: ''
err: ''
test_cases:
- id: composer
input:
@ -12,12 +31,50 @@ test_cases:
changed: true
mocks:
run_command:
- command: ["/testbin/php", "/testbin/composer", "help", "install", "--working-dir", "/var/www/foo", "--no-interaction", "--format=json"]
rc: 0
out: |
{"definition": {"options": ["a", "b", "c"]}}
err: ''
- *help_install
- command: ["/testbin/php", "/testbin/composer", "install", "--working-dir", "/var/www/foo"]
rc: 0
out: ''
err: ''
- id: create_project_runs
input:
command: create-project
arguments: "vendor/package"
working_dir: "/var/www/foo"
output:
changed: true
mocks:
os_path_exists:
return_value: false
run_command:
- *help_create_project
- *run_create_project
- id: create_project_skips_when_exists
input:
command: create-project
arguments: "vendor/package"
working_dir: "/var/www/foo"
output:
changed: false
mocks:
os_path_exists:
return_value: true
run_command:
- *help_create_project
- id: create_project_force
input:
command: create-project
arguments: "vendor/package"
working_dir: "/var/www/foo"
force: true
output:
changed: true
mocks:
os_path_exists:
return_value: true
run_command:
- *help_create_project
- *run_create_project