Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 'uses unit' disappear when I had a new unit?

Tags:

delphi

dunit

I have a Unit test project for my Application using DUnit framework. This project have a unit surrounded by a $IFDEF to output test-results in a xml file instead of the gui or just command line. XML_OUTPUT define is enabled by switching the Build configuration.

program DelphiCodeToDoc_Tests;

uses
  TestFramework,
  TextTestRunner,
  Sysutils,
  Forms,
  GUITestRunner,
{$IFDEF XML_OUTPUT}
  XmlTestRunner2 in 'DUnit_addon\XmlTestRunner2.pas',
{$ENDIF}
  DCTDSetupTests in 'IntegrationTests\DCTDSetupTests.pas',
  ...

This works perfectly. The issue starts when I'm adding a new unit to this project from the IDE (a new unit with 'File>New>Unit').

The Test project is now :

uses
  TestFramework,
  TextTestRunner,
  Sysutils,
  Forms,
  GUITestRunner,
  DCTDSetupTests in 'IntegrationTests\DCTDSetupTests.pas',
  ...
  MyNewUnit in 'IntegrationTests\MyNewUnit.pas';

As you see, the test XML_OUTPUT has disappeared ... Each time I'm adding a unit, Delphi IDE deletes this test.

Do you know why and how I can avoid it ?

like image 949
TridenT Avatar asked Nov 29 '22 06:11

TridenT


2 Answers

You can add a proxy unit to the main program to bypass this problematic behaviour (which many of us consider a bug, not a feature).

program DelphiCodeToDoc_Tests;

uses
  ...
  XMLTestRunnerProxy,  
  ...

and

unit XMLTestRunnerProxy;

interface

{$IFDEF XML_OUTPUT}
uses
  XmlTestRunner2 in 'DUnit_addon\XmlTestRunner2.pas';
{$ENDIF}

implementation

end.
like image 123
gabr Avatar answered Dec 09 '22 14:12

gabr


Only code that is actually used is compiled into your application anyway, so normally, it doesn't hurt to have units in the Uses clause that aren't used.

You can see all the code that is linked into your application when you run the program within your IDE. You should see blue dots next to all of the compiled code.

The one caveat is that you should check the initialization section of the units that are of concern. Any code that is in the initialization section is automatically included simply by including the unit because any code in that section is run as soon as the application starts up. You could add your compiler directive inside the initialization section of the unit, if needed, to avoid any initialization code from being linked in and ran.

like image 39
Marcus Adams Avatar answered Dec 09 '22 13:12

Marcus Adams