navid/haze / cb1bac46fbd9fca0f0e2a401590740b0840b47821983a25743fc29c1572577d9 / cmake/run_format_test.cmake

run_format_test.cmake

← cmake · 1459 bytes · raw · download

if(NOT DEFINED HZ OR NOT DEFINED SOURCE OR NOT DEFINED EXPECTED OR NOT DEFINED OUTPUT)
    message(FATAL_ERROR "HZ, SOURCE, EXPECTED, and OUTPUT are required")
endif()

execute_process(
    COMMAND "${HZ}" fmt "${SOURCE}"
    OUTPUT_FILE "${OUTPUT}"
    RESULT_VARIABLE result
    ERROR_VARIABLE error)
if(NOT result EQUAL 0)
    message(FATAL_ERROR "hz fmt failed: ${error}")
endif()

file(READ "${OUTPUT}" formatted)
file(READ "${EXPECTED}" expected)

if(NOT formatted STREQUAL expected)
    message(FATAL_ERROR "formatted output does not match expected output")
endif()

execute_process(
    COMMAND "${HZ}" fmt -check "${SOURCE}"
    RESULT_VARIABLE check_unformatted)
if(check_unformatted EQUAL 0)
    message(FATAL_ERROR "hz fmt -check accepted an unformatted file")
endif()

execute_process(
    COMMAND "${HZ}" fmt -check "${EXPECTED}"
    RESULT_VARIABLE check_formatted)
if(NOT check_formatted EQUAL 0)
    message(FATAL_ERROR "hz fmt -check rejected an already formatted file")
endif()

set(copy_path "${OUTPUT}.write.hz")
configure_file("${SOURCE}" "${copy_path}" COPYONLY)
execute_process(
    COMMAND "${HZ}" fmt -w "${copy_path}"
    RESULT_VARIABLE write_result
    ERROR_VARIABLE write_error)
if(NOT write_result EQUAL 0)
    message(FATAL_ERROR "hz fmt -w failed: ${write_error}")
endif()

file(READ "${copy_path}" rewritten)
if(NOT rewritten STREQUAL expected)
    message(FATAL_ERROR "hz fmt -w did not write the expected output")
endif()