Skip to content

Commit 0acfee0

Browse files
committed
Set working directory directly in cmake
1 parent 1a123ee commit 0acfee0

File tree

4 files changed

+19
-85
lines changed

4 files changed

+19
-85
lines changed

test/CMakeLists.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,3 @@ add_subdirectory(quadrature)
3131
add_subdirectory(math)
3232
add_subdirectory(stringlist)
3333
add_subdirectory(terminal)
34-
35-
set(setRootDir
36-
test_np
37-
test_zip
38-
)
39-
40-
foreach(target ${setRootDir})
41-
target_compile_definitions(${target} PRIVATE TEST_ROOT_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
42-
endforeach()

test/io/CMakeLists.txt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@ set(
55
)
66
fypp_f90("${fyppFlags}" "${fppFiles}" outFiles)
77

8-
set(needsCPP
9-
test_np.f90
10-
test_zip.f90
11-
)
12-
13-
if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
14-
set_source_files_properties(${needsCPP} PROPERTIES COMPILE_FLAGS "-cpp")
15-
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
16-
set_source_files_properties(${needsCPP} PROPERTIES COMPILE_FLAGS "-fpp")
17-
endif()
18-
198
ADDTEST(loadtxt)
209
ADDTEST(savetxt)
2110

@@ -26,7 +15,10 @@ set_tests_properties(savetxt_qp PROPERTIES LABELS quadruple_precision)
2615

2716
ADDTEST(filesystem)
2817
ADDTEST(getline)
29-
ADDTEST(np)
3018
ADDTEST(open)
3119
ADDTEST(parse_mode)
20+
21+
ADDTEST(np)
3222
ADDTEST(zip)
23+
set_tests_properties(np PROPERTIES WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
24+
set_tests_properties(zip PROPERTIES WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})

test/io/test_np.f90

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module test_np
99

1010
public :: collect_np
1111

12+
character(*), parameter :: path_to_zip_files = "test/io/zip_files/"
13+
1214
contains
1315

1416
!> Collect all exported unit tests
@@ -737,10 +739,8 @@ subroutine npz_load_arr_empty_0(error)
737739
integer :: stat
738740
character(*), parameter :: filename = "empty_0.npz"
739741
character(*), parameter :: tmp = temp_dir//"/empty_0"
740-
character(:), allocatable :: path
741-
742+
character(*), parameter :: path = path_to_zip_files//filename
742743

743-
path = get_path(filename)
744744
call load_npz(path, arrays, stat, tmp_dir=tmp)
745745
call check(error, stat, "Loading an npz that contains a single empty array shouldn't fail.")
746746
if (allocated(error)) return
@@ -763,9 +763,8 @@ subroutine npz_load_arr_rand_2_3(error)
763763
integer :: stat
764764
character(*), parameter :: filename = "rand_2_3.npz"
765765
character(*), parameter :: tmp = temp_dir//"/rand_2_3"
766-
character(:), allocatable :: path
766+
character(*), parameter :: path = path_to_zip_files//filename
767767

768-
path = get_path(filename)
769768
call load_npz(path, arrays, stat, tmp_dir=tmp)
770769
call check(error, stat, "Loading an npz file that contains a valid nd_array shouldn't fail.")
771770
if (allocated(error)) return
@@ -788,10 +787,8 @@ subroutine npz_load_arr_arange_10_20(error)
788787
integer :: stat, i
789788
character(*), parameter :: filename = "arange_10_20.npz"
790789
character(*), parameter :: tmp = temp_dir//"/arange_10_20"
790+
character(*), parameter :: path = path_to_zip_files//filename
791791

792-
character(:), allocatable :: path
793-
794-
path = get_path(filename)
795792
call load_npz(path, arrays, stat, tmp_dir=tmp)
796793
call check(error, stat, "Loading an npz file that contains a valid nd_array shouldn't fail.")
797794
if (allocated(error)) return
@@ -821,9 +818,8 @@ subroutine npz_load_arr_cmplx(error)
821818
integer :: stat
822819
character(*), parameter :: filename = "cmplx_arr.npz"
823820
character(*), parameter :: tmp = temp_dir//"/cmplx_arr"
824-
character(:), allocatable :: path
821+
character(*), parameter :: path = path_to_zip_files//filename
825822

826-
path = get_path(filename)
827823
call load_npz(path, arrays, stat, tmp_dir=tmp)
828824
call check(error, stat, "Loading an npz file that contains a valid nd_array shouldn't fail.")
829825
if (allocated(error)) return
@@ -853,9 +849,8 @@ subroutine npz_load_two_arr_iint64_rdp(error)
853849
integer :: stat
854850
character(*), parameter :: filename = "two_arr_iint64_rdp.npz"
855851
character(*), parameter :: tmp = temp_dir//"/two_arr_iint64_rdp"
856-
character(:), allocatable :: path
852+
character(*), parameter :: path = path_to_zip_files//filename
857853

858-
path = get_path(filename)
859854
call load_npz(path, arrays, stat, tmp_dir=tmp)
860855
call check(error, stat, "Loading an npz file that contains valid nd_arrays shouldn't fail.")
861856
if (allocated(error)) return
@@ -900,9 +895,8 @@ subroutine npz_load_two_arr_iint64_rdp_comp(error)
900895
integer :: stat
901896
character(*), parameter :: filename = "two_arr_iint64_rdp_comp.npz"
902897
character(*), parameter :: tmp = temp_dir//"/two_arr_iint64_rdp_comp"
903-
character(:), allocatable :: path
898+
character(*), parameter :: path = path_to_zip_files//filename
904899

905-
path = get_path(filename)
906900
call load_npz(path, arrays, stat, tmp_dir=tmp)
907901
call check(error, stat, "Loading a compressed npz file that contains valid nd_arrays shouldn't fail.")
908902
if (allocated(error)) return
@@ -1220,18 +1214,6 @@ subroutine npz_save_two_arrays(error)
12201214
call delete_file(output_file)
12211215
end
12221216

1223-
!> Makes sure that we find the file when running both `ctest` and `fpm test`.
1224-
function get_path(file) result(path)
1225-
character(*), intent(in) :: file
1226-
character(:), allocatable :: path
1227-
1228-
#ifdef TEST_ROOT_DIR
1229-
path = TEST_ROOT_DIR//'/io/zip_files/'//file
1230-
#else
1231-
path = 'test/io/zip_files/'//file
1232-
#endif
1233-
end
1234-
12351217
subroutine delete_file(filename)
12361218
character(len=*), intent(in) :: filename
12371219

test/io/test_zip.f90

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module test_zip
88

99
public :: collect_zip
1010

11+
character(*), parameter :: path_to_zip_files = "test/io/zip_files/"
12+
1113
contains
1214

1315
!> Collect all exported unit tests
@@ -87,12 +89,7 @@ subroutine unzip_zip_has_empty_file(error)
8789

8890
integer :: stat
8991
character(*), parameter :: filename = "empty.zip"
90-
character(:), allocatable :: path
91-
92-
path = get_path(filename)
93-
if (.not. allocated(path)) then
94-
call test_failed(error, "The file '"//filename//"' could not be found."); return
95-
end if
92+
character(*), parameter :: path = path_to_zip_files//filename
9693

9794
call unzip(path, stat=stat)
9895
call check(error, stat, "Listing the contents of a zip file that contains an empty file should not fail.")
@@ -103,12 +100,7 @@ subroutine unzip_zip_has_txt_file(error)
103100

104101
integer :: stat
105102
character(*), parameter :: filename = "textfile.zip"
106-
character(:), allocatable :: path
107-
108-
path = get_path(filename)
109-
if (.not. allocated(path)) then
110-
call test_failed(error, "The file '"//filename//"' could not be found."); return
111-
end if
103+
character(*), parameter :: path = path_to_zip_files//filename
112104

113105
call unzip(path, stat=stat)
114106
call check(error, stat, "Listing the contents of a zip file that contains an empty file should not fail.")
@@ -119,12 +111,7 @@ subroutine unzip_npz_array_empty_0_file(error)
119111

120112
integer :: stat
121113
character(*), parameter :: filename = "empty_0.npz"
122-
character(:), allocatable :: path
123-
124-
path = get_path(filename)
125-
if (.not. allocated(path)) then
126-
call test_failed(error, "The file '"//filename//"' could not be found."); return
127-
end if
114+
character(*), parameter :: path = path_to_zip_files//filename
128115

129116
call unzip(path, stat=stat)
130117
call check(error, stat, "Listing the contents of a zip file that contains an empty file should not fail.")
@@ -135,12 +122,7 @@ subroutine unzip_two_files(error)
135122

136123
integer :: stat
137124
character(*), parameter :: filename = "two_files.zip"
138-
character(:), allocatable :: path
139-
140-
path = get_path(filename)
141-
if (.not. allocated(path)) then
142-
call test_failed(error, "The file '"//filename//"' could not be found."); return
143-
end if
125+
character(*), parameter :: path = path_to_zip_files//filename
144126

145127
call unzip(path, stat=stat)
146128
call check(error, stat, "Listing the contents of a zip file that contains an empty file should not fail.")
@@ -151,9 +133,8 @@ subroutine unzip_compressed_npz(error)
151133

152134
integer :: stat
153135
character(*), parameter :: filename = "two_arr_iint64_rdp_comp.npz"
154-
character(:), allocatable :: path
136+
character(*), parameter :: path = path_to_zip_files//filename
155137

156-
path = get_path(filename)
157138
call unzip(path, stat=stat)
158139
call check(error, stat, "Listing the contents of a compressed npz file should not fail.")
159140
end
@@ -292,18 +273,6 @@ subroutine zip_without_comp(error)
292273
call delete_file(output_file)
293274
end
294275

295-
!> Makes sure that we find the file when running both `ctest` and `fpm test`.
296-
function get_path(file) result(path)
297-
character(*), intent(in) :: file
298-
character(:), allocatable :: path
299-
300-
#ifdef TEST_ROOT_DIR
301-
path = TEST_ROOT_DIR//'/io/zip_files/'//file
302-
#else
303-
path = 'test/io/zip_files/'//file
304-
#endif
305-
end
306-
307276
subroutine delete_file(filename)
308277
character(len=*), intent(in) :: filename
309278

0 commit comments

Comments
 (0)