/ tests / test_cli / test_init.py
test_init.py
 1  """Test the init subcommand."""
 2  
 3  from __future__ import annotations
 4  
 5  import pathlib
 6  import re
 7  import typing
 8  
 9  from hpclb import cli, project
10  
11  if typing.TYPE_CHECKING:
12      from typer.testing import CliRunner
13  
14  
15  def test_basic_init(tmp_path: pathlib.Path, runner: CliRunner) -> None:
16      """Make sure basic usage of the init subcommand behaves as expected."""
17      project_path = tmp_path / "project"
18      res = runner.invoke(
19          cli.app, ["init", "--name", "basic_init", str(project_path), "--offline"]
20      )
21  
22      assert res.exit_code == 0, res.output
23      created = project.Project(project_path)
24  
25      assert created.config_file == project_path / "hpclb.yaml"
26      assert created.config_file.exists()
27      assert (project_path / "pyproject.toml").exists()
28      profile_list = created.verdi(["profile", "list"]).stdout.splitlines()
29      assert profile_list[0].split()[-1] == str(project_path / ".aiida")
30      assert len(profile_list) == 2  # config dir line + presto
31  
32  
33  def test_init_existing_empty(tmp_path: pathlib.Path, runner: CliRunner) -> None:
34      """Check no error when the project path exists but is empty."""
35      project_path = tmp_path
36      project_path.mkdir(exist_ok=True)
37      res = runner.invoke(
38          cli.app, ["init", "--name", "empty_init", str(project_path), "--offline"]
39      )
40      assert res.exit_code == 0, res.output
41  
42  
43  def test_init_existing_nonempty(tmp_path: pathlib.Path, runner: CliRunner) -> None:
44      """Check does not overwrite existing project config."""
45      (tmp_path / "hpclb.yaml").touch()
46      res = runner.invoke(
47          cli.app, ["init", "--name", "existing_init", str(tmp_path), "--offline"]
48      )
49      assert res.exit_code == 2
50      assert re.findall(
51          r"Project is already initialized",
52          res.output,
53          re.MULTILINE,
54      )