def test_unzip_single_file_same_name(): def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'foo') # same name as what's in the zip errors = [] unpack_zip(zipname, target_path, errors) assert [] == errors assert os.path.isfile(target_path) assert codecs.open(target_path, 'r', 'utf-8').read() == "hello world\n" with_tmp_zipfile(dict(foo="hello world\n"), do_test)
def test_unzip_one_directory_same_name(): def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'foo') # same name as what's in the zip errors = [] unpack_zip(zipname, target_path, errors) assert [] == errors assert os.path.isdir(target_path) assert codecs.open(os.path.join(target_path, 'bar'), 'r', 'utf-8').read() == "hello world\n" with_tmp_zipfile({'foo/bar': "hello world\n"}, do_test)
def test_unzip_target_already_exists_and_is_file(): def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'boo') with codecs.open(target_path, 'w', 'utf-8') as f: f.write("\n") errors = [] unpack_zip(zipname, target_path, errors) assert [( "%s exists and isn't a directory, not unzipping a directory over it." % target_path)] == errors with_tmp_zipfile(dict(foo="hello world\n", bar="goodbye world\n"), do_test)
def test_unzip_two_files(): def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'boo') errors = [] unpack_zip(zipname, target_path, errors) assert [] == errors assert os.path.isdir(target_path) assert codecs.open(os.path.join(target_path, 'foo'), 'r', 'utf-8').read() == "hello world\n" assert codecs.open(os.path.join(target_path, 'bar'), 'r', 'utf-8').read() == "goodbye world\n" with_tmp_zipfile(dict(foo="hello world\n", bar="goodbye world\n"), do_test)
def test_unzip_target_already_exists_and_is_file_and_single_file_in_zip_same_name( ): def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'foo') # same name with codecs.open(target_path, 'w', 'utf-8') as f: f.write("\n") errors = [] unpack_zip(zipname, target_path, errors) assert [] == errors assert os.path.isfile(target_path) assert codecs.open(target_path, 'r', 'utf-8').read() == "hello world\n" with_tmp_zipfile(dict(foo="hello world\n"), do_test)
def test_unzip_target_already_exists_and_is_file_and_single_dir_in_zip(): def do_test(zipname, workingdir): target_path = os.path.join( workingdir, 'boo') # different name so we should keep "foo" with codecs.open(target_path, 'w', 'utf-8') as f: f.write("\n") errors = [] unpack_zip(zipname, target_path, errors) assert os.path.isfile(target_path) assert [( "%s exists and isn't a directory, not unzipping a directory over it." % target_path)] == errors with_tmp_zipfile({'foo/bar': "hello world\n"}, do_test)
def test_unzip_target_already_exists_and_is_directory_and_single_file_in_zip_different_name( ): def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'boo') # different name from file in zip os.makedirs(target_path) errors = [] unpack_zip(zipname, target_path, errors) assert [] == errors assert os.path.isdir(target_path) assert codecs.open(os.path.join(target_path, 'foo'), 'r', 'utf-8').read() == "hello world\n" with_tmp_zipfile(dict(foo="hello world\n"), do_test)
def test_unzip_target_already_exists_and_is_directory_and_single_dir_in_zip_same_name( ): def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'foo') # same name as dir in zip os.makedirs(target_path) errors = [] unpack_zip(zipname, target_path, errors) assert [] == errors assert os.path.isdir(target_path) assert codecs.open(os.path.join(target_path, 'bar'), 'r', 'utf-8').read() == "hello world\n" with_tmp_zipfile({'foo/bar': "hello world\n"}, do_test)
def test_unzip_target_already_exists_and_is_directory_and_single_file_in_zip_same_name( ): def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'foo') # same name as file in zip os.makedirs(target_path) errors = [] unpack_zip(zipname, target_path, errors) assert os.path.isdir(target_path) assert [ "%s exists and is a directory, not unzipping a plain file over it." % target_path ] == errors with_tmp_zipfile(dict(foo="hello world\n"), do_test)
def test_unzip_target_already_exists_and_is_file_and_single_file_in_zip_different_name( ): def do_test(zipname, workingdir): target_path = os.path.join(workingdir, 'boo') # different from the file in the zip with codecs.open(target_path, 'w', 'utf-8') as f: f.write("original\n") errors = [] unpack_zip(zipname, target_path, errors) assert [( "%s exists and isn't a directory, not unzipping a directory over it." % target_path)] == errors assert os.path.isfile(target_path) assert codecs.open(target_path, 'r', 'utf-8').read() == "original\n" with_tmp_zipfile(dict(foo="hello world\n"), do_test)
def test_prepare_download_of_zip_file_checksum(monkeypatch): 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." with_tmp_zipfile(dict(foo='hello\n'), provide_download_of_zip)
def test_prepare_download_of_zip_file_no_zip_extension(monkeypatch): 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' with_tmp_zipfile(dict(foo='hello\n'), provide_download_of_zip)
def test_prepare_download_of_zip_file_no_unzip(monkeypatch): 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'] with_tmp_zipfile(dict(foo='hello\n'), provide_download_of_zip_no_unzip)