aboutsummaryrefslogtreecommitdiff
path: root/test/fpm_test/test_package_dependencies.f90
blob: a3192ff2f968791ce5da23d2984f0e936d481d70 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
!> Define tests for the `fpm_dependency` module
module test_package_dependencies
    use fpm_filesystem, only: get_temp_filename
    use testsuite, only : new_unittest, unittest_t, error_t, test_failed
    use fpm_dependency
    use fpm_manifest
    use fpm_manifest_dependency
    use fpm_toml
    implicit none
    private

    public :: collect_package_dependencies

    type, extends(dependency_tree_t) :: mock_dependency_tree_t
    contains
        procedure :: resolve_dependency => resolve_dependency_once
    end type mock_dependency_tree_t


contains


    !> Collect all exported unit tests
    subroutine collect_package_dependencies(testsuite)

        !> Collection of tests
        type(unittest_t), allocatable, intent(out) :: testsuite(:)

        testsuite = [ &
            & new_unittest("cache-load-dump", test_cache_load_dump), &
            & new_unittest("cache-dump-load", test_cache_dump_load), &
            & new_unittest("status-after-load", test_status), &
            & new_unittest("add-dependencies", test_add_dependencies)]

    end subroutine collect_package_dependencies


    !> Round trip of the dependency cache from a dependency tree to a TOML document
    !> to a dependency tree
    subroutine test_cache_dump_load(error)

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

        type(dependency_tree_t) :: deps
        type(dependency_config_t) :: dep
        integer :: unit

        call new_dependency_tree(deps)
        call resize(deps%dep, 5)
        deps%ndep = 3
        dep%name = "dep1"
        dep%path = "fpm-tmp1-dir"
        call new_dependency_node(deps%dep(1), dep, proj_dir=dep%path)
        dep%name = "dep2"
        dep%path = "fpm-tmp2-dir"
        call new_dependency_node(deps%dep(2), dep, proj_dir=dep%path)
        dep%name = "dep3"
        dep%path = "fpm-tmp3-dir"
        call new_dependency_node(deps%dep(3), dep, proj_dir=dep%path)

        open(newunit=unit, status='scratch')
        call deps%dump(unit, error)
        if (.not.allocated(error)) then
            rewind(unit)

            call new_dependency_tree(deps)
            call resize(deps%dep, 2)
            call deps%load(unit, error)
            close(unit)
        end if
        if (allocated(error)) return

        if (deps%ndep /= 3) then
            call test_failed(error, "Expected three dependencies in loaded cache")
            return
        end if

    end subroutine test_cache_dump_load


    !> Round trip of the dependency cache from a TOML data structure to
    !> a dependency tree to a TOML data structure
    subroutine test_cache_load_dump(error)

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

        type(toml_table) :: table
        type(toml_table), pointer :: ptr
        type(toml_key), allocatable :: list(:)
        type(dependency_tree_t) :: deps

        table = toml_table()
        call add_table(table, "dep1", ptr)
        call set_value(ptr, "version", "1.1.0")
        call set_value(ptr, "proj-dir", "fpm-tmp1-dir")
        call add_table(table, "dep2", ptr)
        call set_value(ptr, "version", "0.55.3")
        call set_value(ptr, "proj-dir", "fpm-tmp2-dir")
        call set_value(ptr, "git", "https://github.com/fortran-lang/dep2")
        call add_table(table, "dep3", ptr)
        call set_value(ptr, "version", "20.1.15")
        call set_value(ptr, "proj-dir", "fpm-tmp3-dir")
        call set_value(ptr, "git", "https://gitlab.com/fortran-lang/dep3")
        call set_value(ptr, "rev", "c0ffee")
        call add_table(table, "dep4", ptr)
        call set_value(ptr, "proj-dir", "fpm-tmp4-dir")

        call new_dependency_tree(deps)
        call deps%load(table, error)
        if (allocated(error)) return

        if (deps%ndep /= 4) then
            call test_failed(error, "Expected four dependencies in loaded cache")
            return
        end if

        call table%destroy
        table = toml_table()

        call deps%dump(table, error)
        if (allocated(error)) return

        call table%get_keys(list)

        if (size(list) /= 4) then
            call test_failed(error, "Expected four dependencies in dumped cache")
            return
        end if

    end subroutine test_cache_load_dump


    subroutine test_status(error)

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

        type(toml_table) :: table
        type(toml_table), pointer :: ptr
        type(dependency_tree_t) :: deps

        table = toml_table()
        call add_table(table, "dep1", ptr)
        call set_value(ptr, "version", "1.1.0")
        call set_value(ptr, "proj-dir", "fpm-tmp1-dir")
        call add_table(table, "dep2", ptr)
        call set_value(ptr, "version", "0.55.3")
        call set_value(ptr, "proj-dir", "fpm-tmp2-dir")
        call set_value(ptr, "git", "https://github.com/fortran-lang/dep2")

        call new_dependency_tree(deps)
        call deps%load(table, error)
        if (allocated(error)) return

        if (deps%finished()) then
            call test_failed(error, "Newly initialized dependency tree cannot be reolved")
            return
        end if

    end subroutine test_status


    subroutine test_add_dependencies(error)

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

        type(toml_table) :: table
        type(toml_table), pointer :: ptr
        type(mock_dependency_tree_t) :: deps
        type(dependency_config_t), allocatable :: nodes(:)

        table = toml_table()
        call add_table(table, "sub1", ptr)
        call set_value(ptr, "path", "external")
        call add_table(table, "lin2", ptr)
        call set_value(ptr, "git", "https://github.com/fortran-lang/lin2")
        call add_table(table, "pkg3", ptr)
        call set_value(ptr, "git", "https://gitlab.com/fortran-lang/pkg3")
        call set_value(ptr, "rev", "c0ffee")
        call add_table(table, "proj4", ptr)
        call set_value(ptr, "path", "vendor")

        call new_dependencies(nodes, table, error)
        if (allocated(error)) return

        call new_dependency_tree(deps%dependency_tree_t)
        call deps%add(nodes, error)
        if (allocated(error)) return

        if (deps%finished()) then
            call test_failed(error, "Newly added nodes cannot be already resolved")
            return
        end if

        if (deps%ndep /= 4) then
            call test_failed(error, "Expected for dependencies in tree")
            return
        end if

        call deps%resolve(".", error)
        if (allocated(error)) return

        if (.not.deps%finished()) then
            call test_failed(error, "Mocked dependency tree must resolve in one step")
            return
        end if

    end subroutine test_add_dependencies


    !> Resolve a single dependency node
    subroutine resolve_dependency_once(self, dependency, root, error)
        !> Mock instance of the dependency tree
        class(mock_dependency_tree_t), intent(inout) :: self
        !> Dependency configuration to add
        type(dependency_node_t), intent(inout) :: dependency
        !> Current installation prefix
        character(len=*), intent(in) :: root
        !> Error handling
        type(error_t), allocatable, intent(out) :: error

        if (dependency%done) then
            call test_failed(error, "Should only visit this node once")
            return
        end if
        dependency%done = .true.

    end subroutine resolve_dependency_once


end module test_package_dependencies