<commit_before>"""Tests for neurodocker.parser"""
# Author: <NAME> <<EMAIL>>
from __future__ import absolute_import
import os

import pytest

from neurodocker.parser import SpecsParser


class TestSpecsParser(object):

    def test_init(self):
        specs = {'base': 'ubuntu:16.04',
                 'pkg_manager': 'apt',
                 'miniconda': {'conda_install': ['traits'],
                               'pip_install': ['https://github.com/nipy/nipype/archive/master.tar.gz'],
                               'python_version': '3.5.1'},
                 'ants': {'use_binaries': True, 'version': '2.1.0'},
                 'fsl': {'use_binaries': True, 'version': '5.0.8'}}
        parser = SpecsParser(specs=specs)

        correct = {'base': 'ubuntu:16.04',
                   'pkg_manager': 'apt',
                   'miniconda': {'conda_install': 'traits',
                                 'pip_install': 'https://github.com/nipy/nipype/archive/master.tar.gz',
                                 'python_version': '3.5.1'},
                    'ants': {'use_binaries': True, 'version': '2.1.0'},
                    'fsl': {'use_binaries': True, 'version': '5.0.8'}}

        assert parser.specs == correct, "error parsing conda and/or pip packages"<commit_msg>complete testing for neurodocker.parser<commit_after>"""Tests for neurodocker.parser"""
# Author: <NAME> <<EMAIL>>
from __future__ import absolute_import

import pytest

from neurodocker.parser import SpecsParser


class TestSpecsParser(object):
    @pytest.fixture(autouse=True)
    def setup(self, tmpdir):
        self.tmpdir = tmpdir

    def test_str(self):
        specs = {'base': 'ubuntu:17.04',
                 'pkg_manager': 'yum'}
        assert str(SpecsParser(specs)) == str(specs)

    def test_use_dict_or_json(self):
        import os.path as op
        from neurodocker.utils import save_json
        specs = {'base': 'ubuntu:17.04',
                 'pkg_manager': 'yum'}
        filepath = op.join(self.tmpdir.strpath, 'specs.json')
        save_json(specs, filepath)
        assert SpecsParser(specs).specs == SpecsParser(filepath).specs

    def test__validate_keys(self):
        # Missing base image.
        specs = {'pkg_manager': 'apt'}
        with pytest.raises(KeyError):
            SpecsParser(specs)

        # Missing package manager.
        specs = {'base': 'ubuntu:17.04'}
        with pytest.raises(KeyError):
            SpecsParser(specs)

        # Invalid top-level key.
        specs = {'base': 'ubuntu:17.04',
                 'pkg_manager': 'yum',
                 'fake_software': 'fake_val',}
        with pytest.raises(KeyError) as e:
            SpecsParser(specs)
        assert 'fake_software' in str(e.value)

    def test__parse_conda_pip(self):
        specs_l = {'base': 'ubuntu:17.04',
                   'pkg_manager': 'yum',
                   'miniconda': {'python_version': '3.6.1',
                                 'conda_install': ['nipype', 'numpy'],
                                 'pip_install': ['pandas', 'pylsl']}
                   }
        specs_s = {'base': 'ubuntu:17.04',
                   'pkg_manager': 'yum',
                   'miniconda': {'python_version': '3.6.1',
                                 'conda_install': 'nipype numpy',
                                 'pip_install': 'pandas pylsl'}
                   }
        assert SpecsParser(specs_l).specs == SpecsParser(specs_s).specs
{'fraction_non_alphanumeric': 0.14231774615161197, 'fraction_numerical': 0.01626488527446994, 'mean_word_length': 1.9260832625318607, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '906165', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1034, 'n_words': 208}