Skip to content
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
12 changes: 12 additions & 0 deletions src/tctools/patch_plc/patch_plc_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,18 @@ def get_project_sources(self, tree) -> FileItems:
# In the XML there are always backslashes, force to native type
ref_set.add(path)

if self._element_files is None: # Group doesn't yet exist, create it:
# It must be the first of the <ItemGroup> set
element_neighbour: Element = tree.find(
"PropertyGroup", namespaces={"": "*"}
)
self._element_files = etree.XML("<ItemGroup></ItemGroup>")
element_neighbour.addnext(self._element_files)

if self._element_folders is None:
self._element_folders = etree.XML("<ItemGroup></ItemGroup>")
self._element_files.addnext(self._element_folders)

return sources

def sources_to_remove(
Expand Down
56 changes: 56 additions & 0 deletions tests/plc_code/TwinCAT Project1/MyPlc/MyPlc_empty.plcproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<FileVersion>1.0.0.0</FileVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{3863e7e1-422f-4d5c-92f7-9f7ce21f2ff0}</ProjectGuid>
<SubObjectsSortedByName>True</SubObjectsSortedByName>
<DownloadApplicationInfo>true</DownloadApplicationInfo>
<WriteProductVersion>true</WriteProductVersion>
<GenerateTpy>false</GenerateTpy>
<Name>MyPlc</Name>
<ProgramVersion>3.1.4023.0</ProgramVersion>
<Application>{9b80525c-51bc-47f5-868f-9f61efadf857}</Application>
<TypeSystem>{8c1ac7e2-784c-4117-8c69-6df5131a9b7a}</TypeSystem>
<Implicit_Task_Info>{4cbef591-f0aa-4f71-8e8c-8748ed72f381}</Implicit_Task_Info>
<Implicit_KindOfTask>{bc222fda-9a00-499c-8fe1-56590e05c741}</Implicit_KindOfTask>
<Implicit_Jitter_Distribution>{85a038e2-e5b4-4af4-916e-a04812cc85b0}</Implicit_Jitter_Distribution>
<LibraryReferences>{387c9843-650a-4728-bc5d-67105fea5b12}</LibraryReferences>
</PropertyGroup>
<ItemGroup>
<PlaceholderReference Include="Tc2_Standard">
<DefaultResolution>Tc2_Standard, * (Beckhoff Automation GmbH)</DefaultResolution>
<Namespace>Tc2_Standard</Namespace>
</PlaceholderReference>
<PlaceholderReference Include="Tc2_System">
<DefaultResolution>Tc2_System, * (Beckhoff Automation GmbH)</DefaultResolution>
<Namespace>Tc2_System</Namespace>
</PlaceholderReference>
<PlaceholderReference Include="Tc3_Module">
<DefaultResolution>Tc3_Module, * (Beckhoff Automation GmbH)</DefaultResolution>
<Namespace>Tc3_Module</Namespace>
</PlaceholderReference>
</ItemGroup>
<ItemGroup>
<None Include="MyPlc.tmc">
<SubType>Content</SubType>
</None>
</ItemGroup>
<ProjectExtensions>
<PlcProjectOptions>
<XmlArchive>
<Data>
<o xml:space="preserve" t="OptionKey">
<v n="Name">"&lt;ProjectRoot&gt;"</v>
<d n="SubKeys" t="Hashtable" />
<d n="Values" t="Hashtable" />
</o>
</Data>
<TypeList>
<Type n="Hashtable">System.Collections.Hashtable</Type>
<Type n="OptionKey">{54dd0eac-a6d8-46f2-8c27-2f43c7e49861}</Type>
<Type n="String">System.String</Type>
</TypeList>
</XmlArchive>
</PlcProjectOptions>
</ProjectExtensions>
</Project>
11 changes: 11 additions & 0 deletions tests/test_patch_plc.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,14 @@ def test_reset_duplicate(plc_dir, project, caplog):
assert "Refusing to add" in msg and "MAIN.TcPOU" in msg

assert project.read_text() == content_before # The project should not be changed


def test_reset_empty_project(plc_dir, caplog):
project = plc_dir / "MyPlc_empty.plcproj" # Project with 0 sources

code = PatchPlc(str(project), "reset", str(plc_dir), "-r").run()
assert code == 0

content_after = project.read_text()
assert 'Include="POUs\\untracked_source\\F_UntrackedFunc.TcPOU"' in content_after
assert 'Include="POUs\\untracked_source"' in content_after
Loading