Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -375,37 +375,48 @@ locate_install_unixodbc <- function() {
"/opt/R/x86_64/lib"
)

list.files(
common_dirs,
pattern = libodbcinst_filename(),
full.names = TRUE
)
file_names <- libodbcinst_filename()
for (file_name in file_names) {
paths <- list.files(
common_dirs,
pattern = file_name,
full.names = TRUE
)
if (length(paths)) {
return(paths)
}
}
return(character())
}

system_safely <- function(x) {
tryCatch(
{
unixodbc_prefix <- system(x, intern = TRUE, ignore.stderr = TRUE)
candidates <- list.files(unixodbc_prefix,
pattern = libodbcinst_filename(), full.names = TRUE)
if (!length(candidates)) {
stop("Unable to locate unixodbc using odbc_config")
file_names <- libodbcinst_filename()
for (file_name in file_names) {
candidates <- list.files(unixodbc_prefix,
pattern = file_name, full.names = TRUE)
if (length(candidates)) {
return(candidates[1])
}
}
return(candidates[1])
stop("Unable to locate unixodbc using odbc_config")
},
error = function(e) {},
warning = function(w) {}
)
character()
}

# Returns a pattern to be used with
# list.files( ..., pattern = ... ).
# Returns a vector of possible lib filenames.
# Vector is ordered and should be iterated over
# in same order.
libodbcinst_filename <- function() {
if (is_macos()) {
"libodbcinst.dylib|libodbcinst.a"
return(c("libodbcinst.dylib", "libodbcinst.a"))
} else {
"libodbcinst.so|libodbcinst.a"
return(c("libodbcinst.so", "libodbcinst.a"))
}
}

Expand Down
44 changes: 44 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,50 @@ test_that("locate_install_unixodbc() returns reasonable values", {
expect_true(grepl("(\\.dylib|\\.a)$", res[1]))
})

# https://github.com/r-dbi/odbc/issues/919
test_that("locate_install_unixodbc() prefers shlib", {
# odbc_config / pkg-config cflags point to nonexistent files on CRAN (#903)
skip_on_cran()
{
local_mocked_bindings(
is_macos = function() {TRUE},
list.files = function (path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, ...) {
# Mimic being able to find any pattern passed as argument
return(pattern)
}
)
res <- locate_install_unixodbc()
expect_equal(length(res), 1)
expect_true(grepl("(\\.dylib)$", res[1]))
}
{
local_mocked_bindings(
is_macos = function() {TRUE},
list.files = function (path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, ...) {
# Mimic being able to find .a only
if(pattern == "libodbcinst.a") return(pattern)
return(character())
}
)
res <- locate_install_unixodbc()
expect_equal(length(res), 1)
expect_true(grepl("(\\.a)$", res[1]))
}
{
local_mocked_bindings(
is_macos = function() {TRUE},
list.files = function (path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, ...) {
# Mimic being able to find shlib only
if(pattern == "libodbcinst.dylib") return(pattern)
return(character())
}
)
res <- locate_install_unixodbc()
expect_equal(length(res), 1)
expect_true(grepl("(\\.dylib)$", res[1]))
}
})

test_that("databricks() errors informatively when spark ini isn't writeable", {
local_mocked_bindings(is_writeable = function(path) {FALSE})
expect_snapshot(
Expand Down
Loading