c89f64cc7c
Since Buck has no native support for web tests, wrap the tests in a Python shim that calls wct. As in other Polymer cases, we need to prepare a set of inputs and create a directory containing exactly what wct expects to be there. Buck exposes resources to python_tests using the pkg_resources API, which is cumbersome to use, and easier just to ship around zip files as we do elsewhere. Unlike other npm binaries we've encountered, the web-component-tester module has numerous native dependencies, up to and including Selenium. Rather than get in the game of distributing platform-specific binaries, punt and require `wct` to be on the user's $PATH for now. Tests are currently excluded in .buckconfig but can be run directly with either: buck test //polygerrit-ui/app:polygerrit_tests buck test --include web Change-Id: Ia314213925ac27ff271374a96ed539fb2acf0187
106 lines
3.0 KiB
Python
106 lines
3.0 KiB
Python
# Copyright (C) 2015 The Android Open Source Project
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from __future__ import print_function
|
|
|
|
import atexit
|
|
from distutils import spawn
|
|
import json
|
|
import os
|
|
import pkg_resources
|
|
import shlex
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import zipfile
|
|
|
|
|
|
def _write_wct_conf(root, exports):
|
|
with open(os.path.join(root, 'wct.conf.js'), 'w') as f:
|
|
f.write('module.exports = %s;\n' % json.dumps(exports))
|
|
|
|
|
|
def _wct_cmd():
|
|
return ['wct'] + shlex.split(os.environ.get('WCT_ARGS', ''))
|
|
|
|
|
|
class PolyGerritWctTests(unittest.TestCase):
|
|
|
|
# Should really be setUpClass/tearDownClass, but Buck's test runner doesn't
|
|
# produce sane stack traces from those methods. There's only one test method
|
|
# anyway, so just use setUp.
|
|
|
|
def _check_wct(self):
|
|
self.assertTrue(
|
|
spawn.find_executable('wct'),
|
|
msg='wct not found; try `npm install -g web-component-tester`')
|
|
|
|
def _extract_resources(self):
|
|
tmpdir = tempfile.mkdtemp()
|
|
atexit.register(lambda: shutil.rmtree(tmpdir))
|
|
root = os.path.join(tmpdir, 'polygerrit')
|
|
os.mkdir(root)
|
|
|
|
tr = 'test_resources.zip'
|
|
zip_path = os.path.join(tmpdir, tr)
|
|
s = pkg_resources.resource_stream(__name__, tr)
|
|
with open(zip_path, 'w') as f:
|
|
shutil.copyfileobj(s, f)
|
|
|
|
with zipfile.ZipFile(zip_path, 'r') as z:
|
|
z.extractall(root)
|
|
|
|
return tmpdir, root
|
|
|
|
def test_wct(self):
|
|
self._check_wct()
|
|
tmpdir, root = self._extract_resources()
|
|
|
|
cmd = _wct_cmd()
|
|
print('Running %s in %s' % (cmd, root), file=sys.stderr)
|
|
|
|
_write_wct_conf(root, {
|
|
'suites': ['test'],
|
|
'webserver': {
|
|
'pathMappings': [
|
|
{'/components/bower_components': 'bower_components'},
|
|
],
|
|
},
|
|
'plugins': {
|
|
'local': {
|
|
# For some reason wct tries to install selenium into its node_modules
|
|
# directory on first run. If you've installed into /usr/local and
|
|
# aren't running wct as root, you're screwed. Turning this option off
|
|
# seems to still work, so there's that.
|
|
'skipSeleniumInstall': True,
|
|
},
|
|
},
|
|
})
|
|
|
|
p = subprocess.Popen(cmd, cwd=root,
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
out, err = p.communicate()
|
|
sys.stdout.write(out)
|
|
sys.stderr.write(err)
|
|
self.assertEquals(0, p.returncode)
|
|
|
|
# Only remove tmpdir if successful, to allow debugging.
|
|
shutil.rmtree(tmpdir)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|