def test_build_package_name(tmp_path, monkeypatch): """The zip file name comes from the metadata.""" to_be_zipped_dir = tmp_path / BUILD_DIRNAME to_be_zipped_dir.mkdir() # the metadata metadata_data = {'name': 'name-from-metadata'} metadata_file = tmp_path / 'metadata.yaml' with metadata_file.open('wt', encoding='ascii') as fh: yaml.dump(metadata_data, fh) # zip it monkeypatch.chdir(tmp_path) # so the zip file is left in the temp dir builder = Builder({ 'from': tmp_path, 'entrypoint': 'whatever', 'requirement': [], }) zipname = builder.handle_package() assert zipname == "name-from-metadata.charm"
def test_build_package_name(tmp_path, monkeypatch, config): """The zip file name comes from the metadata.""" to_be_zipped_dir = tmp_path / BUILD_DIRNAME to_be_zipped_dir.mkdir() # the metadata metadata_data = {"name": "name-from-metadata"} metadata_file = tmp_path / "metadata.yaml" with metadata_file.open("wt", encoding="ascii") as fh: yaml.dump(metadata_data, fh) # zip it monkeypatch.chdir(tmp_path) # so the zip file is left in the temp dir builder = Builder( { "from": tmp_path, "entrypoint": "whatever", "requirement": [], }, config, ) zipname = builder.handle_package() assert zipname == "name-from-metadata.charm"
def test_build_package_tree_structure(tmp_path, monkeypatch): """The zip file is properly built internally.""" # the metadata metadata_data = {'name': 'name-from-metadata'} metadata_file = tmp_path / 'metadata.yaml' with metadata_file.open('wt', encoding='ascii') as fh: yaml.dump(metadata_data, fh) # create some dirs and files! a couple of files outside, and the dir we'll zip... file_outside_1 = tmp_path / 'file_outside_1' with file_outside_1.open('wb') as fh: fh.write(b'content_out_1') file_outside_2 = tmp_path / 'file_outside_2' with file_outside_2.open('wb') as fh: fh.write(b'content_out_2') to_be_zipped_dir = tmp_path / BUILD_DIRNAME to_be_zipped_dir.mkdir() # ...also outside a dir with a file... dir_outside = tmp_path / 'extdir' dir_outside.mkdir() file_ext = dir_outside / 'file_ext' with file_ext.open('wb') as fh: fh.write(b'external file') # ...then another file inside, and another dir... file_inside = to_be_zipped_dir / 'file_inside' with file_inside.open('wb') as fh: fh.write(b'content_in') dir_inside = to_be_zipped_dir / 'somedir' dir_inside.mkdir() # ...also inside, a link to the external dir... dir_linked_inside = to_be_zipped_dir / 'linkeddir' dir_linked_inside.symlink_to(dir_outside) # ...and finally another real file, and two symlinks file_deep_1 = dir_inside / 'file_deep_1' with file_deep_1.open('wb') as fh: fh.write(b'content_deep') file_deep_2 = dir_inside / 'file_deep_2' file_deep_2.symlink_to(file_inside) file_deep_3 = dir_inside / 'file_deep_3' file_deep_3.symlink_to(file_outside_1) # zip it monkeypatch.chdir(tmp_path) # so the zip file is left in the temp dir builder = Builder({ 'from': tmp_path, 'entrypoint': 'whatever', 'requirement': [], }) zipname = builder.handle_package() # check the stuff outside is not in the zip, the stuff inside is zipped (with # contents!), and all relative to build dir zf = zipfile.ZipFile(zipname) assert 'file_outside_1' not in [x.filename for x in zf.infolist()] assert 'file_outside_2' not in [x.filename for x in zf.infolist()] assert zf.read('file_inside') == b"content_in" assert zf.read('somedir/file_deep_1') == b"content_deep" # own assert zf.read('somedir/file_deep_2') == b"content_in" # from file inside assert zf.read( 'somedir/file_deep_3') == b"content_out_1" # from file outside 1 assert zf.read('linkeddir/file_ext' ) == b"external file" # from file in the outside linked dir
def test_build_package_tree_structure(tmp_path, monkeypatch, config): """The zip file is properly built internally.""" # the metadata metadata_data = {"name": "name-from-metadata"} metadata_file = tmp_path / "metadata.yaml" with metadata_file.open("wt", encoding="ascii") as fh: yaml.dump(metadata_data, fh) # create some dirs and files! a couple of files outside, and the dir we'll zip... file_outside_1 = tmp_path / "file_outside_1" with file_outside_1.open("wb") as fh: fh.write(b"content_out_1") file_outside_2 = tmp_path / "file_outside_2" with file_outside_2.open("wb") as fh: fh.write(b"content_out_2") to_be_zipped_dir = tmp_path / BUILD_DIRNAME to_be_zipped_dir.mkdir() # ...also outside a dir with a file... dir_outside = tmp_path / "extdir" dir_outside.mkdir() file_ext = dir_outside / "file_ext" with file_ext.open("wb") as fh: fh.write(b"external file") # ...then another file inside, and another dir... file_inside = to_be_zipped_dir / "file_inside" with file_inside.open("wb") as fh: fh.write(b"content_in") dir_inside = to_be_zipped_dir / "somedir" dir_inside.mkdir() # ...also inside, a link to the external dir... dir_linked_inside = to_be_zipped_dir / "linkeddir" dir_linked_inside.symlink_to(dir_outside) # ...and finally another real file, and two symlinks file_deep_1 = dir_inside / "file_deep_1" with file_deep_1.open("wb") as fh: fh.write(b"content_deep") file_deep_2 = dir_inside / "file_deep_2" file_deep_2.symlink_to(file_inside) file_deep_3 = dir_inside / "file_deep_3" file_deep_3.symlink_to(file_outside_1) # zip it monkeypatch.chdir(tmp_path) # so the zip file is left in the temp dir builder = Builder( { "from": tmp_path, "entrypoint": "whatever", "requirement": [], }, config, ) zipname = builder.handle_package() # check the stuff outside is not in the zip, the stuff inside is zipped (with # contents!), and all relative to build dir zf = zipfile.ZipFile(zipname) assert "file_outside_1" not in [x.filename for x in zf.infolist()] assert "file_outside_2" not in [x.filename for x in zf.infolist()] assert zf.read("file_inside") == b"content_in" assert zf.read("somedir/file_deep_1") == b"content_deep" # own assert zf.read("somedir/file_deep_2") == b"content_in" # from file inside assert zf.read( "somedir/file_deep_3") == b"content_out_1" # from file outside 1 assert (zf.read("linkeddir/file_ext") == b"external file" ) # from file in the outside linked dir