def test_get_config_file(git_repo):
    config = util.jupyter_releaser_CONFIG
    config.write_text(testutil.TOML_CONFIG, encoding="utf-8")
    config = util.read_config()
    assert config["hooks"][
        "before-build-python"] == "python setup.py --version"
    assert config["options"]["dist_dir"] == "foo"
def test_get_config_python(py_package):
    text = util.PYPROJECT.read_text(encoding="utf-8")
    text = testutil.TOML_CONFIG.replace("\n[", "\n[tool.jupyter-releaser.")
    util.PYPROJECT.write_text(text, encoding="utf-8")
    config = util.read_config()
    assert config["hooks"]["before-build-python"] == "python setup.py --version"
    assert config["options"]["dist_dir"] == "foo"
def test_get_config_npm(npm_package):
    package_json = util.PACKAGE_JSON
    data = json.loads(package_json.read_text(encoding="utf-8"))
    data["jupyter-releaser"] = toml.loads(testutil.TOML_CONFIG)
    package_json.write_text(json.dumps(data))
    config = util.read_config()
    assert config["hooks"]["after-build-python"] == [
        "python setup.py --version",
        "python setup.py --name",
    ]
    assert config["options"]["dist_dir"] == "foo"
Esempio n. 4
0
    def invoke(self, ctx):
        """Handle jupyter-releaser config while invoking a command"""
        # Get the command name and make sure it is valid
        cmd_name = ctx.protected_args[0]
        if not cmd_name in self.commands:
            super().invoke(ctx)

        if cmd_name == "list-envvars":
            envvars = dict()
            for cmd_name in self.commands:
                for param in self.commands[cmd_name].params:
                    if isinstance(param, click.Option):
                        if param.envvar:
                            envvars[param.name] = param.envvar

            for key in sorted(envvars):
                util.log(f"{key.replace('_', '-')}: {envvars[key]}")

            return

        orig_dir = os.getcwd()

        if cmd_name.replace("-", "_") in self._needs_checkout_dir:
            if not osp.exists(util.CHECKOUT_NAME):
                raise ValueError("Please run prep-git first")
            os.chdir(util.CHECKOUT_NAME)

        # Read in the config
        config = util.read_config()
        hooks = config.get("hooks", {})
        options = config.get("options", {})

        # Print a separation header
        util.log(f'\n\n{"-" * 50}')
        util.log(cmd_name)
        util.log(f'{"-" * 50}\n\n')

        # Handle all of the parameters
        for param in self.commands[cmd_name].get_params(ctx):
            # Defer to env var overrides
            if param.envvar and os.environ.get(param.envvar):
                continue
            name = param.name
            if name in options or name.replace("_", "-") in options:
                arg = f"--{name.replace('_', '-')}"
                # Defer to cli overrides
                if arg not in ctx.args:
                    val = options.get(name, options.get(name.replace("_", "-")))
                    if isinstance(val, list):
                        for v in val:
                            ctx.args.append(arg)
                            ctx.args.append(v)
                    else:
                        ctx.args.append(arg)
                        ctx.args.append(val)

        # Handle before hooks
        before = f"before-{cmd_name}"
        if before in hooks:
            before_hooks = hooks[before]
            if isinstance(before_hooks, str):
                before_hooks = [before_hooks]
            for hook in before_hooks:
                util.run(hook)

        # Run the actual command
        super().invoke(ctx)

        # Handle after hooks
        after = f"after-{cmd_name}"
        if after in hooks:
            after_hooks = hooks[after]
            if isinstance(after_hooks, str):
                after_hooks = [after_hooks]
            for hook in after_hooks:
                util.run(hook)

        os.chdir(orig_dir)