diff --git a/src/specify_cli/workflows/command_add.py b/src/specify_cli/workflows/command_add.py index 37f03433b2..13b215510f 100644 --- a/src/specify_cli/workflows/command_add.py +++ b/src/specify_cli/workflows/command_add.py @@ -95,7 +95,11 @@ def workflow_add( ) def _validate_and_install_local( - yaml_path: cli.Path, source_label: str, expected_id: str | None = None + yaml_path: cli.Path, + source_label: str, + expected_id: str | None = None, + *, + preserve_source_mode: bool = True, ) -> None: """Validate and install a workflow from a local YAML file.""" try: @@ -149,7 +153,12 @@ def _validate_and_install_local( existed_before = dest_dir.is_dir() try: - staged_file = cli._stage_workflow_file(dest_dir) + staged_file = cli._stage_workflow_file( + dest_dir, + use_project_file_mode=( + not preserve_source_mode and not dest_file.exists() + ), + ) except OSError as exc: cli.console.print( f"[red]Error:[/red] Failed to install workflow " @@ -161,7 +170,8 @@ def _validate_and_install_local( # Write the exact bytes parsed above so a concurrent source edit # cannot desynchronize installed content from validated metadata. staged_file.write_bytes(source_content) - staged_file.set_mode(source_mode) + if preserve_source_mode: + staged_file.set_mode(source_mode) except OSError as exc: cli._safe_discard_staged_workflow_file( staged_file, dest_dir, existed_before @@ -447,6 +457,7 @@ def _validate_and_install_local( tmp_path, download_url, expected_id=source if from_url else None, + preserve_source_mode=False, ) else: with tempfile.TemporaryDirectory( diff --git a/tests/specify_cli/workflows/test_command_add.py b/tests/specify_cli/workflows/test_command_add.py index 263fe79add..4aa9e77a87 100644 --- a/tests/specify_cli/workflows/test_command_add.py +++ b/tests/specify_cli/workflows/test_command_add.py @@ -2527,6 +2527,104 @@ def test_add_catalog_fresh_install_uses_project_file_mode( ) assert stat.S_IMODE(workflow_file.stat().st_mode) == 0o644 + @pytest.mark.skipif(os.name == "nt", reason="POSIX permission bits") + @pytest.mark.parametrize("mode", ["url", "from_url"]) + def test_add_url_fresh_install_uses_project_file_mode( + self, project_dir, monkeypatch, mode + ): + from typer.testing import CliRunner + from specify_cli import app + from specify_cli.workflows.catalog import WorkflowCatalog + + monkeypatch.chdir(project_dir) + data = self.WORKFLOW_YAML.format(version="1.0.0").encode() + + if mode == "from_url": + monkeypatch.setattr( + WorkflowCatalog, + "get_workflow_info", + lambda self, wid: { + "id": wid, + "name": "Align Workflow", + "version": "1.0.0", + "url": "https://example.com/ignored.yml", + "_install_allowed": True, + "_catalog_name": "test-catalog", + }, + ) + args = [ + "workflow", + "add", + "align-wf", + "--from", + "https://example.com/workflow.yml", + ] + prompt_input = "y\n" + else: + args = ["workflow", "add", "https://example.com/workflow.yml"] + prompt_input = None + + previous_umask = os.umask(0o022) + try: + with pytest.MonkeyPatch.context() as mp: + mp.setattr( + "specify_cli.authentication.http.open_url", + lambda url, timeout=None, extra_headers=None, + redirect_validator=None: self._FakeResponse(data, url), + ) + result = CliRunner().invoke(app, args, input=prompt_input) + finally: + os.umask(previous_umask) + + assert result.exit_code == 0, result.output + workflow_file = ( + project_dir + / ".specify" + / "workflows" + / "align-wf" + / "workflow.yml" + ) + assert stat.S_IMODE(workflow_file.stat().st_mode) == 0o644 + + @pytest.mark.skipif(os.name == "nt", reason="POSIX permission bits") + def test_add_url_reinstall_preserves_existing_file_mode( + self, project_dir, monkeypatch + ): + from typer.testing import CliRunner + from specify_cli import app + + monkeypatch.chdir(project_dir) + workflow_file = ( + project_dir + / ".specify" + / "workflows" + / "align-wf" + / "workflow.yml" + ) + workflow_file.parent.mkdir(parents=True) + workflow_file.write_text( + self.WORKFLOW_YAML.format(version="1.0.0"), encoding="utf-8" + ) + workflow_file.chmod(0o640) + + new_data = self.WORKFLOW_YAML.format(version="2.0.0").encode() + previous_umask = os.umask(0o022) + try: + with pytest.MonkeyPatch.context() as mp: + mp.setattr( + "specify_cli.authentication.http.open_url", + lambda url, timeout=None, extra_headers=None, + redirect_validator=None: self._FakeResponse(new_data, url), + ) + result = CliRunner().invoke( + app, ["workflow", "add", "https://example.com/workflow.yml"] + ) + finally: + os.umask(previous_umask) + + assert result.exit_code == 0, result.output + assert stat.S_IMODE(workflow_file.stat().st_mode) == 0o640 + def test_concurrent_catalog_reinstalls_keep_file_and_registry_aligned( self, project_dir, monkeypatch ):