-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_archive.py
More file actions
165 lines (124 loc) · 5.01 KB
/
test_archive.py
File metadata and controls
165 lines (124 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from pathlib import Path
import fastar
import pytest
from fastapi_cloud_cli.commands.deploy import archive
@pytest.fixture
def src_path(tmp_path: Path) -> Path:
path = tmp_path / "source"
path.mkdir()
return path
@pytest.fixture
def dst_path(tmp_path: Path) -> Path:
path = tmp_path / "destination"
path.mkdir()
return path
@pytest.fixture
def tar_path(tmp_path: Path) -> Path:
return tmp_path / "archive.tar"
def test_archive_creates_tar_file(src_path: Path, tar_path: Path) -> None:
(src_path / "main.py").write_text("print('hello')")
(src_path / "config.json").write_text('{"key": "value"}')
(src_path / "subdir").mkdir()
(src_path / "subdir" / "utils.py").write_text("def helper(): pass")
archive(src_path, tar_path)
assert tar_path.exists()
def test_archive_excludes_venv_and_similar_folders(
src_path: Path, tar_path: Path, dst_path: Path
) -> None:
"""Should exclude .venv directory from archive."""
# the only files we want to include
(src_path / "main.py").write_text("print('hello')")
(src_path / "static").mkdir()
(src_path / "static" / "index.html").write_text("<html></html>")
# virtualenv
(src_path / ".venv").mkdir()
(src_path / ".venv" / "lib").mkdir()
(src_path / ".venv" / "lib" / "package.py").write_text("# package")
# pycache
(src_path / "__pycache__").mkdir()
(src_path / "__pycache__" / "main.cpython-311.pyc").write_text("bytecode")
# pyc files
(src_path / "main.pyc").write_text("bytecode")
# mypy/pytest
(src_path / ".mypy_cache").mkdir()
(src_path / ".mypy_cache" / "file.json").write_text("{}")
(src_path / ".pytest_cache").mkdir()
(src_path / ".pytest_cache" / "cache.db").write_text("data")
archive(src_path, tar_path)
with fastar.open(tar_path, "r") as tar:
tar.unpack(dst_path)
assert set(dst_path.glob("**/*")) == {
dst_path / "main.py",
dst_path / "static",
dst_path / "static" / "index.html",
}
def test_archive_preserves_relative_paths(
src_path: Path, tar_path: Path, dst_path: Path
) -> None:
(src_path / "src").mkdir()
(src_path / "src" / "app").mkdir()
(src_path / "src" / "app" / "main.py").write_text("print('hello')")
archive(src_path, tar_path)
with fastar.open(tar_path, "r") as tar:
tar.unpack(dst_path)
assert set(dst_path.glob("**/*")) == {
dst_path / "src",
dst_path / "src" / "app",
dst_path / "src" / "app" / "main.py",
}
def test_archive_respects_fastapicloudignore(
src_path: Path, tar_path: Path, dst_path: Path
) -> None:
"""Should exclude files specified in .fastapicloudignore."""
(src_path / "main.py").write_text("print('hello')")
(src_path / "config.py").write_text("CONFIG = 'value'")
(src_path / "secrets.env").write_text("SECRET_KEY=xyz")
(src_path / "data").mkdir()
(src_path / "data" / "file.txt").write_text("data")
(src_path / ".fastapicloudignore").write_text("secrets.env\ndata/\n")
archive(src_path, tar_path)
with fastar.open(tar_path, "r") as tar:
tar.unpack(dst_path)
assert set(dst_path.glob("**/*")) == {
dst_path / "main.py",
dst_path / "config.py",
}
def test_archive_respects_fastapicloudignore_unignore(
src_path: Path, tar_path: Path, dst_path: Path
) -> None:
"""Test we can use .fastapicloudignore to unignore files inside .gitignore"""
(src_path / "main.py").write_text("print('hello')")
(src_path / "ignore_me.txt").write_text("You should ignore me")
(src_path / "static/build").mkdir(exist_ok=True, parents=True)
(src_path / "static/build/style.css").write_text("body { background: #bada55 }")
# Rignore needs a .git folder to make .gitignore work
(src_path / ".git").mkdir(exist_ok=True, parents=True)
(src_path / ".git" / "config").write_text("[core]\n\trepositoryformatversion = 0")
(src_path / ".gitignore").write_text("ignore_me.txt\nbuild/")
(src_path / ".fastapicloudignore").write_text("!static/build")
archive(src_path, tar_path)
with fastar.open(tar_path, "r") as tar:
tar.unpack(dst_path)
assert set(dst_path.glob("**/*")) == {
dst_path / "main.py",
dst_path / "static",
dst_path / "static" / "build",
dst_path / "static" / "build" / "style.css",
}
def test_archive_includes_hidden_files_but_excludes_env(
src_path: Path, tar_path: Path, dst_path: Path
) -> None:
"""Should include hidden files but exclude .env files."""
(src_path / "main.py").write_text("print('hello')")
(src_path / ".env").write_text("SECRET_KEY=xyz")
(src_path / ".env.local").write_text("LOCAL_KEY=abc")
(src_path / ".config").mkdir()
(src_path / ".config" / "settings.json").write_text('{"setting": "value"}')
archive(src_path, tar_path)
with fastar.open(tar_path, "r") as tar:
tar.unpack(dst_path)
assert set(dst_path.glob("**/*")) == {
dst_path / "main.py",
dst_path / ".config",
dst_path / ".config" / "settings.json",
}