Skip to content

Commit

Permalink
ENH: Force test failure on bad file writing
Browse files Browse the repository at this point in the history
When attempting to write to an invalid file
location, ensure that an itk::ExceptionObject
is thrown.
  • Loading branch information
hjmjohnson committed Aug 8, 2022
1 parent 8c098dd commit 554e45b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Modules/IO/ImageBase/include/itkIOTestHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ class IOTestHelper
std::cerr << "Exception Object caught: " << std::endl << err << std::endl;
throw;
}

// Test if writing to an invalid location causes exception to be thrown:
const std::string bad_root_path{ "/a_blatantly_obvious/bad_file_path/that/should/never/exist/on/the/computer/" };
const std::string bad_filename{ bad_root_path + filename };
try
{
writer->SetFileName(bad_filename);
writer->Update();
}
catch (const itk::ExceptionObject & err)
{
// This is the correct behavior
std::cout << "Correctly caught exception for attempting to write to an invalid file." << std::endl;
return;
}
catch (...)
{
itkGenericExceptionMacro(<< "IO library exception not converted to an itk::ExceptionObject.");
}
itkGenericExceptionMacro(<< "Invalid file writing path did not throw an exception: " << bad_filename << " with "
<< imageio->GetNameOfClass());
}

//
Expand Down

0 comments on commit 554e45b

Please sign in to comment.