summaryrefslogtreecommitdiff
path: root/Tests/RunCMake/Byproducts/RunCMakeTest.cmake
blob: a7584ee4bb14d6b7fe019cd4aaa6fbab4b33f1aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
include(RunCMake)

function(run_CleanByproducts case)
  set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CleanByproducts-${case}-build)
  set(RunCMake_TEST_OPTIONS "${ARGN}")

  run_cmake(CleanByproducts)
  set(RunCMake_TEST_NO_CLEAN 1)

  run_cmake_command(CleanByProducts-build ${CMAKE_COMMAND} --build .)
  include("${RunCMake_TEST_BINARY_DIR}/files.cmake")

  message("Checking that all expected files are present")
  check_files(EXPECTED_PRESENT "${RunCMake_TEST_BINARY_DIR}" TRUE)
  check_files(EXPECTED_DELETED "${RunCMake_TEST_BINARY_DIR}" TRUE)

  run_cmake_command(CleanByProducts-clean ${CMAKE_COMMAND} --build . --target clean)

  message("Checking that only the expected files are present after cleaning")
  check_files(EXPECTED_PRESENT "${RunCMake_TEST_BINARY_DIR}" TRUE)
  check_files(EXPECTED_DELETED "${RunCMake_TEST_BINARY_DIR}" FALSE)
endfunction()

function(check_files list path has_to_exist)
  foreach(file IN LISTS ${list})
    message("Checking ${file}")
    set(file_exists FALSE)
    if(EXISTS "${path}/${file}")
      set(file_exists TRUE)
    endif()

    if(file_exists AND NOT has_to_exist)
      message(FATAL_ERROR "${file} should have been deleted")
    elseif(NOT file_exists AND has_to_exist)
      message(FATAL_ERROR "${file} does not exist")
    elseif(file_exists AND has_to_exist)
      message("${file} found as expected")
    elseif(NOT file_exists AND NOT has_to_exist)
      message("${file} deleted as expected")
    endif()

  endforeach()
endfunction()


# Iterate through all possible test values
set(counter 0)
foreach(test_clean_no_custom TRUE FALSE)
  foreach(test_build_events TRUE FALSE)
    foreach(test_custom_command TRUE FALSE)
      foreach(test_custom_target TRUE FALSE)
        math(EXPR counter "${counter} + 1")
        message("Test ${counter} - CLEAN_NO_CUSTOM: ${test_clean_no_custom}, Build events: ${test_build_events}, Custom command: ${test_custom_command}, Custom target: ${test_custom_target}")
        run_CleanByproducts("buildevents${counter}" -DCLEAN_NO_CUSTOM=${test_clean_no_custom} -DTEST_BUILD_EVENTS=${test_build_events} -DTEST_CUSTOM_COMMAND=${test_custom_command} -DTEST_CUSTOM_TARGET=${test_custom_target})
      endforeach()
    endforeach()
  endforeach()
endforeach()