| 1 |
module test_os |
| 2 |
use testsuite, only: new_unittest, unittest_t, error_t, test_failed |
| 3 |
use fpm_filesystem, only: join_path, mkdir, os_delete_dir, is_dir, get_local_prefix, get_home |
| 4 |
use fpm_environment, only: os_is_unix, get_env, set_env, delete_env |
| 5 |
use fpm_os, only: get_absolute_path, get_absolute_path_by_cd, get_current_directory |
| 6 |
|
| 7 |
implicit none |
| 8 |
private |
| 9 |
public :: collect_os |
| 10 |
|
| 11 |
character(len=*), parameter :: tmp_folder = 'tmp' |
| 12 |
|
| 13 |
contains |
| 14 |
|
| 15 |
!> Collect unit tests. |
| 16 |
subroutine collect_os(tests) |
| 17 |
|
| 18 |
!> Unit tests to collect. |
| 19 |
type(unittest_t), allocatable, intent(out) :: tests(:) |
| 20 |
|
| 21 |
tests = [ & |
| 22 |
& new_unittest('empty-path', empty_path, should_fail=.true.), & |
| 23 |
& new_unittest('only-tilde', only_tilde), & |
| 24 |
& new_unittest('set-environment-variable', set_environment), & |
| 25 |
& new_unittest('invalid-tilde-path', invalid_tilde_path, should_fail=.true.), & |
| 26 |
& new_unittest('tilde-correct-separator', tilde_correct_separator), & |
| 27 |
& new_unittest('tilde-wrong-separator', tilde_wrong_separator, should_fail=.true.), & |
| 28 |
& new_unittest('tilde-nonexistent-path', tilde_nonexistent_path, should_fail=.true.), & |
| 29 |
& new_unittest('abs-path-nonexisting', abs_path_nonexisting, should_fail=.true.), & |
| 30 |
& new_unittest('abs-path-root', abs_path_root), & |
| 31 |
& new_unittest('abs-path-home', abs_path_home), & |
| 32 |
& new_unittest('abs-path-cd-root', abs_path_home), & |
| 33 |
& new_unittest('abs-path-cd-home', abs_path_cd_home), & |
| 34 |
& new_unittest('abs-path-cd-current', abs_path_cd_current) & |
| 35 |
] |
| 36 |
|
| 37 |
end subroutine collect_os |
| 38 |
|
| 39 |
subroutine delete_tmp_folder |
| 40 |
if (is_dir(tmp_folder)) call os_delete_dir(os_is_unix(), tmp_folder) |
| 41 |
end |
| 42 |
|
| 43 |
subroutine empty_path(error) |
| 44 |
type(error_t), allocatable, intent(out) :: error |
| 45 |
character(len=:), allocatable :: result |
| 46 |
call get_absolute_path('', result, error) |
| 47 |
end |
| 48 |
|
| 49 |
subroutine only_tilde(error) |
| 50 |
type(error_t), allocatable, intent(out) :: error |
| 51 |
character(len=:), allocatable :: result |
| 52 |
character(len=:), allocatable :: home |
| 53 |
|
| 54 |
call get_absolute_path('~', result, error) |
| 55 |
|
| 56 |
if (allocated(error)) then |
| 57 |
call test_failed(error, "Unexpected error resolving '~'") |
| 58 |
return |
| 59 |
end if |
| 60 |
|
| 61 |
if (.not. allocated(result)) then |
| 62 |
call test_failed(error, "Unexpected null result resolving '~'") |
| 63 |
return |
| 64 |
end if |
| 65 |
|
| 66 |
call get_home(home, error) |
| 67 |
if (allocated(error)) return |
| 68 |
|
| 69 |
if (result /= home) then |
| 70 |
call test_failed(error, "Result '"//result//"' doesn't equal home directory '"//home//"'") |
| 71 |
return |
| 72 |
end if |
| 73 |
|
| 74 |
end subroutine |
| 75 |
|
| 76 |
subroutine invalid_tilde_path(error) |
| 77 |
type(error_t), allocatable, intent(out) :: error |
| 78 |
character(len=:), allocatable :: result |
| 79 |
call get_absolute_path('~a', result, error) |
| 80 |
end |
| 81 |
|
| 82 |
subroutine tilde_correct_separator(error) |
| 83 |
type(error_t), allocatable, intent(out) :: error |
| 84 |
character(len=:), allocatable :: result |
| 85 |
character(len=:), allocatable :: separator |
| 86 |
character(len=:), allocatable :: home |
| 87 |
|
| 88 |
if (os_is_unix()) then |
| 89 |
separator = '/' |
| 90 |
else |
| 91 |
separator = '\' |
| 92 |
end if |
| 93 |
|
| 94 |
call get_absolute_path('~'//separator, result, error) |
| 95 |
if (allocated(error)) return |
| 96 |
|
| 97 |
call get_home(home, error) |
| 98 |
if (allocated(error)) return |
| 99 |
|
| 100 |
if (result /= home) then |
| 101 |
call test_failed(error, "Result '"//result//"' doesn't equal home directory '"//home//"'") |
| 102 |
return |
| 103 |
end if |
| 104 |
end |
| 105 |
|
| 106 |
subroutine tilde_wrong_separator(error) |
| 107 |
type(error_t), allocatable, intent(out) :: error |
| 108 |
character(len=:), allocatable :: result |
| 109 |
character(len=:), allocatable :: separator |
| 110 |
|
| 111 |
if (os_is_unix()) then |
| 112 |
separator = '\' |
| 113 |
else |
| 114 |
separator = '/' |
| 115 |
end if |
| 116 |
|
| 117 |
call get_absolute_path('~'//separator, result, error) |
| 118 |
end |
| 119 |
|
| 120 |
!> Entering a non-existing path with ~ should fail. |
| 121 |
subroutine tilde_nonexistent_path(error) |
| 122 |
type(error_t), allocatable, intent(out) :: error |
| 123 |
character(len=:), allocatable :: result |
| 124 |
call get_absolute_path('~/abcde', result, error) |
| 125 |
end |
| 126 |
|
| 127 |
!> Entering a non-existing absolute path should fail. |
| 128 |
subroutine abs_path_nonexisting(error) |
| 129 |
type(error_t), allocatable, intent(out) :: error |
| 130 |
character(len=:), allocatable :: result |
| 131 |
call get_absolute_path('/abcde', result, error) |
| 132 |
end |
| 133 |
|
| 134 |
!> Get the absolute path of the root directory. |
| 135 |
subroutine abs_path_root(error) |
| 136 |
type(error_t), allocatable, intent(out) :: error |
| 137 |
|
| 138 |
character(len=:), allocatable :: home_path, result |
| 139 |
|
| 140 |
if (os_is_unix()) then |
| 141 |
call get_absolute_path('/', result, error) |
| 142 |
if (allocated(error)) return |
| 143 |
|
| 144 |
if (result /= '/') then |
| 145 |
call test_failed(error, "Result '"//result//"' doesn't equal input value: '/'"); return |
| 146 |
end if |
| 147 |
else |
| 148 |
home_path = get_env('HOMEDRIVE','') //'\' |
| 149 |
|
| 150 |
call get_absolute_path(home_path, result, error) |
| 151 |
if (allocated(error)) return |
| 152 |
|
| 153 |
if (result /= home_path) then |
| 154 |
call test_failed(error, "Result '"//result//"' doesn't equal input value: '"//home_path//"'"); return |
| 155 |
end if |
| 156 |
end if |
| 157 |
end |
| 158 |
|
| 159 |
!> Get the absolute path of the home directory. |
| 160 |
subroutine abs_path_home(error) |
| 161 |
type(error_t), allocatable, intent(out) :: error |
| 162 |
|
| 163 |
character(len=:), allocatable :: home, result |
| 164 |
|
| 165 |
call get_home(home, error) |
| 166 |
if (allocated(error)) return |
| 167 |
|
| 168 |
call get_absolute_path(home, result, error) |
| 169 |
if (allocated(error)) return |
| 170 |
|
| 171 |
if (result /= home) then |
| 172 |
call test_failed(error, "Result '"//result//"' doesn't equal home directory '"//home//"'"); return |
| 173 |
end if |
| 174 |
end |
| 175 |
|
| 176 |
!> Get the absolute path of the root directory using `getcwd`/`_getcwd`. |
| 177 |
subroutine abs_path_cd_root(error) |
| 178 |
type(error_t), allocatable, intent(out) :: error |
| 179 |
|
| 180 |
character(len=:), allocatable :: home_path, current_dir_before, current_dir_after, result |
| 181 |
|
| 182 |
call get_current_directory(current_dir_before, error) |
| 183 |
if (allocated(error)) return |
| 184 |
|
| 185 |
if (os_is_unix()) then |
| 186 |
call get_absolute_path_by_cd('/', result, error) |
| 187 |
|
| 188 |
if (result /= '/') then |
| 189 |
call test_failed(error, "Result '"//result//"' doesn't equal input value: '/'"); return |
| 190 |
end if |
| 191 |
else |
| 192 |
home_path = get_env('HOMEDRIVE','')//'\' |
| 193 |
|
| 194 |
call get_absolute_path_by_cd(home_path, result, error) |
| 195 |
|
| 196 |
if (result /= home_path) then |
| 197 |
call test_failed(error, "Result '"//result//"' doesn't equal input value: '"//home_path//"'"); return |
| 198 |
end if |
| 199 |
end if |
| 200 |
|
| 201 |
call get_current_directory(current_dir_after, error) |
| 202 |
if (allocated(error)) return |
| 203 |
|
| 204 |
if (current_dir_before /= current_dir_after) then |
| 205 |
call test_failed(error, "Current directory before getting absolute path '"//current_dir_before// & |
| 206 |
& "' doesn't equal current directory after getting absolute path '"//current_dir_after//"'."); return |
| 207 |
end if |
| 208 |
end |
| 209 |
|
| 210 |
!> Get the absolute path of the root directory using `getcwd`/`_getcwd`. |
| 211 |
subroutine abs_path_cd_home(error) |
| 212 |
type(error_t), allocatable, intent(out) :: error |
| 213 |
|
| 214 |
character(len=:), allocatable :: home, current_dir_before, current_dir_after, result |
| 215 |
|
| 216 |
call get_current_directory(current_dir_before, error) |
| 217 |
if (allocated(error)) return |
| 218 |
|
| 219 |
call get_home(home, error) |
| 220 |
if (allocated(error)) return |
| 221 |
|
| 222 |
call get_absolute_path_by_cd(home, result, error) |
| 223 |
if (allocated(error)) return |
| 224 |
|
| 225 |
call get_current_directory(current_dir_after, error) |
| 226 |
if (allocated(error)) return |
| 227 |
|
| 228 |
if (current_dir_before /= current_dir_after) then |
| 229 |
call test_failed(error, "Current directory before getting absolute path '"//current_dir_before// & |
| 230 |
& "' doesn't equal current directory after getting absolute path '"//current_dir_after//"'."); return |
| 231 |
end if |
| 232 |
|
| 233 |
if (result /= home) then |
| 234 |
call test_failed(error, "Result '"//result//"' doesn't equal home directory '"//home//"'"); return |
| 235 |
end if |
| 236 |
end |
| 237 |
|
| 238 |
!> Get the absolute path of the current directory using `getcwd`/`_getcwd`. |
| 239 |
subroutine abs_path_cd_current(error) |
| 240 |
type(error_t), allocatable, intent(out) :: error |
| 241 |
|
| 242 |
character(len=:), allocatable :: current_dir, result |
| 243 |
|
| 244 |
call get_current_directory(current_dir, error) |
| 245 |
if (allocated(error)) return |
| 246 |
|
| 247 |
call get_absolute_path_by_cd('.', result, error) |
| 248 |
if (allocated(error)) return |
| 249 |
|
| 250 |
if (result /= current_dir) then |
| 251 |
call test_failed(error, "Result '"//result//"' doesn't equal current directory '"//current_dir//"'"); return |
| 252 |
end if |
| 253 |
end |
| 254 |
|
| 255 |
!> Test creation and deletion of an environment variable |
| 256 |
subroutine set_environment(error) |
| 257 |
type(error_t), allocatable, intent(out) :: error |
| 258 |
|
| 259 |
character(*), parameter :: vname = 'hiufewhiugw' |
| 260 |
character(*), parameter :: vvalue = '1234567890' |
| 261 |
|
| 262 |
character(:), allocatable :: old_value,new_value,final_value |
| 263 |
logical :: success |
| 264 |
|
| 265 |
!> Ensure there's no such variable |
| 266 |
old_value = get_env(vname,default='ERROR') |
| 267 |
if (old_value/='ERROR') then |
| 268 |
call test_failed(error, "There is already an env variable named "//vname) |
| 269 |
return |
| 270 |
end if |
| 271 |
|
| 272 |
!> Create variable |
| 273 |
success = set_env(vname,value=vvalue) |
| 274 |
if (.not.success) then |
| 275 |
call test_failed(error, "Cannot create environment variable "//vname) |
| 276 |
return |
| 277 |
end if |
| 278 |
|
| 279 |
!> Check new value |
| 280 |
new_value = get_env(vname,default='ERROR') |
| 281 |
if (new_value/=vvalue) then |
| 282 |
call test_failed(error, "Env "//vname//"="//new_value//'; expected '//vvalue) |
| 283 |
return |
| 284 |
end if |
| 285 |
|
| 286 |
!> Delete variable |
| 287 |
success = delete_env(vname) |
| 288 |
if (.not.success) then |
| 289 |
call test_failed(error, "Cannot delete environment variable "//vname) |
| 290 |
return |
| 291 |
end if |
| 292 |
|
| 293 |
!> Ensure it does not exist anymore |
| 294 |
!> Do not test this on Windows: due to a Windows bug, environment variables do not get deleted |
| 295 |
!> https://developercommunity.visualstudio.com/t/-putenv-sname-doesnt-always-delete-windows-copy-of/1587426 |
| 296 |
if (os_is_unix()) then |
| 297 |
final_value = get_env(vname,default='ERROR') |
| 298 |
if (final_value/='ERROR') then |
| 299 |
call test_failed(error, "Env "//vname//"="//final_value//'; it should not exist.') |
| 300 |
return |
| 301 |
end if |
| 302 |
endif |
| 303 |
|
| 304 |
end subroutine set_environment |
| 305 |
|
| 306 |
end module test_os |