Debian Codegraph @

test/fpm_test/test_module_dependencies.f90

raw
1 !> Define tests for the `fpm_sources` module (module dependency checking)
2 module test_module_dependencies
3 use testsuite, only : new_unittest, unittest_t, error_t, test_failed
4 use fpm_targets, only: targets_from_sources, resolve_module_dependencies, &
5 build_target_t, build_target_ptr, &
6 FPM_TARGET_EXECUTABLE, FPM_TARGET_OBJECT, FPM_TARGET_ARCHIVE
7 use fpm_model, only: fpm_model_t, srcfile_t, &
8 FPM_UNIT_UNKNOWN, FPM_UNIT_PROGRAM, FPM_UNIT_MODULE, &
9 FPM_UNIT_SUBMODULE, FPM_UNIT_SUBPROGRAM, FPM_UNIT_CSOURCE, &
10 FPM_UNIT_CHEADER, FPM_SCOPE_UNKNOWN, FPM_SCOPE_LIB, &
11 FPM_SCOPE_DEP, FPM_SCOPE_APP, FPM_SCOPE_TEST
12 use fpm_strings, only: string_t, operator(.in.), is_valid_module_name, is_valid_module_prefix
13 use fpm, only: check_modules_for_duplicates
14 implicit none
15 private
16
17 public :: collect_module_dependencies, operator(.in.)
18
19 interface operator(.in.)
20 module procedure target_in
21 end interface
22
23 contains
24
25
26 !> Collect all exported unit tests
27 subroutine collect_module_dependencies(testsuite)
28
29 !> Collection of tests
30 type(unittest_t), allocatable, intent(out) :: testsuite(:)
31
32 testsuite = [ &
33 & new_unittest("library-module-use", test_library_module_use), &
34 & new_unittest("program-module-use", test_program_module_use), &
35 & new_unittest("program-with-module", test_program_with_module), &
36 & new_unittest("program-own-module-use", test_program_own_module_use), &
37 & new_unittest("missing-library-use", &
38 test_missing_library_use, should_fail=.true.), &
39 & new_unittest("missing-program-use", &
40 test_missing_program_use, should_fail=.true.), &
41 & new_unittest("invalid-library-use", &
42 test_invalid_library_use, should_fail=.true.), &
43 & new_unittest("package-with-no-duplicates", &
44 test_package_with_no_module_duplicates), &
45 & new_unittest("package-with-duplicates-in-same-source", &
46 test_package_module_duplicates_same_source, should_fail=.true.), &
47 & new_unittest("package-with-duplicates-in-one-package", &
48 test_package_module_duplicates_one_package, should_fail=.true.), &
49 & new_unittest("package-with-duplicates-in-two-packages", &
50 test_package_module_duplicates_two_packages, should_fail=.true.), &
51 & new_unittest("subdirectory-module-use", &
52 test_subdirectory_module_use), &
53 & new_unittest("invalid-subdirectory-module-use", &
54 test_invalid_subdirectory_module_use, should_fail=.true.), &
55 & new_unittest("tree-shake-module", &
56 test_tree_shake_module, should_fail=.false.), &
57 & new_unittest("tree-shake-subprogram-with-module", &
58 test_tree_shake_subprogram_with_module, should_fail=.false.), &
59 & new_unittest("valid-enforced-module-names", &
60 check_valid_enforced_module_names, should_fail=.false.), &
61 & new_unittest("valid-enforced-module-names-dashed", &
62 check_valid_enforced_module_names_dashed, should_fail=.false.), &
63 & new_unittest("invalid-enforced-module-names", &
64 check_invalid_enforced_module_names, should_fail=.false.), &
65 & new_unittest("invalid-module-names", &
66 check_invalid_module_names, should_fail=.false.), &
67 & new_unittest("custom-module-prefixes", &
68 check_valid_custom_prefix, should_fail=.false.), &
69 & new_unittest("custom-prefixed-module-names", &
70 check_custom_prefixed_modules, should_fail=.false.) &
71 ]
72
73 end subroutine collect_module_dependencies
74
75
76 !> Check library module using another library module
77 subroutine test_library_module_use(error)
78
79 !> Error handling
80 type(error_t), allocatable, intent(out) :: error
81
82 type(fpm_model_t) :: model
83 type(build_target_ptr), allocatable :: targets(:)
84
85 allocate(model%external_modules(0))
86 allocate(model%packages(1))
87 allocate(model%packages(1)%sources(2))
88
89 model%package_name = "test"
90 model%build_prefix = ""
91 model%packages(1)%name = "package1"
92
93 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
94 scope = FPM_SCOPE_LIB, &
95 provides=[string_t('my_mod_1')])
96
97 model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_2.f90", &
98 scope = FPM_SCOPE_LIB, &
99 provides=[string_t('my_mod_2')], &
100 uses=[string_t('my_mod_1')])
101
102 call targets_from_sources(targets,model,.false.,error=error)
103 if (allocated(error)) return
104
105 if (allocated(error)) then
106 return
107 end if
108 if (size(targets) /= 3) then
109 call test_failed(error,'Incorrect number of targets - expecting three')
110 return
111 end if
112
113 call check_target(targets(1)%ptr,type=FPM_TARGET_ARCHIVE,n_depends=2, &
114 deps = [targets(2),targets(3)], &
115 links = targets(2:3), error=error)
116
117 if (allocated(error)) return
118
119
120 call check_target(targets(2)%ptr,type=FPM_TARGET_OBJECT,n_depends=0, &
121 source=model%packages(1)%sources(1),error=error)
122
123 if (allocated(error)) return
124
125
126 call check_target(targets(3)%ptr,type=FPM_TARGET_OBJECT,n_depends=1, &
127 deps=[targets(2)],source=model%packages(1)%sources(2),error=error)
128
129 if (allocated(error)) return
130
131 end subroutine test_library_module_use
132
133
134 !> Check a program using a library module
135 !> Each program generates two targets: object file and executable
136 !>
137 subroutine test_program_module_use(error)
138
139 !> Error handling
140 type(error_t), allocatable, intent(out) :: error
141
142 call test_scope(FPM_SCOPE_APP,error)
143 if (allocated(error)) return
144
145 call test_scope(FPM_SCOPE_TEST,error)
146 if (allocated(error)) return
147
148 contains
149
150 subroutine test_scope(exe_scope,error)
151 integer, intent(in) :: exe_scope
152 type(error_t), allocatable, intent(out) :: error
153
154 type(fpm_model_t) :: model
155 type(build_target_ptr), allocatable :: targets(:)
156 character(:), allocatable :: scope_str
157
158 allocate(model%external_modules(0))
159 allocate(model%packages(1))
160 allocate(model%packages(1)%sources(2))
161
162 model%package_name = "test_scope"
163 model%build_prefix = ""
164 model%packages(1)%name = "package1"
165
166 scope_str = merge('FPM_SCOPE_APP ','FPM_SCOPE_TEST',exe_scope==FPM_SCOPE_APP)//' - '
167
168 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
169 scope = FPM_SCOPE_LIB, &
170 provides=[string_t('my_mod_1')])
171
172 model%packages(1)%sources(2) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/my_program.f90", &
173 scope=exe_scope, &
174 uses=[string_t('my_mod_1')])
175
176 call targets_from_sources(targets,model,.false.,error=error)
177 if (allocated(error)) return
178
179 if (size(targets) /= 4) then
180 call test_failed(error,scope_str//'Incorrect number of targets - expecting three')
181 return
182 end if
183
184 call check_target(targets(1)%ptr,type=FPM_TARGET_ARCHIVE,n_depends=1, &
185 deps=[targets(2)],links=[targets(2)],error=error)
186
187 if (allocated(error)) return
188
189 call check_target(targets(2)%ptr,type=FPM_TARGET_OBJECT,n_depends=0, &
190 source=model%packages(1)%sources(1),error=error)
191
192 if (allocated(error)) return
193
194 call check_target(targets(3)%ptr,type=FPM_TARGET_OBJECT,n_depends=1, &
195 deps=[targets(2)],source=model%packages(1)%sources(2),error=error)
196
197 if (allocated(error)) return
198
199 call check_target(targets(4)%ptr,type=FPM_TARGET_EXECUTABLE,n_depends=2, &
200 deps=[targets(1),targets(3)], &
201 links=[targets(3)], error=error)
202
203 if (allocated(error)) return
204
205 end subroutine test_scope
206
207 end subroutine test_program_module_use
208
209
210 !> Check program with module in single source file
211 !> (Resulting target should not include itself as a dependency)
212 subroutine test_program_with_module(error)
213
214 !> Error handling
215 type(error_t), allocatable, intent(out) :: error
216
217 type(fpm_model_t) :: model
218 type(build_target_ptr), allocatable :: targets(:)
219
220 allocate(model%external_modules(0))
221 allocate(model%packages(1))
222 allocate(model%packages(1)%sources(1))
223
224 model%package_name = "test_program_with_module"
225 model%build_prefix = ""
226 model%packages(1)%name = "package1"
227
228 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/my_program.f90", &
229 scope = FPM_SCOPE_APP, &
230 provides=[string_t('app_mod')], &
231 uses=[string_t('app_mod')])
232
233 call targets_from_sources(targets,model,.false.,error=error)
234 if (allocated(error)) return
235
236 if (size(targets) /= 2) then
237 write(*,*) size(targets)
238 call test_failed(error,'Incorrect number of targets - expecting two')
239 return
240 end if
241
242 call check_target(targets(1)%ptr,type=FPM_TARGET_OBJECT,n_depends=0, &
243 source=model%packages(1)%sources(1),error=error)
244
245 if (allocated(error)) return
246
247 call check_target(targets(2)%ptr,type=FPM_TARGET_EXECUTABLE,n_depends=1, &
248 deps=[targets(1)],links=[targets(1)],error=error)
249
250 if (allocated(error)) return
251
252 end subroutine test_program_with_module
253
254
255 !> Check program using modules in same directory
256 subroutine test_program_own_module_use(error)
257
258 !> Error handling
259 type(error_t), allocatable, intent(out) :: error
260
261 call test_scope(FPM_SCOPE_APP,error)
262 if (allocated(error)) return
263
264 call test_scope(FPM_SCOPE_TEST,error)
265 if (allocated(error)) return
266
267 contains
268
269 subroutine test_scope(exe_scope,error)
270 integer, intent(in) :: exe_scope
271 type(error_t), allocatable, intent(out) :: error
272
273 type(fpm_model_t) :: model
274 type(build_target_ptr), allocatable :: targets(:)
275 character(:), allocatable :: scope_str
276
277 allocate(model%external_modules(0))
278 allocate(model%packages(1))
279 allocate(model%packages(1)%sources(3))
280
281 model%package_name = "test_scope"
282 model%build_prefix = ""
283 model%packages(1)%name = "package1"
284
285 scope_str = merge('FPM_SCOPE_APP ','FPM_SCOPE_TEST',exe_scope==FPM_SCOPE_APP)//' - '
286
287 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="app/app_mod1.f90", &
288 scope = exe_scope, &
289 provides=[string_t('app_mod1')])
290
291 model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="app/app_mod2.f90", &
292 scope = exe_scope, &
293 provides=[string_t('app_mod2')],uses=[string_t('app_mod1')])
294
295 model%packages(1)%sources(3) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/my_program.f90", &
296 scope=exe_scope, &
297 uses=[string_t('app_mod2')])
298
299 call targets_from_sources(targets,model,.false.,error=error)
300 if (allocated(error)) return
301
302 if (size(targets) /= 4) then
303 call test_failed(error,scope_str//'Incorrect number of targets - expecting three')
304 return
305 end if
306
307 call check_target(targets(1)%ptr,type=FPM_TARGET_OBJECT,n_depends=0, &
308 source=model%packages(1)%sources(1),error=error)
309
310 if (allocated(error)) return
311
312 call check_target(targets(2)%ptr,type=FPM_TARGET_OBJECT,n_depends=1, &
313 source=model%packages(1)%sources(2),deps=[targets(1)],error=error)
314
315 if (allocated(error)) return
316
317 call check_target(targets(3)%ptr,type=FPM_TARGET_OBJECT,n_depends=1, &
318 source=model%packages(1)%sources(3),deps=[targets(2)],error=error)
319
320 if (allocated(error)) return
321
322 call check_target(targets(4)%ptr,type=FPM_TARGET_EXECUTABLE,n_depends=1, &
323 deps=[targets(3)],links=targets(1:3), error=error)
324
325 if (allocated(error)) return
326
327 end subroutine test_scope
328 end subroutine test_program_own_module_use
329
330
331 !> Check missing library module dependency
332 subroutine test_missing_library_use(error)
333
334 !> Error handling
335 type(error_t), allocatable, intent(out) :: error
336
337 type(fpm_model_t) :: model
338 type(build_target_ptr), allocatable :: targets(:)
339
340 allocate(model%external_modules(0))
341 allocate(model%packages(1))
342 allocate(model%packages(1)%sources(2))
343
344 model%package_name = "test_missing_library_use"
345 model%build_prefix = ""
346 model%packages(1)%name = "package1"
347
348 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
349 scope = FPM_SCOPE_LIB, &
350 provides=[string_t('my_mod_1')])
351
352 model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_2.f90", &
353 scope = FPM_SCOPE_LIB, &
354 provides=[string_t('my_mod_2')], &
355 uses=[string_t('my_mod_3')])
356
357 call targets_from_sources(targets,model,.false.,error=error)
358
359 end subroutine test_missing_library_use
360
361
362 !> Check missing program module dependency
363 subroutine test_missing_program_use(error)
364
365 !> Error handling
366 type(error_t), allocatable, intent(out) :: error
367
368 type(fpm_model_t) :: model
369 type(build_target_ptr), allocatable :: targets(:)
370
371 allocate(model%external_modules(0))
372 allocate(model%packages(1))
373 allocate(model%packages(1)%sources(2))
374
375 model%package_name = "test_missing_program_use"
376 model%build_prefix = ""
377 model%packages(1)%name = "package1"
378
379 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
380 scope = FPM_SCOPE_LIB, &
381 provides=[string_t('my_mod_1')])
382
383 model%packages(1)%sources(2) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/my_program.f90", &
384 scope=FPM_SCOPE_APP, &
385 uses=[string_t('my_mod_2')])
386
387 call targets_from_sources(targets,model,.false.,error=error)
388
389 end subroutine test_missing_program_use
390
391
392 !> Check library module using a non-library module
393 subroutine test_invalid_library_use(error)
394
395 !> Error handling
396 type(error_t), allocatable, intent(out) :: error
397
398 type(fpm_model_t) :: model
399 type(build_target_ptr), allocatable :: targets(:)
400
401 allocate(model%external_modules(0))
402 allocate(model%packages(1))
403 allocate(model%packages(1)%sources(2))
404
405 model%package_name = "test_invalid_library_use"
406 model%build_prefix = ""
407 model%packages(1)%name = "package1"
408
409 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="app/app_mod.f90", &
410 scope = FPM_SCOPE_APP, &
411 provides=[string_t('app_mod')])
412
413 model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod.f90", &
414 scope = FPM_SCOPE_LIB, &
415 provides=[string_t('my_mod')], &
416 uses=[string_t('app_mod')])
417
418 call targets_from_sources(targets,model,.false.,error=error)
419
420 end subroutine test_invalid_library_use
421
422
423 !> Check program using a non-library module in a sub-directory
424 subroutine test_subdirectory_module_use(error)
425
426 !> Error handling
427 type(error_t), allocatable, intent(out) :: error
428
429 type(fpm_model_t) :: model
430 type(build_target_ptr), allocatable :: targets(:)
431
432 allocate(model%external_modules(0))
433 allocate(model%packages(1))
434 allocate(model%packages(1)%sources(2))
435
436 model%package_name = "test_subdirectory_module_use"
437 model%build_prefix = ""
438 model%packages(1)%name = "package1"
439
440 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="app/subdir/app_mod.f90", &
441 scope = FPM_SCOPE_APP, &
442 provides=[string_t('app_mod')])
443
444 model%packages(1)%sources(2) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/my_program.f90", &
445 scope=FPM_SCOPE_APP, &
446 uses=[string_t('app_mod')])
447
448 call targets_from_sources(targets,model,.false.,error=error)
449
450 end subroutine test_subdirectory_module_use
451
452 !> Check program with no duplicate modules
453 subroutine test_package_with_no_module_duplicates(error)
454
455 type(error_t), allocatable, intent(out) :: error
456
457 type(fpm_model_t) :: model
458 logical :: duplicates_found = .false.
459
460 allocate(model%packages(1))
461 allocate(model%packages(1)%sources(2))
462
463 model%package_name = "test_package_with_no_module_duplicates"
464 model%build_prefix = ""
465 model%packages(1)%name = "package1"
466
467 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
468 scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1')])
469
470 model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_2.f90", &
471 scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_2')])
472
473 call check_modules_for_duplicates(model, duplicates_found)
474 if (duplicates_found) then
475 call test_failed(error,'Duplicate modules found')
476 return
477 end if
478 end subroutine test_package_with_no_module_duplicates
479
480 !> Check program with duplicate modules in same source file
481 subroutine test_package_module_duplicates_same_source(error)
482
483 type(error_t), allocatable, intent(out) :: error
484
485 type(fpm_model_t) :: model
486 logical :: duplicates_found
487
488 allocate(model%packages(1))
489 allocate(model%packages(1)%sources(1))
490
491 model%package_name = "test_package_module_duplicates_same_source"
492 model%build_prefix = ""
493 model%packages(1)%name = "package1"
494
495 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
496 scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1'), string_t('my_mod_1')])
497
498 call check_modules_for_duplicates(model, duplicates_found)
499 if (duplicates_found) then
500 call test_failed(error,'Duplicate modules found')
501 return
502 end if
503 end subroutine test_package_module_duplicates_same_source
504
505 !> Check program with duplicate modules in two different source files in one package
506 subroutine test_package_module_duplicates_one_package(error)
507
508 type(error_t), allocatable, intent(out) :: error
509
510 type(fpm_model_t) :: model
511 logical :: duplicates_found
512
513 allocate(model%packages(1))
514 allocate(model%packages(1)%sources(2))
515
516 model%package_name = "test_package_module_duplicates_one_package"
517 model%build_prefix = ""
518 model%packages(1)%name = "package1"
519
520 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1_a.f90", &
521 scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1')])
522
523 model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1_b.f90", &
524 scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1')])
525
526 call check_modules_for_duplicates(model, duplicates_found)
527 if (duplicates_found) then
528 call test_failed(error,'Duplicate modules found')
529 return
530 end if
531 end subroutine test_package_module_duplicates_one_package
532
533 !> Check program with duplicate modules in two different packages
534 subroutine test_package_module_duplicates_two_packages(error)
535
536 type(error_t), allocatable, intent(out) :: error
537
538 type(fpm_model_t) :: model
539 logical :: duplicates_found
540
541 allocate(model%packages(2))
542 allocate(model%packages(1)%sources(1))
543 allocate(model%packages(2)%sources(1))
544
545 model%package_name = "test_package_module_duplicates_two_packages"
546 model%build_prefix = ""
547 model%packages(1)%name = "package1"
548 model%packages(2)%name = "package2"
549
550 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/subdir1/my_mod_1.f90", &
551 scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1')])
552
553 model%packages(2)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/subdir2/my_mod_1.f90", &
554 scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1')])
555
556 call check_modules_for_duplicates(model, duplicates_found)
557 if (duplicates_found) then
558 call test_failed(error,'Duplicate modules found')
559 return
560 end if
561 end subroutine test_package_module_duplicates_two_packages
562
563
564 !> Check tree-shaking of unused modules
565 !> Unused module should not be included in targets
566 subroutine test_tree_shake_module(error)
567
568 !> Error handling
569 type(error_t), allocatable, intent(out) :: error
570
571 type(fpm_model_t) :: model
572 type(build_target_ptr), allocatable :: targets(:)
573 character(:), allocatable :: scope_str
574
575 allocate(model%external_modules(0))
576 allocate(model%packages(1))
577 allocate(model%packages(1)%sources(4))
578
579 model%package_name = "test_tree_shake_module"
580 model%build_prefix = ""
581 model%packages(1)%name = "package1"
582
583 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
584 scope = FPM_SCOPE_LIB, &
585 provides=[string_t('my_mod_1')]) ! indirectly used
586
587 model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_2.f90", &
588 scope = FPM_SCOPE_LIB, &
589 provides=[string_t('my_mod_2')], &
590 uses=[string_t('my_mod_1')]) ! directly used
591
592 model%packages(1)%sources(3) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_3.f90", &
593 scope = FPM_SCOPE_LIB, &
594 provides=[string_t('my_mod_3')]) ! unused module
595
596 model%packages(1)%sources(4) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/my_program.f90", &
597 scope=FPM_SCOPE_APP, &
598 uses=[string_t('my_mod_2')])
599
600 call targets_from_sources(targets,model,prune=.true.,error=error)
601 if (allocated(error)) return
602
603 if (size(targets) /= 5) then
604 call test_failed(error,scope_str//'Incorrect number of targets - expecting five')
605 return
606 end if
607
608 call check_target(targets(1)%ptr,type=FPM_TARGET_ARCHIVE,n_depends=2, &
609 deps=[targets(2),targets(3)], &
610 links=[targets(2),targets(3)],error=error)
611
612 if (allocated(error)) return
613
614 call check_target(targets(2)%ptr,type=FPM_TARGET_OBJECT,n_depends=0, &
615 source=model%packages(1)%sources(1),error=error)
616
617 if (allocated(error)) return
618
619 call check_target(targets(3)%ptr,type=FPM_TARGET_OBJECT,n_depends=1, &
620 deps=[targets(2)],source=model%packages(1)%sources(2),error=error)
621
622 if (allocated(error)) return
623
624 call check_target(targets(4)%ptr,type=FPM_TARGET_OBJECT,n_depends=1, &
625 deps=[targets(3)],source=model%packages(1)%sources(4),error=error)
626
627 if (allocated(error)) return
628
629 call check_target(targets(5)%ptr,type=FPM_TARGET_EXECUTABLE,n_depends=2, &
630 deps=[targets(1),targets(4)], &
631 links=[targets(4)], error=error)
632
633 if (allocated(error)) return
634
635 end subroutine test_tree_shake_module
636
637
638 !> Check tree-shaking of modules used via a subprogram source
639 !> (Subprogram type is a source containing any non-module subroutines/functions)
640 !> Subprograms cannot be pruned, so neither can their dependencies
641 subroutine test_tree_shake_subprogram_with_module(error)
642
643 !> Error handling
644 type(error_t), allocatable, intent(out) :: error
645
646 type(fpm_model_t) :: model
647 type(build_target_ptr), allocatable :: targets(:)
648 character(:), allocatable :: scope_str
649
650 allocate(model%external_modules(0))
651 allocate(model%packages(1))
652 allocate(model%packages(1)%sources(4))
653
654 model%package_name = "test_tree_shake_subprogram_with_module"
655 model%build_prefix = ""
656 model%packages(1)%name = "package1"
657
658 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
659 scope = FPM_SCOPE_LIB, &
660 provides=[string_t('my_mod_1')]) ! used via subprogram
661
662 model%packages(1)%sources(2) = new_test_source(FPM_UNIT_SUBPROGRAM,file_name="src/my_subprogram.f90", &
663 scope = FPM_SCOPE_LIB, &
664 uses=[string_t('my_mod_1')]) ! subprogram (never pruned)
665
666 model%packages(1)%sources(3) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_3.f90", &
667 scope = FPM_SCOPE_LIB, &
668 provides=[string_t('my_mod_3')]) ! unused module
669
670 model%packages(1)%sources(4) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/my_program.f90", &
671 scope=FPM_SCOPE_APP)
672
673 call targets_from_sources(targets,model,prune=.true.,error=error)
674 if (allocated(error)) return
675
676 if (size(targets) /= 5) then
677 call test_failed(error,scope_str//'Incorrect number of targets - expecting five')
678 return
679 end if
680
681 call check_target(targets(1)%ptr,type=FPM_TARGET_ARCHIVE,n_depends=2, &
682 deps=[targets(2)], &
683 links=[targets(2),targets(3)],error=error)
684
685 if (allocated(error)) return
686
687 call check_target(targets(2)%ptr,type=FPM_TARGET_OBJECT,n_depends=0, &
688 source=model%packages(1)%sources(1),error=error)
689
690 if (allocated(error)) return
691
692 call check_target(targets(3)%ptr,type=FPM_TARGET_OBJECT,n_depends=1, &
693 deps=[targets(2)],source=model%packages(1)%sources(2),error=error)
694
695 if (allocated(error)) return
696
697 call check_target(targets(4)%ptr,type=FPM_TARGET_OBJECT,n_depends=0, &
698 source=model%packages(1)%sources(4),error=error)
699
700 if (allocated(error)) return
701
702 call check_target(targets(5)%ptr,type=FPM_TARGET_EXECUTABLE,n_depends=2, &
703 deps=[targets(1),targets(4)], &
704 links=[targets(4)], error=error)
705
706 if (allocated(error)) return
707
708 end subroutine test_tree_shake_subprogram_with_module
709
710
711 !> Check program using a non-library module in a differente sub-directory
712 subroutine test_invalid_subdirectory_module_use(error)
713
714 !> Error handling
715 type(error_t), allocatable, intent(out) :: error
716
717 type(fpm_model_t) :: model
718 type(build_target_ptr), allocatable :: targets(:)
719
720 allocate(model%external_modules(0))
721 allocate(model%packages(1))
722 allocate(model%packages(1)%sources(2))
723
724 model%package_name = "test_invalid_subdirectory_module_use"
725 model%build_prefix = ""
726 model%packages(1)%name = "package1"
727
728 model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="app/diff_dir/app_mod.f90", &
729 scope = FPM_SCOPE_APP, &
730 provides=[string_t('app_mod')])
731
732 model%packages(1)%sources(2) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/prog_dir/my_program.f90", &
733 scope=FPM_SCOPE_APP, &
734 uses=[string_t('app_mod')])
735
736 call targets_from_sources(targets,model,.false.,error=error)
737
738 end subroutine test_invalid_subdirectory_module_use
739
740 !> Helper to create a new srcfile_t
741 function new_test_source(type,file_name, scope, uses, provides) result(src)
742 integer, intent(in) :: type
743 character(*), intent(in) :: file_name
744 integer, intent(in) :: scope
745 type(string_t), intent(in), optional :: uses(:)
746 type(string_t), intent(in), optional :: provides(:)
747 type(srcfile_t) :: src
748
749 src%file_name = file_name
750 src%unit_scope = scope
751 src%unit_type = type
752
753 if (present(provides)) then
754 src%modules_provided = provides
755 else
756 allocate(src%modules_provided(0))
757 end if
758
759 if (present(uses)) then
760 src%modules_used = uses
761 else
762 allocate(src%modules_used(0))
763 end if
764
765 allocate(src%include_dependencies(0))
766
767 end function new_test_source
768
769
770 !> Helper to check an expected output target
771 subroutine check_target(target,type,n_depends,deps,links,source,error)
772 type(build_target_t), intent(in) :: target
773 integer, intent(in) :: type
774 integer, intent(in) :: n_depends
775 type(srcfile_t), intent(in), optional :: source
776 type(build_target_ptr), intent(in), optional :: deps(:)
777 type(build_target_ptr), intent(in), optional :: links(:)
778 type(error_t), intent(out), allocatable :: error
779
780 integer :: i
781
782 if (target%target_type /= type) then
783 call test_failed(error,'Unexpected target_type for target "'//target%output_file//'"')
784 return
785 end if
786
787 if (size(target%dependencies) /= n_depends) then
788 call test_failed(error,'Wrong number of dependencies for target "'//target%output_file//'"')
789 return
790 end if
791
792 if (present(deps)) then
793
794 do i=1,size(deps)
795
796 if (.not.(deps(i)%ptr .in. target%dependencies)) then
797 call test_failed(error,'Missing dependency ('//deps(i)%ptr%output_file//&
798 ') for target "'//target%output_file//'"')
799 return
800 end if
801
802 end do
803
804 end if
805
806 if (present(links)) then
807
808 do i=1,size(links)
809
810 if (.not.(links(i)%ptr%output_file .in. target%link_objects)) then
811 call test_failed(error,'Missing object ('//links(i)%ptr%output_file//&
812 ') for executable "'//target%output_file//'"')
813 return
814 end if
815
816 end do
817
818 if (size(links) > size(target%link_objects)) then
819
820 call test_failed(error,'There are missing link objects for target "'&
821 //target%output_file//'"')
822 return
823
824 elseif (size(links) < size(target%link_objects)) then
825
826 call test_failed(error,'There are more link objects than expected for target "'&
827 //target%output_file//'"')
828 return
829
830 end if
831
832 end if
833
834 if (present(source)) then
835
836 if (allocated(target%source)) then
837 if (target%source%file_name /= source%file_name) then
838 call test_failed(error,'Incorrect source ('//target%source%file_name//') for target "'//&
839 target%output_file//'"'//new_line('a')//' expected "'//source%file_name//'"')
840 return
841 end if
842
843 else
844 call test_failed(error,'Expecting source for target "'//target%output_file//'" but none found')
845 return
846 end if
847
848 else
849
850 if (allocated(target%source)) then
851 call test_failed(error,'Found source ('//target%source%file_name//') for target "'//&
852 target%output_file//'" but none expected')
853 return
854 end if
855
856 end if
857
858 end subroutine check_target
859
860 !> Check several module names whose name is valid and begins with the package name
861 subroutine check_valid_enforced_module_names(error)
862
863 !> Error handling
864 type(error_t), allocatable, intent(out) :: error
865
866 integer :: i,j
867 type(string_t) :: package,modules,prefix
868 logical, parameter :: enforcing(2) = [.false.,.true.]
869 character(*), parameter :: package_name = 'my_pkg'
870 character(len=80), parameter :: module_names(*) = [ character(len=80) :: &
871 'my_pkg__mod_1', &
872 'my_pkg___mod_1', &
873 'my_pkg____mod_1', &
874 'my_pkg', &
875 'my_pkg__1', &
876 'my_pkg__my_pkg' ]
877
878
879 package = string_t(package_name)
880 prefix = string_t("") ! Prefix not used
881
882 do i=1,size(module_names)
883
884 modules = string_t(module_names(i))
885
886 !> All these names are valid both with and without enforcing
887 do j=1,2
888 if (.not.is_valid_module_name(modules,package,prefix,enforcing(j))) then
889 call test_failed(error,'Valid dummy module name ['//modules%s//'] of package ['// &
890 package%s//'] unexpectedly fails naming check (enforcing='// &
891 merge('T','F',enforcing(j))//').')
892 return
893 endif
894 end do
895 end do
896
897 end subroutine check_valid_enforced_module_names
898
899 !> Check several module names whose name is valid and begins with the package name
900 subroutine check_valid_enforced_module_names_dashed(error)
901
902 !> Error handling
903 type(error_t), allocatable, intent(out) :: error
904
905 integer :: i,j
906 type(string_t) :: package,modules,prefix
907 logical, parameter :: enforcing(2) = [.false.,.true.]
908 character(*), parameter :: package_name = 'my-pkg'
909 character(len=80), parameter :: module_names(*) = [ character(len=80) :: &
910 'my_pkg__mod_1', &
911 'my_pkg___mod_1', &
912 'my_pkg____mod_1', &
913 'my_pkg', &
914 'my_pkg__1', &
915 'my_pkg__my_pkg' ]
916
917
918 package = string_t(package_name)
919 prefix = string_t("") ! Prefix not used
920
921 do i=1,size(module_names)
922
923 modules = string_t(trim(module_names(i)))
924
925 !> All these names are valid both with and without enforcing
926 do j=1,2
927 if (.not.is_valid_module_name(modules,package,prefix,enforcing(j))) then
928 call test_failed(error,'Valid dummy module name ['//modules%s//'] of package ['// &
929 package%s//'] unexpectedly fails naming check (enforcing='// &
930 merge('T','F',enforcing(j))//').')
931 return
932 endif
933 end do
934 end do
935
936 end subroutine check_valid_enforced_module_names_dashed
937
938 !> Check several module names whose name is invalid
939 subroutine check_invalid_enforced_module_names(error)
940
941 !> Error handling
942 type(error_t), allocatable, intent(out) :: error
943
944 integer :: i
945 type(string_t) :: package,modules,prefix
946 character(*), parameter :: package_name = 'my_pkg'
947 character(len=80), parameter :: module_names(*) = [ character(len=80) :: &
948 'mod_1', &
949 'my_pkmod_1', &
950 'my_mod_1', &
951 'pkg_mod_1', &
952 'y_pkg_mod_1', &
953 '_my_pkg_mod_1', &
954 'my_pkgmy_mod', &
955 'my_pkg_', &
956 'my_pkg__' ]
957
958
959 package = string_t(package_name)
960 prefix = string_t("") ! Prefix not used
961
962 !> All these cases should report an invalid name
963 do i=1,size(module_names)
964
965 modules = string_t(trim(module_names(i)))
966
967 if (is_valid_module_name(modules,package,prefix,.true.)) then
968 call test_failed(error,'Invalid dummy module name ['//modules%s//'] of package ['// &
969 package%s//'] unexpectedly passes naming check (enforcing=T).')
970 return
971 end if
972
973 end do
974
975 end subroutine check_invalid_enforced_module_names
976
977 !> Check module names whose name does not name the convention:
978 !> - Begin with a literal
979 !> - len(name)<=63
980 !> - Contains literals, numbers, or underscores only
981 subroutine check_invalid_module_names(error)
982
983 !> Error handling
984 type(error_t), allocatable, intent(out) :: error
985
986 integer :: i
987 type(string_t) :: modules,package,prefix
988
989 !> Examples taken from Metcalf/Reid/Cohen
990 character(len=80), parameter :: module_names(*) = [ character(len=80) :: &
991 '1a', &
992 'a thing', &
993 '$sign', &
994 '_begin_with_underscore', &
995 'contains-dashes', &
996 'and/other?symbols@2' ]
997
998 package = string_t("")
999 prefix = string_t("") ! Prefix not used
1000
1001 !> All these cases should report an invalid name
1002 do i=1,size(module_names)
1003
1004 modules = string_t(module_names(i))
1005
1006 if (is_valid_module_name(modules,package,prefix,.false.)) then
1007 call test_failed(error,'Invalid Fortran module name ['//modules%s//'] ' &
1008 //' unexpectedly passes naming check.')
1009 return
1010 end if
1011
1012 end do
1013
1014 end subroutine check_invalid_module_names
1015
1016 !> Check several module prefixes that are valid
1017 subroutine check_valid_custom_prefix(error)
1018
1019 !> Error handling
1020 type(error_t), allocatable, intent(out) :: error
1021
1022 integer :: i
1023 type(string_t) :: prefix
1024 character(len=80), parameter :: valid_prefixes(*) = [ character(len=80) :: &
1025 'm', &
1026 'mp', &
1027 'mypkg', &
1028 'mypkg123', &
1029 'mypackage', &
1030 'tomlf' ]
1031
1032 character(len=80), parameter :: invalid_prefixes(*) = [ character(len=80) :: &
1033 'm_', &
1034 'm-p', &
1035 'my_pkg', &
1036 'my-pkg123', &
1037 'my package', &
1038 'toml-f', &
1039 '123pkg', &
1040 'mypkg_' ]
1041 ! All valid
1042 do i=1,size(valid_prefixes)
1043
1044 prefix = string_t(valid_prefixes(i))
1045
1046 !> All these names are valid both with and without enforcing
1047 if (.not.is_valid_module_prefix(prefix)) then
1048 call test_failed(error,'Valid dummy module prefix ['//prefix%s//'] '//&
1049 ' unexpectedly fails naming check.')
1050 return
1051 endif
1052 end do
1053
1054 ! All invalid
1055 do i=1,size(invalid_prefixes)
1056
1057 prefix = string_t(invalid_prefixes(i))
1058
1059 !> All these names are valid both with and without enforcing
1060 if (is_valid_module_prefix(prefix)) then
1061 call test_failed(error,'Invalid dummy module prefix ['//prefix%s//'] '//&
1062 ' unexpectedly passed naming check.')
1063 return
1064 endif
1065 end do
1066
1067 end subroutine check_valid_custom_prefix
1068
1069 !> Check several module prefixes that are valid
1070 subroutine check_custom_prefixed_modules(error)
1071
1072 !> Error handling
1073 type(error_t), allocatable, intent(out) :: error
1074
1075 integer :: i
1076 type(string_t) :: prefix,modules,package
1077 character(len=*), parameter :: custom_prefix = 'mp123'
1078 character(len=*), parameter :: package_name = 'my-package'
1079
1080 character(len=80), parameter :: module_names(*) = [ character(len=80) :: &
1081 'mp123', &
1082 'mp123_utils', &
1083 'mp123__utils', &
1084 'mp123_test', &
1085 'mp123_my_module_name',&
1086 'my_package__utils',&
1087 'my_package',&
1088 'my_package__123']
1089
1090 character(len=80), parameter :: invalid_names(*) = [ character(len=80) :: &
1091 'utils', &
1092 'mp_123_utils', &
1093 'mypackage__utils', &
1094 'my_package_utils', &
1095 'my_package_test']
1096
1097 prefix = string_t(custom_prefix)
1098 package = string_t(package_name)
1099
1100 ! All valid
1101 do i=1,size(module_names)
1102
1103 modules = string_t(module_names(i))
1104
1105 !> All these names are valid both with and without enforcing
1106 if (.not.is_valid_module_name(modules,package,prefix,.true.)) then
1107 call test_failed(error,'Valid prefixed module ['//modules%s//'] ' //&
1108 ' from package ['//package%s//'] with prefix ['//&
1109 prefix%s//'] unexpectedly fails naming check.')
1110 return
1111 endif
1112 end do
1113
1114 ! All invalid
1115 do i=1,size(invalid_names)
1116
1117 modules = string_t(invalid_names(i))
1118
1119 !> All these names are valid both with and without enforcing
1120 if (is_valid_module_name(modules,package,prefix,.true.)) then
1121 call test_failed(error,'Invalid prefixed module ['//modules%s//'] ' //&
1122 ' from package ['//package%s//'] with prefix ['//&
1123 prefix%s//'] unexpectedly passed naming check.')
1124 return
1125 endif
1126 end do
1127
1128 end subroutine check_custom_prefixed_modules
1129
1130 !> Helper to check if a build target is in a list of build_target_ptr
1131 logical function target_in(needle,haystack)
1132 type(build_target_t), intent(in), target :: needle
1133 type(build_target_ptr), intent(in) :: haystack(:)
1134
1135 integer :: i
1136
1137 target_in = .false.
1138 do i=1,size(haystack)
1139
1140 if (associated(haystack(i)%ptr,needle)) then
1141 target_in = .true.
1142 return
1143 end if
1144
1145 end do
1146
1147 end function target_in
1148
1149
1150 end module test_module_dependencies