I'm writing a small integration testing framework to a suite of application written in Delphi 10.2 (win32 target).
My idea was to detect a specific command-lien parameter and, if it is present, run the test, printing the result to STDOUT and the console and then exiting with either 0 (no error) or 1 (error) so that the build script can pickup the result.
That worked well except for the last part: I'm unable to set the process exit code properly and read it back from the calling script. I'm always getting the value -1073741515 back instead.
Since the program is, in theory, a GUI application, I wrote some code that would allow it to attach to the calling console (or create one if not present) and maybe my problem comes from that.
Here is my code:
UIAbstraction.writeln('Integration testing of '+ModuleName, dmEmphasis);
if Errors.Count > 0 then
begin
UIAbstraction.writeln('Erros occured during integration tests:');
Errors.ForEach(procedure (const Aline: string)
begin
UIAbstraction.writeln(ALine, dmError);
end
);
UIAbstraction.Detach;
ExitProcess(cardinal(1));
end
else
begin
UIAbstraction.writeln('Integration tests sucessfull', dmSuccess);
UIAbstraction.Detach;
ExitProcess(cardinal(0));
end;
UIAbstraction is a small class that, well, abstract the UI (the goal is to be easily able to inject a different implementation later that could integrate with a different integration test system). It mostly has 2 meaningful methods: Attach and detach:
procedure TConsoleConnector.Attach;
begin
IsAttached:=AttachConsole(-1);
if not IsAttached then
IsAttached := GetlastError = 5; // 5 = ACCESS_DENIED = already attached
if not IsAttached
then
begin
IsAttached:=AllocConsole;
if not IsAttached then
RaiseLastOSError;
end;
end;
procedure TConsoleConnector.Detach;
begin
if IsAttached then
FreeConsole;
IsAttached:=false;
end;
Unfortunately, when I run this code from the command line and follows it with echo %errorlevel%
I always get -1073741515
instead of the expected 0 or 1.