Skip to content

Commit

Permalink
Merge pull request #191 from mottosso/master
Browse files Browse the repository at this point in the history
Fixed #106
  • Loading branch information
mottosso committed Jun 6, 2015
2 parents 4efd8b7 + 5946822 commit d45e4a3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pyblish/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,11 +878,18 @@ def discover(type=None, regex=None, paths=None):
continue

mod_name, _ = os.path.splitext(fname)

try:
# Discard traces of previously
# imported modules of this name.
sys.modules.pop(mod_name)
except:
pass

try:
sys.path.insert(0, path)
module = pyblish.lib.import_module(mod_name)
reload(module)

except Exception as err:
log.warning("Skipped: \"%s\" (%s)", mod_name, err)
continue
Expand Down
71 changes: 71 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import os
import shutil
import tempfile
import contextlib

import pyblish.plugin
from pyblish.vendor.nose.tools import *

import lib


@contextlib.contextmanager
def tempdir():
try:
tempdir = tempfile.mkdtemp()
yield tempdir
finally:
shutil.rmtree(tempdir)


def test_unique_id():
"""Plug-ins and instances have a unique id"""
Expand Down Expand Up @@ -47,3 +63,58 @@ def test_asset():

assert_true(asseta in context)
assert_true(assetb in context)


@with_setup(lib.setup_empty, lib.teardown)
def test_import_mechanism_duplication():
"""Plug-ins don't linger after a second discovery
E.g. when changing the name of a plug-in and then rediscover
the previous plug-ins is still around.
"""

with tempdir() as temp:
print("Writing temporarily to: %s" % temp)
module = os.path.join(temp, "selector.py")
pyblish.api.register_plugin_path(temp)

with open(module, "w") as f:
f.write("""
import pyblish.api
class MySelector(pyblish.api.Selector):
pass
""")

with open(module) as f:
print("File contents after first write:")
print(f.read())

# MySelector should be accessible by now
plugins = [p.__name__ for p in pyblish.api.discover()]

assert "MySelector" in plugins, plugins
assert "MyOtherSelector" not in plugins, plugins

# Remove module, and it's .pyc equivalent
[os.remove(os.path.join(temp, fname))
for fname in os.listdir(temp)]

with open(module, "w") as f:
f.write("""
import pyblish.api
class MyOtherSelector(pyblish.api.Selector):
pass
""")

with open(module) as f:
print("File contents after second write:")
print(f.read())

# MySelector should be gone in favour of MyOtherSelector
plugins = [p.__name__ for p in pyblish.api.discover()]

assert "MyOtherSelector" in plugins, plugins
assert "MySelector" not in plugins, plugins

0 comments on commit d45e4a3

Please sign in to comment.