Skip to content

Commit

Permalink
fix windows delete_env
Browse files Browse the repository at this point in the history
  • Loading branch information
perazz committed Jun 24, 2024
1 parent 38b5b26 commit 8d73c85
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
10 changes: 6 additions & 4 deletions src/fpm_os.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdlib.h>
#include <stdio.h>

/// @brief Determine the absolute, canonicalized path for a given path.
/// @param path
Expand Down Expand Up @@ -41,12 +42,13 @@ int c_unsetenv(const char *envname) {
#ifndef _WIN32
return unsetenv(envname);
#else
char* str = malloc(10*sizeof(char));
str = '\0';
// _putenv_s returns a non-zero code when deleting a variable
char* str = malloc(64*sizeof(char));
*str = '\0';
int errcode = _putenv_s(envname,str);
// Windows returns a non-0 code when setting empty variable
if (errcode==-1) errcode=0;
free(str);
return 0;
return errcode;
#endif
}

Expand Down
48 changes: 26 additions & 22 deletions test/fpm_test/test_os.f90
Original file line number Diff line number Diff line change
Expand Up @@ -251,52 +251,56 @@ subroutine abs_path_cd_current(error)
call test_failed(error, "Result '"//result//"' doesn't equal current directory '"//current_dir//"'"); return
end if
end

!> Test creation and deletion of an environment variable
subroutine set_environment(error)
type(error_t), allocatable, intent(out) :: error
character(*), parameter :: vname = 'fgslsdfkjei13325xssghhjewfbew'

character(*), parameter :: vname = 'hiufewhiugw'
character(*), parameter :: vvalue = '1234567890'

character(:), allocatable :: old_value,new_value,final_value
logical :: success

!> Ensure there's no such variable
old_value = get_env(vname,default='ERROR')
if (old_value/='ERROR') then
if (old_value/='ERROR') then
call test_failed(error, "There is already an env variable named "//vname)
return
end if

!> Create variable
success = set_env(vname,value=vvalue)
if (.not.success) then
if (.not.success) then
call test_failed(error, "Cannot create environment variable "//vname)
return
end if

!> Check new value
new_value = get_env(vname,default='ERROR')
if (new_value/=vvalue) then
if (new_value/=vvalue) then
call test_failed(error, "Env "//vname//"="//new_value//'; expected '//vvalue)
return
end if
end if

!> Delete variable
success = delete_env(vname)
if (.not.success) then
if (.not.success) then
call test_failed(error, "Cannot delete environment variable "//vname)
return
end if

!> Ensure it does not exist anymore
final_value = get_env(vname,default='ERROR')
if (final_value/='ERROR') then
call test_failed(error, "Env "//vname//"="//final_value//'; it should not exist.')
return
end if

end if

!> Ensure it does not exist anymore
!> Do not test this on Windows: due to a Windows bug, environment variables do not get deleted
!> https://developercommunity.visualstudio.com/t/-putenv-sname-doesnt-always-delete-windows-copy-of/1587426
if (os_is_unix()) then
final_value = get_env(vname,default='ERROR')
if (final_value/='ERROR') then
call test_failed(error, "Env "//vname//"="//final_value//'; it should not exist.')
return
end if
endif

end subroutine set_environment

end module test_os

0 comments on commit 8d73c85

Please sign in to comment.