Skip to content

Commit

Permalink
#772: Throw an error if installation of R package fails and run integ…
Browse files Browse the repository at this point in the history
…ration tests for R package in standard container
  • Loading branch information
tomuben committed Nov 20, 2024
1 parent e46eb9b commit 1293fa3
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 139 deletions.
57 changes: 53 additions & 4 deletions ext/scripts/install_scripts/install_via_r_remotes.pl
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,57 @@ =head1 SYNOPSIS
}


my $combining_template = "library(remotes)\n<<<<0>>>>";
my $combining_template = '
library(remotes)
install_or_fail <- function(package_name, version){
tryCatch({install_version(package_name, version, repos="https://cloud.r-project.org", Ncpus=4)
library(package_name, character.only = TRUE)},
error = function(e){
print(e)
stop(paste("installation failed for:",package_name ))},
warning = function(w){
catch <-
grepl("download of package .* failed", w$message) ||
grepl("(dependenc|package).*(is|are) not available", w$message) ||
grepl("installation of package.*had non-zero exit status", w$message) ||
grepl("installation of one or more packages failed", w$message)
if(catch){ print(w$message)
stop(paste("installation failed for:",package_name ))}}
)
}
<<<<0>>>>
';

my $combining_template_validation = '
available_packages <- available.packages()
validate_or_fail <- function(package_name){
# Check if the package is in the list of available packages
is_installed <- package_name %in% rownames(available_packages)
# Check the result
if (!is_installed) {
stop(paste("Package nor installed:", package_name))
}
}
<<<<0>>>>
';


my @separators = ("\n");
my @templates = ('install_version("<<<<0>>>>",NULL,repos="https://cloud.r-project.org", Ncpus=4)');
my @templates = ('install_or_fail("<<<<0>>>>",NULL)');
if($with_versions){
@templates = ('install_version("<<<<0>>>>","<<<<1>>>>",repos="https://cloud.r-project.org", Ncpus=4)');
@templates = ('install_or_fail("<<<<0>>>>","<<<<1>>>>")');
}

my @validation_templates = ('validate_or_fail("<<<<0>>>>")');

sub identity {
my ($line) = @_;
return $line
Expand All @@ -77,10 +121,15 @@ sub replace_missing_version{
if($with_versions and $allow_no_version){
@rendered_line_transformation_functions = (\&replace_missing_version);
}
my @rendered_line_transformation_functions_validation = (\&identity);

my $script =
package_mgmt_utils::generate_joined_and_transformed_string_from_file(
$file,$element_separator,$combining_template,\@templates,\@separators,\@rendered_line_transformation_functions);
$file,$element_separator,$combining_template,\@templates,\@separators,\@rendered_line_transformation_functions) .
package_mgmt_utils::generate_joined_and_transformed_string_from_file(
$file,$element_separator,$combining_template_validation,\@validation_templates,\@separators,\@rendered_line_transformation_functions_validation);



if($with_versions and not $allow_no_version){
if (index($script, "<<<<1>>>>") != -1) {
Expand Down
96 changes: 0 additions & 96 deletions ext/scripts/install_scripts/install_via_r_versions.pl

This file was deleted.

36 changes: 0 additions & 36 deletions ext/scripts/tests/install_scripts/run_r_versions_tests.sh

This file was deleted.

3 changes: 0 additions & 3 deletions ext/scripts/tests/install_scripts/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,5 @@ bash run_apt_tests.sh "$@"
echo Run Pip Tests
bash run_pip_tests.sh "$@"

# echo Run R versions Tests
# bash run_r_versions_tests.sh "$@"

echo Run R remotes Tests
bash run_r_remotes_tests.sh "$@"
60 changes: 60 additions & 0 deletions test_container/tests/test/standard-flavor/all/import_r_modules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3
from typing import List

from exasol_python_test_framework import udf
from exasol_python_test_framework.udf.udf_debug import UdfDebugger

class ImportAllModulesTest(udf.TestCase):

def setUp(self):
self.query('create schema import_all_r_modules', ignore_errors=True)

def get_all_root_modules(self) -> List[str]:
self.query(udf.fixindent('''
CREATE OR REPLACE r SCALAR SCRIPT import_all_r_modules.get_all_root_modules()
EMITS (module_name VARCHAR(200000)) AS
run <- function(ctx) {
library(data.table)
file_pattern <- "cran_packages"
directory <- "/build_info/packages"
files <- list.files(path = directory, pattern = file_pattern, full.names = TRUE, recursive = TRUE)
for (file in files) {
package_list <- fread(file, sep="|", header = FALSE, col.names = c("Package", "Version"))
package_names <- package_list[[1]]
ctx$emit(package_names)
}
}
/
'''))
rows = self.query('''SELECT import_all_r_modules.get_all_root_modules() FROM dual''')
print("Number of modules:",len(rows))
root_modules = [row[0] for row in rows]
print(f"Found {len(root_modules)} root modules.")
return root_modules

def create_check_installed_package_udf(self):
self.query(udf.fixindent('''
CREATE OR REPLACE r SCALAR SCRIPT
import_all_r_modules.check_installed_package(package_name VARCHAR(200000))
RETURNS DECIMAL(11,0) AS
run <- function(ctx) {
library(ctx$package_name, character.only = TRUE)
0
}
/
'''))

def test_import_all_modules(self):
root_modules = self.get_all_root_modules()
assert len(root_modules) > 0
self.create_check_installed_package_udf()
for root_module in root_modules:
# with UdfDebugger(test_case=self):
rows = self.query(f'''SELECT import_all_r_modules.check_installed_package('{root_module}') FROM dual''')

def tearDown(self):
self.query("drop schema import_all_r_modules cascade", ignore_errors=True)


if __name__ == '__main__':
udf.main()

0 comments on commit 1293fa3

Please sign in to comment.