ソースを参照

Fix mock Python download

Thomas Kluyver 6 年 前
コミット
4f43b100fa
2 ファイル変更13 行追加5 行削除
  1. 6 4
      nsist/tests/test_main.py
  2. 7 1
      nsist/util.py

+ 6 - 4
nsist/tests/test_main.py

@@ -3,20 +3,21 @@ from pathlib import Path
 import re
 import responses
 from shutil import copy
-from testpath import MockCommand, assert_isfile, assert_isdir
+from testpath import MockCommand, modified_env, assert_isfile, assert_isdir
 from testpath.tempdir import TemporaryWorkingDirectory
 from zipfile import ZipFile
 
 from nsist import main
+from nsist.util import CACHE_ENV_VAR
 from .utils import test_dir
 
 example_dir = Path(test_dir, 'console_example')
 
-def respond_python_zip():
+def respond_python_zip(req):
     buf = io.BytesIO()
     with ZipFile(buf, 'w') as zf:
         zf.writestr('python.exe', b'')
-    return 200, {}, buf
+    return 200, {}, buf.getvalue()
 
 @responses.activate
 def test_console_example():
@@ -29,7 +30,8 @@ def test_console_example():
             copy(str(src), td)
 
 
-        with MockCommand('makensis') as makensis:
+        with modified_env({CACHE_ENV_VAR: td}), \
+             MockCommand('makensis') as makensis:
             ec = main(['installer.cfg'])
 
         assert ec == 0

+ 7 - 1
nsist/util.py

@@ -25,8 +25,14 @@ def download(url, target):
             if chunk:
                 f.write(chunk)
 
+CACHE_ENV_VAR = 'PYNSIST_CACHE_DIR'
+
 def get_cache_dir(ensure_existence=False):
-    if os.name == 'posix' and sys.platform != 'darwin':
+    specified = os.environ.get(CACHE_ENV_VAR, None)
+
+    if specified:
+        p = Path(specified)
+    elif os.name == 'posix' and sys.platform != 'darwin':
         # Linux, Unix, AIX, etc.
         # use ~/.cache if empty OR not set
         xdg = os.environ.get("XDG_CACHE_HOME", None) or (os.path.expanduser('~/.cache'))