Skip to content

Commit 1ce4931

Browse files
committed
refac
1 parent dd663ac commit 1ce4931

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

gappsscript/apps_script_tools.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
logger = logging.getLogger(__name__)
1919

20+
_VALID_SCRIPT_FILE_TYPES = frozenset({"SERVER_JS", "HTML", "JSON"})
21+
2022
# These locks serialize updates only within this process. Other worker
2123
# processes or service instances can still race while merging the same script_id.
2224
_SCRIPT_UPDATE_LOCKS: weakref.WeakValueDictionary[str, asyncio.Lock] = (
@@ -36,7 +38,11 @@ def _normalize_script_file(file: Dict[str, Any]) -> Dict[str, str]:
3638
are dropped. Fields the caller omitted stay omitted so a merge can fall
3739
back to the existing value instead of blanking it.
3840
"""
39-
return {key: file[key] for key in ("name", "type", "source") if key in file}
41+
return {
42+
key: file[key]
43+
for key in ("name", "type", "source")
44+
if key in file and file[key] is not None
45+
}
4046

4147

4248
def _merge_script_files(
@@ -63,8 +69,20 @@ def _merge_script_files(
6369
raise UserInputError(
6470
f"File at index {index} is missing a non-empty 'name'."
6571
)
66-
key = (name, file.get("type"))
67-
if key not in merged and file.get("type") is None:
72+
file_type = file.get("type")
73+
if file_type is not None:
74+
if file_type not in _VALID_SCRIPT_FILE_TYPES:
75+
raise UserInputError(
76+
f"File '{name}' has unsupported type '{file_type}'; it must "
77+
"be one of SERVER_JS, HTML, or JSON."
78+
)
79+
if file_type == "JSON" and name != "appsscript":
80+
raise UserInputError(
81+
f"JSON file '{name}' must use the manifest name 'appsscript'."
82+
)
83+
84+
key = (name, file_type)
85+
if key not in merged and file_type is None:
6886
same_name = [existing for existing in merged if existing[0] == name]
6987
if len(same_name) != 1:
7088
raise UserInputError(

tests/gappsscript/test_apps_script_tools.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,32 @@ def test_merge_script_files_keeps_existing_fields_when_omitted():
212212
assert merged == [{"name": "Code", "type": "SERVER_JS", "source": "new code"}]
213213

214214

215+
def test_merge_script_files_keeps_existing_type_when_update_type_is_none():
216+
existing = [{"name": "Code", "type": "SERVER_JS", "source": "code"}]
217+
updates = [{"name": "Code", "type": None, "source": "new code"}]
218+
219+
merged = _merge_script_files(existing, updates)
220+
221+
assert merged == [{"name": "Code", "type": "SERVER_JS", "source": "new code"}]
222+
223+
224+
@pytest.mark.parametrize("file_type", ["TEXT", "", 123])
225+
def test_merge_script_files_rejects_unsupported_explicit_type(file_type):
226+
with pytest.raises(UserInputError, match="unsupported type"):
227+
_merge_script_files(
228+
[],
229+
[{"name": "Code", "type": file_type, "source": "source"}],
230+
)
231+
232+
233+
def test_merge_script_files_rejects_json_file_without_manifest_name():
234+
with pytest.raises(UserInputError, match="manifest name 'appsscript'"):
235+
_merge_script_files(
236+
[],
237+
[{"name": "config", "type": "JSON", "source": "{}"}],
238+
)
239+
240+
215241
def test_merge_script_files_keeps_same_name_different_type():
216242
"""Script API names exclude extensions, so Code.gs and Code.html collide."""
217243
existing = [

0 commit comments

Comments
 (0)