Merge topic 'curl-tls-verify'
[kiteware-cmake.git] / Tests / CTestUpdateGIT.cmake.in
blob5e3448b86a261fcbbc1c0da408ba81bb65136d01
1 # This script drives creation of a git repository and checks
2 # that CTest can update from it.
4 #-----------------------------------------------------------------------------
5 # Test in a directory next to this script.
6 get_filename_component(TOP "${CMAKE_CURRENT_LIST_FILE}" PATH)
7 string(APPEND TOP "/@CTestUpdateGIT_DIR@")
8 set(UPDATE_EXTRA Updated{module})
10 # Include code common to all update tests.
11 include("@CMAKE_CURRENT_SOURCE_DIR@/CTestUpdateCommon.cmake")
13 #-----------------------------------------------------------------------------
14 # Report git tools in use.
15 message("Using GIT tools:")
16 set(GIT "@GIT_EXECUTABLE@")
17 message(" git = ${GIT}")
19 set(AUTHOR_CONFIG "[user]
20 \tname = Test Author
21 \temail = testauthor@cmake.org
24 #-----------------------------------------------------------------------------
25 # Initialize the testing directory.
26 message("Creating test directory...")
27 init_testing()
29 if(UNIX)
30   set(src "@CMAKE_CURRENT_SOURCE_DIR@")
31   configure_file(${src}/CTestUpdateGIT.sh.in ${TOP}/git.sh @ONLY)
32   set(GIT ${TOP}/git.sh)
33 endif()
35 # Adapt to the system default branch name.
36 execute_process(
37   COMMAND ${GIT} --git-dir= config --get init.defaultBranch
38   RESULT_VARIABLE defaultBranchFailed
39   OUTPUT_VARIABLE defaultBranch
40   ERROR_VARIABLE defaultBranchError
41   OUTPUT_STRIP_TRAILING_WHITESPACE
42   ERROR_STRIP_TRAILING_WHITESPACE
43   )
44 if(defaultBranch STREQUAL "")
45   set(defaultBranch master)
46 endif()
47 message("Detected default branch name '${defaultBranch}'")
49 #-----------------------------------------------------------------------------
50 # Create the repository.
51 message("Creating repository...")
52 file(MAKE_DIRECTORY ${TOP}/repo.git)
53 run_child(
54   WORKING_DIRECTORY ${TOP}/repo.git
55   COMMAND ${GIT} --bare init
56   )
57 file(REMOVE_RECURSE ${TOP}/repo.git/hooks)
59 # Create submodule repository.
60 message("Creating submodule...")
61 file(MAKE_DIRECTORY ${TOP}/module.git)
62 run_child(
63   WORKING_DIRECTORY ${TOP}/module.git
64   COMMAND ${GIT} --bare init
65   )
66 file(REMOVE_RECURSE ${TOP}/module.git/hooks)
67 run_child(WORKING_DIRECTORY ${TOP}
68   COMMAND ${GIT} clone module.git module
69   )
70 file(REMOVE_RECURSE ${TOP}/module/.git/hooks)
71 file(APPEND ${TOP}/module/.git/config "
72 ${AUTHOR_CONFIG}")
73 create_content(module)
74 run_child(WORKING_DIRECTORY ${TOP}/module
75   COMMAND ${GIT} add .
76   )
77 run_child(WORKING_DIRECTORY ${TOP}/module
78   COMMAND ${GIT} commit -m "Initial content"
79   )
80 run_child(WORKING_DIRECTORY ${TOP}/module
81   COMMAND ${GIT} push origin ${defaultBranch}:refs/heads/${defaultBranch}
82   )
84 #-----------------------------------------------------------------------------
85 # Import initial content into the repository.
86 message("Importing content...")
88 # Import the content into the repository.
89 run_child(WORKING_DIRECTORY ${TOP}
90   COMMAND ${GIT} clone repo.git import
91   )
92 file(REMOVE_RECURSE ${TOP}/import/.git/hooks)
93 file(APPEND ${TOP}/import/.git/config "
94 ${AUTHOR_CONFIG}")
95 create_content(import)
96 file(WRITE ${TOP}/import/HEAD "HEAD\n")
97 file(WRITE ${TOP}/import/${defaultBranch} "${defaultBranch}\n")
98 run_child(WORKING_DIRECTORY ${TOP}/import
99   COMMAND ${GIT} add .
100   )
101 run_child(WORKING_DIRECTORY ${TOP}/import
102   COMMAND ${GIT} config core.safecrlf false
103   )
104 run_child(WORKING_DIRECTORY ${TOP}/import
105   COMMAND ${GIT} submodule add ../module.git module
106   )
107 run_child(WORKING_DIRECTORY ${TOP}/import
108   COMMAND ${GIT} commit -m "Initial content"
109   )
110 run_child(WORKING_DIRECTORY ${TOP}/import
111   COMMAND ${GIT} push origin ${defaultBranch}:refs/heads/${defaultBranch}
112   )
114 #-----------------------------------------------------------------------------
115 # Modify the submodule.
116 change_content(module)
117 run_child(WORKING_DIRECTORY ${TOP}/module
118   COMMAND ${GIT} add -u
119   )
120 run_child(WORKING_DIRECTORY ${TOP}/module
121   COMMAND ${GIT} commit -m "Changed content"
122   )
123 run_child(WORKING_DIRECTORY ${TOP}/module
124   COMMAND ${GIT} push origin ${defaultBranch}:refs/heads/${defaultBranch}
125   )
127 #-----------------------------------------------------------------------------
128 # Create a working tree.
129 message("Checking out revision 1...")
130 run_child(
131   WORKING_DIRECTORY ${TOP}
132   COMMAND ${GIT} clone repo.git user-source
133   )
134 file(REMOVE_RECURSE ${TOP}/user-source/.git/hooks)
135 file(APPEND ${TOP}/user-source/.git/config "${AUTHOR_CONFIG}")
136 run_child(
137   WORKING_DIRECTORY ${TOP}/user-source
138   COMMAND ${GIT} submodule init
139   )
140 run_child(
141   WORKING_DIRECTORY ${TOP}/user-source
142   COMMAND ${GIT} submodule update
143   )
145 # Save the first revision name.
146 execute_process(
147   WORKING_DIRECTORY ${TOP}/user-source
148   COMMAND ${GIT} rev-parse HEAD
149   OUTPUT_VARIABLE revision1
150   OUTPUT_STRIP_TRAILING_WHITESPACE
151   )
153 #-----------------------------------------------------------------------------
154 # Create an empty commit.
155 message("Creating empty commit...")
156 run_child(
157   WORKING_DIRECTORY ${TOP}/user-source
158   COMMAND ${GIT} commit --allow-empty -m "Empty commit"
159   )
161 #-----------------------------------------------------------------------------
162 # Make changes in the working tree.
163 message("Changing content...")
164 update_content(user-source files_added files_removed dirs_added)
165 if(dirs_added)
166   run_child(
167     WORKING_DIRECTORY ${TOP}/user-source
168     COMMAND ${GIT} add -- ${dirs_added}
169     )
170 endif()
171 run_child(
172   WORKING_DIRECTORY ${TOP}/user-source
173   COMMAND ${GIT} add -- ${files_added}
174   )
175 run_child(
176   WORKING_DIRECTORY ${TOP}/user-source
177   COMMAND ${GIT} rm -- ${files_removed}
178   )
179 run_child(WORKING_DIRECTORY ${TOP}/user-source/module
180   COMMAND ${GIT} checkout ${defaultBranch} --
181   )
182 run_child(
183   WORKING_DIRECTORY ${TOP}/user-source
184   COMMAND ${GIT} add -u
185   )
187 #-----------------------------------------------------------------------------
188 # Commit the changes to the repository.
189 message("Committing revision 2...")
190 run_child(
191   WORKING_DIRECTORY ${TOP}/user-source
192   COMMAND ${GIT} commit -m "Changed content"
193   )
194 run_child(
195   WORKING_DIRECTORY ${TOP}/user-source
196   COMMAND ${GIT} push origin HEAD
197   )
199 #-----------------------------------------------------------------------------
200 # Make changes in the working tree.
201 message("Changing content again...")
202 change_content(user-source)
203 run_child(
204   WORKING_DIRECTORY ${TOP}/user-source
205   COMMAND ${GIT} add -u
206   )
208 #-----------------------------------------------------------------------------
209 # Commit the changes to the repository.
210 message("Committing revision 3...")
211 run_child(
212   WORKING_DIRECTORY ${TOP}/user-source
213   COMMAND ${GIT} commit -m "Changed content again"
214   )
215 run_child(
216   WORKING_DIRECTORY ${TOP}/user-source
217   COMMAND ${GIT} push origin HEAD
218   )
220 #-----------------------------------------------------------------------------
221 # Go back to before the changes so we can test updating.
222 macro(rewind_source src_dir)
223   message("Backing up to revision 1...")
224   run_child(
225     WORKING_DIRECTORY ${TOP}/${src_dir}
226     COMMAND ${GIT} reset --hard ${revision1}
227     )
228   run_child(
229     WORKING_DIRECTORY ${TOP}/${src_dir}
230     COMMAND ${GIT} submodule update
231     )
232 endmacro()
233 rewind_source(user-source)
235 # Make sure pull does not try to rebase (which does not work with
236 # modified files) even if ~/.gitconfig sets "branch.master.rebase".
237 run_child(
238   WORKING_DIRECTORY ${TOP}/user-source
239   COMMAND ${GIT} config branch.${defaultBranch}.rebase false
240   )
242 # Create a modified file.
243 modify_content(user-source)
245 #-----------------------------------------------------------------------------
246 # Test updating the user work directory with the command-line interface.
247 message("Running CTest Dashboard Command Line...")
249 # Create the user build tree.
250 create_build_tree(user-source user-binary)
251 file(APPEND ${TOP}/user-binary/CTestConfiguration.ini
252   "# GIT command configuration
253 UpdateCommand: ${GIT}
256 # Run the dashboard command line interface.
257 set(UPDATE_NO_MODIFIED 1)
258 run_dashboard_command_line(user-binary)
259 set(UPDATE_NO_MODIFIED 0)
261 rewind_source(user-source)
262 modify_content(user-source)
264 message("Running CTest Dashboard Command Line (custom update)...")
266 # Create the user build tree.
267 create_build_tree(user-source user-binary-custom)
268 file(APPEND ${TOP}/user-binary-custom/CTestConfiguration.ini
269   "# GIT command configuration
270 UpdateCommand: ${GIT}
271 GITUpdateCustom: ${GIT};pull;origin;${defaultBranch}
274 # Run the dashboard command line interface.
275 run_dashboard_command_line(user-binary-custom)
277 #-----------------------------------------------------------------------------
278 # Test initial checkout and update with a dashboard script.
279 message("Running CTest Dashboard Script...")
281 create_dashboard_script(dash-binary
282   "# git command configuration
283 set(CTEST_GIT_COMMAND \"${GIT}\")
284 set(CTEST_GIT_UPDATE_OPTIONS)
285 execute_process(
286   WORKING_DIRECTORY \"${TOP}\"
287   COMMAND \"${GIT}\" clone repo.git dash-source
288   )
290 # Test .git file.
291 file(RENAME \"${TOP}/dash-source/.git\" \"${TOP}/dash-source/repo.git\")
292 file(WRITE \"${TOP}/dash-source/.git\" \"gitdir: repo.git\n\")
294 execute_process(
295   WORKING_DIRECTORY \"${TOP}/dash-source\"
296   COMMAND \"${GIT}\" reset --hard ${revision1}
297   )
298 execute_process(
299   WORKING_DIRECTORY \"${TOP}/dash-source\"
300   COMMAND \"${GIT}\" submodule init
301   )
302 execute_process(
303   WORKING_DIRECTORY \"${TOP}/dash-source\"
304   COMMAND \"${GIT}\" submodule update
305   )
308 # Run the dashboard script with CTest.
309 run_dashboard_script(dash-binary)
311 rewind_source(dash-source)
313 #-----------------------------------------------------------------------------
314 # Test custom update with a dashboard script.
315 message("Running CTest Dashboard Script (custom update)...")
317 create_dashboard_script(dash-binary-custom
318   "# git command configuration
319 set(CTEST_GIT_COMMAND \"${GIT}\")
320 set(CTEST_GIT_UPDATE_OPTIONS)
321 set(CTEST_GIT_UPDATE_CUSTOM \${CTEST_GIT_COMMAND} pull origin ${defaultBranch})
324 # Run the dashboard script with CTest.
325 run_dashboard_script(dash-binary-custom)
328 rewind_source(dash-source)
330 #-----------------------------------------------------------------------------
331 # Test no update with a dashboard script.
332 message("Running CTest Dashboard Script (No update)...")
334 create_dashboard_script(dash-binary-no-update
335   "# git command configuration
336 set(CTEST_GIT_COMMAND \"${GIT}\")
337 set(CTEST_UPDATE_VERSION_ONLY TRUE)
340 # Run the dashboard script with CTest.
341 set(NO_UPDATE 1)
342 run_dashboard_script(dash-binary-no-update)
343 unset(NO_UPDATE)
345 rewind_source(dash-source)
347 #-----------------------------------------------------------------------------
348 # Test ctest_update(QUIET)
349 message("Running CTest Dashboard Script (update quietly)...")
351 set(ctest_update_args QUIET)
352 create_dashboard_script(dash-binary-quiet
353   "# git command configuration
354 set(CTEST_GIT_COMMAND \"${GIT}\")
355 set(CTEST_GIT_UPDATE_OPTIONS)
356 set(CTEST_GIT_UPDATE_CUSTOM \${CTEST_GIT_COMMAND} pull origin ${defaultBranch})
358 unset(ctest_update_args)
360 # Run the dashboard script with CTest.
361 run_dashboard_script(dash-binary-quiet)
363 # Make sure the output seems quiet.
364 if("${OUTPUT}" MATCHES "Updating the repository")
365   message(FATAL_ERROR "Found 'Updating the repository' in quiet output")
366 endif()
368 #-----------------------------------------------------------------------------
369 # Test ctest_update(RETURN_VALUE) on failure
370 message("Running CTest Dashboard Script (fail to update)...")
372 set(ctest_update_check [[
374 if(NOT ret LESS 0)
375   message(FATAL_ERROR "ctest_update incorrectly succeeded with ${ret}")
376 endif()
378 create_dashboard_script(dash-binary-fail
379   "set(CTEST_GIT_COMMAND \"update-command-does-not-exist\")
381 unset(ctest_update_check)
383 # Run the dashboard script with CTest.
384 set(FAIL_UPDATE 1)
385 run_dashboard_script(dash-binary-fail)
386 unset(FAIL_UPDATE)