def test_getlibinfo_malformed_metadata_field(tmp_path, monkeypatch): """Some metadata field is not really valid.""" monkeypatch.chdir(tmp_path) test_path = _create_lib(metadata_id="LIBID = foo = 23") with pytest.raises(CommandError) as err: _get_lib_info(lib_path=test_path) assert str(err.value) == r"Bad metadata line in {}: b'LIBID = foo = 23\n'".format(test_path)
def test_getlibinfo_bad_path(path): """Different combinations of a bad library path.""" with pytest.raises(CommandError) as err: _get_lib_info(lib_path=pathlib.Path(path)) assert str(err.value) == ( "Library path {} must conform to the lib/charms/<charm>/v<API>/<libname>.py structure." .format(path))
def test_getlibinfo_bad_name(name): """Different combinations of a bad library name.""" with pytest.raises(CommandError) as err: _get_lib_info(full_name=name) assert str(err.value) == ( "Library full name {!r} must conform to the charms.<charm>.v<API>.<libname> structure." .format(name))
def test_getlibinfo_api_not_int(tmp_path, monkeypatch): """The API is not an integer.""" monkeypatch.chdir(tmp_path) test_path = _create_lib(metadata_api="LIBAPI = v3") with pytest.raises(CommandError) as err: _get_lib_info(lib_path=test_path) assert str(err.value) == ( "Library {} metadata field LIBAPI is not zero or a positive integer.".format(test_path))
def test_getlibinfo_missing_metadata_field(tmp_path, monkeypatch): """Some metadata field is not present.""" monkeypatch.chdir(tmp_path) test_path = _create_lib(metadata_patch="", metadata_api="") with pytest.raises(CommandError) as err: _get_lib_info(lib_path=test_path) assert str(err.value) == ( "Library {} is missing the mandatory metadata fields: LIBAPI, LIBPATCH.".format(test_path))
def create_lib_filepath(charm_name, lib_name, api=0, patch=1, lib_id="test-lib-id"): """Helper to create the structures on disk for a given lib.""" charm_name = create_importable_name(charm_name) base_dir = pathlib.Path("lib") lib_file = base_dir / "charms" / charm_name / "v{}".format(api) / "{}.py".format(lib_name) lib_file.parent.mkdir(parents=True, exist_ok=True) # save the content to that specific file under custom structure template = textwrap.dedent( """ # test content for a library LIBID = "{lib_id}" LIBAPI = {api} LIBPATCH = {patch} # more text and python code... """ ) content = template.format(lib_id=lib_id, api=api, patch=patch) lib_file.write_text(content) # use _get_lib_info to get the hash of the file, as the used hash is WITHOUT the metadata # files (no point in duplicating that logic here) libdata = _get_lib_info(lib_path=lib_file) return content, libdata.content_hash
def test_getlibinfo_missing_library_from_path(): """Partial case for when the library is not found in disk, starting from the path.""" test_path = pathlib.Path('lib') / 'charms' / 'testcharm' / 'v3' / 'testlib.py' # no create lib! lib_data = _get_lib_info(lib_path=test_path) assert lib_data.lib_id is None assert lib_data.api == 3 assert lib_data.patch == -1 assert lib_data.content_hash is None assert lib_data.content is None assert lib_data.full_name == 'charms.testcharm.v3.testlib' assert lib_data.path == test_path assert lib_data.lib_name == 'testlib' assert lib_data.charm_name == 'testcharm'
def test_getlibinfo_success_content(tmp_path, monkeypatch): """Check that content and its hash are ok.""" monkeypatch.chdir(tmp_path) extra_content = """ extra lines for the file extra non-ascii, for sanity: ñáéíóú the content is everything, this plus metadata the hash should be of this, excluding metadata """ test_path = _create_lib(extra_content=extra_content) lib_data = _get_lib_info(lib_path=test_path) assert lib_data.content == test_path.read_text() assert lib_data.content_hash == hashlib.sha256(extra_content.encode('utf8')).hexdigest()
def test_getlibinfo_success_simple(tmp_path, monkeypatch): """Simple basic case of success getting info from the library.""" monkeypatch.chdir(tmp_path) test_path = _create_lib() lib_data = _get_lib_info(lib_path=test_path) assert lib_data.lib_id == 'test-lib-id' assert lib_data.api == 3 assert lib_data.patch == 14 assert lib_data.content_hash is not None assert lib_data.content is not None assert lib_data.full_name == 'charms.testcharm.v3.testlib' assert lib_data.path == test_path assert lib_data.lib_name == 'testlib' assert lib_data.charm_name == 'testcharm'
def test_getlibinfo_bad_api(name): """Different combinations of a bad api in the path/name.""" with pytest.raises(CommandError) as err: _get_lib_info(full_name=name) assert str(err.value) == ( "The API version in the library path must be 'vN' where N is an integer.")