Skip to content

Keep a custom project name when withNoRecreate is used - #1610

Open
kenny-ish wants to merge 1 commit into
testcontainers:mainfrom
kenny-ish:fix-compose-no-recreate-project-name
Open

kenny-ish wants to merge 1 commit into
testcontainers:mainfrom
kenny-ish:fix-compose-no-recreate-project-name

Conversation

@kenny-ish

Copy link
Copy Markdown

Summary

DockerComposeEnvironment.withNoRecreate() set the project name to the fixed value testcontainers-node as a side effect. Combined with withProjectName, the outcome depended on the call order:

new DockerComposeEnvironment(dir, file).withProjectName("mine").withNoRecreate(); // project: "testcontainers-node"
new DockerComposeEnvironment(dir, file).withNoRecreate().withProjectName("mine"); // project: "mine"

In the first form the custom name was silently dropped, and unrelated environments using withNoRecreate() shared one Compose project.

This change stops withNoRecreate() from touching the project name. The name is resolved once in up(): an explicit withProjectName always wins, otherwise the generated testcontainers-<uuid> name is used, or the fixed testcontainers-node name when recreation is disabled. The helpers that match containers to the project now read options.projectName, which is the resolved value. Since the name is derived from the builder state rather than stored during withNoRecreate(), a boolean form of the setter (#1530) will get the generated name back when it is turned off again.

The compose docs now mention the fixed project name used by withNoRecreate().

Verification

  • npx vitest run packages/testcontainers/src/docker-compose-environment/docker-compose-environment.test.ts -t "custom project name"
    • Red: the new test should keep a custom project name when no recreate option is set fails on main because the started container is named testcontainers-node-container-1 instead of custom-<uuid>-container-1.
    • Green: both project name tests pass with the change. The existing should not recreate the containers when no recreate option is set test still passes, so the fixed project name is still used when no name is set.
  • npm run lint:ci, npm run knip, npm run check-compiles and mkdocs build --strict pass.

Not breaking

Environments that only call withNoRecreate() keep using the testcontainers-node project, and withProjectName followed by nothing else behaves as before. Only the combination of withProjectName and withNoRecreate changes, and it now honours the explicit name in either order.

Closes #1524

withNoRecreate() replaced the project name with the fixed value
"testcontainers-node" as a side effect, so a name set earlier with
withProjectName() was silently discarded and the result depended on the
order of the two calls.

Resolve the project name in up() instead: an explicit project name
always wins, otherwise the generated name is used, or the fixed name
when recreation is disabled.

Fixes testcontainers#1524

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep a custom project name when withNoRecreate() is used

1 participant