예제 #1
0
def test_binder():
    """Testing binder URL generation and checks."""
    file_path = 'myfile.py'
    conf1 = {'url': 'http://test1.com', 'org': 'org',
             'repo': 'repo', 'branch': 'branch',
             'dependencies': '../requirements.txt'}
    url = gen_binder_url(file_path, conf1)
    assert url == 'http://test1.com/v2/gh/org/repo/branch?filepath=_downloads/myfile.ipynb'

    # Assert filepath prefix is added
    prefix = 'my_prefix/foo'
    conf1['filepath_prefix'] = prefix
    url = gen_binder_url(file_path, conf1)
    assert url == 'http://test1.com/v2/gh/org/repo/branch?filepath={}/_downloads/myfile.ipynb'.format(prefix)
    conf1.pop('filepath_prefix')

    # URL must have http
    with pytest.raises(ValueError) as excinfo:
        conf2 = deepcopy(conf1)
        conf2['url'] = 'test1.com'
        url = check_binder_conf(conf2)

    excinfo.match(r'did not supply a valid url')

    # Assert missing params
    for key in conf1.keys():
        conf3 = deepcopy(conf1)
        conf3.pop(key)
        with pytest.raises(ValueError) as excinfo:
            url = check_binder_conf(conf3)
        excinfo.match(r"binder_conf is missing values for")

    # Dependencies file
    dependency_file_tests = ['requirements_not.txt', 'doc-requirements.txt']
    for ifile in dependency_file_tests:
        with pytest.raises(ValueError) as excinfo:
            conf3 = deepcopy(conf1)
            conf3['dependencies'] = ifile
            url = check_binder_conf(conf3)
        excinfo.match(r"Did not find one of `requirements.txt` or `environment.yml`")

    with pytest.raises(ValueError) as excinfo:
        conf6 = deepcopy(conf1)
        conf6['dependencies'] = {'test': 'test'}
        url = check_binder_conf(conf6)
    excinfo.match(r"`dependencies` value should be a list of strings")

    # Check returns the correct object
    conf4 = check_binder_conf({})
    conf5 = check_binder_conf(None)
    for iconf in [conf4, conf5]:
        assert iconf == {}

    # Assert extra unkonwn params
    with pytest.raises(ValueError) as excinfo:
        conf7 = deepcopy(conf1)
        conf7['foo'] = 'blah'
        url = check_binder_conf(conf7)
    excinfo.match(r"Unknown Binder config key")
def alt_gen_binder_rst(fpath,
                       binder_conf,
                       gallery_conf,
                       img="https://mybinder.org/badge_logo.svg"):
    """Generate the RST + link for the Binder badge.
    Parameters
    ----------
    fpath: str
        The path to the `.py` file for which a Binder badge will be generated.
    binder_conf: dict or None
        If a dictionary it must have the following keys:
        'binderhub_url'
            The URL of the BinderHub instance that's running a Binder service.
        'org'
            The GitHub organization to which the documentation will be pushed.
        'repo'
            The GitHub repository to which the documentation will be pushed.
        'branch'
            The Git branch on which the documentation exists (e.g., gh-pages).
        'dependencies'
            A list of paths to dependency files that match the Binderspec.
    Returns
    -------
    rst : str
        The reStructuredText for the Binder badge that links to this file.
    """
    binder_conf = binder.check_binder_conf(binder_conf)
    binder_url = binder.gen_binder_url(fpath, binder_conf, gallery_conf)
    rst = ("\n"
           ".. image:: {0}\n"
           "    :target: {1}\n"
           "    :alt: Launch Binder\n").format(img, binder_url)
    return rst
예제 #3
0
def test_binder():
    """Testing binder URL generation and checks."""
    file_path = 'blahblah/mydir/myfile.py'
    conf_base = {'binderhub_url': 'http://test1.com', 'org': 'org',
                 'repo': 'repo', 'branch': 'branch',
                 'dependencies': '../requirements.txt'}
    conf_base = check_binder_conf(conf_base)
    gallery_conf_base = {'gallery_dirs': 'mydir', 'src_dir': 'blahblah'}

    url = gen_binder_url(file_path, conf_base, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?filepath=notebooks/mydir/myfile.ipynb')
    assert url == expected

    # Assert filepath prefix is added
    prefix = 'my_prefix/foo'
    conf1 = deepcopy(conf_base)
    conf1['filepath_prefix'] = prefix
    url = gen_binder_url(file_path, conf1, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?filepath={}/notebooks/'
                'mydir/myfile.ipynb').format(prefix)

    assert url == expected
    conf1.pop('filepath_prefix')

    # URL must have http
    conf2 = deepcopy(conf1)
    conf2['binderhub_url'] = 'test1.com'
    with pytest.raises(ConfigError, match='did not supply a valid url'):
        url = check_binder_conf(conf2)

    # Assert missing params
    for key in conf1.keys():
        if key == 'notebooks_dir':
            continue
        conf3 = deepcopy(conf1)
        conf3.pop(key)
        with pytest.raises(ConfigError, match='binder_conf is missing values'):
            url = check_binder_conf(conf3)

    # Dependencies file
    dependency_file_tests = ['requirements_not.txt', 'doc-requirements.txt']
    for ifile in dependency_file_tests:
        conf3 = deepcopy(conf1)
        conf3['dependencies'] = ifile
        with pytest.raises(ConfigError,
                           match=r"Did not find one of `requirements.txt` "
                           "or `environment.yml`"):
            url = check_binder_conf(conf3)

    conf6 = deepcopy(conf1)
    conf6['dependencies'] = {'test': 'test'}
    with pytest.raises(ConfigError, match='`dependencies` value should be a '
                       'list of strings'):
        url = check_binder_conf(conf6)

    # Missing requirements file should raise an error
    conf7 = deepcopy(conf1)
    conf7['dependencies'] = ['requirements.txt', 'this_doesntexist.txt']
    # Hack to test the case when dependencies point to a non-existing file
    # w/o needing to do a full build

    def apptmp():
        pass
    apptmp.srcdir = '/'
    with pytest.raises(ConfigError, match="Couldn't find the Binder "
                       "requirements file"):
        url = _copy_binder_reqs(apptmp, conf7)

    # Check returns the correct object
    conf4 = check_binder_conf({})
    conf5 = check_binder_conf(None)
    for iconf in [conf4, conf5]:
        assert iconf == {}

    # Assert extra unkonwn params
    conf7 = deepcopy(conf1)
    conf7['foo'] = 'blah'
    with pytest.raises(ConfigError, match='Unknown Binder config key'):
        url = check_binder_conf(conf7)

    # Assert using lab correctly changes URL
    conf_lab = deepcopy(conf_base)
    conf_lab['use_jupyter_lab'] = True
    url = gen_binder_url(file_path, conf_lab, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?urlpath=lab/tree/notebooks/mydir/myfile.ipynb')
    assert url == expected

    # Assert using static folder correctly changes URL
    conf_static = deepcopy(conf_base)
    file_path = 'blahblah/mydir/myfolder/myfile.py'
    conf_static['notebooks_dir'] = 'ntbk_folder'
    url = gen_binder_url(file_path, conf_static, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?filepath=ntbk_folder/mydir/myfolder/myfile.ipynb')
    assert url == expected
예제 #4
0
def test_binder():
    """Testing binder URL generation and checks."""
    file_path = 'blahblah/mydir/myfile.py'
    conf_base = {'url': 'http://test1.com', 'org': 'org',
                 'repo': 'repo', 'branch': 'branch',
                 'dependencies': '../requirements.txt'}
    conf_base = check_binder_conf(conf_base)
    gallery_conf_base = {'gallery_dirs': ['mydir'], 'src_dir': 'blahblah'}

    url = gen_binder_url(file_path, conf_base, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?filepath=notebooks/mydir/myfile.ipynb')
    assert url == expected

    # Assert filepath prefix is added
    prefix = 'my_prefix/foo'
    conf1 = deepcopy(conf_base)
    conf1['filepath_prefix'] = prefix
    url = gen_binder_url(file_path, conf1, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?filepath={}/notebooks/'
                'mydir/myfile.ipynb').format(prefix)

    assert url == expected
    conf1.pop('filepath_prefix')

    # URL must have http
    with pytest.raises(ValueError) as excinfo:
        conf2 = deepcopy(conf1)
        conf2['url'] = 'test1.com'
        url = check_binder_conf(conf2)

    excinfo.match(r'did not supply a valid url')

    # Assert missing params
    for key in conf1.keys():
        if key == 'notebooks_dir':
            continue
        conf3 = deepcopy(conf1)
        conf3.pop(key)
        with pytest.raises(ValueError) as excinfo:
            url = check_binder_conf(conf3)
        excinfo.match(r"binder_conf is missing values for")

    # Dependencies file
    dependency_file_tests = ['requirements_not.txt', 'doc-requirements.txt']
    for ifile in dependency_file_tests:
        with pytest.raises(ValueError) as excinfo:
            conf3 = deepcopy(conf1)
            conf3['dependencies'] = ifile
            url = check_binder_conf(conf3)
        excinfo.match(r"Did not find one of `requirements.txt` "
                      "or `environment.yml`")

    with pytest.raises(ValueError) as excinfo:
        conf6 = deepcopy(conf1)
        conf6['dependencies'] = {'test': 'test'}
        url = check_binder_conf(conf6)
    excinfo.match(r"`dependencies` value should be a list of strings")

    # Check returns the correct object
    conf4 = check_binder_conf({})
    conf5 = check_binder_conf(None)
    for iconf in [conf4, conf5]:
        assert iconf == {}

    # Assert extra unkonwn params
    with pytest.raises(ValueError) as excinfo:
        conf7 = deepcopy(conf1)
        conf7['foo'] = 'blah'
        url = check_binder_conf(conf7)
    excinfo.match(r"Unknown Binder config key")

    # Assert using lab correctly changes URL
    conf_lab = deepcopy(conf_base)
    conf_lab['use_jupyter_lab'] = True
    url = gen_binder_url(file_path, conf_lab, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?urlpath=lab/tree/notebooks/mydir/myfile.ipynb')
    assert url == expected

    # Assert using static folder correctly changes URL
    conf_static = deepcopy(conf_base)
    file_path = 'blahblah/mydir/myfolder/myfile.py'
    conf_static['notebooks_dir'] = 'ntbk_folder'
    url = gen_binder_url(file_path, conf_static, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?filepath=ntbk_folder/mydir/myfolder/myfile.ipynb')
    assert url == expected
예제 #5
0
def test_binder():
    """Testing binder URL generation and checks."""
    file_path = 'blahblah/mydir/myfile.py'
    conf_base = {'binderhub_url': 'http://test1.com', 'org': 'org',
                 'repo': 'repo', 'branch': 'branch',
                 'dependencies': '../requirements.txt'}
    conf_base = check_binder_conf(conf_base)
    gallery_conf_base = {'gallery_dirs': 'mydir', 'src_dir': 'blahblah'}

    url = gen_binder_url(file_path, conf_base, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?filepath=notebooks/mydir/myfile.ipynb')
    assert url == expected

    # Assert filepath prefix is added
    prefix = 'my_prefix/foo'
    conf1 = deepcopy(conf_base)
    conf1['filepath_prefix'] = prefix
    url = gen_binder_url(file_path, conf1, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?filepath={}/notebooks/'
                'mydir/myfile.ipynb').format(prefix)

    assert url == expected
    conf1.pop('filepath_prefix')

    # URL must have http
    conf2 = deepcopy(conf1)
    conf2['binderhub_url'] = 'test1.com'
    with pytest.raises(ValueError, match='did not supply a valid url'):
        url = check_binder_conf(conf2)

    # Assert missing params
    for key in conf1.keys():
        if key == 'notebooks_dir':
            continue
        conf3 = deepcopy(conf1)
        conf3.pop(key)
        with pytest.raises(ValueError, match='binder_conf is missing values'):
            url = check_binder_conf(conf3)

    # Dependencies file
    dependency_file_tests = ['requirements_not.txt', 'doc-requirements.txt']
    for ifile in dependency_file_tests:
        conf3 = deepcopy(conf1)
        conf3['dependencies'] = ifile
        with pytest.raises(ValueError,
                           match=r"Did not find one of `requirements.txt` "
                           "or `environment.yml`"):
            url = check_binder_conf(conf3)

    conf6 = deepcopy(conf1)
    conf6['dependencies'] = {'test': 'test'}
    with pytest.raises(ValueError, match='`dependencies` value should be a '
                       'list of strings'):
        url = check_binder_conf(conf6)

    # Missing requirements file should raise an error
    conf7 = deepcopy(conf1)
    conf7['dependencies'] = ['requirements.txt', 'this_doesntexist.txt']
    # Hack to test the case when dependencies point to a non-existing file
    # w/o needing to do a full build

    def apptmp():
        pass
    apptmp.srcdir = '/'
    with pytest.raises(ValueError, match="Couldn't find the Binder "
                       "requirements file"):
        url = _copy_binder_reqs(apptmp, conf7)

    # Check returns the correct object
    conf4 = check_binder_conf({})
    conf5 = check_binder_conf(None)
    for iconf in [conf4, conf5]:
        assert iconf == {}

    # Assert extra unkonwn params
    conf7 = deepcopy(conf1)
    conf7['foo'] = 'blah'
    with pytest.raises(ValueError, match='Unknown Binder config key'):
        url = check_binder_conf(conf7)

    # Assert using lab correctly changes URL
    conf_lab = deepcopy(conf_base)
    conf_lab['use_jupyter_lab'] = True
    url = gen_binder_url(file_path, conf_lab, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?urlpath=lab/tree/notebooks/mydir/myfile.ipynb')
    assert url == expected

    # Assert using static folder correctly changes URL
    conf_static = deepcopy(conf_base)
    file_path = 'blahblah/mydir/myfolder/myfile.py'
    conf_static['notebooks_dir'] = 'ntbk_folder'
    url = gen_binder_url(file_path, conf_static, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?filepath=ntbk_folder/mydir/myfolder/myfile.ipynb')
    assert url == expected
예제 #6
0
def test_binder():
    """Testing binder URL generation and checks."""
    file_path = 'blahblah/mydir/myfile.py'
    conf_base = {
        'url': 'http://test1.com',
        'org': 'org',
        'repo': 'repo',
        'branch': 'branch',
        'dependencies': '../requirements.txt'
    }
    conf_base = check_binder_conf(conf_base)
    gallery_conf_base = {'gallery_dirs': ['mydir'], 'src_dir': 'blahblah'}

    url = gen_binder_url(file_path, conf_base, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?filepath=notebooks/mydir/myfile.ipynb')
    assert url == expected

    # Assert filepath prefix is added
    prefix = 'my_prefix/foo'
    conf1 = deepcopy(conf_base)
    conf1['filepath_prefix'] = prefix
    url = gen_binder_url(file_path, conf1, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?filepath={}/notebooks/'
                'mydir/myfile.ipynb').format(prefix)

    assert url == expected
    conf1.pop('filepath_prefix')

    # URL must have http
    with pytest.raises(ValueError) as excinfo:
        conf2 = deepcopy(conf1)
        conf2['url'] = 'test1.com'
        url = check_binder_conf(conf2)

    excinfo.match(r'did not supply a valid url')

    # Assert missing params
    for key in conf1.keys():
        if key == 'notebooks_dir':
            continue
        conf3 = deepcopy(conf1)
        conf3.pop(key)
        with pytest.raises(ValueError) as excinfo:
            url = check_binder_conf(conf3)
        excinfo.match(r"binder_conf is missing values for")

    # Dependencies file
    dependency_file_tests = ['requirements_not.txt', 'doc-requirements.txt']
    for ifile in dependency_file_tests:
        with pytest.raises(ValueError) as excinfo:
            conf3 = deepcopy(conf1)
            conf3['dependencies'] = ifile
            url = check_binder_conf(conf3)
        excinfo.match(r"Did not find one of `requirements.txt` "
                      "or `environment.yml`")

    with pytest.raises(ValueError) as excinfo:
        conf6 = deepcopy(conf1)
        conf6['dependencies'] = {'test': 'test'}
        url = check_binder_conf(conf6)
    excinfo.match(r"`dependencies` value should be a list of strings")

    # Check returns the correct object
    conf4 = check_binder_conf({})
    conf5 = check_binder_conf(None)
    for iconf in [conf4, conf5]:
        assert iconf == {}

    # Assert extra unkonwn params
    with pytest.raises(ValueError) as excinfo:
        conf7 = deepcopy(conf1)
        conf7['foo'] = 'blah'
        url = check_binder_conf(conf7)
    excinfo.match(r"Unknown Binder config key")

    # Assert using lab correctly changes URL
    conf_lab = deepcopy(conf_base)
    conf_lab['use_jupyter_lab'] = True
    url = gen_binder_url(file_path, conf_lab, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?urlpath=lab/tree/notebooks/mydir/myfile.ipynb')
    assert url == expected

    # Assert using static folder correctly changes URL
    conf_static = deepcopy(conf_base)
    file_path = 'blahblah/mydir/myfolder/myfile.py'
    conf_static['notebooks_dir'] = 'ntbk_folder'
    url = gen_binder_url(file_path, conf_static, gallery_conf_base)
    expected = ('http://test1.com/v2/gh/org/repo/'
                'branch?filepath=ntbk_folder/mydir/myfolder/myfile.ipynb')
    assert url == expected