-
Notifications
You must be signed in to change notification settings - Fork 0
fix: devbase list 経路の env 変数展開 + env init で VS Code 自動オープン設定 #71
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
Merged
+261
−18
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9627183
fix: TUI(list) 経路で env の変数参照を展開する
takemi-ohama 438ca9e
feat: devbase env init で DEVBASE_OPEN_EDITOR を対話設定する (既定1)
takemi-ohama e8d1970
test: _load_project_env テストの os.environ 漏出を防止
takemi-ohama e088b4b
fix: env 変数展開を $VAR/${VAR} 限定の専用関数にし $ エスケープを尊重
takemi-ohama 55043ab
docs: _load_project_env docstring の shell インラインコメント挙動の記述を修正
takemi-ohama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """エディタ動作設定コレクター (VS Code 自動オープン)。 | ||
|
|
||
| ``devbase up`` / ``devbase list`` 後に dev コンテナへ接続した VS Code を自動で | ||
| 開くか (``DEVBASE_OPEN_EDITOR``) を ``devbase env init`` 時に対話的に設定する。 | ||
| 既定は有効 (``1``)。プロジェクト個別の ``env`` で ``DEVBASE_OPEN_EDITOR=0`` を | ||
| 指定すれば、このグローバル既定を上書きして個別に無効化できる | ||
| (プロジェクト env はグローバル ``.env`` より後に読まれるため優先される)。 | ||
| """ | ||
|
|
||
| from devbase.log import get_logger | ||
| from devbase.env import keys | ||
| from devbase.env.store import EnvFile, safe_input | ||
| from devbase.env.collector import Collector | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| # 応答の真偽解釈 (大小無視)。空入力は default に倒れる (safe_input が処理)。 | ||
| _YES = {"1", "y", "yes", "true", "on"} | ||
| _NO = {"0", "n", "no", "false", "off"} | ||
|
|
||
|
|
||
| def _normalize(answer: str, default: str) -> str: | ||
| """ユーザー応答を ``"1"`` / ``"0"`` に正規化する。未知の値は default。""" | ||
| a = answer.strip().lower() | ||
| if a in _YES: | ||
| return "1" | ||
| if a in _NO: | ||
| return "0" | ||
| return default | ||
|
|
||
|
|
||
| def collect_open_editor(env_file: EnvFile) -> None: | ||
| """``DEVBASE_OPEN_EDITOR`` を対話的に設定する (既定: ``1`` = 有効)。 | ||
|
|
||
| 既存値 (``0`` / ``1``) があればそれを既定として提示し、空入力で維持する。 | ||
| 非対話 (EOF) 環境では default が確定する。 | ||
| """ | ||
| existing = env_file.get(keys.DEVBASE_OPEN_EDITOR) | ||
| default = existing if existing in ("0", "1") else "1" | ||
| answer = safe_input( | ||
| f"{keys.DEVBASE_OPEN_EDITOR}: devbase up/list 後に VS Code を自動オープンしますか? " | ||
| f"[Y/n] (既定={default}): ", | ||
| default, | ||
| ) | ||
| value = _normalize(answer, default) | ||
| env_file.set(keys.DEVBASE_OPEN_EDITOR, value) | ||
| logger.info("%s = %s", keys.DEVBASE_OPEN_EDITOR, value) | ||
|
|
||
|
|
||
| COLLECTOR = Collector( | ||
| name="editor", | ||
| display_name="VS Code 自動オープン (DEVBASE_OPEN_EDITOR)", | ||
| collect_fn=collect_open_editor, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| """collectors/editor.py: VS Code 自動オープン (DEVBASE_OPEN_EDITOR) コレクタ""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import builtins | ||
|
|
||
| import pytest | ||
|
|
||
| from devbase.env import keys | ||
| from devbase.env.store import EnvFile | ||
| from devbase.env.collector import CollectorRegistry | ||
| from devbase.env.collectors import editor | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def env_file(tmp_path): | ||
| return EnvFile(tmp_path / ".env") | ||
|
|
||
|
|
||
| def _patch_input(monkeypatch, responses): | ||
| """input() を順番に responses で返すモックに差し替える (尽きたら EOFError)。""" | ||
| it = iter(responses) | ||
|
|
||
| def fake_input(prompt=""): | ||
| try: | ||
| return next(it) | ||
| except StopIteration: | ||
| raise EOFError | ||
|
|
||
| monkeypatch.setattr(builtins, "input", fake_input) | ||
|
|
||
|
|
||
| def test_default_enabled_on_eof(monkeypatch, env_file): | ||
| """入力 EOF (非対話/CI) → 既定 1 が設定される""" | ||
| _patch_input(monkeypatch, []) | ||
|
|
||
| editor.collect_open_editor(env_file) | ||
|
|
||
| assert env_file.get(keys.DEVBASE_OPEN_EDITOR) == "1" | ||
|
|
||
|
|
||
| def test_empty_input_keeps_default(monkeypatch, env_file): | ||
| """空入力 (Enter のみ) → 既定 1 を維持""" | ||
| _patch_input(monkeypatch, [""]) | ||
|
|
||
| editor.collect_open_editor(env_file) | ||
|
|
||
| assert env_file.get(keys.DEVBASE_OPEN_EDITOR) == "1" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("answer", ["n", "N", "no", "0", "false", "off"]) | ||
| def test_can_disable(monkeypatch, env_file, answer): | ||
| """否定的な応答 → 0 (無効) に設定できる (選択可能)""" | ||
| _patch_input(monkeypatch, [answer]) | ||
|
|
||
| editor.collect_open_editor(env_file) | ||
|
|
||
| assert env_file.get(keys.DEVBASE_OPEN_EDITOR) == "0" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("answer", ["y", "Y", "yes", "1", "true", "on"]) | ||
| def test_can_enable(monkeypatch, env_file, answer): | ||
| """肯定的な応答 → 1 (有効) に設定できる""" | ||
| _patch_input(monkeypatch, [answer]) | ||
|
|
||
| editor.collect_open_editor(env_file) | ||
|
|
||
| assert env_file.get(keys.DEVBASE_OPEN_EDITOR) == "1" | ||
|
|
||
|
|
||
| def test_existing_value_used_as_default(monkeypatch, env_file): | ||
| """既存値 (0) があれば空入力でそれを既定として維持する""" | ||
| env_file.set(keys.DEVBASE_OPEN_EDITOR, "0") | ||
| _patch_input(monkeypatch, [""]) # Enter → 既存の 0 を維持 | ||
|
|
||
| editor.collect_open_editor(env_file) | ||
|
|
||
| assert env_file.get(keys.DEVBASE_OPEN_EDITOR) == "0" | ||
|
|
||
|
|
||
| def test_unknown_answer_falls_back_to_default(monkeypatch, env_file): | ||
| """未知の応答は既定 (1) にフォールバック""" | ||
| _patch_input(monkeypatch, ["maybe"]) | ||
|
|
||
| editor.collect_open_editor(env_file) | ||
|
|
||
| assert env_file.get(keys.DEVBASE_OPEN_EDITOR) == "1" | ||
|
|
||
|
|
||
| def test_collector_registered(): | ||
| """CollectorRegistry が editor コレクタを自動検出する""" | ||
| registry = CollectorRegistry() | ||
| registry.discover() | ||
| names = {c.name for c in registry.collectors} | ||
| assert "editor" in names |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.