From 344db094427b4b1c337beaf8169b1032a7596bc9 Mon Sep 17 00:00:00 2001
From: Dorchies David <david.dorchies@inrae.fr>
Date: Fri, 4 Feb 2022 16:15:26 +0100
Subject: [PATCH] feat(loadConfig): rsic2 configuration handling

Fix #1
---
 DESCRIPTION                      |  6 ++++
 NAMESPACE                        |  3 ++
 inst/config.yml                  | 13 ++++++++
 man/loadConfig.Rd                | 55 ++++++++++++++++++++++++++++++++
 rsic2.Rproj                      |  1 +
 tests/testthat.R                 |  4 +++
 tests/testthat/test-loadConfig.R | 33 +++++++++++++++++++
 7 files changed, 115 insertions(+)
 create mode 100644 NAMESPACE
 create mode 100644 inst/config.yml
 create mode 100644 man/loadConfig.Rd
 create mode 100644 tests/testthat.R
 create mode 100644 tests/testthat/test-loadConfig.R

diff --git a/DESCRIPTION b/DESCRIPTION
index e8e6d1b..1ef2716 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -10,7 +10,13 @@ License: What license is it under?
 Encoding: UTF-8
 LazyData: true
 Suggests: 
+    R.utils,
+    RandomFields,
     testthat (>= 3.0.0)
 Config/testthat/edition: 3
 Depends: 
     R (>= 2.10)
+RoxygenNote: 7.1.1
+Roxygen: list(markdown = TRUE)
+Imports: 
+    terra
diff --git a/NAMESPACE b/NAMESPACE
new file mode 100644
index 0000000..d4e57ad
--- /dev/null
+++ b/NAMESPACE
@@ -0,0 +1,3 @@
+# Generated by roxygen2: do not edit by hand
+
+export(loadConfig)
diff --git a/inst/config.yml b/inst/config.yml
new file mode 100644
index 0000000..ebdfcb2
--- /dev/null
+++ b/inst/config.yml
@@ -0,0 +1,13 @@
+default:
+  sic:
+    path: "*** The path of your installation of SIC should be defined ***"
+    edisic: "exe/EdiSIC.exe"
+    talweg: "exe/TALWEG.exe"
+    fluvia: "exe/FLUVIA.exe"
+    sirene: "exe/SIRENE.exe"
+    export: "exe/SicExport.exe"
+    fortran:
+      prms:
+        INTERF: "0"
+  project:
+    path: "*** The path of your XML project file should be defined ***"
diff --git a/man/loadConfig.Rd b/man/loadConfig.Rd
new file mode 100644
index 0000000..a441095
--- /dev/null
+++ b/man/loadConfig.Rd
@@ -0,0 +1,55 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/loadConfig.R
+\name{loadConfig}
+\alias{loadConfig}
+\title{Read default configuration of the package and complete it with eventual user config}
+\usage{
+loadConfig(
+  sic_path = NULL,
+  xml_path = NULL,
+  userFile = "config.yml",
+  pathDefaultCfg = system.file("config.yml", package = "rsic2")
+)
+}
+\arguments{
+\item{userFile}{location of the user config YML file}
+
+\item{pathDefaultCfg}{The location of the default configuration (located in "inst/config.yml" of the package by default)}
+}
+\value{
+A configuration as it is returned by \link[config:get]{config::get}.
+
+Configuration of RSIC2 as the following structure:
+\itemize{
+\item sic
+\itemize{
+\item path: Path of local SIC installation (should be defined by \code{sic_path} parameter or in the user config file)
+\item edisic: sub-path to EdiSIC program
+\item talweg: sub-path to TALWEG program
+\item fluvia: sub-path to FLUVIA program
+\item sirene: sub-path to SIRENE program
+\item export: sub-path to SicExport program
+\item fortran:
+\itemize{
+\item prms:
+\itemize{
+\item INTERF: default \code{INTERF} parameter injected in command line arguments of TALWEG, FLUVIA, SIRENE
+}
+}
+}
+\item project
+\itemize{
+\item path: Path to the XML project file (should be defined by \code{xml_path} parameter or in the user config file)
+}
+}
+}
+\description{
+Read default configuration of the package and complete it with eventual user config
+}
+\examples{
+library(sic2)
+sic_path <- tempdir(check = TRUE)
+xml_path <- R.utils::tmpfile()
+cfg <- loadConfig(sic_path, xml_path)
+str(cfg)
+}
diff --git a/rsic2.Rproj b/rsic2.Rproj
index 497f8bf..270314b 100644
--- a/rsic2.Rproj
+++ b/rsic2.Rproj
@@ -18,3 +18,4 @@ StripTrailingWhitespace: Yes
 BuildType: Package
 PackageUseDevtools: Yes
 PackageInstallArgs: --no-multiarch --with-keep.source
+PackageRoxygenize: rd,collate,namespace
diff --git a/tests/testthat.R b/tests/testthat.R
new file mode 100644
index 0000000..184d066
--- /dev/null
+++ b/tests/testthat.R
@@ -0,0 +1,4 @@
+library(testthat)
+library(rsic2)
+
+test_check("rsic2")
diff --git a/tests/testthat/test-loadConfig.R b/tests/testthat/test-loadConfig.R
new file mode 100644
index 0000000..1828a28
--- /dev/null
+++ b/tests/testthat/test-loadConfig.R
@@ -0,0 +1,33 @@
+test_that("incomplete config throw an error", {
+  expect_error(loadConfig())
+})
+
+sic_path <- tempdir(check = TRUE)
+xml_path <- tempfile(fileext = ".xml")
+writeLines("<XML>Fake project</XML>", xml_path)
+
+test_that("Wrong SIC path throw an error", {
+  expect_error(loadConfig(file.path(sic_path, "fake"), xml_path))
+})
+
+test_that("Wrong XML path throw an error", {
+  expect_error(loadConfig(sic_path, file.path(xml_path, "fake.xml")))
+})
+
+test_that("SIC and XML paths injected by parameters should be provided in cfg", {
+  cfg <- loadConfig(sic_path, xml_path)
+  expect_equal(cfg$sic$path, sic_path)
+  expect_equal(cfg$project$path, xml_path)
+})
+
+test_that("SIC and XML paths injected by user yml file should be provided in cfg", {
+  tmpCfgPath <- tempfile(fileext = ".yml")
+  ymlCfg <- list(default = list(
+    sic = list(path = sic_path),
+    project = list(path = xml_path)
+  ))
+  yaml::write_yaml(ymlCfg, tmpCfgPath)
+  cfg <- loadConfig(userFile = tmpCfgPath)
+  expect_equal(cfg$sic$path, sic_path)
+  expect_equal(cfg$project$path, xml_path)
+})
-- 
GitLab