Debian Codegraph @

test/fpm_test/test_installer.f90

raw
1 !> Define tests for the `fpm_installer` module
2 !>
3 !> The tests here setup a mock environment to allow testing for Unix and Windows
4 !> platforms at the same time.
5 module test_installer
6 use testsuite, only : new_unittest, unittest_t, error_t, test_failed, &
7 & check_string
8 use fpm_environment, only : OS_WINDOWS, OS_LINUX
9 use fpm_filesystem, only : join_path
10 use fpm_installer
11 use fpm_targets, only: build_target_ptr, add_target, FPM_TARGET_ARCHIVE, &
12 FPM_TARGET_SHARED
13 implicit none
14 private
15
16 public :: collect_installer
17
18
19 type, extends(installer_t) :: mock_installer_t
20 character(len=:), allocatable :: expected_dir
21 character(len=:), allocatable :: expected_run
22 contains
23 procedure :: make_dir
24 procedure :: run
25 end type mock_installer_t
26
27 contains
28
29 !> Collect all exported unit tests
30 subroutine collect_installer(testsuite)
31 !> Collection of tests
32 type(unittest_t), allocatable, intent(out) :: testsuite(:)
33
34 testsuite = [ &
35 & new_unittest("install-lib", test_install_lib), &
36 & new_unittest("install-pkgconfig", test_install_pkgconfig), &
37 & new_unittest("install-sitepackages", test_install_sitepackages), &
38 & new_unittest("install-mod", test_install_mod), &
39 & new_unittest("install-module-custom", test_install_module_custom), &
40 & new_unittest("install-exe-unix", test_install_exe_unix), &
41 & new_unittest("install-exe-win", test_install_exe_win), &
42 & new_unittest("install-test-unix", test_install_tests_unix), &
43 & new_unittest("install-test-win", test_install_tests_win), &
44 & new_unittest("install-shared-lib-unix", test_install_shared_library_unix)]
45
46 end subroutine collect_installer
47
48 subroutine test_install_exe_unix(error)
49 !> Error handling
50 type(error_t), allocatable, intent(out) :: error
51
52 type(mock_installer_t) :: mock
53 type(installer_t) :: installer
54
55 call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
56 mock%installer_t = installer
57 mock%os = OS_LINUX
58 mock%expected_dir = "PREFIX/bin"
59 mock%expected_run = 'mock "name" "'//mock%expected_dir//'"'
60
61 call mock%install_executable("name", error)
62
63 end subroutine test_install_exe_unix
64
65 subroutine test_install_exe_win(error)
66 !> Error handling
67 type(error_t), allocatable, intent(out) :: error
68
69 type(mock_installer_t) :: mock
70 type(installer_t) :: installer
71
72 call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
73 mock%installer_t = installer
74 mock%os = OS_WINDOWS
75 mock%expected_dir = "PREFIX\bin"
76 mock%expected_run = 'mock "name.exe" "'//mock%expected_dir//'"'
77
78 call mock%install_executable("name", error)
79
80 end subroutine test_install_exe_win
81
82 subroutine test_install_tests_unix(error)
83 !> Error handling
84 type(error_t), allocatable, intent(out) :: error
85
86 type(mock_installer_t) :: mock
87 type(installer_t) :: installer
88
89 call new_installer(installer, prefix="PREFIX", testdir="tdir", verbosity=0, copy="mock")
90 mock%installer_t = installer
91 mock%os = OS_LINUX
92 mock%expected_dir = "PREFIX/tdir"
93 mock%expected_run = 'mock "name" "'//mock%expected_dir//'"'
94
95 call mock%install_test("name", error)
96
97 end subroutine test_install_tests_unix
98
99 subroutine test_install_tests_win(error)
100 !> Error handling
101 type(error_t), allocatable, intent(out) :: error
102
103 type(mock_installer_t) :: mock
104 type(installer_t) :: installer
105
106 call new_installer(installer, prefix="PREFIX", testdir="tdir", verbosity=0, copy="mock")
107 mock%installer_t = installer
108 mock%os = OS_WINDOWS
109 mock%expected_dir = "PREFIX\tdir"
110 mock%expected_run = 'mock "name.exe" "'//mock%expected_dir//'"'
111
112 call mock%install_test("name", error)
113
114 end subroutine test_install_tests_win
115
116 subroutine test_install_lib(error)
117 !> Error handling
118 type(error_t), allocatable, intent(out) :: error
119
120 type(mock_installer_t) :: mock
121 type(installer_t) :: installer
122 type(build_target_ptr), allocatable :: targets(:)
123
124 call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
125 mock%installer_t = installer
126 mock%expected_dir = join_path("PREFIX", "lib")
127 mock%expected_run = 'mock "name" "'//join_path("PREFIX", "lib")//'"'
128
129 call add_target(targets,"name",FPM_TARGET_ARCHIVE,"name")
130
131 call mock%install_library(targets(1)%ptr, error)
132
133 deallocate(targets(1)%ptr)
134
135 end subroutine test_install_lib
136
137 subroutine test_install_pkgconfig(error)
138 !> Error handling
139 type(error_t), allocatable, intent(out) :: error
140
141 type(mock_installer_t) :: mock
142 type(installer_t) :: installer
143
144 call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
145 mock%installer_t = installer
146 mock%os = OS_WINDOWS
147 mock%expected_dir = "PREFIX\lib\pkgconfig"
148 mock%expected_run = 'mock "name" "'//mock%expected_dir//'"'
149
150 call mock%install("name", "lib/pkgconfig", error)
151
152 end subroutine test_install_pkgconfig
153
154 subroutine test_install_sitepackages(error)
155 !> Error handling
156 type(error_t), allocatable, intent(out) :: error
157
158 type(mock_installer_t) :: mock
159 type(installer_t) :: installer
160
161 call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
162 mock%installer_t = installer
163 mock%os = OS_LINUX
164 mock%expected_dir = "PREFIX/lib/python3.7/site-packages"
165 mock%expected_run = 'mock "name" "'//mock%expected_dir//'"'
166
167 call mock%install("name", join_path("lib", "python3.7", "site-packages"), &
168 error)
169
170 end subroutine test_install_sitepackages
171
172 subroutine test_install_mod(error)
173 !> Error handling
174 type(error_t), allocatable, intent(out) :: error
175
176 type(mock_installer_t) :: mock
177 type(installer_t) :: installer
178
179 call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
180 mock%installer_t = installer
181 mock%expected_dir = join_path("PREFIX", "include")
182 mock%expected_run = 'mock "name" "'//join_path("PREFIX", "include")//'"'
183
184 call mock%install_header("name", error)
185
186 end subroutine test_install_mod
187
188 subroutine test_install_module_custom(error)
189 !> Error handling
190 type(error_t), allocatable, intent(out) :: error
191
192 type(mock_installer_t) :: mock
193 type(installer_t) :: installer
194
195 call new_installer(installer, prefix="PREFIX", moduledir="custom_modules", verbosity=0, copy="mock")
196 mock%installer_t = installer
197 mock%expected_dir = join_path("PREFIX", "custom_modules")
198 mock%expected_run = 'mock "test_module.mod" "'//join_path("PREFIX", "custom_modules")//'"'
199
200 call mock%install_module("test_module.mod", error)
201
202 end subroutine test_install_module_custom
203
204 subroutine test_install_shared_library_unix(error)
205 !> Error handling
206 type(error_t), allocatable, intent(out) :: error
207
208 type(mock_installer_t) :: mock
209 type(installer_t) :: installer
210 character(len=*), parameter :: libname = "libname.so"
211 type(build_target_ptr), allocatable :: targets(:)
212
213 call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
214 mock%installer_t = installer
215 mock%expected_dir = join_path("PREFIX", "lib")
216 mock%expected_run = 'mock "'//libname//'" "'//mock%expected_dir//'"'
217
218 call add_target(targets,"name",FPM_TARGET_SHARED,libname)
219
220 call mock%install_library(targets(1)%ptr, error)
221
222 deallocate(targets(1)%ptr)
223
224 end subroutine test_install_shared_library_unix
225
226
227 !> Create a new directory in the prefix
228 subroutine make_dir(self, dir, error)
229 !> Instance of the installer
230 class(mock_installer_t), intent(inout) :: self
231 !> Directory to be created
232 character(len=*), intent(in) :: dir
233 !> Error handling
234 type(error_t), allocatable, intent(out) :: error
235
236 call check_string(error, self%expected_dir, dir, "dir")
237
238 end subroutine make_dir
239
240 !> Run an installation command
241 subroutine run(self, command, error)
242 !> Instance of the installer
243 class(mock_installer_t), intent(inout) :: self
244 !> Command to be launched
245 character(len=*), intent(in) :: command
246 !> Error handling
247 type(error_t), allocatable, intent(out) :: error
248
249 call check_string(error, self%expected_run, command, "run")
250 end subroutine run
251
252 end module test_installer