|
| 1 | +const crossSpawn = require('cross-spawn'); |
| 2 | +const deasync = require('deasync-promise'); |
| 3 | +const glob = require('glob-all'); |
| 4 | +const JSZip = require('jszip'); |
| 5 | +const tape = require('tape'); |
| 6 | +const { removeSync, readFileSync } = require('fs-extra'); |
| 7 | +const { sep } = require('path'); |
| 8 | + |
| 9 | +const { getUserCachePath } = require('./lib/shared'); |
| 10 | + |
| 11 | +const initialWorkingDir = process.cwd(); |
| 12 | + |
| 13 | +const mkCommand = cmd => (args, options = {}) => { |
| 14 | + const { error, stdout, stderr, status } = crossSpawn.sync( |
| 15 | + cmd, |
| 16 | + args, |
| 17 | + Object.assign( |
| 18 | + { |
| 19 | + env: Object.assign( |
| 20 | + process.env, |
| 21 | + { SLS_DEBUG: 't' }, |
| 22 | + process.env.CI ? { LC_ALL: 'C.UTF-8', LANG: 'C.UTF-8' } : {} |
| 23 | + ) |
| 24 | + }, |
| 25 | + options |
| 26 | + ) |
| 27 | + ); |
| 28 | + if (error) throw error; |
| 29 | + if (status) { |
| 30 | + console.error(stdout.toString()); // eslint-disable-line no-console |
| 31 | + console.error(stderr.toString()); // eslint-disable-line no-console |
| 32 | + throw new Error(`${cmd} failed with status code ${status}`); |
| 33 | + } |
| 34 | + return stdout && stdout.toString().trim(); |
| 35 | +}; |
| 36 | +const sls = mkCommand('sls'); |
| 37 | +const git = mkCommand('git'); |
| 38 | +const npm = mkCommand('npm'); |
| 39 | + |
| 40 | +const setup = () => { |
| 41 | + removeSync(getUserCachePath()); |
| 42 | +}; |
| 43 | + |
| 44 | +const teardown = () => { |
| 45 | + [ |
| 46 | + 'puck', |
| 47 | + 'puck2', |
| 48 | + 'puck3', |
| 49 | + 'node_modules', |
| 50 | + '.serverless', |
| 51 | + '.requirements.zip', |
| 52 | + '.requirements-cache', |
| 53 | + 'foobar', |
| 54 | + 'package-lock.json', |
| 55 | + 'slimPatterns.yml', |
| 56 | + 'serverless.yml.bak', |
| 57 | + getUserCachePath(), |
| 58 | + ...glob.sync('serverless-python-requirements-*.tgz') |
| 59 | + ].map(path => removeSync(path)); |
| 60 | + git(['checkout', 'serverless.yml']); |
| 61 | + process.chdir(initialWorkingDir); |
| 62 | + removeSync('tests/base with a space'); |
| 63 | +}; |
| 64 | + |
| 65 | +const test = (desc, func) => |
| 66 | + tape.test(desc, t => { |
| 67 | + setup(); |
| 68 | + try { |
| 69 | + func(t); |
| 70 | + } finally { |
| 71 | + teardown(); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | +const getPythonBin = (version = 3) => { |
| 76 | + if (![2, 3].includes(version)) throw new Error('version must be 2 or 3'); |
| 77 | + if (process.platform === 'win32') |
| 78 | + return `c:/python${version === 2 ? '27' : '36'}-x64/python.exe`; |
| 79 | + else return version === 2 ? 'python2.7' : 'python3.6'; |
| 80 | +}; |
| 81 | + |
| 82 | +const listZipFiles = filename => |
| 83 | + Object.keys(deasync(new JSZip().loadAsync(readFileSync(filename))).files); |
| 84 | + |
| 85 | +test('default pythonBin can package flask with default options', t => { |
| 86 | + process.chdir('tests/base'); |
| 87 | + const path = npm(['pack', '../..']); |
| 88 | + npm(['i', path]); |
| 89 | + sls(['package']); |
| 90 | + const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip'); |
| 91 | + t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged'); |
| 92 | + t.end(); |
| 93 | +}); |
| 94 | + |
| 95 | +test('py3.6 can package flask with default options', t => { |
| 96 | + process.chdir('tests/base'); |
| 97 | + const path = npm(['pack', '../..']); |
| 98 | + npm(['i', path]); |
| 99 | + sls([`--pythonBin=${getPythonBin(3)}`, 'package']); |
| 100 | + const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip'); |
| 101 | + t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged'); |
| 102 | + t.end(); |
| 103 | +}); |
| 104 | + |
| 105 | +test('py3.6 can package flask with zip option', t => { |
| 106 | + process.chdir('tests/base'); |
| 107 | + const path = npm(['pack', '../..']); |
| 108 | + npm(['i', path]); |
| 109 | + sls([`--pythonBin=${getPythonBin(3)}`, '--zip=true', 'package']); |
| 110 | + const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip'); |
| 111 | + t.true( |
| 112 | + zipfiles.includes('.requirements.zip'), |
| 113 | + 'zipped requirements are packaged' |
| 114 | + ); |
| 115 | + t.true(zipfiles.includes(`unzip_requirements.py`), 'unzip util is packaged'); |
| 116 | + t.false( |
| 117 | + zipfiles.includes(`flask${sep}__init__.py`), |
| 118 | + "flask isn't packaged on its own" |
| 119 | + ); |
| 120 | + t.end(); |
| 121 | +}); |
| 122 | + |
| 123 | +test('py3.6 can package flask with slim option', t => { |
| 124 | + process.chdir('tests/base'); |
| 125 | + const path = npm(['pack', '../..']); |
| 126 | + npm(['i', path]); |
| 127 | + sls([`--pythonBin=${getPythonBin(3)}`, '--slim=true', 'package']); |
| 128 | + const zipfiles = listZipFiles('.serverless/sls-py-req-test.zip'); |
| 129 | + t.true(zipfiles.includes(`flask${sep}__init__.py`), 'flask is packaged'); |
| 130 | + t.deepEqual( |
| 131 | + zipfiles.filter(filename => filename.endsWith('.pyc')), |
| 132 | + [], |
| 133 | + 'no pyc files packaged' |
| 134 | + ); |
| 135 | + t.end(); |
| 136 | +}); |
0 commit comments