Files
modelscope/tests/utils/test_plugin.py
suluyana ac61e0bd8f [Fix]dynamic module auto map double dash (#1761)
* fix: resolve cross-repo auto_map when cache paths contain --

modelscope_hub 0.1.x layout embeds -- in local dirs; rejoining that
path into class_reference broke transformers' split("--"). Pass the
local snapshot as pretrained_model_name_or_path with a bare class name
instead, and stop mutating the args tuple in place.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: reject empty model name in trusted owner cache paths

check_model_from_owner_group treated paths like iic--/snapshots/v1 as
trusted because split('--') still yields two parts. Require both owner
and name segments to be non-empty.

Co-authored-by: Cursor <cursoragent@cursor.com>

* test: cover remote pretrained args tuple rebuild in dynamic module patcher

The TypeError from mutating *args was already fixed with the auto_map
double-dash change; add an explicit regression test for that branch.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: derive valid module names from snapshot revision paths

Path(model_dir).stem on revisions like v1.0.4 produced v1.0, so
importlib treated it as package v1 and raised ModuleNotFoundError.
Use the owner--name (+ revision) segment and sanitize to an identifier.

Co-authored-by: Cursor <cursoragent@cursor.com>

* test: cover module naming for dotted snapshot revisions

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: handle pretrained_model_name_or_path passed via kwargs

When the arg is only in kwargs, downloading and then forcing a
positional overwrite caused a duplicate-keyword TypeError. Resolve and
update kwargs or args consistently for both the download and cross-repo
branches.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix lint

* fix(docker): catch JSONDecodeError when querying Docker Hub tags

Non-JSON Hub responses (block pages, 5xx HTML) would bypass URLError
handling and crash json.load; surface them as RuntimeError instead.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: suluyan <suluyan.sly@aliabab-inc.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-20 19:49:53 +08:00

167 lines
6.0 KiB
Python

# Copyright (c) Alibaba, Inc. and its affiliates.
import os
import shutil
import tempfile
import unittest
from modelscope.models.builder import MODELS
from modelscope.utils.plugins import (PluginsManager, discover_plugins,
import_all_plugins, import_file_plugins,
import_plugins, pushd)
from modelscope.utils.test_utils import test_level
@unittest.skip('skipping')
class PluginTest(unittest.TestCase):
def setUp(self):
self.plugins_root = 'tests/utils/plugins/'
self.tmp_dir = tempfile.TemporaryDirectory().name
if not os.path.exists(self.tmp_dir):
os.makedirs(self.tmp_dir)
self.package = 'adaseq'
self.plugins_manager = PluginsManager()
def tearDown(self):
shutil.rmtree(self.tmp_dir)
super().tearDown()
def test_no_plugins(self):
available_plugins = set(discover_plugins())
assert available_plugins == set()
def test_file_plugins(self):
with pushd(self.plugins_root):
available_plugins = set(discover_plugins())
assert available_plugins == {'dummy'}
import_file_plugins()
assert MODELS.get('dummy-model', 'dummy-group') is not None
def test_custom_plugins(self):
with pushd(self.plugins_root):
available_plugins = set(discover_plugins())
assert available_plugins == {'dummy'}
import_plugins(['dummy'])
assert MODELS.get('dummy-model', 'dummy-group') is not None
def test_all_plugins(self):
with pushd(self.plugins_root):
available_plugins = set(discover_plugins())
assert available_plugins == {'dummy'}
import_all_plugins()
assert MODELS.get('dummy-model', 'dummy-group') is not None
def test_install_plugins(self):
"""
examples for the modelscope install method
> modelscope install adaseq ofasys
> modelscope install git+https://github.com/modelscope/AdaSeq.git
> modelscope install adaseq -i <url> -f <url>
> modelscope install adaseq --extra-index-url <url> --trusted-host <hostname>
"""
install_args = [self.package]
status_code, install_args = self.plugins_manager.install_plugins(
install_args)
self.assertEqual(status_code, 0)
install_args = ['random_blabla']
status_code, install_args = self.plugins_manager.install_plugins(
install_args)
self.assertEqual(status_code, 1)
install_args = [self.package, 'random_blabla']
status_code, install_args = self.plugins_manager.install_plugins(
install_args)
self.assertEqual(status_code, 1)
# move this from tear down to avoid unexpected uninstall
uninstall_args = [self.package, '-y']
self.plugins_manager.uninstall_plugins(uninstall_args)
@unittest.skip
def test_install_plugins_with_git(self):
install_args = ['git+https://github.com/modelscope/AdaSeq.git']
status_code, install_args = self.plugins_manager.install_plugins(
install_args)
self.assertEqual(status_code, 0)
# move this from tear down to avoid unexpected uninstall
uninstall_args = ['git+https://github.com/modelscope/AdaSeq.git', '-y']
self.plugins_manager.uninstall_plugins(uninstall_args)
def test_uninstall_plugins(self):
"""
examples for the modelscope uninstall method
> modelscope uninstall adaseq
> modelscope uninstall -y adaseq
"""
install_args = [self.package]
status_code, install_args = self.plugins_manager.install_plugins(
install_args)
self.assertEqual(status_code, 0)
uninstall_args = [self.package, '-y']
status_code, uninstall_args = self.plugins_manager.uninstall_plugins(
uninstall_args)
self.assertEqual(status_code, 0)
def test_list_plugins(self):
"""
examples for the modelscope list method
> modelscope list
> modelscope list --all
> modelscope list -a
# """
modelscope_plugin = os.path.join(self.tmp_dir, 'modelscope_plugin')
self.plugins_manager.file_path = modelscope_plugin
result = self.plugins_manager.list_plugins()
self.assertEqual(len(result.items()), 0)
from modelscope.utils.plugins import OFFICIAL_PLUGINS
result = self.plugins_manager.list_plugins(show_all=True)
self.assertEqual(len(result.items()), len(OFFICIAL_PLUGINS))
class ModuleNameFromModelDirTest(unittest.TestCase):
"""Regression: snapshot revision paths must not become dotted module names."""
def test_snapshot_revision_with_dots(self):
from modelscope.utils.plugins import _module_name_from_model_dir
tmp = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmp, ignore_errors=True)
model_dir = os.path.join(tmp, 'models',
'iic--cv_unet_skin_retouching_torch',
'snapshots', 'v1.0.4')
os.makedirs(model_dir)
name = _module_name_from_model_dir(model_dir)
self.assertNotIn('.', name)
self.assertTrue(
name.startswith('iic__cv_unet_skin_retouching_torch__v1_0_4'))
# Old Path(model_dir).stem produced 'v1.0', which importlib treats as
# package 'v1' → ModuleNotFoundError: No module named 'v1'.
self.assertNotEqual(name, 'v1.0')
self.assertFalse(name.startswith('v1'))
def test_flat_cache_layout_unchanged(self):
from modelscope.utils.plugins import _module_name_from_model_dir
tmp = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmp, ignore_errors=True)
model_dir = os.path.join(tmp, 'iic', 'cv_unet_skin_retouching_torch')
os.makedirs(model_dir)
self.assertEqual(
_module_name_from_model_dir(model_dir),
'cv_unet_skin_retouching_torch')
if __name__ == '__main__':
unittest.main()