Commit cf95df81 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

style: import UserError and use Recipe().install()

- import UserError from zc.buildout for shorter usage
- use Recipe(...).install() one-liner when recipe varible is uncessary
parent cb8165cb
......@@ -8,10 +8,10 @@ import os
import re
import stat
import subprocess
import zc.buildout
import six.moves.urllib as urllib
from zc.buildout import UserError
from zc.buildout.easy_install import allow_picked_versions
from slapos.recipe import rmtree
from slapos.recipe.downloadunpacked import Recipe as Download
......@@ -42,13 +42,12 @@ class Recipe(object):
if self.gems:
self.gems = self.gems.split()
else:
raise zc.buildout.UserError(
"Configuration error, 'gems' option is missing")
raise UserError("Configuration error, 'gems' option is missing")
self.url = options.get('url')
if(self.url and options.get('version')):
raise zc.buildout.UserError(
"Configuration error, 'url' and 'version' options are mutually exclusive")
raise UserError("Configuration error, "
"'url' and 'version' options are mutually exclusive")
# Allow to define specific ruby executable. If not, take just 'ruby'
self.ruby_executable = options.get('ruby-executable', 'ruby')
......@@ -67,17 +66,14 @@ class Recipe(object):
try:
return subprocess.check_output(cmd, env=env, universal_newlines=True)
except OSError as e:
raise zc.buildout.UserError(
'System error, command failed: %s: %s' % (e, cmd))
raise UserError('System error, command failed: %s: %s' % (e, cmd))
except subprocess.CalledProcessError as e:
self.log.error(e.output)
if e.returncode < 0:
raise zc.buildout.UserError(
'System error, command received signal %s: %s'
% (-e.returncode, e.cmd)
)
raise UserError('System error, command received signal %s: %s'
% (-e.returncode, e.cmd))
elif e.returncode > 0:
raise zc.buildout.UserError(
raise UserError(
'System error, command failed with exit code %s: %s'
% (e.returncode, e.cmd)
)
......@@ -124,11 +120,11 @@ class Recipe(object):
% (' '.join(cons_dict.values()), dep_dict['gemname'], gemname)
)
raise zc.buildout.UserError(
raise UserError(
'Configuration error, version %s for gem %s '
'does not satisfy dependency constraint %s of gem %s'
% (dep_dict['version'], dep_dict['gemname'],
' '.join(cons_dict.values()), gemname)
' '.join(cons_dict.values()), gemname)
)
def _join_paths(self, *paths):
......@@ -141,10 +137,8 @@ class Recipe(object):
map(strip, line.split('=', 1)) for line in env
]])
except ValueError: # Unpacking impossible
raise zc.buildout.UserError(
'Configuration error, '
'every environment line should contain a "=" sign'
)
raise UserError('Configuration error, '
'every environment line should contain a "=" sign')
return env
def _get_env(self):
......@@ -195,7 +189,7 @@ class Recipe(object):
f.close()
if r:
return url or r.group(0), r.group(1)
raise zc.buildout.UserError("Can't find rubygems version.")
raise UserError("Can't find rubygems version.")
def _install_rubygems(self, url, version):
srcdir = os.path.join(self.buildout['buildout']['parts-directory'],
......@@ -204,8 +198,7 @@ class Recipe(object):
'url': url,
'destination': srcdir,
}
recipe = Download(self.buildout, self.name, options)
recipe.install()
Download(self.buildout, self.name, options).install()
current_dir = os.getcwd()
os.chdir(srcdir)
......@@ -313,7 +306,7 @@ class Recipe(object):
url, version = self._get_rubygems_url_and_version()
if int(version.split(".")[0]) < 2:
raise zc.buildout.UserError("Rubygems version must be >= 2.0.0")
raise UserError("Rubygems version must be >= 2.0.0")
self._install_rubygems(url, version)
gem_executable = glob.glob(os.path.join(bindir, 'gem*'))[0]
......@@ -322,7 +315,7 @@ class Recipe(object):
for gem_dict in gem_dict_list:
if self.deployment:
if 'version' not in gem_dict:
raise zc.buildout.UserError(
raise UserError(
'Configuration error, '
'version for gem %s is missing' % gem_dict['gemname']
)
......@@ -333,7 +326,7 @@ class Recipe(object):
match = [gem_d for gem_d in gem_dict_list
if dep_dict['gemname'] == gem_d['gemname']]
if not match:
raise zc.buildout.UserError(
raise UserError(
'Configuration error, '
'version for dependency %s is missing'
% dep_dict['gemname']
......
......@@ -13,7 +13,7 @@ import re
from six import BytesIO
import zc.buildout
from zc.buildout import UserError
import rubygems
......@@ -135,8 +135,7 @@ class RubyGemsTestCase(unittest.TestCase):
class RubyGemsDefaultTestCase(RubyGemsTestCase):
@fixture({'recipe': {'gems': 'sass'}})
def test_success(self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
recipe.install()
rubygems.Recipe(buildout, name, options).install()
expected_install_arg_list_list = [
[
......@@ -151,7 +150,7 @@ class RubyGemsDefaultTestCase(RubyGemsTestCase):
@fixture({'recipe': {}})
def test_missing_gems(self, path, patches, buildout, name, options, version):
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
"Configuration error, 'gems' option is missing",
rubygems.Recipe, buildout, name, options
)
......@@ -159,48 +158,43 @@ class RubyGemsDefaultTestCase(RubyGemsTestCase):
@fixture({'recipe': {'gems': 'sass'}})
def test_oserror(self, path, patches, buildout, name, options, version):
patches['check_output'].side_effect = OSError
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'System error, command failed: .*',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@fixture({'recipe': {'gems': 'sass'}})
def test_signal_received(self, path, patches, buildout, name, options, version):
exception = subprocess.CalledProcessError(-1, '')
patches['check_output'].side_effect = exception
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'System error, command received signal 1: .*',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@fixture({'recipe': {'gems': 'sass'}})
def test_non_zero_exitcode(self, path, patches, buildout, name, options, version):
exception = subprocess.CalledProcessError(1, '')
patches['check_output'].side_effect = exception
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'System error, command failed with exit code 1: .*',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@fixture({'recipe': {'gems': 'sass'}})
def test_update(self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
recipe.update()
rubygems.Recipe(buildout, name, options).update()
@fixture({'recipe': {'gems': 'sass', 'environment': 'invalid'}})
def test_invalid_environment(self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
'Configuration error, '
'every environment line should contain a "=" sign',
recipe.install
UserError,
'Configuration error, '
'every environment line should contain a "=" sign',
rubygems.Recipe(buildout, name, options).install,
)
@fixture({'recipe': {
......@@ -209,21 +203,19 @@ class RubyGemsDefaultTestCase(RubyGemsTestCase):
% RUBYGEMS_DEFAULT_VERSION,
}})
def test_version_from_url(self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
recipe.install()
rubygems.Recipe(buildout, name, options).install()
@fixture({'recipe': {'gems': 'sass'}, 'version': RUBYGEMS_DEFAULT_VERSION})
def test_version(self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
recipe.install()
rubygems.Recipe(buildout, name, options).install()
@fixture({'recipe': {'gems': 'sass'}})
def test_no_version(self, path, patches, buildout, name, options, version):
patches['urlopen'].return_value = BytesIO(b'')
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(zc.buildout.UserError,
self.assertRaisesRegexp(
UserError,
"Can't find rubygems version.",
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@fixture({'recipe': {'gems': 'sass'}})
......@@ -234,9 +226,8 @@ class RubyGemsDefaultTestCase(RubyGemsTestCase):
@fixture({'recipe': {'gems': 'sass==1.0'}})
def test_pinned_versions(self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
touch(path / 'rubygems/bin/gem')
recipe.install()
rubygems.Recipe(buildout, name, options).install()
args = patches['check_output'].mock_calls[1][1]
for arg in args[0]:
......@@ -256,8 +247,7 @@ class RubyGemsDefaultTestCase(RubyGemsTestCase):
rubygems_setup_output = fd.read()
patches['check_output'].return_value = rubygems_setup_output
recipe = rubygems.Recipe(buildout, name, options)
recipe.install()
rubygems.Recipe(buildout, name, options).install()
@fixture({'recipe': {
'gems': 'sass',
......@@ -267,7 +257,7 @@ class RubyGemsDefaultTestCase(RubyGemsTestCase):
}})
def test_version_and_url(self, path, patches, buildout, name, options, version):
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
"Configuration error, 'url' and 'version' options are mutually exclusive",
rubygems.Recipe, buildout, name, options
)
......@@ -334,32 +324,29 @@ class RubyGemsDeploymentTestCase(RubyGemsTestCase):
@deployment_fixture({'recipe': {'gems': 'sass'}})
def test_deployment_not_pinned_version_error(
self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'Configuration error, version for gem sass is missing',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@deployment_fixture({'recipe': {'gems': 'rash==0.1.0'}})
def test_deployment_not_pinned_dependency_error(
self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'Configuration error, version for dependency hashie is missing',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@deployment_fixture({'recipe': {'gems': 'rash==0.1.0 hashie==0.3.0'}})
def test_deployment_more_or_equal_constraint_error(
self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'Configuration error, version 0.3.0 for gem hashie '
'does not satisfy dependency constraint >= 0.3.1 of gem rash',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@deployment_fixture({'recipe': {'gems': 'rash==0.1.0 hashie==0.3.1'}})
......@@ -371,12 +358,11 @@ class RubyGemsDeploymentTestCase(RubyGemsTestCase):
@deployment_fixture({'recipe': {'gems': 'rash==0.3.0 hashie==1.1.0'}})
def test_deployment_similar_constraint_error(
self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'Configuration error, version 1.1.0 for gem hashie '
'does not satisfy dependency constraint < 1.1 of gem rash',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@deployment_fixture({'recipe': {'gems': 'rash==0.3.0 hashie==1.0.0'}})
......@@ -393,13 +379,12 @@ class RubyGemsDeploymentTestCase(RubyGemsTestCase):
}})
def test_deployment_more_or_equal_constraint_error(
self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'Configuration error, version 9.0.2 for gem cucumber-messages '
'does not satisfy dependency constraint >= 9.0.3 '
'of gem cucumber-gherkin',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@deployment_fixture({'recipe': {
......@@ -416,12 +401,11 @@ class RubyGemsDeploymentTestCase(RubyGemsTestCase):
@deployment_fixture({'recipe': {'gems': 'hoe==3.4.0 rake==11.0.0'}})
def test_deployment_less_constraint_error(
self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'Configuration error, version 11.0.0 for gem rake '
'does not satisfy dependency constraint < 11.0 of gem hoe',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@deployment_fixture({'recipe': {'gems': 'hoe==3.4.0 rake==10.0.1'}})
......@@ -436,12 +420,11 @@ class RubyGemsDeploymentTestCase(RubyGemsTestCase):
}})
def test_deployment_not_pinned_subdependency_error(
self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'Configuration error, version for dependency '
'cucumber-messages is missing',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@deployment_fixture({'recipe': {
......@@ -463,13 +446,12 @@ class RubyGemsDeploymentTestCase(RubyGemsTestCase):
}})
def test_deployment_subdependency_constraint_error(
self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'Configuration error, version 12.2.0 for gem cucumber-messages '
'does not satisfy dependency constraint >= 12.3 '
'of gem cucumber-gherkin',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@deployment_fixture({'recipe': {
......@@ -493,12 +475,11 @@ class RubyGemsDeploymentTestCase(RubyGemsTestCase):
}})
def test_deployment_similar_second_constraint_error(
self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'Configuration error, version 15.0.0 for gem cucumber-gherkin '
'does not satisfy dependency constraint < 15 of gem cucumber-core',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@deployment_fixture({'recipe': {
......@@ -517,12 +498,11 @@ class RubyGemsDeploymentTestCase(RubyGemsTestCase):
}})
def test_deployment_version_exclusion_constraint_error(
self, path, patches, buildout, name, options, version):
recipe = rubygems.Recipe(buildout, name, options)
self.assertRaisesRegexp(
zc.buildout.UserError,
UserError,
'Configuration error, version 1.0.0 for gem hashie '
'does not satisfy dependency constraint != 1.0.0 of gem sass',
recipe.install
rubygems.Recipe(buildout, name, options).install,
)
@deployment_fixture({'recipe': {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment