Skip to content

Commit 088ae49

Browse files
committed
Add tests for checking and reading package data
1 parent 5823d8e commit 088ae49

File tree

2 files changed

+267
-7
lines changed

2 files changed

+267
-7
lines changed

src/fpm/dependency.f90

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ module fpm_dependency
7373
implicit none
7474
private
7575

76-
public :: dependency_tree_t, new_dependency_tree, dependency_node_t, new_dependency_node, resize
76+
public :: dependency_tree_t, new_dependency_tree, dependency_node_t, new_dependency_node, resize, &
77+
& check_and_read_pkg_data
7778

7879
!> Overloaded reallocation interface
7980
interface resize
@@ -551,7 +552,7 @@ subroutine get_from_registry(self, target_dir, global_settings, error, downloade
551552
close (unit, status='delete')
552553
if (allocated(error)) return
553554

554-
! Verify package data read relevant information.
555+
! Verify package data and read relevant information.
555556
call check_and_read_pkg_data(json, self, target_url, version, error)
556557
if (allocated(error)) return
557558

@@ -624,7 +625,7 @@ subroutine check_and_read_pkg_data(json, node, download_url, version, error)
624625

625626
call get_value(json, 'data', p, stat=stat)
626627
if (stat /= 0) then
627-
call fatal_error(error, "Failed to retrieve package data for '"//join_path(node%namespace, node%name)//"'."); return
628+
call fatal_error(error, "Failed to read package data for '"//join_path(node%namespace, node%name)//"'."); return
628629
end if
629630

630631
if (allocated(node%requested_version)) then
@@ -648,7 +649,7 @@ subroutine check_and_read_pkg_data(json, node, download_url, version, error)
648649

649650
call get_value(q, 'download_url', download_url, stat=stat)
650651
if (stat /= 0) then
651-
call fatal_error(error, "Failed to retrieve download url for '"//join_path(node%namespace, node%name)//"'."); return
652+
call fatal_error(error, "Failed to read download url for '"//join_path(node%namespace, node%name)//"'."); return
652653
end if
653654

654655
download_url = official_registry_base_url//download_url
@@ -659,12 +660,13 @@ subroutine check_and_read_pkg_data(json, node, download_url, version, error)
659660

660661
call get_value(q, 'version', version_str, stat=stat)
661662
if (stat /= 0) then
662-
call fatal_error(error, "Failed to retrieve version data for '"//join_path(node%namespace, node%name)//"'."); return
663+
call fatal_error(error, "Failed to read version data for '"//join_path(node%namespace, node%name)//"'."); return
663664
end if
664665

665666
call new_version(version, version_str, error)
666667
if (allocated(error)) then
667-
call fatal_error(error, "Invalid version: '"//version_str//"'."); return
668+
call fatal_error(error, "'"//version_str//"' is not a valid version for '"// &
669+
& join_path(node%namespace, node%name)//"'."); return
668670
end if
669671
end subroutine
670672

test/fpm_test/test_package_dependencies.f90

Lines changed: 259 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,22 @@ subroutine collect_package_dependencies(tests)
5757
& new_unittest("version-found-in-cache", version_found_in_cache), &
5858
& new_unittest("no-version-in-default-cache", no_version_in_default_cache), &
5959
& new_unittest("no-version-in-cache-or-registry", no_version_in_cache_or_registry, should_fail=.true.), &
60-
& new_unittest("other-versions-in-default-cache", other_versions_in_default_cache) &
60+
& new_unittest("other-versions-in-default-cache", other_versions_in_default_cache), &
61+
& new_unittest("pkg-data-no-code", pkg_data_no_code, should_fail=.true.), &
62+
& new_unittest("pkg-data-corrupt-code", pkg_data_corrupt_code, should_fail=.true.), &
63+
& new_unittest("pkg-data-missing-error-message", pkg_data_missing_error_msg, should_fail=.true.), &
64+
& new_unittest("pkg-data-error-reading-message", pkg_data_error_reading_msg, should_fail=.true.), &
65+
& new_unittest("pkg-data-error-has-message", pkg_data_error_has_msg, should_fail=.true.), &
66+
& new_unittest("pkg-data-error-no-data", pkg_data_no_data, should_fail=.true.), &
67+
& new_unittest("pkg-data-error-reading-data", pkg_data_error_reading_data, should_fail=.true.), &
68+
& new_unittest("pkg-data-requested-version-wrong-key", pkg_data_requested_version_wrong_key, should_fail=.true.), &
69+
& new_unittest("pkg-data-no-version-requested-wrong-key", pkg_data_no_version_requested_wrong_key, should_fail=.true.), &
70+
& new_unittest("pkg-data-error-reading-latest-version", pkg_data_error_reading_latest_version, should_fail=.true.), &
71+
& new_unittest("pkg-data-no-download-url", pkg_data_no_download_url, should_fail=.true.), &
72+
& new_unittest("pkg-data-error-reading-donwload-url", pkg_data_error_reading_download_url, should_fail=.true.), &
73+
& new_unittest("pkg-data-no-version", pkg_data_no_version, should_fail=.true.), &
74+
& new_unittest("pkg-data-error-reading-version", pkg_data_error_reading_version, should_fail=.true.), &
75+
& new_unittest("pkg-data-invalid-version", pkg_data_invalid_version, should_fail=.true.) &
6176
& ]
6277

6378
end subroutine collect_package_dependencies
@@ -920,6 +935,249 @@ subroutine other_versions_in_default_cache(error)
920935

921936
end subroutine other_versions_in_default_cache
922937

938+
!> Package data returned from the registry does not contain a code field.
939+
subroutine pkg_data_no_code(error)
940+
type(error_t), allocatable, intent(out) :: error
941+
942+
type(dependency_node_t) :: node
943+
character(:), allocatable :: url
944+
type(version_t) :: version
945+
type(json_object) :: json
946+
class(json_value), allocatable :: j_value
947+
948+
call json_loads(j_value, '{}')
949+
json = cast_to_object(j_value)
950+
951+
call check_and_read_pkg_data(json, node, url, version, error)
952+
953+
end subroutine pkg_data_no_code
954+
955+
!> Error reading status code from package data.
956+
subroutine pkg_data_corrupt_code(error)
957+
type(error_t), allocatable, intent(out) :: error
958+
959+
type(dependency_node_t) :: node
960+
character(:), allocatable :: url
961+
type(version_t) :: version
962+
type(json_object) :: json
963+
class(json_value), allocatable :: j_value
964+
965+
call json_loads(j_value, '{"code": "integer expected"}')
966+
json = cast_to_object(j_value)
967+
968+
call check_and_read_pkg_data(json, node, url, version, error)
969+
970+
end subroutine pkg_data_corrupt_code
971+
972+
subroutine pkg_data_missing_error_msg(error)
973+
type(error_t), allocatable, intent(out) :: error
974+
975+
type(dependency_node_t) :: node
976+
character(:), allocatable :: url
977+
type(version_t) :: version
978+
type(json_object) :: json
979+
class(json_value), allocatable :: j_value
980+
981+
call json_loads(j_value, '{"code": 123}')
982+
json = cast_to_object(j_value)
983+
984+
call check_and_read_pkg_data(json, node, url, version, error)
985+
986+
end subroutine pkg_data_missing_error_msg
987+
988+
subroutine pkg_data_error_reading_msg(error)
989+
type(error_t), allocatable, intent(out) :: error
990+
991+
type(dependency_node_t) :: node
992+
character(:), allocatable :: url
993+
type(version_t) :: version
994+
type(json_object) :: json
995+
class(json_value), allocatable :: j_value
996+
997+
call json_loads(j_value, '{"code": 123, "message": 123}')
998+
json = cast_to_object(j_value)
999+
1000+
call check_and_read_pkg_data(json, node, url, version, error)
1001+
1002+
end subroutine pkg_data_error_reading_msg
1003+
1004+
subroutine pkg_data_error_has_msg(error)
1005+
type(error_t), allocatable, intent(out) :: error
1006+
1007+
type(dependency_node_t) :: node
1008+
character(:), allocatable :: url
1009+
type(version_t) :: version
1010+
type(json_object) :: json
1011+
class(json_value), allocatable :: j_value
1012+
1013+
call json_loads(j_value, '{"code": 123, "message": "Really bad error message"}')
1014+
json = cast_to_object(j_value)
1015+
1016+
call check_and_read_pkg_data(json, node, url, version, error)
1017+
1018+
end subroutine pkg_data_error_has_msg
1019+
1020+
subroutine pkg_data_no_data(error)
1021+
type(error_t), allocatable, intent(out) :: error
1022+
1023+
type(dependency_node_t) :: node
1024+
character(:), allocatable :: url
1025+
type(version_t) :: version
1026+
type(json_object) :: json
1027+
class(json_value), allocatable :: j_value
1028+
1029+
call json_loads(j_value, '{"code": 200}')
1030+
json = cast_to_object(j_value)
1031+
1032+
call check_and_read_pkg_data(json, node, url, version, error)
1033+
1034+
end subroutine pkg_data_no_data
1035+
1036+
subroutine pkg_data_error_reading_data(error)
1037+
type(error_t), allocatable, intent(out) :: error
1038+
1039+
type(dependency_node_t) :: node
1040+
character(:), allocatable :: url
1041+
type(version_t) :: version
1042+
type(json_object) :: json
1043+
class(json_value), allocatable :: j_value
1044+
1045+
call json_loads(j_value, '{"code": 200, "data": 123}')
1046+
json = cast_to_object(j_value)
1047+
1048+
call check_and_read_pkg_data(json, node, url, version, error)
1049+
1050+
end subroutine pkg_data_error_reading_data
1051+
1052+
subroutine pkg_data_requested_version_wrong_key(error)
1053+
type(error_t), allocatable, intent(out) :: error
1054+
1055+
type(dependency_node_t) :: node
1056+
character(:), allocatable :: url
1057+
type(version_t) :: version
1058+
type(json_object) :: json
1059+
class(json_value), allocatable :: j_value
1060+
1061+
allocate (node%requested_version)
1062+
call json_loads(j_value, '{"code": 200, "data": {"latest_version_data": 123}}') ! Expected key: "version_data"
1063+
json = cast_to_object(j_value)
1064+
1065+
call check_and_read_pkg_data(json, node, url, version, error)
1066+
1067+
end subroutine pkg_data_requested_version_wrong_key
1068+
1069+
subroutine pkg_data_no_version_requested_wrong_key(error)
1070+
type(error_t), allocatable, intent(out) :: error
1071+
1072+
type(dependency_node_t) :: node
1073+
character(:), allocatable :: url
1074+
type(version_t) :: version
1075+
type(json_object) :: json
1076+
class(json_value), allocatable :: j_value
1077+
1078+
call json_loads(j_value, '{"code": 200, "data": {"version_data": 123}}') ! Expected key: "latest_version_data"
1079+
json = cast_to_object(j_value)
1080+
1081+
call check_and_read_pkg_data(json, node, url, version, error)
1082+
1083+
end subroutine pkg_data_no_version_requested_wrong_key
1084+
1085+
subroutine pkg_data_error_reading_latest_version(error)
1086+
type(error_t), allocatable, intent(out) :: error
1087+
1088+
type(dependency_node_t) :: node
1089+
character(:), allocatable :: url
1090+
type(version_t) :: version
1091+
type(json_object) :: json
1092+
class(json_value), allocatable :: j_value
1093+
1094+
call json_loads(j_value, '{"code": 200, "data": {"latest_version_data": 123}}')
1095+
json = cast_to_object(j_value)
1096+
1097+
call check_and_read_pkg_data(json, node, url, version, error)
1098+
1099+
end subroutine pkg_data_error_reading_latest_version
1100+
1101+
subroutine pkg_data_no_download_url(error)
1102+
type(error_t), allocatable, intent(out) :: error
1103+
1104+
type(dependency_node_t) :: node
1105+
character(:), allocatable :: url
1106+
type(version_t) :: version
1107+
type(json_object) :: json
1108+
class(json_value), allocatable :: j_value
1109+
1110+
call json_loads(j_value, '{"code": 200, "data": {"latest_version_data": {}}}')
1111+
json = cast_to_object(j_value)
1112+
1113+
call check_and_read_pkg_data(json, node, url, version, error)
1114+
1115+
end subroutine pkg_data_no_download_url
1116+
1117+
subroutine pkg_data_error_reading_download_url(error)
1118+
type(error_t), allocatable, intent(out) :: error
1119+
1120+
type(dependency_node_t) :: node
1121+
character(:), allocatable :: url
1122+
type(version_t) :: version
1123+
type(json_object) :: json
1124+
class(json_value), allocatable :: j_value
1125+
1126+
call json_loads(j_value, '{"code": 200, "data": {"latest_version_data": {"download_url": 123}}}')
1127+
json = cast_to_object(j_value)
1128+
1129+
call check_and_read_pkg_data(json, node, url, version, error)
1130+
1131+
end subroutine pkg_data_error_reading_download_url
1132+
1133+
subroutine pkg_data_no_version(error)
1134+
type(error_t), allocatable, intent(out) :: error
1135+
1136+
type(dependency_node_t) :: node
1137+
character(:), allocatable :: url
1138+
type(version_t) :: version
1139+
type(json_object) :: json
1140+
class(json_value), allocatable :: j_value
1141+
1142+
call json_loads(j_value, '{"code": 200, "data": {"latest_version_data": {"download_url": "abc"}}}')
1143+
json = cast_to_object(j_value)
1144+
1145+
call check_and_read_pkg_data(json, node, url, version, error)
1146+
1147+
end subroutine pkg_data_no_version
1148+
1149+
subroutine pkg_data_error_reading_version(error)
1150+
type(error_t), allocatable, intent(out) :: error
1151+
1152+
type(dependency_node_t) :: node
1153+
character(:), allocatable :: url
1154+
type(version_t) :: version
1155+
type(json_object) :: json
1156+
class(json_value), allocatable :: j_value
1157+
1158+
call json_loads(j_value, '{"code": 200, "data": {"latest_version_data": {"download_url": "abc", "version": 123}}}')
1159+
json = cast_to_object(j_value)
1160+
1161+
call check_and_read_pkg_data(json, node, url, version, error)
1162+
1163+
end subroutine pkg_data_error_reading_version
1164+
1165+
subroutine pkg_data_invalid_version(error)
1166+
type(error_t), allocatable, intent(out) :: error
1167+
1168+
type(dependency_node_t) :: node
1169+
character(:), allocatable :: url
1170+
type(version_t) :: version
1171+
type(json_object) :: json
1172+
class(json_value), allocatable :: j_value
1173+
1174+
call json_loads(j_value, '{"code": 200, "data": {"latest_version_data": {"download_url": "abc", "version": "abc"}}}')
1175+
json = cast_to_object(j_value)
1176+
1177+
call check_and_read_pkg_data(json, node, url, version, error)
1178+
1179+
end subroutine pkg_data_invalid_version
1180+
9231181
!> Resolve a single dependency node
9241182
subroutine resolve_dependency_once(self, dependency, root, error)
9251183
!> Mock instance of the dependency tree

0 commit comments

Comments
 (0)