Skip to content

Commit e7202c5

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

File tree

2 files changed

+69
-14
lines changed

2 files changed

+69
-14
lines changed

R/utils.R

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -375,37 +375,48 @@ 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) {
386393
tryCatch(
387394
{
388395
unixodbc_prefix <- system(x, intern = TRUE, ignore.stderr = TRUE)
389-
candidates <- list.files(unixodbc_prefix,
390-
pattern = libodbcinst_filename(), full.names = TRUE)
391-
if (!length(candidates)) {
392-
stop("Unable to locate unixodbc using odbc_config")
396+
file_names <- libodbcinst_filename()
397+
for (file_name in file_names) {
398+
candidates <- list.files(unixodbc_prefix,
399+
pattern = file_name, full.names = TRUE)
400+
if (length(candidates)) {
401+
return(candidates[1])
402+
}
393403
}
394-
return(candidates[1])
404+
stop("Unable to locate unixodbc using odbc_config")
395405
},
396406
error = function(e) {},
397407
warning = function(w) {}
398408
)
399409
character()
400410
}
401411

402-
# Returns a pattern to be used with
403-
# list.files( ..., pattern = ... ).
412+
# Returns a vector of possible lib filenames.
413+
# Vector is ordered and should be iterated over
414+
# in same order.
404415
libodbcinst_filename <- function() {
405416
if (is_macos()) {
406-
"libodbcinst.dylib|libodbcinst.a"
417+
return(c("libodbcinst.dylib", "libodbcinst.a"))
407418
} else {
408-
"libodbcinst.so|libodbcinst.a"
419+
return(c("libodbcinst.so", "libodbcinst.a"))
409420
}
410421
}
411422

tests/testthat/test-utils.R

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,50 @@ 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+
list.files = function (path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, ...) {
225+
# Mimic being able to find any pattern passed as argument
226+
return(pattern)
227+
}
228+
)
229+
res <- locate_install_unixodbc()
230+
expect_equal(length(res), 1)
231+
expect_true(grepl("(\\.dylib)$", res[1]))
232+
}
233+
{
234+
local_mocked_bindings(
235+
is_macos = function() {TRUE},
236+
list.files = function (path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, ...) {
237+
# Mimic being able to find .a only
238+
if(pattern == "libodbcinst.a") return(pattern)
239+
return(character())
240+
}
241+
)
242+
res <- locate_install_unixodbc()
243+
expect_equal(length(res), 1)
244+
expect_true(grepl("(\\.a)$", res[1]))
245+
}
246+
{
247+
local_mocked_bindings(
248+
is_macos = function() {TRUE},
249+
list.files = function (path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, ...) {
250+
# Mimic being able to find shlib only
251+
if(pattern == "libodbcinst.dylib") return(pattern)
252+
return(character())
253+
}
254+
)
255+
res <- locate_install_unixodbc()
256+
expect_equal(length(res), 1)
257+
expect_true(grepl("(\\.dylib)$", res[1]))
258+
}
259+
})
260+
217261
test_that("databricks() errors informatively when spark ini isn't writeable", {
218262
local_mocked_bindings(is_writeable = function(path) {FALSE})
219263
expect_snapshot(

0 commit comments

Comments
 (0)