Test R latest MRAN date handling
MODIFY
repo2docker/buildpacks/r.py
repo2docker/buildpacks/r.py
@@ -195,7 +195,7 @@ class RBuildPack(PythonBuildPack):
Starts from `startdate` and tries up to `max_prior` previous days.
Raises `requests.HTTPError` with the last tried URL if no working snapshot found.
"""
- for days in range(0, max_prior + 1):
+ for days in range(max_prior + 1):
test_date = startdate - datetime.timedelta(days=days)
mran_url = "https://mran.microsoft.com/snapshot/{}".format(
test_date.isoformat()

MODIFY
tests/unit/test_r.py
tests/unit/test_r.py
from datetime import date
import pytest
+from requests.models import Response
+from unittest.mock import patch
from repo2docker import buildpacks
@@ -62,6 +64,32 @@ def test_mran_date(tmpdir, runtime, expected):
assert r.checkpoint_date == date(*expected)
+@pytest.mark.parametrize(
+ "expected", ["2019-12-29", "2019-12-26"],
+)
+def test_mran_latestdate(tmpdir, expected):
+ def mock_request_head(url):
+ r = Response()
+ if url == "https://mran.microsoft.com/snapshot/" + expected:
+ r.status_code = 200
+ else:
+ r.status_code = 404
+ r.reason = "Mock MRAN no snapshot"
+ return r
+
+ tmpdir.chdir()
+
+ with open("DESCRIPTION", "w") as f:
+ f.write("")
+
+ with patch("requests.head", side_effect=mock_request_head):
+ with patch("datetime.date") as mockdate:
+ mockdate.today.return_value = date(2019, 12, 31)
+ r = buildpacks.RBuildPack()
+ r.detect()
+ assert r.checkpoint_date.isoformat() == expected
+
+
def test_install_from_base(tmpdir):
# check that for R==3.4 we install from ubuntu
tmpdir.chdir()

Test R latest MRAN date handling