Skip to content
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

Render profile name when using Jinja2 #13721

Merged
merged 4 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions conans/client/profile_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ def _load_profile(self, profile_name, cwd):

# All profiles will be now rendered with jinja2 as first pass
base_path = os.path.dirname(profile_path)
file_path = os.path.basename(profile_path)
memsharded marked this conversation as resolved.
Show resolved Hide resolved
context = {"platform": platform,
"os": os,
"profile_dir": base_path,
"profile_name": file_path,
"conan_version": conan_version}
rtemplate = Environment(loader=FileSystemLoader(base_path)).from_string(text)
text = rtemplate.render(context)
Expand Down
56 changes: 56 additions & 0 deletions conans/test/integration/configuration/test_profile_jinja.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import platform
import textwrap
import os

from conan import conan_version
from conans.test.assets.genconanfile import GenConanfile
Expand Down Expand Up @@ -105,3 +106,58 @@ def test_profile_version():
client.run("install . -pr=profile1.jinja")
assert f"*:myoption={conan_version}" in client.out
assert "*:myoption2=True" in client.out


def test_profile_template_profile_name():
"""
The property profile_name should be parsed as the profile file name when rendering profiles
"""
client = TestClient()
tpl1 = textwrap.dedent("""
[conf]
user.profile:name = {{ profile_name }}
""")
tpl2 = textwrap.dedent("""
include(default)
[conf]
user.profile:name = {{ profile_name }}
""")
default = textwrap.dedent("""
[settings]
os=Windows
[conf]
user.profile:name = {{ profile_name }}
""")

conanfile = textwrap.dedent("""
from conan import ConanFile
class Pkg(ConanFile):
def configure(self):
self.output.info("PROFILE NAME: {}".format(self.conf.get("user.profile:name")))
""")
client.save({"conanfile.py": conanfile,
"profile_folder/foobar": tpl1,
"another_folder/foo.profile": tpl1,
"include_folder/include_default": tpl2,
os.path.join(client.cache.profiles_path, "baz"): tpl1,
os.path.join(client.cache.profiles_path, "default"): default})

# show only file name as profile name
client.run("install . -pr=profile_folder/foobar")
assert "conanfile.py: PROFILE NAME: foobar" in client.out

# profile_name should include file extension
client.run("install . -pr=another_folder/foo.profile")
assert "conanfile.py: PROFILE NAME: foo.profile" in client.out

# default profile should compute profile_name by default too
client.run("install . -pr=default")
assert "conanfile.py: PROFILE NAME: default" in client.out

# profile names should show only their names
client.run("install . -pr=baz")
assert "conanfile.py: PROFILE NAME: baz" in client.out

# included profiles should respect the inherited profile name
client.run("install . -pr=include_folder/include_default")
assert "conanfile.py: PROFILE NAME: include_default" in client.out