Skip to content

Commit e31a514

Browse files
committed
adding more tests and fixing a bug with pip not using download cache without docker
1 parent 1af4e41 commit e31a514

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

lib/pip.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ function installRequirements(targetFolder, servicePath, serverless, options) {
6262
// Push our local OS-specific paths for requirements and target directory
6363
pipCmd.push('-t', targetFolder);
6464
pipCmd.push('-r', targetRequirementsTxt);
65+
66+
// If we want a download cache...
67+
if (options.useDownloadCache) {
68+
const downloadCacheDir = path.join(getUserCachePath(options), 'downloadCache_slspyc');
69+
serverless.cli.log(`Using download cache directory ${downloadCacheDir}`);
70+
fse.ensureDirSync(downloadCacheDir);
71+
pipCmd.push('--cache-dir', downloadCacheDir);
72+
}
6573

6674
// Check if pip has Debian's --system option and set it if so
6775
const pipTestRes = spawnSync(
@@ -139,15 +147,6 @@ function installRequirements(targetFolder, servicePath, serverless, options) {
139147
} else {
140148
cmd = pipCmd[0];
141149
cmdOptions = pipCmd.slice(1);
142-
143-
// If we want a download cache...
144-
if (options.useDownloadCache) {
145-
const downloadCacheDir = path.join(getUserCachePath(options), 'downloadCache_slspyc');
146-
serverless.cli.log(`Using download cache directory ${downloadCacheDir}`);
147-
fse.ensureDirSync(downloadCacheDir);
148-
pipCmd.push('--cache-dir', downloadCacheDir);
149-
}
150-
151150
}
152151
serverless.cli.log(`Trying to run command...`);
153152
console.log(cmd)

test.bats

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ setup() {
1212
teardown() {
1313
rm -rf puck puck2 puck3 node_modules .serverless .requirements.zip .requirements-cache
1414
if [ -f serverless.yml.bak ]; then mv serverless.yml.bak serverless.yml; fi
15+
# Remove our items in appdirs user cache dir
16+
node -e 'require("../../lib/clean").cleanupStaticCache()'
1517
}
1618

1719
@test "py3.6 can package flask with default options" {
@@ -67,13 +69,42 @@ teardown() {
6769
ls puck/flask
6870
}
6971

70-
@test "py3.6 uses cache with dockerizePip option" {
72+
@test "py3.6 uses download cache with useDownloadCache option" {
7173
cd tests/base
7274
npm i $(npm pack ../..)
7375
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
74-
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n pipCmdExtraArgs: ["--cache-dir", ".requirements-cache"]/' serverless.yml
76+
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useDownloadCache: true/' serverless.yml
77+
sls package
78+
USR_CACHE_DIR=`node -e 'console.log(require("../../lib/shared").getUserCachePath())'`
79+
ls $USR_CACHE_DIR/downloadCache_slspyc/http
80+
}
81+
82+
@test "py3.6 uses download cache with useDownloadCache + cacheLocation option" {
83+
cd tests/base
84+
npm i $(npm pack ../..)
85+
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
86+
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useDownloadCache: true\n cacheLocation: .requirements-cache/' serverless.yml
87+
sls package
88+
ls .requirements-cache/downloadCache_slspyc/http
89+
}
90+
91+
@test "py3.6 uses download cache with dockerizePip + useDownloadCache option" {
92+
cd tests/base
93+
npm i $(npm pack ../..)
94+
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
95+
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useDownloadCache: true/' serverless.yml
96+
sls --dockerizePip=true package
97+
USR_CACHE_DIR=`node -e 'console.log(require("../../lib/shared").getUserCachePath())'`
98+
ls $USR_CACHE_DIR/downloadCache_slspyc/http
99+
}
100+
101+
@test "py3.6 uses download cache with dockerizePip + useDownloadCache + cacheLocation option" {
102+
cd tests/base
103+
npm i $(npm pack ../..)
104+
! uname -sm|grep Linux || groups|grep docker || id -u|egrep '^0$' || skip "can't dockerize on linux if not root & not in docker group"
105+
perl -p -i'.bak' -e 's/(pythonRequirements:$)/\1\n useDownloadCache: true\n cacheLocation: .requirements-cache/' serverless.yml
75106
sls --dockerizePip=true package
76-
ls .requirements-cache/http
107+
ls .requirements-cache/downloadCache_slspyc/http
77108
}
78109

79110
@test "py2.7 can package flask with default options" {

0 commit comments

Comments
 (0)