Debian Codegraph @

test/fpm_test/test_settings.f90

raw
1 module test_settings
2 use testsuite, only: new_unittest, unittest_t, error_t, test_failed
3 use fpm_settings, only: fpm_global_settings, get_global_settings, get_registry_settings, official_registry_base_url
4 use fpm_filesystem, only: is_dir, join_path, mkdir, filewrite, os_delete_dir, exists, get_local_prefix
5 use fpm_environment, only: os_is_unix
6 use tomlf, only: toml_table, new_table
7 use fpm_toml, only: add_table, set_value
8 use fpm_os, only: get_absolute_path, get_current_directory
9
10 implicit none
11 private
12 public :: collect_settings
13
14 character(len=*), parameter :: tmp_folder = 'tmp'
15 character(len=*), parameter :: config_file_name = 'config.toml'
16
17 contains
18
19 !> Collect unit tests.
20 subroutine collect_settings(tests)
21
22 !> Unit tests to collect.
23 type(unittest_t), allocatable, intent(out) :: tests(:)
24
25 tests = [ &
26 & new_unittest('no-folder', no_folder, should_fail=.true.), &
27 & new_unittest('no-file', no_file, should_fail=.true.), &
28 & new_unittest('empty-file', empty_file), &
29 & new_unittest('default-config-settings', default_config_settings), &
30 & new_unittest('error-reading-table', error_reading_table, should_fail=.true.), &
31 & new_unittest('empty-registry-table', empty_registry_table), &
32 & new_unittest('invalid-key', invalid_key, should_fail=.true.), &
33 & new_unittest('invalid-type', invalid_type, should_fail=.true.), &
34 & new_unittest('has-non-existent-path-to-registry', has_non_existent_path_to_registry, should_fail=.true.), &
35 & new_unittest('has-existent-path-to-registry', has_existent_path_to_registry), &
36 & new_unittest('absolute-path-to-registry', absolute_path_to_registry), &
37 & new_unittest('relative-path-to-registry', relative_path_to_registry), &
38 & new_unittest('relative-path-to-registry-file-read', relative_path_to_registry_file_read), &
39 & new_unittest('canonical-path-to-registry', canonical_path_to_registry), &
40 & new_unittest('has-url-to-registry', has_url_to_registry), &
41 & new_unittest('has-both-path-and-url-to-registry', has_both_path_and_url_to_registry, should_fail=.true.), &
42 & new_unittest('has-both-path-and-cache-path', has_both_path_and_cache_path, should_fail=.true.), &
43 & new_unittest('abs-cache-path-no-dir', abs_cache_path_no_dir), &
44 & new_unittest('abs-cache-path-has-dir', abs_cache_path_has_dir), &
45 & new_unittest('rel-cache-path-no-dir', rel_cache_path_no_dir), &
46 & new_unittest('rel-cache-path-has-dir', rel_cache_path_has_dir) &
47 ]
48
49 end subroutine collect_settings
50
51 subroutine delete_tmp_folder
52 if (is_dir(tmp_folder)) call os_delete_dir(os_is_unix(), tmp_folder)
53 end subroutine
54
55 subroutine setup_global_settings(global_settings, error)
56 type(fpm_global_settings), intent(out) :: global_settings
57 type(error_t), allocatable, intent(out) :: error
58
59 character(:), allocatable :: cwd
60
61 call get_current_directory(cwd, error)
62 if (allocated(error)) return
63
64 global_settings%path_to_config_folder = join_path(cwd, tmp_folder)
65 global_settings%config_file_name = config_file_name
66 end subroutine
67
68 !> Throw error when custom path to config file was entered but no folder exists.
69 subroutine no_folder(error)
70 type(error_t), allocatable, intent(out) :: error
71 type(fpm_global_settings) :: global_settings
72
73 call delete_tmp_folder
74
75 call setup_global_settings(global_settings, error)
76 if (allocated(error)) return
77
78 call get_global_settings(global_settings, error)
79 end subroutine
80
81 !> Throw error when custom path to config file was entered but no file exists.
82 subroutine no_file(error)
83 type(error_t), allocatable, intent(out) :: error
84 type(fpm_global_settings) :: global_settings
85
86 call delete_tmp_folder
87 call mkdir(tmp_folder)
88
89 call setup_global_settings(global_settings, error)
90 if (allocated(error)) return
91
92 call get_global_settings(global_settings, error)
93 end subroutine
94
95 !> No custom path and config file specified, use default path and file name.
96 subroutine default_config_settings(error)
97 type(error_t), allocatable, intent(out) :: error
98 type(fpm_global_settings) :: global_settings
99
100 character(:), allocatable :: default_path
101
102 call delete_tmp_folder
103
104 call get_global_settings(global_settings, error)
105 if (allocated(error)) return
106
107 if (os_is_unix()) then
108 default_path = join_path(get_local_prefix(), 'share', 'fpm')
109 else
110 default_path = join_path(get_local_prefix(), 'fpm')
111 end if
112
113 if (global_settings%path_to_config_folder /= default_path) then
114 call test_failed(error, "Path to config folder not set correctly :'"//global_settings%config_file_name//"'")
115 return
116 end if
117
118 if (global_settings%config_file_name /= 'config.toml') then
119 call test_failed(error, "Config file name not set correctly :'"//global_settings%config_file_name//"'")
120 return
121 end if
122 end subroutine
123
124 !> Config file exists and the path to that file is set.
125 subroutine empty_file(error)
126 type(error_t), allocatable, intent(out) :: error
127 type(fpm_global_settings) :: global_settings
128
129 character(:), allocatable :: cwd
130
131 call delete_tmp_folder
132 call mkdir(tmp_folder)
133
134 call filewrite(join_path(tmp_folder, config_file_name), [''])
135
136 call setup_global_settings(global_settings, error)
137 if (allocated(error)) return
138
139 call get_global_settings(global_settings, error)
140
141 call delete_tmp_folder
142
143 if (allocated(error)) return
144
145 call get_current_directory(cwd, error)
146 if (allocated(error)) return
147
148 if (global_settings%path_to_config_folder /= join_path(cwd, tmp_folder)) then
149 call test_failed(error, "global_settings%path_to_config_folder not set correctly :'" &
150 & //global_settings%path_to_config_folder//"'"); return
151 end if
152
153 if (.not. allocated(global_settings%registry_settings)) then
154 call test_failed(error, 'global_settings%registry_settings not be allocated'); return
155 end if
156
157 if (global_settings%registry_settings%url /= official_registry_base_url) then
158 call test_failed(error, 'Wrong default url'); return
159 end if
160
161 if (global_settings%registry_settings%cache_path /= join_path(global_settings%path_to_config_folder, &
162 & 'dependencies')) then
163 call test_failed(error, 'Wrong default cache_path'); return
164 end if
165 end subroutine
166
167 !> Invalid TOML file.
168 subroutine error_reading_table(error)
169 type(error_t), allocatable, intent(out) :: error
170 type(fpm_global_settings) :: global_settings
171
172 call delete_tmp_folder
173 call mkdir(tmp_folder)
174
175 call filewrite(join_path(tmp_folder, config_file_name), ['['])
176
177 call setup_global_settings(global_settings, error)
178 if (allocated(error)) return
179
180 call get_global_settings(global_settings, error)
181
182 call delete_tmp_folder
183
184 if (allocated(global_settings%registry_settings)) then
185 call test_failed(error, 'Registry settings should not be allocated'); return
186 end if
187 end subroutine
188
189 subroutine empty_registry_table(error)
190 type(error_t), allocatable, intent(out) :: error
191 type(fpm_global_settings) :: global_settings
192 type(toml_table) :: table
193 type(toml_table), pointer :: child
194
195 call new_table(table)
196 call add_table(table, 'registry', child)
197
198 call get_registry_settings(child, global_settings, error)
199 if (allocated(error)) return
200
201 if (.not. allocated(global_settings%registry_settings)) then
202 call test_failed(error, 'Registry settings not allocated'); return
203 end if
204
205 if (allocated(global_settings%registry_settings%path)) then
206 call test_failed(error, "Path shouldn't be allocated"); return
207 end if
208
209 if (global_settings%registry_settings%url /= official_registry_base_url) then
210 call test_failed(error, "Url not be allocated"); return
211 end if
212 end subroutine
213
214 subroutine invalid_key(error)
215 type(error_t), allocatable, intent(out) :: error
216 type(fpm_global_settings) :: global_settings
217 type(toml_table) :: table
218 type(toml_table), pointer :: child
219
220 call new_table(table)
221 call add_table(table, 'registry', child)
222 call set_value(child, 'invalid_key', 'abc')
223
224 call get_registry_settings(child, global_settings, error)
225 end subroutine
226
227 subroutine invalid_type(error)
228 type(error_t), allocatable, intent(out) :: error
229 type(fpm_global_settings) :: global_settings
230 type(toml_table) :: table
231 type(toml_table), pointer :: child
232
233 call new_table(table)
234 call add_table(table, 'registry', child)
235 call set_value(child, 'path', 42)
236
237 call get_registry_settings(child, global_settings, error)
238 end subroutine
239
240 subroutine has_non_existent_path_to_registry(error)
241 type(error_t), allocatable, intent(out) :: error
242 type(fpm_global_settings) :: global_settings
243 type(toml_table) :: table
244 type(toml_table), pointer :: child
245
246 call new_table(table)
247 call add_table(table, 'registry', child)
248 call set_value(child, 'path', 'nonexistent_path')
249
250 call get_registry_settings(child, global_settings, error)
251 end subroutine
252
253 subroutine has_existent_path_to_registry(error)
254 type(error_t), allocatable, intent(out) :: error
255 type(fpm_global_settings) :: global_settings
256 type(toml_table) :: table
257 type(toml_table), pointer :: child
258 character(:), allocatable :: cwd
259
260 call delete_tmp_folder
261 call mkdir(tmp_folder)
262
263 call setup_global_settings(global_settings, error)
264 if (allocated(error)) return
265
266 call new_table(table)
267 call add_table(table, 'registry', child)
268 call set_value(child, 'path', '.')
269
270 call get_registry_settings(child, global_settings, error)
271
272 if (.not. allocated(global_settings%registry_settings%path)) then
273 call test_failed(error, 'Path not allocated')
274 return
275 end if
276
277 call delete_tmp_folder
278
279 call get_current_directory(cwd, error)
280 if (allocated(error)) return
281
282 if (global_settings%registry_settings%path /= join_path(cwd, tmp_folder)) then
283 call test_failed(error, "Path not set correctly: '"//global_settings%registry_settings%path//"'")
284 return
285 end if
286
287 if (allocated(global_settings%registry_settings%url)) then
288 call test_failed(error, "Url shouldn't be allocated")
289 return
290 end if
291
292 end subroutine
293
294 subroutine absolute_path_to_registry(error)
295 type(error_t), allocatable, intent(out) :: error
296 type(fpm_global_settings) :: global_settings
297 character(len=:), allocatable :: abs_path
298 type(toml_table) :: table
299 type(toml_table), pointer :: child
300
301 call delete_tmp_folder
302 call mkdir(tmp_folder)
303
304 call get_absolute_path(tmp_folder, abs_path, error)
305 if (allocated(error)) return
306
307 call setup_global_settings(global_settings, error)
308 if (allocated(error)) return
309
310 call new_table(table)
311 call add_table(table, 'registry', child)
312 call set_value(child, 'path', abs_path)
313
314 call get_registry_settings(child, global_settings, error)
315
316 call delete_tmp_folder
317
318 if (allocated(error)) return
319
320 if (.not. allocated(global_settings%registry_settings)) then
321 call test_failed(error, 'Registry settings not allocated')
322 return
323 end if
324
325 if (.not. allocated(global_settings%registry_settings%path)) then
326 call test_failed(error, 'Path not allocated')
327 return
328 end if
329
330 if (global_settings%registry_settings%path /= abs_path) then
331 call test_failed(error, "Path not set correctly: '"//global_settings%registry_settings%path//"'")
332 return
333 end if
334 end subroutine
335
336 subroutine relative_path_to_registry(error)
337 type(error_t), allocatable, intent(out) :: error
338 type(fpm_global_settings) :: global_settings
339 character(len=:), allocatable :: abs_path
340 type(toml_table) :: table
341 type(toml_table), pointer :: child
342
343 call delete_tmp_folder
344 call mkdir(join_path(tmp_folder, 'abc'))
345
346 call setup_global_settings(global_settings, error)
347 if (allocated(error)) return
348
349 call new_table(table)
350 call add_table(table, 'registry', child)
351 call set_value(child, 'path', 'abc')
352
353 call get_registry_settings(child, global_settings, error)
354
355 call get_absolute_path(tmp_folder, abs_path, error)
356
357 call delete_tmp_folder
358
359 if (allocated(error)) return
360
361 if (.not. allocated(global_settings%registry_settings)) then
362 call test_failed(error, 'Registry settings not allocated')
363 return
364 end if
365
366 if (global_settings%registry_settings%path /= join_path(abs_path, 'abc')) then
367 call test_failed(error, "Path not set correctly: '"//global_settings%registry_settings%path//"'")
368 return
369 end if
370 end subroutine
371
372 ! Test that the registry path is set correctly when the path is written to and read from a config file.
373 subroutine relative_path_to_registry_file_read(error)
374 type(error_t), allocatable, intent(out) :: error
375 type(fpm_global_settings) :: global_settings
376 character(len=:), allocatable :: abs_path
377
378 call delete_tmp_folder
379 call mkdir(join_path(tmp_folder, 'abc'))
380
381 call filewrite(join_path(tmp_folder, config_file_name), ['[registry]', 'path="abc"'])
382
383 call setup_global_settings(global_settings, error)
384 if (allocated(error)) return
385
386 call get_global_settings(global_settings, error)
387
388 call get_absolute_path(tmp_folder, abs_path, error)
389
390 call delete_tmp_folder
391
392 if (allocated(error)) return
393
394 if (.not. allocated(global_settings%registry_settings)) then
395 call test_failed(error, 'Registry settings not allocated')
396 return
397 end if
398
399 if (global_settings%registry_settings%path /= join_path(abs_path, 'abc')) then
400 call test_failed(error, "Path not set correctly: '"//global_settings%registry_settings%path//"'")
401 return
402 end if
403 end subroutine
404
405 subroutine canonical_path_to_registry(error)
406 type(error_t), allocatable, intent(out) :: error
407 type(fpm_global_settings) :: global_settings
408 character(len=:), allocatable :: abs_path
409 type(toml_table) :: table
410 type(toml_table), pointer :: child
411
412 call delete_tmp_folder
413 call mkdir(tmp_folder)
414
415 call setup_global_settings(global_settings, error)
416 if (allocated(error)) return
417
418 call new_table(table)
419 call add_table(table, 'registry', child)
420 call set_value(child, 'path', join_path('..', tmp_folder))
421
422 call get_registry_settings(child, global_settings, error)
423
424 call get_absolute_path(tmp_folder, abs_path, error)
425
426 call delete_tmp_folder
427
428 if (allocated(error)) return
429
430 if (.not. allocated(global_settings%registry_settings)) then
431 call test_failed(error, 'Registry settings not allocated')
432 return
433 end if
434
435 if (global_settings%registry_settings%path /= abs_path) then
436 call test_failed(error, "Path not set correctly: '"//global_settings%registry_settings%path//"'")
437 return
438 end if
439 end subroutine
440
441 subroutine has_url_to_registry(error)
442 type(error_t), allocatable, intent(out) :: error
443 type(fpm_global_settings) :: global_settings
444 type(toml_table) :: table
445 type(toml_table), pointer :: child
446
447 call delete_tmp_folder
448 call mkdir(tmp_folder)
449
450 call setup_global_settings(global_settings, error)
451 if (allocated(error)) return
452
453 call new_table(table)
454 call add_table(table, 'registry', child)
455 call set_value(child, 'url', 'http')
456
457 call get_registry_settings(child, global_settings, error)
458
459 call delete_tmp_folder
460
461 if (.not. allocated(global_settings%registry_settings)) then
462 call test_failed(error, 'Registry settings not allocated')
463 return
464 end if
465
466 if (allocated(global_settings%registry_settings%path)) then
467 call test_failed(error, "Path shouldn't be allocated: '" &
468 & //global_settings%registry_settings%path//"'")
469 return
470 end if
471
472 if (.not. allocated(global_settings%registry_settings%url)) then
473 call test_failed(error, 'Url not allocated')
474 return
475 end if
476
477 if (global_settings%registry_settings%url /= 'http') then
478 call test_failed(error, 'Failed to parse url')
479 return
480 end if
481 end subroutine
482
483 subroutine has_both_path_and_url_to_registry(error)
484 type(error_t), allocatable, intent(out) :: error
485 type(fpm_global_settings) :: global_settings
486 type(toml_table) :: table
487 type(toml_table), pointer :: child
488
489 call delete_tmp_folder
490 call mkdir(tmp_folder)
491
492 call setup_global_settings(global_settings, error)
493 if (allocated(error)) return
494
495 call new_table(table)
496 call add_table(table, 'registry', child)
497 call set_value(child, 'path', '.')
498 call set_value(child, 'url', 'http')
499
500 call get_registry_settings(child, global_settings, error)
501
502 call delete_tmp_folder
503 end subroutine
504
505 subroutine has_both_path_and_cache_path(error)
506 type(error_t), allocatable, intent(out) :: error
507 type(fpm_global_settings) :: global_settings
508 type(toml_table) :: table
509 type(toml_table), pointer :: child
510
511 call delete_tmp_folder
512 call mkdir(tmp_folder)
513
514 call setup_global_settings(global_settings, error)
515 if (allocated(error)) return
516
517 call new_table(table)
518 call add_table(table, 'registry', child)
519 call set_value(child, 'path', '.')
520 call set_value(child, 'cache_path', 'cache')
521
522 call get_registry_settings(child, global_settings, error)
523
524 call delete_tmp_folder
525 end subroutine
526
527 ! Custom cache location defined via absolute path but directory doesn't exist. Create it.
528 subroutine abs_cache_path_no_dir(error)
529 type(error_t), allocatable, intent(out) :: error
530 type(fpm_global_settings) :: global_settings
531 character(len=:), allocatable :: abs_path, abs_path_to_cache
532 type(toml_table) :: table
533 type(toml_table), pointer :: child
534
535 call delete_tmp_folder
536 call mkdir(tmp_folder)
537
538 call get_absolute_path(tmp_folder, abs_path, error)
539 if (allocated(error)) return
540
541 abs_path_to_cache = join_path(abs_path, 'cache')
542
543 call setup_global_settings(global_settings, error)
544 if (allocated(error)) return
545
546 call new_table(table)
547 call add_table(table, 'registry', child)
548 call set_value(child, 'cache_path', abs_path_to_cache)
549
550 call get_registry_settings(child, global_settings, error)
551
552 if (.not. exists(abs_path_to_cache)) then
553 call test_failed(error, "Cache directory '"//abs_path_to_cache//"' not created.")
554 return
555 end if
556
557 if (global_settings%registry_settings%cache_path /= abs_path_to_cache) then
558 call test_failed(error, "Cache path '"//abs_path_to_cache//"' not registered.")
559 return
560 end if
561
562 call delete_tmp_folder
563 end subroutine
564
565 ! Custom cache location defined via absolute path for existing directory.
566 subroutine abs_cache_path_has_dir(error)
567 type(error_t), allocatable, intent(out) :: error
568 type(fpm_global_settings) :: global_settings
569 character(len=:), allocatable :: abs_path
570 type(toml_table) :: table
571 type(toml_table), pointer :: child
572
573 call delete_tmp_folder
574 call mkdir(join_path(tmp_folder, 'cache'))
575
576 call get_absolute_path(join_path(tmp_folder, 'cache'), abs_path, error)
577 if (allocated(error)) return
578
579 call setup_global_settings(global_settings, error)
580 if (allocated(error)) return
581
582 call new_table(table)
583 call add_table(table, 'registry', child)
584 call set_value(child, 'cache_path', abs_path)
585
586 call get_registry_settings(child, global_settings, error)
587
588 if (.not. exists(abs_path)) then
589 call test_failed(error, "Cache directory '"//abs_path//"' not created.")
590 return
591 end if
592
593 if (global_settings%registry_settings%cache_path /= abs_path) then
594 call test_failed(error, "Cache path '"//abs_path//"' not registered.")
595 return
596 end if
597
598 call delete_tmp_folder
599 end subroutine
600
601 ! Custom cache location defined via relative path but directory doesn't exist. Create it.
602 subroutine rel_cache_path_no_dir(error)
603 type(error_t), allocatable, intent(out) :: error
604 type(fpm_global_settings) :: global_settings
605 character(:), allocatable :: cache_path, abs_path
606 type(toml_table) :: table
607 type(toml_table), pointer :: child
608
609 call delete_tmp_folder
610 call mkdir(tmp_folder)
611
612 call setup_global_settings(global_settings, error)
613 if (allocated(error)) return
614
615 call new_table(table)
616 call add_table(table, 'registry', child)
617 call set_value(child, 'cache_path', 'cache')
618
619 call get_registry_settings(child, global_settings, error)
620
621 cache_path = join_path(tmp_folder, 'cache')
622
623 if (.not. exists(cache_path)) then
624 call test_failed(error, "Cache directory '"//cache_path//"' not created.")
625 return
626 end if
627
628 call get_absolute_path(cache_path, abs_path, error)
629 if (allocated(error)) return
630
631 if (global_settings%registry_settings%cache_path /= abs_path) then
632 call test_failed(error, "Cache path '"//cache_path//"' not registered.")
633 return
634 end if
635
636 call delete_tmp_folder
637 end subroutine
638
639 ! Custom cache location defined via relative path for existing directory.
640 subroutine rel_cache_path_has_dir(error)
641 type(error_t), allocatable, intent(out) :: error
642 type(fpm_global_settings) :: global_settings
643 character(len=:), allocatable :: cache_path, abs_path
644 type(toml_table) :: table
645 type(toml_table), pointer :: child
646
647 call delete_tmp_folder
648
649 cache_path = join_path(tmp_folder, 'cache')
650 call mkdir(cache_path)
651
652 call setup_global_settings(global_settings, error)
653 if (allocated(error)) return
654
655 call new_table(table)
656 call add_table(table, 'registry', child)
657 call set_value(child, 'cache_path', 'cache')
658
659 call get_registry_settings(child, global_settings, error)
660
661 if (.not. exists(cache_path)) then
662 call test_failed(error, "Cache directory '"//cache_path//"' not created.")
663 return
664 end if
665
666 call get_absolute_path(cache_path, abs_path, error)
667 if (allocated(error)) return
668
669 if (global_settings%registry_settings%cache_path /= abs_path) then
670 call test_failed(error, "Cache path '"//cache_path//"' not registered.")
671 return
672 end if
673
674 call delete_tmp_folder
675 end subroutine
676
677 end module test_settings