]> git.lyx.org Git - lyx.git/blob - development/autotests/ExportTests.cmake
Cmake export tests: Macro to interpret error/warning messages at loading lyx files
[lyx.git] / development / autotests / ExportTests.cmake
1 #
2 #  Copyright (c) 2014 Kornel Benko <kornel@lyx.org>
3 #  Copyright (c) 2014 Scott Kostyshak <skotysh@lyx.org>
4 #
5 #  Redistribution and use in source and binary forms, with or without
6 #  modification, are permitted provided that the following conditions
7 #  are met:
8 #
9 #  1. Redistributions of source code must retain the copyright
10 #         notice, this list of conditions and the following disclaimer.
11 #  2. Redistributions in binary form must reproduce the copyright
12 #         notice, this list of conditions and the following disclaimer in the
13 #         documentation and/or other materials provided with the distribution.
14 #  3. The name of the author may not be used to endorse or promote products
15 #         derived from this software without specific prior written permission.
16 #
17 #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 #  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 #  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 #  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 #  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 #  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 #  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 #  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 #  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 #  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #
28
29 find_package(Perl)
30
31 if(PERL_FOUND)
32   set(DVI_FORMATS "dvi" "dvi3")
33   set(PDF_FORMATS "pdf" "pdf2" "pdf3" "pdf4" "pdf5")
34 else()
35   set(DVI_FORMATS "dvi")
36   set(PDF_FORMATS "pdf" "pdf2" "pdf3")
37 endif()
38
39 set(potential_languages "ca" "cs" "da" "de" "el" "es" "eu" "fa" "fr" "gl" "he" "hu" "id" "it" "ja" "ko" "nb" "nl" "pl" "pt" "ro" "ru" "sk" "sl" "sr" "sv" "uk" "zh_CN")
40
41 macro(initLangVars varname)
42   foreach(_l ${potential_languages})
43     set(${varname}_${_l})
44   endforeach()
45 endmacro()
46
47 macro(getoutputformats filepath varname)
48   file(STRINGS "${filepath}" lines)
49   # What should we test, if default_output_format is not defined?
50   # For now we test everything ...
51   set(out_formats "xhtml" ${DVI_FORMATS} ${PDF_FORMATS})
52   foreach(_l ${lines})
53     if(_l MATCHES "^\\\\default_output_format +\([^ ]+\)")
54       set(_format ${CMAKE_MATCH_1})
55       if(_format STREQUAL "default")
56         set(out_formats "xhtml" ${DVI_FORMATS} ${PDF_FORMATS})
57       elseif(_format STREQUAL "pdf2" AND "${filepath}" MATCHES "/doc/")
58         set(out_formats "xhtml" ${DVI_FORMATS} ${PDF_FORMATS})
59       elseif(_format MATCHES "pdf$")
60         set(out_formats "xhtml" ${PDF_FORMATS})
61       elseif(_format MATCHES "dvi$")
62         set(out_formats "xhtml" ${DVI_FORMATS})
63       elseif(_format MATCHES "xhtml")
64         set(out_formats "xhtml")
65       else()
66         # Respect all other output formats
67         # like "eps3"
68         set(out_formats "xhtml" ${_format})
69       endif()
70       break()
71     endif()
72   endforeach()
73   set(${varname} ${out_formats})
74 endmacro()
75
76 macro(findexpr found testname listname rsublabel)
77   set(_found 0)
78   set(tmpsublabel "")
79   foreach(_itrx ${${listname}})
80     if ("${_itrx}" MATCHES "^Sublabel:")
81       set(tmpsublabel "")
82       string(REGEX REPLACE "^Sublabel:[ \t]*" "" _itrlabels ${_itrx})
83       string(REGEX MATCHALL "[a-zA-Z]+" _labels ${_itrlabels})
84       foreach(subl ${_labels})
85         if (subl STREQUAL "RESET")
86           set(tmpsublabel "")
87         else()
88           list(APPEND tmpsublabel ${subl})
89         endif()
90       endforeach()
91       # remove doubles in sublabel
92       list(REMOVE_DUPLICATES tmpsublabel)
93     else()
94       set(_itr "^${_itrx}$")
95       if (${testname} MATCHES "${_itr}")
96         set(_found 1)
97         break()
98       endif()
99     endif()
100   endforeach()
101   if (${_found})
102     if (NOT "${tmpsublabel}" STREQUAL "")
103       list(APPEND ${rsublabel} ${tmpsublabel})
104     endif()
105   endif()
106   set(${found} ${_found})
107 endmacro()
108
109 function(join rvalues glue routput)
110   set(locallist ${${rvalues}})
111   list(REMOVE_ITEM locallist "export" "lyx2lyx" "load")
112   string(REGEX REPLACE "([^\\]|^);" "\\1${glue}" out "${locallist}")
113   set(${routput} ${out} PARENT_SCOPE)
114 endfunction()
115
116 macro(maketestname testname inverted listsuspicious listignored listunreliable listlabels)
117   string(REGEX MATCH "\\/[a-z][a-z](_[A-Z][A-Z])?\\/" _v ${${testname}})
118   if(_v)
119     string(REGEX REPLACE "\\/" "" _v ${_v})
120     set(listsuspiciousx ${listsuspicious}_${_v})
121     set(listignoredx ${listignored}_${_v})
122     set(listunreliablex ${listunreliable}_${_v})
123     set(listsuspendedx suspendedTests_${_v})
124   else()
125     set(listsuspiciousx ${listsuspicious})
126     set(listignoredx ${listignored})
127     set(listunreliablex ${listunreliable})
128     set(listsuspendedx suspendedTests)
129   endif()
130   set(sublabel "${${listlabels}}")
131   findexpr(mfound ${testname} ${listignoredx} sublabel)
132   if (NOT mfound)
133     set(sublabel2 "")
134     findexpr(foundunreliable ${testname} ${listunreliablex} sublabel2)
135     if (foundunreliable)
136       set(sublabel "unreliable" ${sublabel} ${sublabel2})
137       list(REMOVE_ITEM sublabel "export" "inverted" "templates" "mathmacros" "manuals" "autotests")
138     else()
139       string(REGEX MATCH "_(systemF|texF|pdf3|pdf2|pdf|dvi|lyx16|xhtml)$" _v ${${testname}})
140       # check if test _may_ be in listsuspicious
141       set(sublabel2 "")
142       if (_v)
143         findexpr(mfound ${testname} ${listsuspiciousx} sublabel2)
144       else()
145         set(mfound OFF)
146       endif()
147       if (mfound)
148         set(sublabel3 "")
149         findexpr(foundsuspended ${testname} ${listsuspendedx} sublabel3)
150         set(${inverted} 1)
151         if (foundsuspended)
152           set(sublabel "suspended" ${sublabel} ${sublabel2} ${sublabel3})
153           list(REMOVE_ITEM sublabel "export" "inverted" )
154         else()
155           set(sublabel "inverted" ${sublabel} ${sublabel2} ${sublabel3})
156         endif()
157       else()
158         set(${inverted} 0)
159       endif()
160     endif()
161     list(REMOVE_DUPLICATES sublabel)
162     if (NOT sublabel STREQUAL "")
163       join(sublabel "." tmpprefixx)
164       if (tmpprefixx)
165         string(TOUPPER "${tmpprefixx}_" tmpprefix)
166       else()
167         set(tmpprefix "")
168       endif()
169       set(${testname} "${tmpprefix}${${testname}}")
170       set(${listlabels} ${sublabel})
171     endif()
172   else()
173     # No testname because ignored
174     set(${testname} "")
175   endif()
176 endmacro()
177
178 macro(loadTestList filename resList depth)
179   # Create list of strings from a file without comments
180   # ENCODING parameter is a new feature in cmake 3.1
181   initLangVars(${resList})
182   initLangVars("sublabel")
183   if (CMAKE_VERSION VERSION_GREATER "3.1")
184     file(STRINGS ${filename} tempList ENCODING "UTF-8")
185   else()
186     file(STRINGS ${filename} tempList)
187   endif()
188   set(${resList})
189   set(sublabel)
190   set(mylabels "")
191   set(languages "")
192   message(STATUS "Reading list ${filename}")
193   foreach(_l ${tempList})
194     set(_newl "${_l}")
195     string(REGEX REPLACE "[ \t]+$" "" _newl "${_l}")
196     string(REGEX REPLACE "[ \t]*#.*$" "" _newl "${_l}")
197     if(_newl)
198       list(APPEND ${resList} "${_newl}")
199       if (_newl MATCHES "^Sublabel:")
200         string(REGEX REPLACE "^Sublabel:[ \t]*" "" _newlabels ${_newl})
201         string(REGEX MATCHALL "([0-9]*[a-zA-Z]+)" _labels ${_newlabels})
202         foreach(labname ${_labels})
203           if (NOT labname STREQUAL "RESET")
204             list(APPEND mylabels ${labname})
205           endif()
206         endforeach()
207         list(REMOVE_DUPLICATES mylabels)
208         set(sublabel ${_newl})
209       else()
210         string(REGEX REPLACE "(\\/|\\||\\(|\\))" "  " _vxx ${_newl})
211         string(REGEX MATCHALL " ([a-z][a-z](_[A-Z][A-Z])?) " _vx ${_vxx})
212         if(_vx)
213           foreach(_v ${_vx})
214             string(REGEX REPLACE " " "" _v ${_v})
215             #message(STATUS " ==> ${resList}_${_v}")
216             #message(STATUS "sublabel = ${sublabel}, sublabel_${_v} = ${sublabel_${_v}}")
217             if (NOT sublabel STREQUAL "${sublabel_${_v}}")
218               list(APPEND ${resList}_${_v} "${sublabel}")
219               set(sublabel_${_v} "${sublabel}")
220               #message(STATUS "Setting variable sublabel_${_v} with \"${sublabel}\"")
221             endif()
222             list(APPEND ${resList}_${_v} "${_newl}")
223             list(APPEND languages ${_v})
224           endforeach()
225           list(REMOVE_DUPLICATES languages)
226           #message(STATUS "languages = ${languages}")
227         endif()
228       endif()
229     endif()
230   endforeach()
231   foreach(_l1 ${mylabels})
232     if (_l1 MATCHES "^([0-9]+)([a-zA-Z]+)$")
233       set(_l ${CMAKE_MATCH_2})
234       set(depth1 ${CMAKE_MATCH_1})
235     else()
236       set(_l ${_l1})
237       set(depth1 "0")
238     endif()
239     list(FIND known_labels ${_l} _ff)
240     if (_ff GREATER -1)
241       message(STATUS "Label \"${_l}\" already in use. Reused in ${filename}")
242     else()
243       assignLabelDepth(${depth}${depth1} ${_l})
244     endif()
245   endforeach()
246   foreach(_lg ${languages})
247     # reset label for each used language string at end of file
248     #message(STATUS "Resetting variable sublabel_${_lg}, previously set to ${sublabel_${_lg}}")
249     set(sublabel_${_lg} "")
250   endforeach()
251 endmacro()
252
253 # This labels should not be used in .*Tests files
254 set(known_labels "")
255 # Create depth info to each label
256 macro(assignLabelDepth depth)
257   foreach(_lab ${ARGN})
258     list(APPEND known_labels ${_lab})
259     set(depth_${_lab} ${depth})
260   endforeach()
261 endmacro()
262
263 assignLabelDepth(0 "export" "key" "layout" "load" "lyx2lyx" "module" "roundtrip" "url")
264 assignLabelDepth(1 "unreliable" "inverted")
265 assignLabelDepth(2 "suspended")
266 assignLabelDepth(3 "examples" "manuals" "mathmacros" "templates" "autotests")
267
268 loadTestList(suspiciousTests suspiciousTests 7)
269 loadTestList(ignoredTests ignoredTests 0)
270 loadTestList(suspendedTests suspendedTests 6)
271 loadTestList(unreliableTests unreliableTests 5)
272
273 foreach(libsubfolderx autotests/export lib/doc lib/examples lib/templates autotests/mathmacros)
274   set(testlabel "export")
275   if (libsubfolderx MATCHES "lib/doc")
276     list(APPEND testlabel "manuals")
277   elseif (libsubfolderx MATCHES "lib/examples")
278     list(APPEND testlabel "examples")
279   elseif (libsubfolderx MATCHES "lib/templates")
280     list(APPEND testlabel "templates")
281   elseif (libsubfolderx MATCHES "autotests/mathmacros")
282     list(APPEND testlabel "mathmacros")
283   elseif (libsubfolderx MATCHES "autotests/.+")
284     list(APPEND testlabel "autotests")
285   endif()
286   set(LIBSUB_SRC_DIR "${TOP_SRC_DIR}/${libsubfolderx}")
287   string(REGEX REPLACE "^(lib|development|autotests)/" "" libsubfolder "${libsubfolderx}")
288   set(LIBSUB_SRC_DIR "${TOP_SRC_DIR}/${libsubfolderx}")
289   message(STATUS "Handling export dir ${LIBSUB_SRC_DIR}")
290   file(GLOB_RECURSE lyx_files RELATIVE "${LIBSUB_SRC_DIR}" "${LIBSUB_SRC_DIR}/*.lyx")
291   list(SORT lyx_files)
292   # Now create 2 lists. One for files in a language dir, one without
293   set(lang_lyx_files)
294   set(nolang_lyx_files)
295   foreach(f ${lyx_files})
296     string(REGEX MATCHALL "^[a-z][a-z](_[A-Z][A-Z])?\\/" _v ${f})
297     if(_v)
298       list(APPEND lang_lyx_files ${f})
299     else()
300       list(APPEND nolang_lyx_files ${f})
301     endif()
302   endforeach()
303   foreach(f ${nolang_lyx_files} ${lang_lyx_files})
304     # Strip extension
305     string(REGEX REPLACE "\\.lyx$" "" f ${f})
306     set(TestName "export/${libsubfolder}/${f}_lyx16")
307     set(mytestlabel ${testlabel})
308     maketestname(TestName inverted suspiciousTests ignoredTests unreliableTests mytestlabel)
309     if(TestName)
310       add_test(NAME ${TestName}
311         WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${LYX_HOME}"
312         COMMAND ${CMAKE_COMMAND} -DLYX_ROOT=${LIBSUB_SRC_DIR}
313         -DLYX_TESTS_USERDIR=${LYX_TESTS_USERDIR}
314         -Dlyx=$<TARGET_FILE:${_lyx}>
315         -DWORKDIR=${CMAKE_CURRENT_BINARY_DIR}/${LYX_HOME}
316         -DLYX_USERDIR_VER=${LYX_USERDIR_VER}
317         -Dformat=lyx16x
318         -Dextension=16.lyx
319         -Dfile=${f}
320         -Dinverted=${inverted}
321         -DTOP_SRC_DIR=${TOP_SRC_DIR}
322         -DPERL_EXECUTABLE=${PERL_EXECUTABLE}
323         -P "${TOP_SRC_DIR}/development/autotests/export.cmake")
324       setmarkedtestlabel(${TestName} ${mytestlabel})
325     endif()
326     if(LYX_PYTHON_EXECUTABLE)
327       set(lyx2lyxtestlabel "lyx2lyx")
328       # For use of lyx2lyx we need the python executable
329       set(mytestlabel ${lyx2lyxtestlabel})
330       set(TestName "lyx2lyx/${libsubfolder}/${f}")
331       maketestname(TestName inverted suspiciousTests ignoredTests unreliableTests mytestlabel)
332       if(TestName)
333         add_test(NAME ${TestName}
334           WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${LYX_HOME}"
335           COMMAND ${CMAKE_COMMAND}
336           "-DLYX_PYTHON_EXECUTABLE=${LYX_PYTHON_EXECUTABLE}"
337           "-DLYX2LYX=${TOP_SRC_DIR}/lib/lyx2lyx/lyx2lyx"
338           "-DLYX_TESTS_USERDIR=${LYX_TESTS_USERDIR}"
339           "-DLYXFILE=${LIBSUB_SRC_DIR}/${f}.lyx"
340           -P "${TOP_SRC_DIR}/development/autotests/lyx2lyxtest.cmake")
341         setmarkedtestlabel(${TestName} ${mytestlabel})
342       endif()
343     endif()
344     set(loadtestlabel "load")
345     set(mytestlabel ${loadtestlabel})
346     set(TestName "check_load/${libsubfolder}/${f}")
347     maketestname(TestName inverted suspiciousTests ignoredTests unreliableTests mytestlabel)
348     if(TestName)
349       add_test(NAME ${TestName}
350         WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${LYX_HOME}"
351         COMMAND ${CMAKE_COMMAND} -DLYXFILE=${LIBSUB_SRC_DIR}/${f}.lyx
352         -DLYX_TESTS_USERDIR=${LYX_TESTS_USERDIR}
353         -Dlyx=$<TARGET_FILE:${_lyx}>
354         -DPARAMS_DIR=${TOP_SRC_DIR}/development/autotests
355         -DWORKDIR=${CMAKE_CURRENT_BINARY_DIR}/${LYX_HOME}
356         -P "${TOP_SRC_DIR}/development/autotests/check_load.cmake")
357       setmarkedtestlabel(${TestName} ${mytestlabel})
358       set_tests_properties(${TestName} PROPERTIES RUN_SERIAL ON)
359     endif()
360     getoutputformats("${LIBSUB_SRC_DIR}/${f}.lyx" formatlist)
361     foreach(format ${formatlist})
362       if(format MATCHES "dvi3|pdf4|pdf5")
363         set(fonttypes "texF" "systemF")
364       else()
365         set(fonttypes "defaultF")
366       endif()
367       foreach(fonttype ${fonttypes})
368         if(fonttype MATCHES "defaultF")
369           set(TestName "export/${libsubfolder}/${f}_${format}")
370         else()
371           set(TestName "export/${libsubfolder}/${f}_${format}_${fonttype}")
372         endif()
373         set(mytestlabel ${testlabel})
374         maketestname(TestName inverted suspiciousTests ignoredTests unreliableTests mytestlabel)
375         if(TestName)
376           add_test(NAME ${TestName}
377             WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${LYX_HOME}"
378             COMMAND ${CMAKE_COMMAND} -DLYX_ROOT=${LIBSUB_SRC_DIR}
379             -DLYX_TESTS_USERDIR=${LYX_TESTS_USERDIR}
380             -Dlyx=$<TARGET_FILE:${_lyx}>
381             -DWORKDIR=${CMAKE_CURRENT_BINARY_DIR}/${LYX_HOME}
382             -Dformat=${format}
383             -Dfonttype=${fonttype}
384             -Dextension=${format}
385             -Dfile=${f}
386             -Dinverted=${inverted}
387             -DTOP_SRC_DIR=${TOP_SRC_DIR}
388             -DPERL_EXECUTABLE=${PERL_EXECUTABLE}
389             -P "${TOP_SRC_DIR}/development/autotests/export.cmake")
390           setmarkedtestlabel(${TestName} ${mytestlabel}) # check for suspended pdf/dvi exports
391         endif()
392       endforeach()
393     endforeach()
394   endforeach()
395 endforeach()