-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add helper to locate configuration files (#680)
- Loading branch information
1 parent
8528e33
commit 6a8e295
Showing
11 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#' List locations of ODBC configuration files | ||
#' | ||
#' @description | ||
#' On MacOS and Linux, odbc uses the unixODBC driver manager to manage | ||
#' information about driver and data sources. This helper returns the filepaths | ||
#' where the driver manager will look for that information. | ||
#' | ||
#' This function is a wrapper around the command line call `odbcinst -j`. | ||
#' | ||
#' Windows does not use `.ini` configuration files; this function will return a | ||
#' 0-length vector on Windows. | ||
#' | ||
#' @seealso | ||
#' The [odbcListDrivers()] and [odbcListDataSources()] helpers return | ||
#' information on the contents of `odbcinst.ini` and `odbc.ini` files, | ||
#' respectively. | ||
#' | ||
#' Learn more about unixODBC and the `odbcinst` utility | ||
#' [here](https://www.unixodbc.org/odbcinst.html). | ||
#' | ||
#' @examplesIf FALSE | ||
#' configs <- odbcListConfig() | ||
#' | ||
#' file.edit(configs[1]) | ||
#' @export | ||
odbcListConfig <- function() { | ||
if (is_windows()) { | ||
return(character(0)) | ||
} | ||
|
||
if (!has_odbc()) { | ||
abort( | ||
c("The unixODBC driver manager is not available. ", | ||
"Please install and try again.") | ||
) | ||
} | ||
|
||
res <- system("odbcinst -j", intern = TRUE) | ||
res <- res[grepl("\\.ini", res)] | ||
res <- strsplit(res, "\\:") | ||
|
||
if (!identical(vapply(res, length, numeric(1)), c(2, 2, 2))) { | ||
abort("Failed to parse output from odbcinst.", .internal = TRUE) | ||
} | ||
|
||
res <- vapply(res, `[[`, character(1), 2) | ||
res <- trimws(res) | ||
names(res) <- c("drivers", "system_dsn", "user_dsn") | ||
|
||
res | ||
} | ||
|
||
system <- NULL | ||
|
||
has_odbc <- function() { | ||
!identical(unname(Sys.which("odbcinst")), "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# odbcListConfig errors informatively without unixODBC | ||
|
||
Code | ||
odbcListConfig() | ||
Condition | ||
Error in `odbcListConfig()`: | ||
! The unixODBC driver manager is not available. | ||
* Please install and try again. | ||
|
||
# odbcListConfig errors informatively with unexpected odbcinst output | ||
|
||
Code | ||
odbcListConfig() | ||
Condition | ||
Error in `odbcListConfig()`: | ||
! Failed to parse output from odbcinst. | ||
i This is an internal error that was detected in the odbc package. | ||
Please report it at <https://github.com/r-dbi/odbc/issues> with a reprex (<https://tidyverse.org/help/>) and the full backtrace. | ||
|
||
--- | ||
|
||
Code | ||
odbcListConfig() | ||
Condition | ||
Error in `odbcListConfig()`: | ||
! Failed to parse output from odbcinst. | ||
i This is an internal error that was detected in the odbc package. | ||
Please report it at <https://github.com/r-dbi/odbc/issues> with a reprex (<https://tidyverse.org/help/>) and the full backtrace. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
test_that("odbcListConfig returns appropriate result", { | ||
skip_on_os(c("windows", "solaris")) | ||
skip_if(!has_odbc(), "odbcinst not available.") | ||
|
||
res <- odbcListConfig() | ||
|
||
expect_type(res, "character") | ||
expect_length(res, 3) | ||
expect_named(res, c("drivers", "system_dsn", "user_dsn")) | ||
expect_match(res, "\\.ini") | ||
}) | ||
|
||
test_that("odbcListConfig returns an empty vector on Windows", { | ||
local_mocked_bindings(is_windows = function() {TRUE}) | ||
|
||
res <- odbcListConfig() | ||
|
||
expect_equal(res, character(0)) | ||
}) | ||
|
||
test_that("odbcListConfig errors informatively without unixODBC", { | ||
local_mocked_bindings(is_windows = function() {FALSE}, | ||
has_odbc = function() {FALSE}) | ||
|
||
expect_snapshot(error = TRUE, odbcListConfig()) | ||
}) | ||
|
||
test_that("odbcListConfig errors informatively with unexpected odbcinst output", { | ||
local_mocked_bindings(is_windows = function() {FALSE}, | ||
has_odbc = function() {TRUE}) | ||
|
||
local_mocked_bindings(system = function(...) {c("beep", "bop")}) | ||
expect_snapshot(error = TRUE, odbcListConfig()) | ||
|
||
local_mocked_bindings(system = function(...) {""}) | ||
expect_snapshot(error = TRUE, odbcListConfig()) | ||
}) |