Skip to content

Commit

Permalink
BUG: Fix itkXMLFileOutputWindowTest regression
Browse files Browse the repository at this point in the history
Fix `itkXMLFileOutputWindowTest` regression: delete the contents of the
target file prior to writing new lines when no filename is given.

When no filename is provided, the `itk::XMLFileOutputWindow` class uses
a hard-coded filename to flush the buffer contents. If the CI builds do
not clean the target folder, as the append flag is set to `On`, lines
will be appended to the existing file, and the check on the written
lines will fail.

Removing the contents of the file is preferred over computing the
difference of the existing line count with respect to the new line
count, since the file will otherwise grow without bounds.

Fixes:
```
Error in numLinesRead == numLinesExpected
  In ITK/Modules/Core/Common/test/itkXMLFileOutputWindowTest.cxx, line 86
  lh: 47
  rh: 7
Expression is not equal
```

raised for example in:
https://open.cdash.org/test/669234154
  • Loading branch information
jhlegarreta authored and dzenanz committed Apr 16, 2022
1 parent 273d4ef commit 4ff05c0
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Modules/Core/Common/test/itkXMLFileOutputWindowTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
#include "itkXMLFileOutputWindow.h"
#include "itkTestingMacros.h"
#include <fstream>
#include <cstdio>
#ifdef _WIN32
# include <direct.h>
# define cwd _getcwd
# define OS_SEP "\\"
#else
# include "unistd.h"
# define cwd getcwd
# define OS_SEP "/"
#endif


int
Expand All @@ -46,6 +56,33 @@ itkXMLFileOutputWindowTest(int argc, char * argv[])

logger->SetInstance(logger);

// If not input filename is provided, remove the contents in the existing file to avoid counting existing lines when
// contents are appended
if (argc == 1)
{
// In order to initialize the filename, some text needs to be written first
const char * regularText = "text";
logger->DisplayText(regularText);

// Get the filename
const char * fileBaseName = logger->GetFileName();
const std::size_t size = 4096;
char tmp[size];
char * status = cwd(tmp, size);
if (!status)
{
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error getting the current directory. Cannot delete the contents of the target file: "
<< fileBaseName << std::endl;
return EXIT_FAILURE;
}
std::string fileName = tmp + std::string(OS_SEP) + std::string(fileBaseName);

// Delete the contents
std::ofstream ofs;
ofs.open(fileName, std::ofstream::out | std::ofstream::trunc);
ofs.close();
}

// Check special cases
const char * text = nullptr;
Expand Down

0 comments on commit 4ff05c0

Please sign in to comment.