11import tarfile
22from pathlib import Path
33
4+ import pytest
5+
46from fastapi_cloud_cli .commands .deploy import archive
57
68
7- def test_archive_creates_tar_file (tmp_path : Path ) -> None :
8- (tmp_path / "main.py" ).write_text ("print('hello')" )
9- (tmp_path / "config.json" ).write_text ('{"key": "value"}' )
10- (tmp_path / "subdir" ).mkdir ()
11- (tmp_path / "subdir" / "utils.py" ).write_text ("def helper(): pass" )
9+ @pytest .fixture
10+ def src_path (tmp_path : Path ) -> Path :
11+ path = tmp_path / "source"
12+ path .mkdir ()
13+ return path
14+
15+
16+ @pytest .fixture
17+ def tar_path (tmp_path : Path ) -> Path :
18+ return tmp_path / "archive.tar"
19+
1220
13- tar_path = archive (tmp_path )
21+ def test_archive_creates_tar_file (src_path : Path , tar_path : Path ) -> None :
22+ (src_path / "main.py" ).write_text ("print('hello')" )
23+ (src_path / "config.json" ).write_text ('{"key": "value"}' )
24+ (src_path / "subdir" ).mkdir ()
25+ (src_path / "subdir" / "utils.py" ).write_text ("def helper(): pass" )
1426
27+ archive (src_path , tar_path )
1528 assert tar_path .exists ()
16- assert tar_path .suffix == ".tar"
17- assert tar_path .name .startswith ("fastapi-cloud-deploy-" )
1829
1930
20- def test_archive_excludes_venv_and_similar_folders (tmp_path : Path ) -> None :
31+ def test_archive_excludes_venv_and_similar_folders (
32+ src_path : Path , tar_path : Path
33+ ) -> None :
2134 """Should exclude .venv directory from archive."""
2235 # the only files we want to include
23- (tmp_path / "main.py" ).write_text ("print('hello')" )
24- (tmp_path / "static" ).mkdir ()
25- (tmp_path / "static" / "index.html" ).write_text ("<html></html>" )
36+ (src_path / "main.py" ).write_text ("print('hello')" )
37+ (src_path / "static" ).mkdir ()
38+ (src_path / "static" / "index.html" ).write_text ("<html></html>" )
2639 # virtualenv
27- (tmp_path / ".venv" ).mkdir ()
28- (tmp_path / ".venv" / "lib" ).mkdir ()
29- (tmp_path / ".venv" / "lib" / "package.py" ).write_text ("# package" )
40+ (src_path / ".venv" ).mkdir ()
41+ (src_path / ".venv" / "lib" ).mkdir ()
42+ (src_path / ".venv" / "lib" / "package.py" ).write_text ("# package" )
3043 # pycache
31- (tmp_path / "__pycache__" ).mkdir ()
32- (tmp_path / "__pycache__" / "main.cpython-311.pyc" ).write_text ("bytecode" )
44+ (src_path / "__pycache__" ).mkdir ()
45+ (src_path / "__pycache__" / "main.cpython-311.pyc" ).write_text ("bytecode" )
3346 # pyc files
34- (tmp_path / "main.pyc" ).write_text ("bytecode" )
47+ (src_path / "main.pyc" ).write_text ("bytecode" )
3548 # mypy/pytest
36- (tmp_path / ".mypy_cache" ).mkdir ()
37- (tmp_path / ".mypy_cache" / "file.json" ).write_text ("{}" )
38- (tmp_path / ".pytest_cache" ).mkdir ()
39- (tmp_path / ".pytest_cache" / "cache.db" ).write_text ("data" )
49+ (src_path / ".mypy_cache" ).mkdir ()
50+ (src_path / ".mypy_cache" / "file.json" ).write_text ("{}" )
51+ (src_path / ".pytest_cache" ).mkdir ()
52+ (src_path / ".pytest_cache" / "cache.db" ).write_text ("data" )
4053
41- tar_path = archive (tmp_path )
54+ archive (src_path , tar_path )
4255
4356 with tarfile .open (tar_path , "r" ) as tar :
4457 names = tar .getnames ()
4558 assert set (names ) == {"main.py" , "static/index.html" }
4659
4760
48- def test_archive_preserves_relative_paths (tmp_path : Path ) -> None :
49- (tmp_path / "src" ).mkdir ()
50- (tmp_path / "src" / "app" ).mkdir ()
51- (tmp_path / "src" / "app" / "main.py" ).write_text ("print('hello')" )
61+ def test_archive_preserves_relative_paths (src_path : Path , tar_path : Path ) -> None :
62+ (src_path / "src" ).mkdir ()
63+ (src_path / "src" / "app" ).mkdir ()
64+ (src_path / "src" / "app" / "main.py" ).write_text ("print('hello')" )
5265
53- tar_path = archive (tmp_path )
66+ archive (src_path , tar_path )
5467
5568 with tarfile .open (tar_path , "r" ) as tar :
5669 names = tar .getnames ()
5770 assert names == ["src/app/main.py" ]
5871
5972
60- def test_archive_respects_fastapicloudignore (tmp_path : Path ) -> None :
73+ def test_archive_respects_fastapicloudignore (src_path : Path , tar_path : Path ) -> None :
6174 """Should exclude files specified in .fastapicloudignore."""
6275 # Create test files
63- (tmp_path / "main.py" ).write_text ("print('hello')" )
64- (tmp_path / "config.py" ).write_text ("CONFIG = 'value'" )
65- (tmp_path / "secrets.env" ).write_text ("SECRET_KEY=xyz" )
66- (tmp_path / "data" ).mkdir ()
67- (tmp_path / "data" / "file.txt" ).write_text ("data" )
76+ (src_path / "main.py" ).write_text ("print('hello')" )
77+ (src_path / "config.py" ).write_text ("CONFIG = 'value'" )
78+ (src_path / "secrets.env" ).write_text ("SECRET_KEY=xyz" )
79+ (src_path / "data" ).mkdir ()
80+ (src_path / "data" / "file.txt" ).write_text ("data" )
6881
6982 # Create .fastapicloudignore file
70- (tmp_path / ".fastapicloudignore" ).write_text ("secrets.env\n data/\n " )
83+ (src_path / ".fastapicloudignore" ).write_text ("secrets.env\n data/\n " )
7184
7285 # Create archive
73- tar_path = archive (tmp_path )
86+ archive (src_path , tar_path )
7487
7588 # Verify ignored files are excluded
7689 with tarfile .open (tar_path , "r" ) as tar :
@@ -81,21 +94,23 @@ def test_archive_respects_fastapicloudignore(tmp_path: Path) -> None:
8194 }
8295
8396
84- def test_archive_respects_fastapicloudignore_unignore (tmp_path : Path ) -> None :
97+ def test_archive_respects_fastapicloudignore_unignore (
98+ src_path : Path , tar_path : Path
99+ ) -> None :
85100 """Test we can use .fastapicloudignore to unignore files inside .gitignore"""
86101 # Create test files
87- (tmp_path / "main.py" ).write_text ("print('hello')" )
88- (tmp_path / "static/build" ).mkdir (exist_ok = True , parents = True )
89- (tmp_path / "static/build/style.css" ).write_text ("body { background: #bada55 }" )
102+ (src_path / "main.py" ).write_text ("print('hello')" )
103+ (src_path / "static/build" ).mkdir (exist_ok = True , parents = True )
104+ (src_path / "static/build/style.css" ).write_text ("body { background: #bada55 }" )
90105 # Rignore needs a .git folder to make .gitignore work
91- (tmp_path / ".git" ).mkdir (exist_ok = True , parents = True )
92- (tmp_path / ".gitignore" ).write_text ("build/" )
106+ (src_path / ".git" ).mkdir (exist_ok = True , parents = True )
107+ (src_path / ".gitignore" ).write_text ("build/" )
93108
94109 # Create .fastapicloudignore file
95- (tmp_path / ".fastapicloudignore" ).write_text ("!static/build" )
110+ (src_path / ".fastapicloudignore" ).write_text ("!static/build" )
96111
97112 # Create archive
98- tar_path = archive (tmp_path )
113+ archive (src_path , tar_path )
99114
100115 # Verify ignored files are excluded
101116 with tarfile .open (tar_path , "r" ) as tar :
0 commit comments