<commit_before># -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------
# Copyright © 2016, Continuum Analytics, Inc. All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
# ----------------------------------------------------------------------------
import hashlib
import os

from anaconda_project.local_state_file import LocalStateFile
from anaconda_project.plugins.registry import PluginRegistry
from anaconda_project.plugins.requirement import UserConfigOverrides
from anaconda_project.plugins.requirements.download import DownloadRequirement

from anaconda_project.internal.test.tmpfile_utils import with_directory_contents

ENV_VAR = 'DATAFILE'


def test_filename_not_set():
    def check_not_set(dirname):
        local_state = LocalStateFile.load_for_directory(dirname)
        requirement = DownloadRequirement(registry=PluginRegistry(),
                                          env_var=ENV_VAR,
                                          url='http://example.com',
                                          filename=ENV_VAR)
        status = requirement.check_status(dict(PROJECT_DIR=dirname), local_state, UserConfigOverrides())
        assert not status
        assert "Environment variable {} is not set.".format(ENV_VAR) == status.status_description

    with_directory_contents({}, check_not_set)


def test_download_filename_missing():
    def check_missing_filename(dirname):
        local_state = LocalStateFile.load_for_directory(dirname)
        filename = '/data.zip'
        requirement = DownloadRequirement(registry=PluginRegistry(),
                                          env_var=ENV_VAR,
                                          url='http://localhost/data.zip',
                                          filename='data.zip')
        status = requirement.check_status({ENV_VAR: filename,
                                           'PROJECT_DIR': dirname}, local_state, UserConfigOverrides())
        assert not status
        assert 'File not found: {}'.format(filename) == status.status_description

    with_directory_contents({}, check_missing_filename)


def make_file_with_checksum():
    datafile = ("column1,column2,column3\n"
                "value11,value12,value13\n"
                "value21,value22,value23\n"
                "value31,value32,value33")
    checksum = hashlib.md5()
    checksum.update(datafile.encode('utf-8'))
    digest = checksum.hexdigest()
    return datafile, digest


def test_download_checksum():
    datafile, digest = make_file_with_checksum()

    def verify_checksum(dirname):
        local_state = LocalStateFile.load_for_directory(dirname)
        filename = os.path.join(dirname, 'data.zip')
        requirement = DownloadRequirement(registry=PluginRegistry(),
                                          env_var=ENV_VAR,
                                          url='http://localhost/data.zip',
                                          filename='data.zip',
                                          hash_algorithm='md5',
                                          hash_value=digest)
        status = requirement.check_status({ENV_VAR: filename,
                                           'PROJECT_DIR': dirname}, local_state, UserConfigOverrides())
        assert 'File downloaded to {}'.format(filename) == status.status_description
        assert status

    with_directory_contents({'data.zip': datafile}, verify_checksum)


def test_download_with_no_checksum():
    datafile, digest = make_file_with_checksum()

    def downloaded_file_valid(dirname):
        local_state = LocalStateFile.load_for_directory(dirname)
        filename = os.path.join(dirname, 'data.zip')
        requirement = DownloadRequirement(registry=PluginRegistry(),
                                          env_var=ENV_VAR,
                                          url='http://localhost/data.zip',
                                          filename='data.zip')
        status = requirement.check_status({ENV_VAR: filename,
                                           'PROJECT_DIR': dirname}, local_state, UserConfigOverrides())
        assert status
        assert 'File downloaded to {}'.format(filename) == status.status_description

    with_directory_contents({'data.zip': datafile}, downloaded_file_valid)


def test_download_wrong_checksum():
    datafile, digest = make_file_with_checksum()
    digest += '_BROKEN'

    def downloaded_file_valid(dirname):
        local_state = LocalStateFile.load_for_directory(dirname)
        filename = os.path.join(dirname, 'data.zip')
        requirement = DownloadRequirement(registry=PluginRegistry(),
                                          env_var=ENV_VAR,
                                          url='http://localhost/data.zip',
                                          filename='data.zip',
                                          hash_algorithm='md5',
                                          hash_value=digest)
        status = requirement.check_status({ENV_VAR: filename,
                                           'PROJECT_DIR': dirname}, local_state, UserConfigOverrides())
        assert not status
        assert 'File checksum error for {}, expected {} but was {}'.format(
            filename, 'aeca3c870b9de245e8f8c2d27ce85763_BROKEN',
            'aeca3c870b9de245e8f8c2d27ce85763') == status.status_description

    with_directory_contents({'data.zip': datafile}, downloaded_file_valid)


def test_download_error_readfile(monkeypatch):
    datafile, digest = make_file_with_checksum()
    digest += '1'

    def checksum_mock(self, fp):
        raise OSError()

    def downloaded_file_valid(dirname):
        local_state = LocalStateFile.load_for_directory(dirname)
        filename = os.path.join(dirname, 'data.zip')
        requirement = DownloadRequirement(registry=PluginRegistry(),
                                          env_var=ENV_VAR,
                                          url='http://localhost/data.zip',
                                          filename='data.zip',
                                          hash_algorithm='md5',
                                          hash_value=digest)
        status = requirement.check_status({ENV_VAR: filename,
                                           'PROJECT_DIR': dirname}, local_state, UserConfigOverrides())
        assert not status
        assert 'File referenced by: {} cannot be read ({})'.format(ENV_VAR, filename) == status.status_description

    with_directory_contents({'data.zip': datafile}, downloaded_file_valid)


def test_use_variable_name_for_filename():
    problems = []
    requirements = []
    DownloadRequirement._parse(PluginRegistry(),
                               varname='FOO',
                               item='http://example.com/',<commit_msg>removed unecessary download requirement tests<commit_after># -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------
# Copyright © 2016, Continuum Analytics, Inc. All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
# ----------------------------------------------------------------------------
import hashlib
import os

from anaconda_project.local_state_file import LocalStateFile
from anaconda_project.plugins.registry import PluginRegistry
from anaconda_project.plugins.requirement import UserConfigOverrides
from anaconda_project.plugins.requirements.download import DownloadRequirement

from anaconda_project.internal.test.tmpfile_utils import with_directory_contents

ENV_VAR = 'DATAFILE'


def test_filename_not_set():
    def check_not_set(dirname):
        local_state = LocalStateFile.load_for_directory(dirname)
        requirement = DownloadRequirement(registry=PluginRegistry(),
                                          env_var=ENV_VAR,
                                          url='http://example.com',
                                          filename=ENV_VAR)
        status = requirement.check_status(dict(PROJECT_DIR=dirname), local_state, UserConfigOverrides())
        assert not status
        assert "Environment variable {} is not set.".format(ENV_VAR) == status.status_description

    with_directory_contents({}, check_not_set)


def test_download_filename_missing():
    def check_missing_filename(dirname):
        local_state = LocalStateFile.load_for_directory(dirname)
        filename = '/data.zip'
        requirement = DownloadRequirement(registry=PluginRegistry(),
                                          env_var=ENV_VAR,
                                          url='http://localhost/data.zip',
                                          filename='data.zip')
        status = requirement.check_status({ENV_VAR: filename,
                                           'PROJECT_DIR': dirname}, local_state, UserConfigOverrides())
        assert not status
        assert 'File not found: {}'.format(filename) == status.status_description

    with_directory_contents({}, check_missing_filename)


def make_file_with_checksum():
    datafile = ("column1,column2,column3\n"
                "value11,value12,value13\n"
                "value21,value22,value23\n"
                "value31,value32,value33")
    checksum = hashlib.md5()
    checksum.update(datafile.encode('utf-8'))
    digest = checksum.hexdigest()
    return datafile, digest


def test_download_checksum():
    datafile, digest = make_file_with_checksum()

    def verify_checksum(dirname):
        local_state = LocalStateFile.load_for_directory(dirname)
        filename = os.path.join(dirname, 'data.zip')
        requirement = DownloadRequirement(registry=PluginRegistry(),
                                          env_var=ENV_VAR,
                                          url='http://localhost/data.zip',
                                          filename='data.zip',
                                          hash_algorithm='md5',
                                          hash_value=digest)
        status = requirement.check_status({ENV_VAR: filename,
                                           'PROJECT_DIR': dirname}, local_state, UserConfigOverrides())
        assert 'File downloaded to {}'.format(filename) == status.status_description
        assert status

    with_directory_contents({'data.zip': datafile}, verify_checksum)


def test_download_with_no_checksum():
    datafile, digest = make_file_with_checksum()

    def downloaded_file_valid(dirname):
        local_state = LocalStateFile.load_for_directory(dirname)
        filename = os.path.join(dirname, 'data.zip')
        requirement = DownloadRequirement(registry=PluginRegistry(),
                                          env_var=ENV_VAR,
                                          url='http://localhost/data.zip',
                                          filename='data.zip')
        status = requirement.check_status({ENV_VAR: filename,
                                           'PROJECT_DIR': dirname}, local_state, UserConfigOverrides())
        assert status
        assert 'File downloaded to {}'.format(filename) == status.status_description

    with_directory_contents({'data.zip': datafile}, downloaded_file_valid)


def test_use_variable_name_for_filename():
    problems = []
    requirements = []
    DownloadRequirement._parse(PluginRegistry(),
                               varname='FOO',
                               item='http://example.com/',
{'fraction_non_alphanumeric': 0.11280222647416942, 'fraction_numerical': 0.008436249782570881, 'mean_word_length': 1.8957441450516244, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 129, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6745086', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2661, 'n_words': 504}