diff --git a/dvc/commands/experiments/init.py b/dvc/commands/experiments/init.py index 8f3143473..04db754dd 100644 --- a/dvc/commands/experiments/init.py +++ b/dvc/commands/experiments/init.py @@ -69,7 +69,6 @@ class CmdExperimentsInit(CmdBase): ) text = ui.rich_text.assemble( - "\n" if self.args.interactive else "", "Created ", (self.args.name, "bright_blue"), " stage in ", diff --git a/dvc/repo/experiments/init.py b/dvc/repo/experiments/init.py index 12ae7e563..2a9d4f0e5 100644 --- a/dvc/repo/experiments/init.py +++ b/dvc/repo/experiments/init.py @@ -215,7 +215,7 @@ def is_file(path: str) -> bool: return bool(ext) -def init_deps(stage: PipelineStage, log: bool = False) -> None: +def init_deps(stage: PipelineStage) -> None: from funcy import rpartial from dvc.dependency import ParamsDependency @@ -224,7 +224,7 @@ def init_deps(stage: PipelineStage, log: bool = False) -> None: new_deps = [dep for dep in stage.deps if not dep.exists] params, deps = lsplit(rpartial(isinstance, ParamsDependency), new_deps) - if log: + if new_deps: paths = map("[green]{0}[/]".format, new_deps) ui.write(f"Creating {humanize.join(paths)}", styled=True) @@ -319,23 +319,26 @@ def init( from dvc.ui.prompt import Confirm - if not interactive or Confirm.ask( + if interactive and not Confirm.ask( "Do you want to add the above contents to dvc.yaml?", console=ui.error_console, default=True, stream=stream, ): - with _disable_logging(), repo.scm_context(autostage=True, quiet=True): - stage.dump(update_lock=False) - stage.ignore_outs() - if not interactive: - label = "Using experiment project structure:" - display_workspace_tree(context, label) - init_deps(stage, log=not interactive) - if params: - repo.scm_context.track_file(params) - else: raise DvcException("Aborting ...") + if interactive: + ui.error_write() # add a newline after the prompts + + with _disable_logging(), repo.scm_context(autostage=True, quiet=True): + stage.dump(update_lock=False) + stage.ignore_outs() + if not interactive: + label = "Using experiment project structure:" + display_workspace_tree(context, label) + init_deps(stage) + if params: + repo.scm_context.track_file(params) + assert isinstance(stage, PipelineStage) return stage diff --git a/tests/func/experiments/test_init.py b/tests/func/experiments/test_init.py index 443ad0e50..92f56b143 100644 --- a/tests/func/experiments/test_init.py +++ b/tests/func/experiments/test_init.py @@ -213,7 +213,7 @@ def test_init_interactive_params_validation(tmp_dir, dvc, capsys): "Please retry with an existing parameters file.\n" "Path to a parameters file [params.yaml, n to omit]:" ) in err - assert not out + assert out == "Creating script.py\n" def test_init_with_no_defaults_interactive(tmp_dir, dvc): @@ -296,9 +296,8 @@ def test_init_default(tmp_dir, scm, dvc, interactive, overrides, inp, capsys): if interactive: assert "'script.py' does not exist, the file will be created." in err assert "'data' does not exist, the directory will be created." in err - assert not out - return - assert "Using experiment project structure: " in out + else: + assert "Using experiment project structure: " in out assert "Creating script.py and data" in out @@ -383,9 +382,8 @@ def test_init_interactive_live( if interactive: assert "'script.py' does not exist, the file will be created." in err assert "'data' does not exist, the directory will be created." in err - assert not out - return - assert "Using experiment project structure: " in out + else: + assert "Using experiment project structure: " in out assert "Creating script.py and data" in out