* * *

Author Topic: Is there a special definition when using Menu Execute, Run File ?  (Read 1755 times)

BBasile

  • Sr. Member
  • ****
  • Posts: 413
Hello, I'd like to run some kind of unit tests when using the command "Run file" from the "Execute" menu.
The unit involved is part of a bigger project so this would be awesome if i could do something like:

Code: Pascal  [Select]
  1. unit someUnit;
  2. interface
  3. { ...some code...}
  4. implementation
  5. { ...some code...}
  6. initialization
  7.   {$IFDEF  <some definition set when using the "Run File" IDE command>}
  8.   { ...some code only executed when "Run File" is used...}
  9.   {$ENDIF}
  10. end.

BBasile

  • Sr. Member
  • ****
  • Posts: 413
Re: Is there a special definition when using Menu Execute, Run File ?
« Reply #1 on: November 23, 2017, 04:00:53 pm »
I see that actually even without defines the initialization is not  executed.

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Is there a special definition when using Menu Execute, Run File ?
« Reply #2 on: November 23, 2017, 04:22:33 pm »
Unit initialization is only executed from the exe or library. The code is executed before your main program entry and in the order that the units are included.

Are you sure you included this particular unit inside your project ?

The IFDEF's are used for conditional compilation, e.g. if the condition matched the code will be included else it is 'omitted' from compilation.

And with that, the question becomes: what are you trying to accomplish exactly ? Because invoking "run file" can do many different things on even so many setups, depending on the file you feed it and/or your setup.
« Last Edit: November 23, 2017, 04:38:38 pm by molly »

taazz

  • Hero Member
  • *****
  • Posts: 5337
Re: Is there a special definition when using Menu Execute, Run File ?
« Reply #3 on: November 23, 2017, 04:39:37 pm »
Hello, I'd like to run some kind of unit tests when using the command "Run file" from the "Execute" menu.
The unit involved is part of a bigger project so this would be awesome if i could do something like:
menu from which program? windows task manager? sysinternals process explorer? some other application? In sort no, there is no way to distinguish between starting from explorer or any other application unless you use internals to find out which application started you, something along the lines of the tree view in the process explorer.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

GetMem

  • Hero Member
  • *****
  • Posts: 3195
Re: Is there a special definition when using Menu Execute, Run File ?
« Reply #4 on: November 23, 2017, 05:13:21 pm »
What is the "Execute" menu? Are you referring to Run-->Run(F9). If yes the solution is quite simple:
Code: Pascal  [Select]
  1. //...
  2. initialization
  3. {$IFOPT D+}
  4.  ShowMessage('KABOOM');
  5. {$ENDIF}  
If not you can always detect the parent process. Quite trivial on windows through api, extremely difficult on linux via TProcess.
« Last Edit: November 23, 2017, 05:21:29 pm by GetMem »

BBasile

  • Sr. Member
  • ****
  • Posts: 413
Re: Is there a special definition when using Menu Execute, Run File ?
« Reply #5 on: November 23, 2017, 06:15:18 pm »
in the Run menu, Run File. For now i run the tests when launching the debug build, which is not ideal, but works:

https://github.com/BBasile/Coedit/blob/c73c5fab8fa5809e898b9920b62424678cfc890a/src/ce_semver.pas

How this "Run File" is supposed to work ? what it the entry point ?
« Last Edit: November 23, 2017, 06:18:41 pm by BBasile »

molly

  • Hero Member
  • *****
  • Posts: 2345
Re: Is there a special definition when using Menu Execute, Run File ?
« Reply #6 on: November 23, 2017, 06:46:14 pm »
what it the entry point ?
The main program entry point can be found inside the .lpr file and is the first begin statement that is located inside that file.

Code: Pascal  [Select]
  1. program test;
  2.  
  3. uses
  4.   someUnit;  // if this unit contains initialization section then that code is executed before first begin situated below.
  5.  
  6. begin  // first begin in project = Main program entry
  7.   WriteLn('Hello, here is the programs main entry point');
  8. end.
  9.  

How this "Run File" is supposed to work ?
According to the wiki, it compiles, links and executes the file that is currently open inside the editor. And since since you can't run a unit.... i was wondering what it is that you're trying to accomplish as currently things don't make any sense for me  :)

edit: in case you aim is to make that code in someunit to compile conditionally then you could opt for adding a define to your compiler settings and enable that when you need your project to include your version checking statements to be included. But, then you could also opt for not including the someunit at all (using a define or doing so manually)
« Last Edit: November 23, 2017, 07:02:52 pm by molly »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus