Skip to content
Open
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
7 changes: 2 additions & 5 deletions lean/commands/library/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@ def _add_csharp_package_to_csproj(csproj_file: Path, name: str, version: str) ->
if existing_package_reference is not None:
existing_package_reference.set("Version", version)
else:
last_item_group = csproj_tree.find(".//ItemGroup[last()]")
if last_item_group is None:
last_item_group = etree.SubElement(csproj_tree.find(".//Project"), "ItemGroup")

last_item_group.append(etree.fromstring(f'<PackageReference Include="{name}" Version="{version}" />'))
item_group = xml_manager.unconditional_item_group(csproj_tree)
item_group.append(etree.fromstring(f'<PackageReference Include="{name}" Version="{version}" />'))

csproj_file.write_text(xml_manager.to_string(csproj_tree), encoding="utf-8")

Expand Down
7 changes: 2 additions & 5 deletions lean/components/util/library_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,8 @@ def _add_csharp_project_to_csproj(self, csproj_file: Path, library_csproj_path:

existing_project_reference = csproj_tree.find(f".//ProjectReference[@Include='{library_csproj_path}']")
if existing_project_reference is None:
last_item_group = csproj_tree.find(".//ItemGroup[last()]")
if last_item_group is None:
last_item_group = etree.SubElement(csproj_tree.find(".//Project"), "ItemGroup")

last_item_group.append(etree.fromstring(f'<ProjectReference Include="{library_csproj_path}" />'))
item_group = self._xml_manager.unconditional_item_group(csproj_tree)
item_group.append(etree.fromstring(f'<ProjectReference Include="{library_csproj_path}" />'))

csproj_file.write_text(self._xml_manager.to_string(csproj_tree), encoding="utf-8")

Expand Down
19 changes: 19 additions & 0 deletions lean/components/util/xml_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,22 @@ def to_string(self, root: etree.Element) -> str:
"""
etree.indent(root, " " * 4)
return etree.tostring(root, encoding="utf-8", method="xml", pretty_print=True).decode("utf-8")

def unconditional_item_group(self, root: etree.Element) -> etree.Element:
"""Returns a project ItemGroup with no Condition, creating one when none exists.

Direct children only: an ItemGroup inside a Target is not a project reference group.
A Condition on the group would limit a new reference to that configuration.

:param root: the parsed csproj element
:return: an ItemGroup that is included in every build configuration
"""
project = root if str(root.tag).rsplit("}", 1)[-1] == "Project" else root.find(".//Project")
if project is None:
project = root

for item_group in reversed(project.findall("ItemGroup")):
if item_group.get("Condition") is None:
return item_group

return etree.SubElement(project, "ItemGroup")
92 changes: 92 additions & 0 deletions tests/components/util/test_library_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,95 @@ def test_detects_circular_dependencies() -> None:
assert False, "Expected library_manager.add_lean_library_reference_to_project() to raise on circular dependency"
except RuntimeError:
pass


def _csproj_with_conditional_item_group(only_conditional: bool) -> Path:
groups = "" if only_conditional else """
<ItemGroup>
<PackageReference Include="QuantConnect.Lean" Version="2.5.*" />
</ItemGroup>"""
csproj_file = Path.cwd() / "App.csproj"
csproj_file.write_text(f"""<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>{groups}
<ItemGroup Condition="'$(Configuration)' == 'Debug'" />
</Project>
""", encoding="utf-8")
return csproj_file


def _item_groups_by_condition(csproj_file: Path):
csproj_tree = container.xml_manager.parse(csproj_file.read_text(encoding="utf-8"))
groups = csproj_tree.findall(".//ItemGroup")
unconditional = [group for group in groups if group.get("Condition") is None]
conditional = [group for group in groups if group.get("Condition") is not None]
return unconditional, conditional


def test_add_csharp_project_to_csproj_does_not_use_a_conditional_item_group() -> None:
create_fake_lean_cli_directory()

csproj_file = _csproj_with_conditional_item_group(False)
reference = "../Library/Lib/Lib.csproj"
_create_library_manager()._add_csharp_project_to_csproj(csproj_file, reference)

unconditional, conditional = _item_groups_by_condition(csproj_file)
assert len(unconditional) == 1
assert [element.get("Include") for element in unconditional[0].findall("ProjectReference")] == [reference]
assert unconditional[0].find("PackageReference").get("Include") == "QuantConnect.Lean"
assert conditional[0].find("ProjectReference") is None
assert conditional[0].get("Condition") == "'$(Configuration)' == 'Debug'"


def test_add_csharp_project_to_csproj_with_only_a_conditional_item_group_adds_an_unconditional_one() -> None:
create_fake_lean_cli_directory()

csproj_file = _csproj_with_conditional_item_group(True)
reference = "../Library/Lib/Lib.csproj"
_create_library_manager()._add_csharp_project_to_csproj(csproj_file, reference)

unconditional, conditional = _item_groups_by_condition(csproj_file)
assert len(unconditional) == 1
assert [element.get("Include") for element in unconditional[0].findall("ProjectReference")] == [reference]
assert conditional[0].find("ProjectReference") is None


def test_add_csharp_package_to_csproj_does_not_use_a_conditional_item_group() -> None:
from lean.commands.library.add import _add_csharp_package_to_csproj

create_fake_lean_cli_directory()

csproj_file = _csproj_with_conditional_item_group(False)
_add_csharp_package_to_csproj(csproj_file, "Example.Package", "1.2.3")

unconditional, conditional = _item_groups_by_condition(csproj_file)
assert len(unconditional) == 1
assert any(element.get("Include") == "Example.Package" and element.get("Version") == "1.2.3"
for element in unconditional[0].findall("PackageReference"))
assert conditional[0].find("PackageReference") is None


def test_add_csharp_project_to_csproj_does_not_use_an_item_group_inside_a_target() -> None:
create_fake_lean_cli_directory()

csproj_file = Path.cwd() / "App.csproj"
csproj_file.write_text("""<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="QuantConnect.Lean" Version="2.5.*" />
</ItemGroup>
<Target Name="CopyExtra">
<ItemGroup>
<Content Include="extra.txt" />
</ItemGroup>
</Target>
</Project>
""", encoding="utf-8")
reference = "../Library/Lib/Lib.csproj"
_create_library_manager()._add_csharp_project_to_csproj(csproj_file, reference)

csproj_tree = container.xml_manager.parse(csproj_file.read_text(encoding="utf-8"))
project_group = csproj_tree.find("ItemGroup")
target_group = csproj_tree.find("Target/ItemGroup")
assert [element.get("Include") for element in project_group.findall("ProjectReference")] == [reference]
assert target_group.find("ProjectReference") is None
Loading