def test_version(): """Version returned matches version of Pyblish""" runner = CliRunner() result = runner.invoke(pyblish.cli.main, ["--version"]) print "Output: %s" % result.output print "Version: %s" % pyblish.__version__ assert pyblish.__version__ in result.output
def test_publishing(): """Basic publishing works""" count = {"#": 0} class Collector(pyblish.api.ContextPlugin): def process(self, context): self.log.warning("Running") count["#"] += 1 context.create_instance("MyInstance") class Validator(pyblish.api.InstancePlugin): order = pyblish.api.ValidatorOrder def process(self, instance): count["#"] += 10 assert instance.data["name"] == "MyInstance" count["#"] += 100 pyblish.api.register_plugin(Collector) pyblish.api.register_plugin(Validator) runner = CliRunner() runner.invoke(pyblish.cli.main, ["publish"]) assert count["#"] == 111, count
def test_show_gui(): """Showing GUI through cli works""" with tempfile.NamedTemporaryFile(dir=self.tempdir, delete=False, suffix=".py") as f: module_name = os.path.basename(f.name)[:-3] f.write(b"""\ def show(): print("Mock GUI shown successfully") if __name__ == '__main__': show() """) pythonpath = os.pathsep.join([ self.tempdir, os.environ.get("PYTHONPATH", "") ]) print(module_name) runner = CliRunner() result = runner.invoke( pyblish.cli.main, ["gui", module_name], env={"PYTHONPATH": pythonpath} ) assert_equals(result.output.splitlines()[-1].rstrip(), "Mock GUI shown successfully") assert_equals(result.exit_code, 0)
def test_passing_data_to_gui(): """Passing data to GUI works""" with tempfile.NamedTemporaryFile(dir=self.tempdir, delete=False, suffix=".py") as f: module_name = os.path.basename(f.name)[:-3] f.write(b"""\ from pyblish import util def show(): context = util.publish() print(context.data["passedFromTest"]) if __name__ == '__main__': show() """) pythonpath = os.pathsep.join([ self.tempdir, os.environ.get("PYTHONPATH", "") ]) runner = CliRunner() result = runner.invoke( pyblish.cli.main, [ "--data", "passedFromTest", "Data passed successfully", "gui", module_name ], env={"PYTHONPATH": pythonpath} ) assert_equals(result.output.splitlines()[-1].rstrip(), "Data passed successfully") assert_equals(result.exit_code, 0)
def test_invalid_data(mock_log): """Data not JSON-serialisable is treated as string""" runner = CliRunner() runner.invoke(pyblish.cli.main, ["--data", "key", "['test': 'fdf}"]) assert context().data["key"] == "['test': 'fdf}"
def test_plugins(): """CLI returns correct plugins""" runner = CliRunner() result = runner.invoke(pyblish.cli.main, ["--plugins"]) for plugin in pyblish.api.discover(): print "Plugin: %s" % plugin.__name__ assert plugin.__name__ in result.output
def test_data(): """Injecting data works""" runner = CliRunner() runner.invoke(pyblish.cli.main, [ "--data", "key", "10", "publish"]) assert context().data["key"] == 10 assert not context().has_data("notExist")
def test_data(): """Injecting data works""" runner = CliRunner() runner.invoke(pyblish.cli.main, [ "--data", "fail", "I was programmed to fail!", "publish"]) assert context().has_data("fail") assert not context().has_data("notExist")
def test_add_plugin_path(): """Adding a plugin-path works""" custom_path = os.path.join(lib.PLUGINPATH, "custom") runner = CliRunner() runner.invoke( pyblish.cli.main, ["--add-plugin-path", custom_path, "--paths"]) assert custom_path in ctx().obj["plugin_paths"]
def test_custom_data(): """Data as a side-car file works fine""" runner = CliRunner() with runner.isolated_filesystem(): with open("data.yaml", "w") as f: yaml.dump({"key": "value"}, f) runner.invoke(pyblish.cli.main, ["publish"]) assert_equals(context().data("key"), "value")
def test_paths(): """Paths are correctly returned from cli""" plugin = pyblish.api for flag, func in { "--paths": plugin.plugin_paths, "--registered-paths": plugin.registered_paths, "--environment-paths": plugin.environment_paths}.iteritems(): print("Flag: %s" % flag) runner = CliRunner() result = runner.invoke(pyblish.cli.main, [flag]) for path in func(): assert path in result.output
def test_plugins_path(): """Custom path via cli works""" custom_path = os.path.join(lib.PLUGINPATH, "custom") runner = CliRunner() result = runner.invoke(pyblish.cli.main, ["--plugin-path", custom_path, "--plugins"]) plugins = pyblish.api.discover(paths=[custom_path]) for plugin in plugins: print "Output: %s" % result.output assert plugin.__name__ in result.output
def test_visualise_environment_paths(): """Visualising environment paths works well""" current_path = os.environ.get("PYBLISHPLUGINPATH") try: os.environ["PYBLISHPLUGINPATH"] = "/custom/path" runner = CliRunner() result = runner.invoke(pyblish.cli.main, ["--environment-paths"]) assert result.output.startswith("/custom/path") finally: if current_path is not None: os.environ["PYBLISHPLUGINPATH"] = current_path
def test_paths(): """Paths are correctly returned from cli""" plugin = pyblish.plugin for flag, func in { '--paths': plugin.plugin_paths, '--registered-paths': plugin.registered_paths, '--configured-paths': plugin.configured_paths, '--environment-paths': plugin.environment_paths }.iteritems(): print "Flag: %s" % flag runner = CliRunner() result = runner.invoke(pyblish.cli.main, [flag]) for path in func(): assert path in result.output
def test_corrupt_custom_configuration(): """Bad configuration stops the publish""" runner = CliRunner() class DoesNotRun(pyblish.api.Selector): def process(self, context): context.create_instance("MyInstance") with runner.isolated_filesystem(): with open("config.yaml", "w") as f: f.write("[{gf$$%$}") runner.invoke(pyblish.cli.main, ["publish"]) assert_equals(len(context()), 0)
def test_visualise_environment_paths(): """Visualising environment paths works well""" current_path = os.environ.get("PYBLISHPLUGINPATH") try: os.environ["PYBLISHPLUGINPATH"] = "/custom/path" runner = CliRunner() result = runner.invoke(pyblish.cli.main, ["--environment-paths"]) assert result.output.startswith("/custom/path"), result.output finally: if current_path is not None: os.environ["PYBLISHPLUGINPATH"] = current_path
def test_invalid_data(mock_log): """Injecting invalid data does not affect the context""" # Since Context is a singleton, we can modify it # using the CLI and inspect the results directly. runner = CliRunner() runner.invoke(pyblish.cli.main, ["--data", "invalid_key", "['test': 'fdf}"]) assert "invalid_key" not in context().data() # An error message is logged assert mock_log.error.called assert mock_log.error.call_count == 1
def test_all_commands_run(): """All commands run without error""" for args in [[], ["--verbose"], ["publish"], ["config"] ]: runner = CliRunner() result = runner.invoke(pyblish.cli.main, args) print "Args: %s" % args print "Exit code: %s" % result.exit_code print "Output: %s" % result.output assert result.exit_code == 0
def test_invalid_data(mock_log): """Injecting invalid data does not affect the context""" # Since Context is a singleton, we can modify it # using the CLI and inspect the results directly. context = pyblish.plugin.Context() runner = CliRunner() runner.invoke(pyblish.cli.main, ['--data', 'invalid_key', '["test": "fdf}']) assert 'invalid_key' not in context.data() # An error message is logged assert mock_log.error.called assert mock_log.error.call_count == 1
def test_all_commands_run(): """All commands run without error""" for args in [[], # No argument ["--verbose"], ["publish"], ["--verbose", "publish"], ]: runner = CliRunner() result = runner.invoke(pyblish.cli.main, args) print("Args: %s" % args) print("Exit code: %s" % result.exit_code) print("Output: %s" % result.output) assert result.exit_code == 0
def test_all_commands_run(): """All commands run without error""" for args in [ [], # No argument ["--verbose"], ["publish"], ["--verbose", "publish"], ]: runner = CliRunner() result = runner.invoke(pyblish.cli.main, args) print("Args: %s" % args) print("Exit code: %s" % result.exit_code) print("Output: %s" % result.output) assert result.exit_code == 0
def test_paths(): """Paths are correctly returned from cli""" plugin = pyblish.api for flag, func in six.iteritems({ "--paths": plugin.plugin_paths, "--registered-paths": plugin.registered_paths, "--environment-paths": plugin.environment_paths }): print("Flag: %s" % flag) runner = CliRunner() result = runner.invoke(pyblish.cli.main, [flag]) for path in func(): assert path in result.output
def test_all_commands_run(mock_pip): """All commands run without error""" # The CLI will ask for which version of pip # is currently installed. Assume the correct # version. mock_pip.__version__ = mock.MagicMock(return_value=(1, 5)) for args in [[], ['--verbose'], ['publish'], ['install', 'test'], ['uninstall', 'test'], ['packages'], ['config']]: runner = CliRunner() result = runner.invoke(pyblish.cli.main, args) print "Args: %s" % args print "Exit code: %s" % result.exit_code print "Output: %s" % result.output assert result.exit_code >= 0
def test_environment_host_registration(): """Host registration from PYBLISH_HOSTS works""" count = {"#": 0} hosts = ["test1", "test2"] # Test single hosts class SingleHostCollector(pyblish.api.ContextPlugin): order = pyblish.api.CollectorOrder host = hosts[0] def process(self, context): count["#"] += 1 pyblish.api.register_plugin(SingleHostCollector) os.environ["PYBLISH_HOSTS"] = hosts[0] runner = CliRunner() result = runner.invoke(pyblish.cli.main, ["publish"]) print(result.output) assert count["#"] == 1, count # Test multiple hosts pyblish.api.deregister_all_plugins() class MultipleHostsCollector(pyblish.api.ContextPlugin): order = pyblish.api.CollectorOrder host = hosts def process(self, context): count["#"] += 10 pyblish.api.register_plugin(MultipleHostsCollector) os.environ["PYBLISH_HOSTS"] = os.pathsep.join(hosts) runner = CliRunner() result = runner.invoke(pyblish.cli.main, ["publish"]) print(result.output) assert count["#"] == 11, count
def test_add_plugin_path(): """Adding a plugin-path works""" custom_path = os.path.join(pyblish.tests.lib.PLUGINPATH, 'custom') runner = CliRunner() result = runner.invoke(pyblish.cli.main, ['--add-plugin-path', custom_path, '--paths']) # Output is: # /some/path <type> # Where <type> is "custom" paths = dict() for line in result.output.split("\n"): if not line.startswith(" "): continue path, typ = line.strip().rsplit(" <", 1) paths[os.path.normpath(path)] = typ[:-1] assert os.path.normpath(custom_path) in paths assert paths[custom_path] == 'custom'
def test_add_duplicate_plugin_path(): """No duplicate paths may be added""" custom_path = os.path.join(pyblish.tests.lib.PLUGINPATH, 'custom') runner = CliRunner() result = runner.invoke(pyblish.cli.main, [ '--add-plugin-path', custom_path, '--add-plugin-path', custom_path, '--add-plugin-path', custom_path, '--paths' ]) paths = list() for line in result.output.split("\n"): if not line.startswith(" "): continue path, typ = line.strip().rsplit(" <", 1) path = os.path.normpath(path) if path in paths: raise ValueError("Path already present") paths.append(path) assert custom_path in paths
def test_custom_configuration(): """Configuration as a side-car file works fine""" runner = CliRunner() with runner.isolated_filesystem(): with open("select_instances.py", "w") as f: f.write(""" import pyblish.api class SelectInstances(pyblish.api.Selector): def process_context(self, context): context.set_data("key", "value") """) with open("config.yaml", "w") as f: yaml.dump({"paths": [os.getcwd()]}, f) runner.invoke(pyblish.cli.main, ["publish"]) assert_equals(context().data("key"), "value")
def test_set_targets(): """Setting targets works""" pythonpath = os.pathsep.join( [self.tempdir, os.environ.get("PYTHONPATH", "")]) count = {"#": 0} class CollectorOne(pyblish.api.ContextPlugin): order = pyblish.api.CollectorOrder targets = ["imagesequence"] def process(self, context): self.log.warning("Running {0}".format(self.targets)) count["#"] += 1 context.create_instance("MyInstance") class CollectorTwo(pyblish.api.ContextPlugin): order = pyblish.api.CollectorOrder targets = ["model"] def process(self, context): self.log.warning("Running {0}".format(self.targets)) count["#"] += 2 context.create_instance("MyInstance") pyblish.api.register_plugin(CollectorOne) pyblish.api.register_plugin(CollectorTwo) runner = CliRunner() result = runner.invoke(pyblish.cli.main, ["publish", "--targets", "imagesequence"], env={"PYTHONPATH": pythonpath}) print(result.output) assert count["#"] == 1, count
def test_invalid_path(mock_log): """You can't add an invalid path""" runner = CliRunner() runner.invoke(pyblish.cli.main, ['--plugin-path', '/invalid/path']) assert mock_log.error.called assert mock_log.error.call_count == 1