Skip to content

Instantly share code, notes, and snippets.

@Roald87
Created April 18, 2020 09:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Roald87/c68ea920607ec8f32d977d32f3f82712 to your computer and use it in GitHub Desktop.
Save Roald87/c68ea920607ec8f32d977d32f3f82712 to your computer and use it in GitHub Desktop.
Batch script to make hardlinks from a TwinCAT plc project to a Unit test project.
@echo off
title Make hard links from project to test project
echo This script will make hard links of the project files in the POUs folder to the test project folder.
echo Before running this script make sure you already created the neccesarry folder structure in your Plc or TwinCAT project.
for %%i in (.\path\to\project\POUs\*.TcPOU) do (
mklink /H .\path\to\testproject\POUs\%%~nxi %%i
)
pause
@Roald87
Copy link
Author

Roald87 commented Apr 18, 2020

How to

Say you have a TwinCAT project called MyProject which you want to test with the TcUnit library. You create another project MyProject_Tests. So you have the following folder structure:

repos 
  ├── MyProject
  |  ├── MyProject
  |  |  ├── Plc
  |  |  |  ├── POUs
  |  |  |  └── Plc.plcproj
  |  |  └── MyProject.tsproj
  |  └── MyProject.sln
  |
  ├── MyProject_Tests
  |  ├── MyProject_Tests
  |  |  ├── Plc
  |  |  |  ├── POUs
  |  |  |  └── Plc.plcproj
  |  |  └── MyProject_Tests.tsproj
  |  └── MyProject_Tests.sln
  |
  └── hardlinkPousToTestProject.bat  

You then:

  1. Add a folder Tests under Plc in the MyProject_Tests project.
  2. Add a method you want to test to MyProject/MyProject/Plc/POUs.
  3. Edit the path names in hardlinkPousToTestProject.bat such that they point to the correct folders. So for the current example:
    • for %%i in (.\path\to\project\POUs\*.TcPOU) do ( becomes for %%i in (.\MyProject\MyProject\Plc\POUs\*.TcPOU) do (
    • and mklink /H .\path\to\testproject\POUs\%%~nxi %%i becomes mklink /H .\MyProject_Tests\MyProject_Tests\Plc\POUs\%%~nxi %%i
  4. Run the batch script.
  5. Manually add the .TcPOU files to MyProject_Tests\MyProject_Tests\Plc\POUs
  6. Add the MyProject_Tests\MyProject_Tests\Plc\POUs to .gitignore since this file is already in MyProject.

Your file structure should now look like this:

repos 
  ├── MyProject
  |  ├── MyProject
  |  |  ├── Plc
  |  |  |  ├── POUs
  |  |  |  |  └── Function.TcPOU  # Original file
  |  |  |  └── Plc.plcproj
  |  |  └── MyProject.tsproj
  |  └── MyProject.sln
  |
  ├── MyProject_Tests
  |  ├── MyProject_Tests
  |  |  ├── Plc
  |  |  |  ├── POUs
  |  |  |  |  └── Function.TcPOU   # Hard linked file
  |  |  |  ├── Tests
  |  |  |  |  └── Function_Tests.TcPOU  
  |  |  |  └── Plc.plcproj
  |  |  └── MyProject_Tests.tsproj
  |  └── MyProject_Tests.sln
  |
  └── hardlinkPousToTestProject.bat  

Now you can change Function.TcPOU from either MyProject or MyProject_Tests!

@kumaraswamygaviyappa
Copy link

Hello,
Thank you for this batch file.

Is this line needs correction?
Manually add the .TcPOU files to the _MyProject\_Tests_ project

Like this?
Manually add the .TcPOU files to the _MyProject_Tests\Plc\POUs

@Roald87
Copy link
Author

Roald87 commented Jun 30, 2021

Thanks @kumaraswamygaviyappa! Nice catch.

@kumaraswamygaviyappa
Copy link

kumaraswamygaviyappa commented Jun 30, 2021

If the batch file is executed for the second time, Error is generated: A file cannot be created if it already exists.

@Roald87
Copy link
Author

Roald87 commented Jul 1, 2021

Yes that is correct. You only have to link a file once.

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