aboutsummaryrefslogtreecommitdiff
path: root/test/fpm_test/test_installer.f90
blob: 1235ba5bc208374f8f16edd59917d92b3861aee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
!> Define tests for the `fpm_installer` module
!>
!> The tests here setup a mock environment to allow testing for Unix and Windows
!> platforms at the same time.
module test_installer
    use testsuite, only : new_unittest, unittest_t, error_t, test_failed, &
        & check_string
    use fpm_environment, only : OS_WINDOWS, OS_LINUX
    use fpm_filesystem, only : join_path
    use fpm_installer
    implicit none
    private

    public :: collect_installer


    type, extends(installer_t) :: mock_installer_t
        character(len=:), allocatable :: expected_dir
        character(len=:), allocatable :: expected_run
    contains
        procedure :: make_dir
        procedure :: run
    end type mock_installer_t

contains

    !> Collect all exported unit tests
    subroutine collect_installer(testsuite)
        !> Collection of tests
        type(unittest_t), allocatable, intent(out) :: testsuite(:)

        testsuite = [ &
            & new_unittest("install-lib", test_install_lib), &
            & new_unittest("install-pkgconfig", test_install_pkgconfig), &
            & new_unittest("install-sitepackages", test_install_sitepackages), &
            & new_unittest("install-mod", test_install_mod), &
            & new_unittest("install-exe-unix", test_install_exe_unix), &
            & new_unittest("install-exe-win", test_install_exe_win)]

    end subroutine collect_installer

    subroutine test_install_exe_unix(error)
        !> Error handling
        type(error_t), allocatable, intent(out) :: error

        type(mock_installer_t) :: mock
        type(installer_t) :: installer

        call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
        mock%installer_t = installer
        mock%os = OS_LINUX
        mock%expected_dir = "PREFIX/bin"
        mock%expected_run = 'mock "name" "'//mock%expected_dir//'"'

        call mock%install_executable("name", error)

    end subroutine test_install_exe_unix

    subroutine test_install_exe_win(error)
        !> Error handling
        type(error_t), allocatable, intent(out) :: error

        type(mock_installer_t) :: mock
        type(installer_t) :: installer

        call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
        mock%installer_t = installer
        mock%os = OS_WINDOWS
        mock%expected_dir = "PREFIX\bin"
        mock%expected_run = 'mock "name.exe" "'//mock%expected_dir//'"'

        call mock%install_executable("name", error)

    end subroutine test_install_exe_win

    subroutine test_install_lib(error)
        !> Error handling
        type(error_t), allocatable, intent(out) :: error

        type(mock_installer_t) :: mock
        type(installer_t) :: installer

        call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
        mock%installer_t = installer
        mock%expected_dir = join_path("PREFIX", "lib")
        mock%expected_run = 'mock "name" "'//join_path("PREFIX", "lib")//'"'

        call mock%install_library("name", error)

    end subroutine test_install_lib

    subroutine test_install_pkgconfig(error)
        !> Error handling
        type(error_t), allocatable, intent(out) :: error

        type(mock_installer_t) :: mock
        type(installer_t) :: installer

        call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
        mock%installer_t = installer
        mock%os = OS_WINDOWS
        mock%expected_dir = "PREFIX\lib\pkgconfig"
        mock%expected_run = 'mock "name" "'//mock%expected_dir//'"'

        call mock%install("name", "lib/pkgconfig", error)

    end subroutine test_install_pkgconfig

    subroutine test_install_sitepackages(error)
        !> Error handling
        type(error_t), allocatable, intent(out) :: error

        type(mock_installer_t) :: mock
        type(installer_t) :: installer

        call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
        mock%installer_t = installer
        mock%os = OS_LINUX
        mock%expected_dir = "PREFIX/lib/python3.7/site-packages"
        mock%expected_run = 'mock "name" "'//mock%expected_dir//'"'

        call mock%install("name", join_path("lib", "python3.7", "site-packages"), &
            error)

    end subroutine test_install_sitepackages

    subroutine test_install_mod(error)
        !> Error handling
        type(error_t), allocatable, intent(out) :: error

        type(mock_installer_t) :: mock
        type(installer_t) :: installer

        call new_installer(installer, prefix="PREFIX", verbosity=0, copy="mock")
        mock%installer_t = installer
        mock%expected_dir = join_path("PREFIX", "include")
        mock%expected_run = 'mock "name" "'//join_path("PREFIX", "include")//'"'

        call mock%install_header("name", error)

    end subroutine test_install_mod

    !> Create a new directory in the prefix
    subroutine make_dir(self, dir, error)
        !> Instance of the installer
        class(mock_installer_t), intent(inout) :: self
        !> Directory to be created
        character(len=*), intent(in) :: dir
        !> Error handling
        type(error_t), allocatable, intent(out) :: error

        call check_string(error, self%expected_dir, dir, "dir")

    end subroutine make_dir

    !> Run an installation command
    subroutine run(self, command, error)
        !> Instance of the installer
        class(mock_installer_t), intent(inout) :: self
        !> Command to be launched
        character(len=*), intent(in) :: command
        !> Error handling
        type(error_t), allocatable, intent(out) :: error

        call check_string(error, self%expected_run, command, "run")
    end subroutine run

end module test_installer