diff --git a/.claude/skills/design/scripts/logo/generate.py b/.claude/skills/design/scripts/logo/generate.py index f188dc0..19ffc55 100644 --- a/.claude/skills/design/scripts/logo/generate.py +++ b/.claude/skills/design/scripts/logo/generate.py @@ -56,6 +56,15 @@ from urllib.parse import urlparse from urllib.request import HTTPRedirectHandler, Request, build_opener +# Batch mode prints ✓/✗, which raises UnicodeEncodeError when stdout/stderr is +# a cp1252 pipe or file (Windows without PYTHONUTF8). Same guard as the one in +# cip/generate.py. +for _stream in (sys.stdout, sys.stderr): + _encoding = getattr(_stream, "encoding", None) + if _encoding and _encoding.lower().replace("-", "") != "utf8" and hasattr(_stream, "reconfigure"): + _stream.reconfigure(encoding="utf-8", errors=_stream.errors) + + # Load environment variables def load_env(): """Load .env files in priority order""" diff --git a/.claude/skills/design/scripts/logo/tests/test_logo_stdout_encoding.py b/.claude/skills/design/scripts/logo/tests/test_logo_stdout_encoding.py new file mode 100644 index 0000000..a4a104f --- /dev/null +++ b/.claude/skills/design/scripts/logo/tests/test_logo_stdout_encoding.py @@ -0,0 +1,46 @@ +"""Batch mode prints check marks; it must not crash when stdout is a cp1252 pipe or file.""" + +import os +import subprocess +import sys +from pathlib import Path + +GENERATE = Path(__file__).resolve().parents[1] / "generate.py" + + +def test_batch_survives_non_utf8_stdout(tmp_path): + # load_env() reads .env files from the skill folder and from ~/.claude and never + # overrides a variable that is already set. Point the home directory at tmp_path + # and set every provider key to an empty string (treated as unset) so no local + # key can be picked up and no request is ever made: each variant fails offline + # and the loop reaches its "failed" line. + env = dict(os.environ) + env.update({ + "HOME": str(tmp_path), + "USERPROFILE": str(tmp_path), + "APPDATA": str(tmp_path), + "GEMINI_API_KEY": "", + "GOOGLE_API_KEY": "", + "ATLASCLOUD_API_KEY": "", + "MUAPI_API_KEY": "", + "UBERLLM_API_KEY": "", + # PYTHONIOENCODING=cp1252 reproduces a Windows console/pipe on any platform. + "PYTHONUTF8": "0", + "PYTHONIOENCODING": "cp1252", + }) + result = subprocess.run( + [sys.executable, str(GENERATE), "--brand", "Cafe", "--batch", "1", + "--output-dir", str(tmp_path / "logos")], + capture_output=True, + stdin=subprocess.DEVNULL, + env=env, + cwd=tmp_path, + encoding="utf-8", + errors="replace", + ) + + assert "UnicodeEncodeError" not in result.stderr, result.stderr + assert "Failed: minimalist" in result.stdout + assert "BATCH COMPLETE: 0/1 logos generated" in result.stdout + assert "✗" in result.stdout + assert result.returncode == 1 diff --git a/cli/assets/skills/design/scripts/logo/generate.py b/cli/assets/skills/design/scripts/logo/generate.py index f188dc0..19ffc55 100644 --- a/cli/assets/skills/design/scripts/logo/generate.py +++ b/cli/assets/skills/design/scripts/logo/generate.py @@ -56,6 +56,15 @@ from urllib.parse import urlparse from urllib.request import HTTPRedirectHandler, Request, build_opener +# Batch mode prints ✓/✗, which raises UnicodeEncodeError when stdout/stderr is +# a cp1252 pipe or file (Windows without PYTHONUTF8). Same guard as the one in +# cip/generate.py. +for _stream in (sys.stdout, sys.stderr): + _encoding = getattr(_stream, "encoding", None) + if _encoding and _encoding.lower().replace("-", "") != "utf8" and hasattr(_stream, "reconfigure"): + _stream.reconfigure(encoding="utf-8", errors=_stream.errors) + + # Load environment variables def load_env(): """Load .env files in priority order""" diff --git a/cli/assets/skills/design/scripts/logo/tests/test_logo_stdout_encoding.py b/cli/assets/skills/design/scripts/logo/tests/test_logo_stdout_encoding.py new file mode 100644 index 0000000..a4a104f --- /dev/null +++ b/cli/assets/skills/design/scripts/logo/tests/test_logo_stdout_encoding.py @@ -0,0 +1,46 @@ +"""Batch mode prints check marks; it must not crash when stdout is a cp1252 pipe or file.""" + +import os +import subprocess +import sys +from pathlib import Path + +GENERATE = Path(__file__).resolve().parents[1] / "generate.py" + + +def test_batch_survives_non_utf8_stdout(tmp_path): + # load_env() reads .env files from the skill folder and from ~/.claude and never + # overrides a variable that is already set. Point the home directory at tmp_path + # and set every provider key to an empty string (treated as unset) so no local + # key can be picked up and no request is ever made: each variant fails offline + # and the loop reaches its "failed" line. + env = dict(os.environ) + env.update({ + "HOME": str(tmp_path), + "USERPROFILE": str(tmp_path), + "APPDATA": str(tmp_path), + "GEMINI_API_KEY": "", + "GOOGLE_API_KEY": "", + "ATLASCLOUD_API_KEY": "", + "MUAPI_API_KEY": "", + "UBERLLM_API_KEY": "", + # PYTHONIOENCODING=cp1252 reproduces a Windows console/pipe on any platform. + "PYTHONUTF8": "0", + "PYTHONIOENCODING": "cp1252", + }) + result = subprocess.run( + [sys.executable, str(GENERATE), "--brand", "Cafe", "--batch", "1", + "--output-dir", str(tmp_path / "logos")], + capture_output=True, + stdin=subprocess.DEVNULL, + env=env, + cwd=tmp_path, + encoding="utf-8", + errors="replace", + ) + + assert "UnicodeEncodeError" not in result.stderr, result.stderr + assert "Failed: minimalist" in result.stdout + assert "BATCH COMPLETE: 0/1 logos generated" in result.stdout + assert "✗" in result.stdout + assert result.returncode == 1