Skip to content

Commit 94a7efe

Browse files
committed
utils: prefer shlib when looking through unixodbc installations
1 parent 4ebef72 commit 94a7efe

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

R/utils.R

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,18 @@ locate_install_unixodbc <- function() {
375375
"/opt/R/x86_64/lib"
376376
)
377377

378-
list.files(
379-
common_dirs,
380-
pattern = libodbcinst_filename(),
381-
full.names = TRUE
382-
)
378+
file_names <- libodbcinst_filename()
379+
for (file_name in file_names) {
380+
paths <- list.files(
381+
common_dirs,
382+
pattern = file_name,
383+
full.names = TRUE
384+
)
385+
if (length(paths)) {
386+
return(paths)
387+
}
388+
}
389+
return(character())
383390
}
384391

385392
system_safely <- function(x) {
@@ -403,9 +410,9 @@ system_safely <- function(x) {
403410
# list.files( ..., pattern = ... ).
404411
libodbcinst_filename <- function() {
405412
if (is_macos()) {
406-
"libodbcinst.dylib|libodbcinst.a"
413+
return(c("libodbcinst.dylib", "libodbcinst.a"))
407414
} else {
408-
"libodbcinst.so|libodbcinst.a"
415+
return(c("libodbcinst.so", "libodbcinst.a"))
409416
}
410417
}
411418

tests/testthat/test-utils.R

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,59 @@ test_that("locate_install_unixodbc() returns reasonable values", {
214214
expect_true(grepl("(\\.dylib|\\.a)$", res[1]))
215215
})
216216

217+
# https://github.com/r-dbi/odbc/issues/919
218+
test_that("locate_install_unixodbc() prefers shlib", {
219+
# odbc_config / pkg-config cflags point to nonexistent files on CRAN (#903)
220+
skip_on_cran()
221+
{
222+
local_mocked_bindings(
223+
is_macos = function() {TRUE}
224+
)
225+
local_mocked_bindings(
226+
list.files = function (path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, ...) {
227+
# Mimic being able to find any pattern passed as argument
228+
return(pattern)
229+
},
230+
.package = "base"
231+
)
232+
res <- locate_install_unixodbc()
233+
expect_equal(length(res), 1)
234+
expect_true(grepl("(\\.dylib)$", res[1]))
235+
}
236+
{
237+
local_mocked_bindings(
238+
is_macos = function() {TRUE}
239+
)
240+
local_mocked_bindings(
241+
list.files = function (path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, ...) {
242+
# Mimic being able to find .a only
243+
if(pattern == "libodbcinst.a") return(pattern)
244+
return(character())
245+
},
246+
.package = "base"
247+
)
248+
res <- locate_install_unixodbc()
249+
expect_equal(length(res), 1)
250+
expect_true(grepl("(\\.a)$", res[1]))
251+
}
252+
{
253+
local_mocked_bindings(
254+
is_macos = function() {TRUE}
255+
)
256+
local_mocked_bindings(
257+
list.files = function (path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, ...) {
258+
# Mimic being able to find shlib only
259+
if(pattern == "libodbcinst.dylib") return(pattern)
260+
return(character())
261+
},
262+
.package = "base"
263+
)
264+
res <- locate_install_unixodbc()
265+
expect_equal(length(res), 1)
266+
expect_true(grepl("(\\.dylib)$", res[1]))
267+
}
268+
})
269+
217270
test_that("databricks() errors informatively when spark ini isn't writeable", {
218271
local_mocked_bindings(is_writeable = function(path) {FALSE})
219272
expect_snapshot(

0 commit comments

Comments
 (0)