def provide_download_of_zip(dirname):
        with codecs.open(os.path.join(dirname, DEFAULT_PROJECT_FILENAME), 'w',
                         'utf-8') as f:
            f.write(complete_project_file_content(ZIPPED_DATAFILE_CONTENT))

        @gen.coroutine
        def mock_downloader_run(self, loop):
            class Res:
                pass

            res = Res()
            res.code = 200
            assert self._url.endswith(".zip")
            assert self._filename.endswith(".zip")
            with codecs.open(self._filename, 'w', 'utf-8') as f:
                f.write("This is not a zip file.")
            raise gen.Return(res)

        monkeypatch.setattr(
            "anaconda_project.internal.http_client.FileDownloader.run",
            mock_downloader_run)

        project = project_no_dedicated_env(dirname)

        result = prepare_without_interaction(
            project, environ=minimal_environ(PROJECT_DIR=dirname))
        assert not result
        assert [(
            "Failed to unzip %s: File is not a zip file" %
            os.path.join(dirname, "data.zip")
        ), "missing requirement to run this project: A downloaded file which is referenced by DATAFILE.",
                "  Environment variable DATAFILE is not set."] == result.errors
    def provide_download_of_zip_no_unzip(zipname, dirname):
        with codecs.open(os.path.join(dirname, DEFAULT_PROJECT_FILENAME), 'w',
                         'utf-8') as f:
            f.write(
                complete_project_file_content(
                    ZIPPED_DATAFILE_CONTENT_NO_UNZIP))

        @gen.coroutine
        def mock_downloader_run(self, loop):
            class Res:
                pass

            res = Res()
            res.code = 200
            assert self._url.endswith(".zip")
            # we aren't going to unzip so we should be downloading straignt to
            # the specified filename 'data' without the .zip on it
            assert not self._filename.endswith(".zip")
            shutil.copyfile(zipname, self._filename)
            raise gen.Return(res)

        monkeypatch.setattr(
            "anaconda_project.internal.http_client.FileDownloader.run",
            mock_downloader_run)

        project = project_no_dedicated_env(dirname)

        result = prepare_without_interaction(
            project, environ=minimal_environ(PROJECT_DIR=dirname))
        assert hasattr(result, 'environ')
        assert 'DATAFILE' in result.environ
        assert os.path.isfile(os.path.join(dirname, 'data'))
        with zipfile.ZipFile(os.path.join(dirname, 'data')) as zf:
            assert zf.namelist() == ['foo']
    def provide_download_of_zip(zipname, dirname):
        with codecs.open(os.path.join(dirname, DEFAULT_PROJECT_FILENAME), 'w',
                         'utf-8') as f:
            f.write(
                complete_project_file_content(
                    ZIPPED_DATAFILE_CONTENT_NO_ZIP_SUFFIX))

        @gen.coroutine
        def mock_downloader_run(self, loop):
            class Res:
                pass

            res = Res()
            res.code = 200
            # we add .zip to the download filename, even though it wasn't in the URL
            assert not self._url.endswith(".zip")
            assert self._filename.endswith(".zip")
            shutil.copyfile(zipname, self._filename)
            raise gen.Return(res)

        monkeypatch.setattr(
            "anaconda_project.internal.http_client.FileDownloader.run",
            mock_downloader_run)

        project = project_no_dedicated_env(dirname)

        result = prepare_without_interaction(
            project, environ=minimal_environ(PROJECT_DIR=dirname))
        assert hasattr(result, 'environ')
        assert 'DATAFILE' in result.environ
        assert os.path.isdir(os.path.join(dirname, 'data'))
        assert os.path.isfile(os.path.join(dirname, 'data', 'foo'))
        assert codecs.open(os.path.join(dirname, 'data',
                                        'foo')).read() == 'hello\n'
    def provide_download_of_zip(zipname, dirname):
        with codecs.open(os.path.join(dirname, DEFAULT_PROJECT_FILENAME), 'w', 'utf-8') as f:
            f.write(complete_project_file_content(ZIPPED_DATAFILE_CONTENT_CHECKSUM))

        @gen.coroutine
        def mock_downloader_run(self, loop):
            class Res:
                pass

            res = Res()
            res.code = 200
            assert self._url.endswith(".zip")
            assert self._filename.endswith(".zip")
            shutil.copyfile(zipname, self._filename)
            self._hash = '12345abcdef'
            raise gen.Return(res)

        monkeypatch.setattr("anaconda_project.internal.http_client.FileDownloader.run", mock_downloader_run)
        project = project_no_dedicated_env(dirname)

        result = prepare_without_interaction(project, environ=minimal_environ(PROJECT_DIR=dirname))
        assert hasattr(result, 'environ')
        assert 'DATAFILE' in result.environ
        assert os.path.isdir(os.path.join(dirname, 'data'))
        assert os.path.isfile(os.path.join(dirname, 'data', 'foo'))
        assert codecs.open(os.path.join(dirname, 'data', 'foo')).read() == 'hello\n'

        project.frontend.reset()
        status = unprepare(project, result)
        filename = os.path.join(dirname, 'data')
        assert project.frontend.logs == ["Removed downloaded file %s." % filename,
                                         ("Current environment is not in %s, no need to delete it." % dirname)]
        assert status.status_description == "Success."