Coverage for tests/test_cli_unlink.py: 100%
69 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-11-09 00:25 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-11-09 00:25 +0000
1import json 1vwxyzAB
2from pathlib import Path 1vwxyzAB
4from typer.testing import CliRunner 1vwxyzAB
6from fastapi_cloud_cli.cli import cloud_app as app 1vwxyzAB
8runner = CliRunner() 1vwxyzAB
11def test_apps_unlink_removes_cloud_json_and_preserves_fastapicloud_dir( 1vwxyzAB
12 tmp_path: Path,
13) -> None:
14 config_dir = tmp_path / ".fastapicloud" 1abcdefg
15 config_dir.mkdir(parents=True) 1abcdefg
17 cloud_json = config_dir / "cloud.json" 1abcdefg
18 cloud_json.write_text('{"app_id": "123", "team_id": "456"}') 1abcdefg
20 readme_file = config_dir / "README.md" 1abcdefg
21 readme_file.write_text("# FastAPI Cloud Configuration") 1abcdefg
23 gitignore_file = config_dir / ".gitignore" 1abcdefg
24 gitignore_file.write_text("*") 1abcdefg
26 result = runner.invoke(app, ["apps", "unlink", "--path", str(tmp_path)]) 1abcdefg
28 assert result.exit_code == 0 1abcdefg
29 assert "Removed app link" in result.output 1abcdefg
30 assert "Deleted" in result.output 1abcdefg
31 assert config_dir.exists() 1abcdefg
32 assert not cloud_json.exists() 1abcdefg
33 assert readme_file.exists() 1abcdefg
34 assert gitignore_file.exists() 1abcdefg
37def test_unlink_compatibility_shortcut_removes_cloud_json(tmp_path: Path) -> None: 1vwxyzAB
38 config_dir = tmp_path / ".fastapicloud" 1opqrstu
39 config_dir.mkdir(parents=True) 1opqrstu
41 cloud_json = config_dir / "cloud.json" 1opqrstu
42 cloud_json.write_text('{"app_id": "123", "team_id": "456"}') 1opqrstu
44 readme_file = config_dir / "README.md" 1opqrstu
45 readme_file.write_text("# FastAPI Cloud Configuration") 1opqrstu
47 result = runner.invoke(app, ["unlink", "--path", str(tmp_path)]) 1opqrstu
49 assert result.exit_code == 0 1opqrstu
50 assert "Removed app link" in result.output 1opqrstu
51 assert not cloud_json.exists() 1opqrstu
52 assert readme_file.exists() 1opqrstu
53 assert config_dir.exists() 1opqrstu
56def test_apps_unlink_as_json(tmp_path: Path) -> None: 1vwxyzAB
57 config_dir = tmp_path / ".fastapicloud" 1CDEFGHI
58 config_dir.mkdir(parents=True) 1CDEFGHI
60 cloud_json = config_dir / "cloud.json" 1CDEFGHI
61 cloud_json.write_text('{"app_id": "123", "team_id": "456"}') 1CDEFGHI
63 result = runner.invoke(app, ["apps", "unlink", "--path", str(tmp_path), "--json"]) 1CDEFGHI
65 assert result.exit_code == 0 1CDEFGHI
66 assert json.loads(result.stdout) == { 1CDEFGHI
67 "data": {
68 "unlinked": True,
69 "removed_path": str(cloud_json),
70 }
71 }
72 assert result.stderr == "" 1CDEFGHI
73 assert config_dir.exists() 1CDEFGHI
74 assert not cloud_json.exists() 1CDEFGHI
77def test_unlink_compatibility_shortcut_as_json(tmp_path: Path) -> None: 1vwxyzAB
78 config_dir = tmp_path / ".fastapicloud" 1hijklmn
79 config_dir.mkdir(parents=True) 1hijklmn
81 cloud_json = config_dir / "cloud.json" 1hijklmn
82 cloud_json.write_text('{"app_id": "123", "team_id": "456"}') 1hijklmn
84 readme_file = config_dir / "README.md" 1hijklmn
85 readme_file.write_text("# FastAPI Cloud Configuration") 1hijklmn
87 result = runner.invoke(app, ["unlink", "--path", str(tmp_path), "--json"]) 1hijklmn
89 assert result.exit_code == 0 1hijklmn
90 assert json.loads(result.stdout) == { 1hijklmn
91 "data": {
92 "unlinked": True,
93 "removed_path": str(cloud_json),
94 }
95 }
96 assert result.stderr == "" 1hijklmn
97 assert config_dir.exists() 1hijklmn
98 assert not cloud_json.exists() 1hijklmn
99 assert readme_file.exists() 1hijklmn
102def test_apps_unlink_json_returns_not_linked_when_config_is_missing( 1vwxyzAB
103 tmp_path: Path,
104) -> None:
105 result = runner.invoke(app, ["apps", "unlink", "--path", str(tmp_path), "--json"]) 1JKLMNOP
107 assert result.exit_code == 1 1JKLMNOP
108 assert json.loads(result.stdout) == { 1JKLMNOP
109 "error": {
110 "code": "not_linked",
111 "message": "No app is linked to this directory.",
112 "hint": "Run `fastapi cloud link` to link an app.",
113 }
114 }
115 assert result.stderr == "" 1JKLMNOP
118def test_unlink_when_no_configuration_exists(tmp_path: Path) -> None: 1vwxyzAB
119 result = runner.invoke(app, ["unlink", "--path", str(tmp_path)]) 1QRSTUVW
121 assert result.exit_code == 1 1QRSTUVW
122 assert "No app is linked to this directory." in result.output 1QRSTUVW