From 89bea998df0d5e5438bc59dbae6bdb546f59fcc2 Mon Sep 17 00:00:00 2001 From: kartsan03 Date: Fri, 13 Nov 2026 14:14:53 +0000 Subject: [PATCH 1/6] fix(integrations): keep shared scripts executable when the default integration changes `integration use`, every `integration switch` path, and `integration uninstall` of the default integration refresh shared infrastructure through `_set_default_integration()`. The refresh rewrites `.specify/scripts/bash/*.sh` with the packaged mode (0644) and nothing restored the execute bits, so the scripts that generated commands and skills run directly failed with "Permission denied". Restore them after the refresh, as init, install and upgrade already do. Assisted-by: Codex (model: gpt-6.1-sol, autonomous) Assisted-by: Claude Code (model: claude-opus-5-5, autonomous) --- src/specify_cli/integrations/_helpers.py | 5 +++++ .../integrations/test_command_switch.py | 22 +++++++++++++++++++ .../integrations/test_command_uninstall.py | 17 ++++++++++++++ .../integrations/test_command_use.py | 17 ++++++++++++++ 4 files changed, 61 insertions(+) diff --git a/src/specify_cli/integrations/_helpers.py b/src/specify_cli/integrations/_helpers.py index 4a7bc6b346..ca7db4cbf8 100644 --- a/src/specify_cli/integrations/_helpers.py +++ b/src/specify_cli/integrations/_helpers.py @@ -369,6 +369,11 @@ def _set_default_integration( raise _SharedTemplateRefreshError( f"Failed to refresh shared infrastructure for '{key}': {exc}" ) from exc + # The refresh rewrites shared scripts with the packaged (non-executable) + # mode, so restore execute bits as init, install and upgrade do. + if os.name != "nt": + from .. import ensure_executable_scripts + ensure_executable_scripts(project_root) _write_integration_json(project_root, key, installed_keys, settings) _update_init_options_for_integration( diff --git a/tests/specify_cli/integrations/test_command_switch.py b/tests/specify_cli/integrations/test_command_switch.py index 0c928a44a1..c13bb29c2e 100644 --- a/tests/specify_cli/integrations/test_command_switch.py +++ b/tests/specify_cli/integrations/test_command_switch.py @@ -60,6 +60,28 @@ def _project_file_state(project): class TestIntegrationSwitch: + @pytest.mark.skipif( + os.name == "nt", reason="POSIX execute bits are not meaningful on Windows" + ) + @pytest.mark.parametrize( + ("target_state", "flags"), + [("new", []), ("installed", []), ("default", ["--force"]), ("default", ["--refresh-shared-infra"])], + ) + def test_switch_keeps_shared_scripts_executable(self, tmp_path, target_state, flags): + """Every switch path refreshes shared scripts; they must stay runnable.""" + if target_state == "default": + project = _init_project(tmp_path, "codex") + else: + project = _init_codex_switch_project(tmp_path, target_state) + scripts = sorted((project / ".specify" / "scripts" / "bash").glob("*.sh")) + assert scripts and all(os.access(script, os.X_OK) for script in scripts) + + result = _run_in_project(project, ["integration", "switch", "codex", *flags]) + + assert result.exit_code == 0, result.output + not_executable = [script.name for script in scripts if not os.access(script, os.X_OK)] + assert not not_executable, f"switch left shared scripts non-executable: {not_executable}" + @pytest.mark.parametrize("target_state", ["new", "installed", "default"]) @pytest.mark.parametrize( "flags", [[], ["--force"], ["--refresh-shared-infra"], ["--force", "--refresh-shared-infra"]], diff --git a/tests/specify_cli/integrations/test_command_uninstall.py b/tests/specify_cli/integrations/test_command_uninstall.py index 02994cb259..492e99bc96 100644 --- a/tests/specify_cli/integrations/test_command_uninstall.py +++ b/tests/specify_cli/integrations/test_command_uninstall.py @@ -48,6 +48,23 @@ def _registered_agents(project, kind, package_id): return set(registry[kind][package_id]["registered_commands"]) class TestIntegrationUninstall: + @pytest.mark.skipif( + os.name == "nt", reason="POSIX execute bits are not meaningful on Windows" + ) + def test_uninstall_default_keeps_fallback_scripts_executable(self, tmp_path): + """Falling back to another installed integration must keep shared scripts runnable.""" + project = _init_project(tmp_path, "claude") + result = _run_in_project(project, ["integration", "install", "codex", "--script", "sh"]) + assert result.exit_code == 0, result.output + scripts = sorted((project / ".specify" / "scripts" / "bash").glob("*.sh")) + assert scripts and all(os.access(script, os.X_OK) for script in scripts) + + result = _run_in_project(project, ["integration", "uninstall", "claude"]) + + assert result.exit_code == 0, result.output + not_executable = [script.name for script in scripts if not os.access(script, os.X_OK)] + assert not not_executable, f"uninstall left shared scripts non-executable: {not_executable}" + @pytest.mark.parametrize("command", ["uninstall", "switch"]) def test_unreadable_dispatcher_preserves_other_cleanup(self, tmp_path, monkeypatch, caplog, command): from specify_cli.events import install_integration_events diff --git a/tests/specify_cli/integrations/test_command_use.py b/tests/specify_cli/integrations/test_command_use.py index ad47da7d44..8019926616 100644 --- a/tests/specify_cli/integrations/test_command_use.py +++ b/tests/specify_cli/integrations/test_command_use.py @@ -23,6 +23,23 @@ ) class TestIntegrationUse: + @pytest.mark.skipif( + os.name == "nt", reason="POSIX execute bits are not meaningful on Windows" + ) + def test_use_keeps_shared_scripts_executable(self, tmp_path): + """Making an installed integration the default must keep shared scripts runnable.""" + project = _init_project(tmp_path, "claude") + result = _run_in_project(project, ["integration", "install", "codex", "--script", "sh"]) + assert result.exit_code == 0, result.output + scripts = sorted((project / ".specify" / "scripts" / "bash").glob("*.sh")) + assert scripts and all(os.access(script, os.X_OK) for script in scripts) + + result = _run_in_project(project, ["integration", "use", "codex"]) + + assert result.exit_code == 0, result.output + not_executable = [script.name for script in scripts if not os.access(script, os.X_OK)] + assert not not_executable, f"use left shared scripts non-executable: {not_executable}" + @pytest.mark.parametrize("edited_command", [None, "plan", "tasks"]) def test_use_syncs_manifest_without_blessing_user_edits( self, tmp_path, tasks_preset_src, edited_command From 13ff151dc2c9f0141e75ebee46cf2853afd3025d Mon Sep 17 00:00:00 2001 From: kartsan03 Date: Mon, 16 Nov 2026 21:06:18 +0000 Subject: [PATCH 2/6] fix(integrations): set shared script execute bits in the refresh's own write The previous commit restored execute bits with `ensure_executable_scripts()` after the default-integration refresh. That scan follows a symlinked `.specify/scripts` or `.specify/extensions` root and changes modes outside the lifecycle journal, so a rollback could not undo it. Give `.sh` files that start with a shebang their execute bits in the mode `install_shared_infra()` already passes to `_write_shared_bytes()`. The mode is applied to the temp file before the atomic replace, behind the same symlink checks and journal calls as the content, and only files the refresh writes are touched. Drop the scan from `_set_default_integration()`. Assisted-by: Claude Code (model: claude-fable-5-2, autonomous) --- src/specify_cli/integrations/_helpers.py | 5 --- src/specify_cli/shared_infra.py | 22 +++++++++---- .../integrations/test_command_use.py | 31 +++++++++++++++++++ 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/specify_cli/integrations/_helpers.py b/src/specify_cli/integrations/_helpers.py index ca7db4cbf8..4a7bc6b346 100644 --- a/src/specify_cli/integrations/_helpers.py +++ b/src/specify_cli/integrations/_helpers.py @@ -369,11 +369,6 @@ def _set_default_integration( raise _SharedTemplateRefreshError( f"Failed to refresh shared infrastructure for '{key}': {exc}" ) from exc - # The refresh rewrites shared scripts with the packaged (non-executable) - # mode, so restore execute bits as init, install and upgrade do. - if os.name != "nt": - from .. import ensure_executable_scripts - ensure_executable_scripts(project_root) _write_integration_json(project_root, key, installed_keys, settings) _update_init_options_for_integration( diff --git a/src/specify_cli/shared_infra.py b/src/specify_cli/shared_infra.py index 44960706a1..fc989f9abe 100644 --- a/src/specify_cli/shared_infra.py +++ b/src/specify_cli/shared_infra.py @@ -317,6 +317,20 @@ def _ensure_safe_shared_destination( raise ValueError(f"Shared infrastructure destination escapes project root: {label}") from None +def _shared_script_mode(src_path: Path, content: bytes) -> int: + """Return the mode a shared script is written with. + + Packaged scripts are 0644. A POSIX shell script that starts with a shebang + gets an execute bit for each class that can read it (always for the owner), + the rule ``ensure_executable_scripts`` applies, so a refresh that rewrites + it leaves it runnable without a separate chmod pass. + """ + mode = src_path.stat().st_mode & 0o777 + if os.name != "nt" and src_path.suffix == ".sh" and content.startswith(b"#!"): + mode |= ((mode & 0o444) >> 2) | 0o100 + return mode + + def _write_shared_text(project_path: Path, dest: Path, content: str) -> None: _write_shared_bytes(project_path, dest, content.encode("utf-8")) @@ -636,13 +650,9 @@ def _ensure_or_bucket_dir(directory: Path) -> bool: content = _resolve_dynamic_command_refs( content, invoke_separator, invoke_prefix ) + data = content.encode("utf-8") planned_copies.append( - ( - dst_path, - rel, - content.encode("utf-8"), - src_path.stat().st_mode & 0o777, - ) + (dst_path, rel, data, _shared_script_mode(src_path, data)) ) templates_src = shared_templates_source(core_pack=core_pack, repo_root=repo_root) diff --git a/tests/specify_cli/integrations/test_command_use.py b/tests/specify_cli/integrations/test_command_use.py index 8019926616..ea85864799 100644 --- a/tests/specify_cli/integrations/test_command_use.py +++ b/tests/specify_cli/integrations/test_command_use.py @@ -40,6 +40,37 @@ def test_use_keeps_shared_scripts_executable(self, tmp_path): not_executable = [script.name for script in scripts if not os.access(script, os.X_OK)] assert not not_executable, f"use left shared scripts non-executable: {not_executable}" + @pytest.mark.skipif( + os.name == "nt", reason="POSIX execute bits are not meaningful on Windows" + ) + @pytest.mark.parametrize("linked_scripts_root", [False, True]) + def test_use_changes_modes_only_on_scripts_it_writes(self, tmp_path, linked_scripts_root): + """Execute bits come from the refresh's own writes, never from a scan. + + A user's script next to the shared ones keeps its mode, and nothing + behind a symlinked ``.specify/scripts`` (which the refresh skips) is touched. + """ + project = _init_project(tmp_path, "claude") + result = _run_in_project(project, ["integration", "install", "codex", "--script", "sh"]) + assert result.exit_code == 0, result.output + scripts_root = project / ".specify" / "scripts" + if linked_scripts_root: + outside = tmp_path / "outside-scripts" + scripts_root.rename(outside) + scripts_root.symlink_to(outside, target_is_directory=True) + user_script = scripts_root.resolve() / "bash" / "local-helper.sh" + user_script.write_bytes(b"#!/usr/bin/env bash\necho local\n") + user_script.chmod(0o644) + + result = _run_in_project(project, ["integration", "use", "codex"]) + + assert result.exit_code == 0, result.output + assert user_script.stat().st_mode & 0o777 == 0o644 + if not linked_scripts_root: + shared = sorted((scripts_root / "bash").glob("*.sh")) + not_executable = [s.name for s in shared if s != user_script and not os.access(s, os.X_OK)] + assert not not_executable, f"use left shared scripts non-executable: {not_executable}" + @pytest.mark.parametrize("edited_command", [None, "plan", "tasks"]) def test_use_syncs_manifest_without_blessing_user_edits( self, tmp_path, tasks_preset_src, edited_command From 3f7bdc03ab719840a3b2c456151a39f998989462 Mon Sep 17 00:00:00 2001 From: kartsan03 Date: Tue, 17 Nov 2026 01:28:47 +0000 Subject: [PATCH 3/6] test(integrations): cover the shared script mode rule and its rollback Unit tests for `_shared_script_mode()`: an execute bit for each class that can read a `.sh` file with a shebang, and the packaged mode for a shebang-less `.sh`, other file types and Windows. A failed switch away from an external adapter restores the mode and content of the scripts the refresh rewrote. Assisted-by: Claude Code (model: claude-fable-5-2, autonomous) --- .../integrations/test_installed_adapters.py | 26 ++++++++++ tests/test_shared_infra_script_mode.py | 52 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 tests/test_shared_infra_script_mode.py diff --git a/tests/specify_cli/integrations/test_installed_adapters.py b/tests/specify_cli/integrations/test_installed_adapters.py index 1b097d7eb2..a0130bee8d 100644 --- a/tests/specify_cli/integrations/test_installed_adapters.py +++ b/tests/specify_cli/integrations/test_installed_adapters.py @@ -1006,6 +1006,32 @@ def fail_commit(*args, **kwargs): assert snapshot(project) == before +@pytest.mark.skipif(os.name == "nt", reason="POSIX execute bits are not meaningful on Windows") +def test_failed_switch_restores_shared_script_mode_and_content(tmp_path, server, monkeypatch): + """A refresh that makes a managed script executable is undone with the rest of the switch.""" + from specify_cli.integrations import installer + + publish(server) + project = catalog_project(tmp_path, server) + install(project) + scripts = sorted((project / ".specify/scripts/bash").glob("*.sh")) + assert scripts + for script in scripts: + script.chmod(0o644) + before = snapshot(project) + + def fail_commit(*args, **kwargs): + assert all(stat.S_IMODE(script.stat().st_mode) == 0o755 for script in scripts) + raise OSError("sample package commit failure") + + monkeypatch.setattr(installer, "write_records", fail_commit) + result = run(project, ["integration", "switch", "copilot", "--script", "sh"]) + assert result.exit_code == 1, result.output + assert "sample package commit failure" in result.output + assert snapshot(project) == before + assert all(stat.S_IMODE(script.stat().st_mode) == 0o644 for script in scripts) + + @pytest.mark.parametrize("install_allowed", [False, True]) def test_round4_search_advertises_only_allowed_external_installation(tmp_path, server, install_allowed): publish(server, code='raise RuntimeError("discovery must not import sample code")') diff --git a/tests/test_shared_infra_script_mode.py b/tests/test_shared_infra_script_mode.py new file mode 100644 index 0000000000..c66f440fa1 --- /dev/null +++ b/tests/test_shared_infra_script_mode.py @@ -0,0 +1,52 @@ +"""Unit tests for the mode shared scripts are written with.""" + +from __future__ import annotations + +import os + +import pytest + +from specify_cli import shared_infra +from specify_cli.shared_infra import _shared_script_mode + +SHEBANG = b"#!/usr/bin/env bash\necho hi\n" + + +def _source(tmp_path, name, mode): + path = tmp_path / name + path.write_bytes(SHEBANG) + path.chmod(mode) + return path + + +@pytest.mark.skipif(os.name == "nt", reason="POSIX execute bits are not meaningful on Windows") +@pytest.mark.parametrize( + ("source_mode", "expected"), + [(0o644, 0o755), (0o640, 0o750), (0o600, 0o700), (0o755, 0o755), (0o200, 0o300)], +) +def test_shell_script_gets_execute_bit_for_each_class_that_reads(tmp_path, source_mode, expected): + source = _source(tmp_path, "setup-plan.sh", source_mode) + assert _shared_script_mode(source, SHEBANG) == expected + + +@pytest.mark.skipif(os.name == "nt", reason="POSIX execute bits are not meaningful on Windows") +@pytest.mark.parametrize( + ("name", "content"), + [ + ("common.sh", b"echo sourced, no shebang\n"), + ("common.sh", b""), + ("setup-plan.ps1", SHEBANG), + ("setup_plan.py", SHEBANG), + ("sh", SHEBANG), + ], +) +def test_other_files_keep_the_packaged_mode(tmp_path, name, content): + source = _source(tmp_path, name, 0o644) + assert _shared_script_mode(source, content) == 0o644 + + +def test_windows_keeps_the_packaged_mode(tmp_path, monkeypatch): + source = _source(tmp_path, "setup-plan.sh", 0o644) + packaged = source.stat().st_mode & 0o777 + monkeypatch.setattr(shared_infra.os, "name", "nt") + assert _shared_script_mode(source, SHEBANG) == packaged From b5b100f8ce42e8e678badb2a971cc347438f48bf Mon Sep 17 00:00:00 2001 From: kartsan03 Date: Tue, 17 Nov 2026 16:17:05 +0000 Subject: [PATCH 4/6] test(integrations): check the owner execute bit in the rollback test The refresh keeps the packaged script's group and other bits, so a checkout with group-writable scripts (0664) produces 0775 and the exact 0755 check failed before the simulated commit error. The mode mapping itself is covered by tests/test_shared_infra_script_mode.py. Assisted-by: Claude Code (model: claude-opus-5-5, autonomous) --- tests/specify_cli/integrations/test_installed_adapters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/specify_cli/integrations/test_installed_adapters.py b/tests/specify_cli/integrations/test_installed_adapters.py index a0130bee8d..dc8c5c3498 100644 --- a/tests/specify_cli/integrations/test_installed_adapters.py +++ b/tests/specify_cli/integrations/test_installed_adapters.py @@ -1021,7 +1021,7 @@ def test_failed_switch_restores_shared_script_mode_and_content(tmp_path, server, before = snapshot(project) def fail_commit(*args, **kwargs): - assert all(stat.S_IMODE(script.stat().st_mode) == 0o755 for script in scripts) + assert all(script.stat().st_mode & stat.S_IXUSR for script in scripts) raise OSError("sample package commit failure") monkeypatch.setattr(installer, "write_records", fail_commit) From 1ae9c47fa062877db29d249740d173da3cdd3f57 Mon Sep 17 00:00:00 2001 From: kartsan03 Date: Tue, 17 Nov 2026 18:46:40 +0000 Subject: [PATCH 5/6] test(integrations): serve the shared scripts at 0644 in the mode tests The execute-bit tests read the packaged scripts from the checkout, so a checkout whose scripts already had execute bits made them pass without the fix. A fixture now serves a copy with every file at 0644, as the wheel ships them, so they fail on main whatever modes the checkout has. Assisted-by: Claude Code (model: claude-opus-5-5, autonomous) --- tests/specify_cli/integrations/conftest.py | 26 +++++++++++++++++++ .../integrations/test_command_switch.py | 4 ++- .../integrations/test_command_uninstall.py | 4 ++- .../integrations/test_command_use.py | 6 +++-- .../integrations/test_installed_adapters.py | 4 ++- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/tests/specify_cli/integrations/conftest.py b/tests/specify_cli/integrations/conftest.py index 64f14334a4..006384d937 100644 --- a/tests/specify_cli/integrations/conftest.py +++ b/tests/specify_cli/integrations/conftest.py @@ -1,5 +1,7 @@ """Shared fixtures for mirrored integration command tests.""" +import shutil + import pytest from tests.specify_cli.integrations._helpers import _copy_project_template, _init_project @@ -37,3 +39,27 @@ def tasks_preset_src(tmp_path): - {type: command, name: speckit.tasks, file: commands/speckit.tasks.md} """) return preset + + +@pytest.fixture +def packaged_scripts_0644(tmp_path_factory, monkeypatch): + """Serve the shared scripts from a copy with every file at 0644. + + That is how the wheel ships them, so the execute-bit tests fail without the + fix whatever modes the checkout running the tests has. + """ + from specify_cli import shared_infra + + real_source = shared_infra.shared_scripts_source + copy = tmp_path_factory.mktemp("packaged-scripts") / "scripts" + + def source(*, core_pack, repo_root): + if not copy.exists(): + shutil.copytree(real_source(core_pack=core_pack, repo_root=repo_root), copy) + for path in copy.rglob("*"): + if path.is_file(): + path.chmod(0o644) + return copy + + monkeypatch.setattr(shared_infra, "shared_scripts_source", source) + return copy diff --git a/tests/specify_cli/integrations/test_command_switch.py b/tests/specify_cli/integrations/test_command_switch.py index c13bb29c2e..2d17735c4b 100644 --- a/tests/specify_cli/integrations/test_command_switch.py +++ b/tests/specify_cli/integrations/test_command_switch.py @@ -67,7 +67,9 @@ class TestIntegrationSwitch: ("target_state", "flags"), [("new", []), ("installed", []), ("default", ["--force"]), ("default", ["--refresh-shared-infra"])], ) - def test_switch_keeps_shared_scripts_executable(self, tmp_path, target_state, flags): + def test_switch_keeps_shared_scripts_executable( + self, tmp_path, target_state, flags, packaged_scripts_0644 + ): """Every switch path refreshes shared scripts; they must stay runnable.""" if target_state == "default": project = _init_project(tmp_path, "codex") diff --git a/tests/specify_cli/integrations/test_command_uninstall.py b/tests/specify_cli/integrations/test_command_uninstall.py index 492e99bc96..ab08328b78 100644 --- a/tests/specify_cli/integrations/test_command_uninstall.py +++ b/tests/specify_cli/integrations/test_command_uninstall.py @@ -51,7 +51,9 @@ class TestIntegrationUninstall: @pytest.mark.skipif( os.name == "nt", reason="POSIX execute bits are not meaningful on Windows" ) - def test_uninstall_default_keeps_fallback_scripts_executable(self, tmp_path): + def test_uninstall_default_keeps_fallback_scripts_executable( + self, tmp_path, packaged_scripts_0644 + ): """Falling back to another installed integration must keep shared scripts runnable.""" project = _init_project(tmp_path, "claude") result = _run_in_project(project, ["integration", "install", "codex", "--script", "sh"]) diff --git a/tests/specify_cli/integrations/test_command_use.py b/tests/specify_cli/integrations/test_command_use.py index ea85864799..c93fc1fa56 100644 --- a/tests/specify_cli/integrations/test_command_use.py +++ b/tests/specify_cli/integrations/test_command_use.py @@ -26,7 +26,7 @@ class TestIntegrationUse: @pytest.mark.skipif( os.name == "nt", reason="POSIX execute bits are not meaningful on Windows" ) - def test_use_keeps_shared_scripts_executable(self, tmp_path): + def test_use_keeps_shared_scripts_executable(self, tmp_path, packaged_scripts_0644): """Making an installed integration the default must keep shared scripts runnable.""" project = _init_project(tmp_path, "claude") result = _run_in_project(project, ["integration", "install", "codex", "--script", "sh"]) @@ -44,7 +44,9 @@ def test_use_keeps_shared_scripts_executable(self, tmp_path): os.name == "nt", reason="POSIX execute bits are not meaningful on Windows" ) @pytest.mark.parametrize("linked_scripts_root", [False, True]) - def test_use_changes_modes_only_on_scripts_it_writes(self, tmp_path, linked_scripts_root): + def test_use_changes_modes_only_on_scripts_it_writes( + self, tmp_path, linked_scripts_root, packaged_scripts_0644 + ): """Execute bits come from the refresh's own writes, never from a scan. A user's script next to the shared ones keeps its mode, and nothing diff --git a/tests/specify_cli/integrations/test_installed_adapters.py b/tests/specify_cli/integrations/test_installed_adapters.py index dc8c5c3498..14b33e4ef9 100644 --- a/tests/specify_cli/integrations/test_installed_adapters.py +++ b/tests/specify_cli/integrations/test_installed_adapters.py @@ -1007,7 +1007,9 @@ def fail_commit(*args, **kwargs): @pytest.mark.skipif(os.name == "nt", reason="POSIX execute bits are not meaningful on Windows") -def test_failed_switch_restores_shared_script_mode_and_content(tmp_path, server, monkeypatch): +def test_failed_switch_restores_shared_script_mode_and_content( + tmp_path, server, monkeypatch, packaged_scripts_0644 +): """A refresh that makes a managed script executable is undone with the rest of the switch.""" from specify_cli.integrations import installer From b43008b1419db6b2512cb8b5cbfcb28526a32178 Mon Sep 17 00:00:00 2001 From: kartsan03 Date: Tue, 17 Nov 2026 19:40:04 +0000 Subject: [PATCH 6/6] fix(integrations): keep the mode of a shared script that is already executable ensure_executable_scripts() skips a script with any execute bit, but the refresh's mode rule added bits for every class that could read it, so a packaged 0744 or 0754 script came out 0755. Add execute bits only when the packaged script has none, as ensure_executable_scripts() does. Assisted-by: Claude Code (model: claude-opus-5-5, autonomous) --- src/specify_cli/shared_infra.py | 14 ++++++++++---- tests/test_shared_infra_script_mode.py | 9 ++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/specify_cli/shared_infra.py b/src/specify_cli/shared_infra.py index fc989f9abe..68c12d3e40 100644 --- a/src/specify_cli/shared_infra.py +++ b/src/specify_cli/shared_infra.py @@ -321,12 +321,18 @@ def _shared_script_mode(src_path: Path, content: bytes) -> int: """Return the mode a shared script is written with. Packaged scripts are 0644. A POSIX shell script that starts with a shebang - gets an execute bit for each class that can read it (always for the owner), - the rule ``ensure_executable_scripts`` applies, so a refresh that rewrites - it leaves it runnable without a separate chmod pass. + and no execute bit yet gets one for each class that can read it (always for + the owner), the rule ``ensure_executable_scripts`` applies, so a refresh that + rewrites it leaves it runnable without a separate chmod pass. A script that + already has an execute bit keeps its mode. """ mode = src_path.stat().st_mode & 0o777 - if os.name != "nt" and src_path.suffix == ".sh" and content.startswith(b"#!"): + if ( + os.name != "nt" + and src_path.suffix == ".sh" + and content.startswith(b"#!") + and not mode & 0o111 + ): mode |= ((mode & 0o444) >> 2) | 0o100 return mode diff --git a/tests/test_shared_infra_script_mode.py b/tests/test_shared_infra_script_mode.py index c66f440fa1..fa27121775 100644 --- a/tests/test_shared_infra_script_mode.py +++ b/tests/test_shared_infra_script_mode.py @@ -22,13 +22,20 @@ def _source(tmp_path, name, mode): @pytest.mark.skipif(os.name == "nt", reason="POSIX execute bits are not meaningful on Windows") @pytest.mark.parametrize( ("source_mode", "expected"), - [(0o644, 0o755), (0o640, 0o750), (0o600, 0o700), (0o755, 0o755), (0o200, 0o300)], + [(0o644, 0o755), (0o640, 0o750), (0o600, 0o700), (0o200, 0o300)], ) def test_shell_script_gets_execute_bit_for_each_class_that_reads(tmp_path, source_mode, expected): source = _source(tmp_path, "setup-plan.sh", source_mode) assert _shared_script_mode(source, SHEBANG) == expected +@pytest.mark.skipif(os.name == "nt", reason="POSIX execute bits are not meaningful on Windows") +@pytest.mark.parametrize("source_mode", [0o755, 0o744, 0o754, 0o700, 0o710]) +def test_already_executable_script_keeps_its_mode(tmp_path, source_mode): + source = _source(tmp_path, "setup-plan.sh", source_mode) + assert _shared_script_mode(source, SHEBANG) == source_mode + + @pytest.mark.skipif(os.name == "nt", reason="POSIX execute bits are not meaningful on Windows") @pytest.mark.parametrize( ("name", "content"),