Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
PR updates
Browse files Browse the repository at this point in the history
refactored identifying dist-info path to skip local installs.  clarifying comment updates.
  • Loading branch information
derekk-nm committed Jun 3, 2024
1 parent 6aa1592 commit 56c6df3
Showing 1 changed file with 63 additions and 40 deletions.
103 changes: 63 additions & 40 deletions neuralmagic/tests/test_nm-vllm_licenses.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import re
import subprocess
from importlib.metadata import metadata
from pathlib import Path
from typing import Tuple

from pytest import mark, param
from pytest import fixture, mark, param, skip

import vllm

Expand Down Expand Up @@ -31,81 +32,102 @@
]


@fixture(scope="session")
def build_dist_info_path() -> Tuple[str, Path]:
"""
provides the package distribution info location
"""
# figure out the package name from pip (i.e. nightly or not)
cmd = ["pip3 freeze"]
packages = subprocess.check_output(
cmd, shell=True, stderr=subprocess.STDOUT).decode("utf-8")
try:
package = [
line for line in packages.splitlines()
if "nm-vllm" in line
][0]
except IndexError as ie:
raise ValueError(f"nm-vllm is not installed. see:\n{packages}") from ie

if "==" in package:
# it was installed using the package name
package_name, package_version = package.split("==")
elif " @ " in package:
# it was installed using a wheel file
package_name, wheel_file = package.split(" @ ")
package_version = Path(wheel_file).stem.split("-")[1]
elif "-e " in package:
# this env was installed from source, so there is no dist-info
skip("nm-vllm installed from source has no dist-info directory")
else:
raise RuntimeError(f"failed to find nm-vllm via pip. found {package}")

# make the package name part compatible with file naming rules
package_name = package_name.replace("-", "_")
# retrieve the path to the site-packages directory
site_package = Path(vllm.__path__[0]).parent / Path(
f"{package_name}-{package_version}.dist-info")
return package_name, site_package


class TestNMThirdPartyLicenseFiles:
"""
These tests verify that the proper files for licensing purposes exist and
[generally] have the expected content.
"""

@staticmethod
def _package_name_version() -> Tuple[str, str]:
"""
provides the package name as pip understands it, and the package version
:return: (name, version)
"""
site_package = vllm.__path__[0]
package_version = vllm.__version__
return site_package, package_version

@staticmethod
def _build_dist_info_path() -> Path:
"""
builds a Path to the nm-vllm dist-info site package directory
"""
(
package_name,
package_version,
) = TestNMThirdPartyLicenseFiles._package_name_version()
package_name = package_name.replace("vllm", "nm_vllm")
return Path(f"{package_name}-{package_version}.dist-info")

def check_file_exists_and_content(self, file_name: str,
def check_file_exists_and_content(dist_info_path: Path, file_name: str,
content_regex: str):
"""
shared function to check license files
:param dist_info_path: the path to the *.dist-info directory
:param file_name: the file to check.
:param content_regex: the regular expression to search the file content
"""
# since we want to ensure that the files are actually available to the
# user, this test function specifically looks for the files, rather than
# accessing dist-info metadata for the package
dist_info = self._build_dist_info_path()
file_path = dist_info / file_name
file_path = dist_info_path / file_name

assert (file_path.exists()
), f"failed to find the expected license info {file_path}"
), f"failed to find the expected license info '{file_path}'"
license_text = file_path.read_text("utf-8")
assert re.search(content_regex, license_text), (
f"license file {file_path} does not have expected content matching "
f"{content_regex}")
f"license file '{file_path}' does not have expected content "
f"matching '{content_regex}'")

@mark.parametrize(
("file_name", "content_regex"),
[param(lf[0], lf[1], marks=lf[2]) for lf in THIRD_PARTY_LICENSE_FILES],
[param(lf[0], lf[1],
marks=lf[2]) for lf in THIRD_PARTY_LICENSE_FILES],
)
def test_common_license_file_presence_content(self, file_name: str,
content_regex: str):
def test_license_file_presence_content(self,
build_dist_info_path: Tuple[str,
Path],
file_name: str, content_regex: str):
"""
Check Neural Magic license files that are common to the community and
enterprise packages
Check Neural Magic license files
"""
self.check_file_exists_and_content(file_name, content_regex)
package_name, dist_info = build_dist_info_path
self.check_file_exists_and_content(dist_info, file_name, content_regex)

def test_expected_files_included(self, request):
def test_expected_files_included(self,
build_dist_info_path: Tuple[str, Path]):
"""
verifies that the list of license files in the directory matches the
list provided in the METADATA file included with the distribution.
"""
# collect the list of files in the dist_info directory
dist_info = self._build_dist_info_path()
package_name, dist_info = build_dist_info_path
dist_info_license_list = [p.name for p in dist_info.glob("*.license")]
dist_info_license_list.extend(
[p.name for p in dist_info.glob("LICENSE*")])
dist_info_license_list.extend(
[p.name for p in dist_info.glob("NOTICE")])

# collect the list of files that METADATA expects to be available
vllm_metadata = metadata("nm-vllm")
vllm_metadata = metadata(package_name)
all_metadata_licenses = vllm_metadata.get_all("License-File")
metadata_license_list = [
license.replace("licenses/", "")
Expand All @@ -128,10 +150,11 @@ def test_expected_files_included(self, request):
assert not dist_info_licenses_not_in_metadata, (
"additional license files are listed in package dist_info "
"directory, not listed in METADATA.\n"
f"{dist_info_licenses_not_in_metadata}")
f"{dist_info_licenses_not_in_metadata}\ndist_info: "
f"{dist_info_license_list}\nmetadata: {metadata_license_list}")

# check that other tests are verifying all the files listed in
# METADATA we only need to check that the files listed in METADATA
# Since other tests are verifying all the files listed in
# METADATA, we only need to check that the files listed in METADATA
# are a subset of those listed in the files we test.
tested_license_files = [
lf[0] for lf in THIRD_PARTY_LICENSE_FILES if lf[0] != "METADATA"
Expand Down

0 comments on commit 56c6df3

Please sign in to comment.