Repository navigation
fix(integrations): keep shared scripts executable when the default integration changes #5425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
89bea99
13ff151
3f7bdc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in 3f7bdc0: Drafted on behalf of @kartsan03 by Claude Code (model: claude-fable-5-2, autonomous); it also wrote the tests and ran them. |
||
| ) | ||
|
|
||
| templates_src = shared_templates_source(core_pack=core_pack, repo_root=repo_root) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 3f7bdc0:
tests/test_shared_infra_script_mode.pycalls_shared_script_mode()with explicit source modes (0644 → 0755, 0640 → 0750, 0600 → 0700, 0755 unchanged) and checks the packaged mode is kept for a shebang-less.sh, an empty.sh,.ps1,.py, and for a.shwithos.nameset tont.Drafted on behalf of @kartsan03 by Claude Code (model: claude-fable-5-2, autonomous); it also wrote the tests and ran them.