Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cifti/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,14 @@ if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS)
-input ${CMAKE_CURRENT_LIST_DIR}/testdata/deep_nesting.xml )
set_tests_properties( ${TEST_PREFIX}_deep_nesting
PROPERTIES PASS_REGULAR_EXPRESSION "BrainModel" )

# A -output path that cannot be opened must be reported and refused, not
# written to anyway.
add_test( NAME ${TEST_PREFIX}_write_failure
COMMAND ${CMAKE_COMMAND}
-DTOOL=$<TARGET_FILE:${NIFTI_PACKAGE_PREFIX}cifti_tool>
-DINPUT=${CMAKE_CURRENT_LIST_DIR}/testdata/cext_unterminated.nii
-DBADOUT=${CMAKE_CURRENT_BINARY_DIR}/no_such_dir/out.txt
-DGOODOUT=${CMAKE_CURRENT_BINARY_DIR}/write_failure_ok.txt
-P ${CMAKE_CURRENT_LIST_DIR}/test_write_failure.cmake )
endif()
2 changes: 2 additions & 0 deletions cifti/cifti_tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ int disp_cifti_extension(nifti_image * nim, opts_t * opts)
}

fp = open_write_stream(opts->fout);
if( !fp ) return 1; /* open_write_stream() has reported the failure */
if( !ext ) {
fprintf(fp, "** no CIFTI extension in %s\n",nim->fname?nim->fname:"NULL");
close_stream(fp);
Expand All @@ -242,6 +243,7 @@ int eval_cifti_extension(afni_xml_t * ax, opts_t * opts)
opts->fout ? opts->fout : "DEFAULT" );

fp = open_write_stream(opts->fout);
if( !fp ) return 1; /* open_write_stream() has reported the failure */
axml_set_wstream(fp);

if( opts->verb > 1 ) fprintf(stderr, "-- recursive eval from %s\n",
Expand Down
42 changes: 42 additions & 0 deletions cifti/test_write_failure.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Drives cifti_tool at an unopenable -output path. A plain add_test cannot
# express this: PASS_REGULAR_EXPRESSION ignores the exit status, so a crash
# that printed the diagnostic first would still pass.

if(NOT TOOL OR NOT INPUT OR NOT BADOUT OR NOT GOODOUT)
message(FATAL_ERROR "TOOL, INPUT, BADOUT and GOODOUT are all required")
endif()

function(run_tool expect_result out_var)
execute_process(COMMAND ${TOOL} ${ARGN}
RESULT_VARIABLE result
OUTPUT_VARIABLE out
ERROR_VARIABLE err)
set(${out_var} "${out}${err}" PARENT_SCOPE)
if(NOT result STREQUAL "${expect_result}")
message(FATAL_ERROR
"expected result ${expect_result}, got '${result}'\n${out}${err}")
endif()
endfunction()

run_tool(0 bad_disp -input ${INPUT} -disp_cext -output ${BADOUT})
if(NOT bad_disp MATCHES "failed to open")
message(FATAL_ERROR "missing the open-failure diagnostic:\n${bad_disp}")
endif()

run_tool(0 bad_eval -input ${INPUT} -eval_cext -eval_type show_summary
-output ${BADOUT})
if(NOT bad_eval MATCHES "failed to open")
message(FATAL_ERROR "missing the open-failure diagnostic:\n${bad_eval}")
endif()

# An openable -output must still be written, so that refusing every write
# would not pass.
file(REMOVE ${GOODOUT})
run_tool(0 good_disp -input ${INPUT} -disp_cext -output ${GOODOUT})
if(NOT EXISTS ${GOODOUT})
message(FATAL_ERROR "nothing written to ${GOODOUT}")
endif()
file(READ ${GOODOUT} written)
if(NOT written MATCHES "MapName")
message(FATAL_ERROR "extension not displayed:\n${written}")
endif()