<commit_before>"""Test get_artifacts_urls() function."""

from functools import partial

import py
import pytest

from appveyor_artifacts import API_PREFIX, get_artifacts_urls


def mock_query_api(url, replies):
    """Mock JSON replies."""
    return replies[url]


def test_empty(monkeypatch):
    """Test on jobs with no artifacts."""
    monkeypatch.setattr('appveyor_artifacts.query_api', lambda _: list())
    config = dict(always_job_dirs=False, no_job_dirs=None, dir=None)
    assert get_artifacts_urls(config, ['spfxkimxcj6faq57']) == dict()


@pytest.mark.parametrize('always_job_dirs,dir_', [(False, None), (True, py.path.local(__file__).dirpath())])
def test_one(monkeypatch, caplog, always_job_dirs, dir_):
    """Test with one artifact."""
    reply = [{'fileName': '.coverage', 'size': 1692, 'type': 'File'}]
    monkeypatch.setattr('appveyor_artifacts.query_api', lambda _: reply)

    config = dict(always_job_dirs=always_job_dirs, no_job_dirs=None, dir=str(dir_) if dir_ else None)
    actual = get_artifacts_urls(config, ['spfxkimxcj6faq57'])

    expected_local_path = dir_.join('.coverage') if dir_ else py.path.local('.coverage')
    if always_job_dirs:
        expected_local_path = expected_local_path.dirpath().join('spfxkimxcj6faq57', '.coverage')
    expected = {str(expected_local_path): (API_PREFIX + '/buildjobs/spfxkimxcj6faq57/artifacts/.coverage', 1692)}
    assert actual == expected

    messages = [r.message for r in caplog.records()]
    if always_job_dirs:
        assert 'Only one job ID, automatically setting job_dirs = False.' not in messages
    else:
        assert 'Only one job ID, automatically setting job_dirs = False.' in messages


def test_two(monkeypatch, tmpdir):
    """Test with two artifacts in one job."""
    reply = [
        {'fileName': 'appveyor_artifacts.py', 'size': 12479, 'type': 'File'},
        {'fileName': 'README.rst', 'name': 'readme_file.rst', 'size': 1270, 'type': 'File'}
    ]
    monkeypatch.setattr('appveyor_artifacts.query_api', lambda _: reply)

    config = dict(always_job_dirs=False, no_job_dirs=None, dir=str(tmpdir))
    actual = get_artifacts_urls(config, ['spfxkimxcj6faq57'])
    expected = dict([
        (str(tmpdir.join('appveyor_artifacts.py')),
         (API_PREFIX + '/buildjobs/spfxkimxcj6faq57/artifacts/appveyor_artifacts.py', 12479)),
        (str(tmpdir.join('README.rst')), (API_PREFIX + '/buildjobs/spfxkimxcj6faq57/artifacts/README.rst', 1270)),
    ])
    assert actual == expected


def test_multiple_jobs(monkeypatch, tmpdir):
    """Test with multiple jobs.

    From: https://ci.appveyor.com/project/racker-buildbot/luv
    """
    replies = {
        '/buildjobs/v5wnn9k8auqcqovw/artifacts': [
            {'fileName': 'luajit.exe', 'size': 675840, 'type': 'File'},
            {'fileName': 'luv.dll', 'size': 891392, 'type': 'File'}],
        '/buildjobs/bpgcbvqmawv1jw06/artifacts': [
            {'fileName': 'luajit.exe', 'size': 539136, 'type': 'File'},
            {'fileName': 'luv.dll', 'size': 718336, 'type': 'File'}],
    }
    monkeypatch.setattr('appveyor_artifacts.query_api', partial(mock_query_api, replies=replies))

    config = dict(always_job_dirs=False, no_job_dirs=None, dir=str(tmpdir))
    actual = get_artifacts_urls(config, ['v5wnn9k8auqcqovw', 'bpgcbvqmawv1jw06'])
    expected = dict([
        (str(tmpdir.join('v5wnn9k8auqcqovw', 'luajit.exe')),
         (API_PREFIX + '/buildjobs/v5wnn9k8auqcqovw/artifacts/luajit.exe', 675840)),
        (str(tmpdir.join('v5wnn9k8auqcqovw', 'luv.dll')),
         (API_PREFIX + '/buildjobs/v5wnn9k8auqcqovw/artifacts/luv.dll', 891392)),
        (str(tmpdir.join('bpgcbvqmawv1jw06', 'luajit.exe')),
         (API_PREFIX + '/buildjobs/bpgcbvqmawv1jw06/artifacts/luajit.exe', 539136)),
        (str(tmpdir.join('bpgcbvqmawv1jw06', 'luv.dll')),
         (API_PREFIX + '/buildjobs/bpgcbvqmawv1jw06/artifacts/luv.dll', 718336)),
    ])
    assert actual == expected


def test_subdirectory(monkeypatch, tmpdir):
    """Test with artifact "file names" being file paths with subdirectories.

    From: https://ci.appveyor.com/project/sayedihashimi/package-web
    """
    reply = [{'fileName': 'src/OutputRoot/PackageWeb.1.1.17.nupkg', 'size': 60301, 'type': 'NuGetPackage'}]
    monkeypatch.setattr('appveyor_artifacts.query_api', lambda _: reply)

    config = dict(always_job_dirs=False, no_job_dirs=None, dir=str(tmpdir))
    actual = get_artifacts_urls(config, ['r97evl3jva2ejs6b'])
    expected = dict([
        (str(tmpdir.join('src', 'OutputRoot', 'PackageWeb.1.1.17.nupkg')),
         (API_PREFIX + '/buildjobs/r97evl3jva2ejs6b/artifacts/src/OutputRoot/PackageWeb.1.1.17.nupkg', 60301)),
    ])
    assert actual == expected
<commit_msg>Testing config['no_job_dirs']<commit_after>"""Test get_artifacts_urls() function."""

from functools import partial

import py
import pytest

from appveyor_artifacts import API_PREFIX, get_artifacts_urls


def mock_query_api(url, replies):
    """Mock JSON replies."""
    return replies[url]


def test_empty(monkeypatch):
    """Test on jobs with no artifacts."""
    monkeypatch.setattr('appveyor_artifacts.query_api', lambda _: list())
    config = dict(always_job_dirs=False, no_job_dirs=None, dir=None)
    assert get_artifacts_urls(config, ['spfxkimxcj6faq57']) == dict()


@pytest.mark.parametrize('always_job_dirs,dir_', [(False, None), (True, py.path.local(__file__).dirpath())])
def test_one(monkeypatch, caplog, always_job_dirs, dir_):
    """Test with one artifact."""
    reply = [{'fileName': '.coverage', 'size': 1692, 'type': 'File'}]
    monkeypatch.setattr('appveyor_artifacts.query_api', lambda _: reply)

    config = dict(always_job_dirs=always_job_dirs, no_job_dirs=None, dir=str(dir_) if dir_ else None)
    actual = get_artifacts_urls(config, ['spfxkimxcj6faq57'])

    expected_local_path = dir_.join('.coverage') if dir_ else py.path.local('.coverage')
    if always_job_dirs:
        expected_local_path = expected_local_path.dirpath().join('spfxkimxcj6faq57', '.coverage')
    expected = {str(expected_local_path): (API_PREFIX + '/buildjobs/spfxkimxcj6faq57/artifacts/.coverage', 1692)}
    assert actual == expected

    messages = [r.message for r in caplog.records()]
    if always_job_dirs:
        assert 'Only one job ID, automatically setting job_dirs = False.' not in messages
    else:
        assert 'Only one job ID, automatically setting job_dirs = False.' in messages


@pytest.mark.parametrize('no_job_dirs', ['', 'skip'])
def test_two(monkeypatch, caplog, no_job_dirs):
    """Test with two artifacts in one job."""
    reply = [
        {'fileName': 'artifacts.py', 'size': 12479, 'type': 'File'},
        {'fileName': 'README.rst', 'name': 'readme_file.rst', 'size': 1270, 'type': 'File'}
    ]
    monkeypatch.setattr('appveyor_artifacts.query_api', lambda _: reply)

    config = dict(always_job_dirs=False, no_job_dirs=no_job_dirs, dir=None)
    actual = get_artifacts_urls(config, ['spfxkimxcj6faq57'])
    expected = dict([
        (py.path.local('artifacts.py'), (API_PREFIX + '/buildjobs/spfxkimxcj6faq57/artifacts/artifacts.py', 12479)),
        (py.path.local('README.rst'), (API_PREFIX + '/buildjobs/spfxkimxcj6faq57/artifacts/README.rst', 1270)),
    ])
    assert actual == expected

    messages = [r.message for r in caplog.records()]
    if no_job_dirs:
        assert 'Only one job ID, automatically setting job_dirs = False.' not in messages
    else:
        assert 'Only one job ID, automatically setting job_dirs = False.' in messages


def test_multiple_jobs(monkeypatch, tmpdir):
    """Test with multiple jobs.

    From: https://ci.appveyor.com/project/racker-buildbot/luv
    """
    replies = {
        '/buildjobs/v5wnn9k8auqcqovw/artifacts': [
            {'fileName': 'luajit.exe', 'size': 675840, 'type': 'File'},
            {'fileName': 'luv.dll', 'size': 891392, 'type': 'File'}],
        '/buildjobs/bpgcbvqmawv1jw06/artifacts': [
            {'fileName': 'luajit.exe', 'size': 539136, 'type': 'File'},
            {'fileName': 'luv.dll', 'size': 718336, 'type': 'File'}],
    }
    monkeypatch.setattr('appveyor_artifacts.query_api', partial(mock_query_api, replies=replies))

    config = dict(always_job_dirs=False, no_job_dirs=None, dir=str(tmpdir))
    actual = get_artifacts_urls(config, ['v5wnn9k8auqcqovw', 'bpgcbvqmawv1jw06'])
    expected = dict([
        (str(tmpdir.join('v5wnn9k8auqcqovw', 'luajit.exe')),
         (API_PREFIX + '/buildjobs/v5wnn9k8auqcqovw/artifacts/luajit.exe', 675840)),
        (str(tmpdir.join('v5wnn9k8auqcqovw', 'luv.dll')),
         (API_PREFIX + '/buildjobs/v5wnn9k8auqcqovw/artifacts/luv.dll', 891392)),
        (str(tmpdir.join('bpgcbvqmawv1jw06', 'luajit.exe')),
         (API_PREFIX + '/buildjobs/bpgcbvqmawv1jw06/artifacts/luajit.exe', 539136)),
        (str(tmpdir.join('bpgcbvqmawv1jw06', 'luv.dll')),
         (API_PREFIX + '/buildjobs/bpgcbvqmawv1jw06/artifacts/luv.dll', 718336)),
    ])
    assert actual == expected


def test_subdirectory(monkeypatch, tmpdir):
    """Test with artifact "file names" being file paths with subdirectories.

    From: https://ci.appveyor.com/project/sayedihashimi/package-web
    """
    reply = [{'fileName': 'src/OutputRoot/PackageWeb.1.1.17.nupkg', 'size': 60301, 'type': 'NuGetPackage'}]
    monkeypatch.setattr('appveyor_artifacts.query_api', lambda _: reply)

    config = dict(always_job_dirs=False, no_job_dirs=None, dir=str(tmpdir))
    actual = get_artifacts_urls(config, ['r97evl3jva2ejs6b'])
    expected = dict([
        (str(tmpdir.join('src', 'OutputRoot', 'PackageWeb.1.1.17.nupkg')),
         (API_PREFIX + '/buildjobs/r97evl3jva2ejs6b/artifacts/src/OutputRoot/PackageWeb.1.1.17.nupkg', 60301)),
    ])
    assert actual == expected

{'fraction_non_alphanumeric': 0.1539331619537275, 'fraction_numerical': 0.03352185089974293, 'mean_word_length': 4.1872, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2115198', 'n_tokens_mistral': 3829, 'n_tokens_neox': 3597, 'n_words': 620}