Skip to content

Commit

Permalink
Control error in profiles format (conan-io#585)
Browse files Browse the repository at this point in the history
* Control error in profiles format

* PR review

* Comments profile test

* ENV var caps fixed

* scopes splitlines
  • Loading branch information
lasote authored Oct 20, 2016
1 parent b5d2cd4 commit 2d22f5f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
2 changes: 2 additions & 0 deletions conans/client/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ def _read_profile(self, profile_name):
try:
profile = self._client_cache.load_profile(profile_name)
return profile
except ConanException as exc:
raise ConanException("Error reading '%s' profile: %s" % (profile_name, exc))
except Exception:
current_profiles = ", ".join(self._client_cache.current_profiles()) or "[]"
raise ConanException("Specified profile '%s' doesn't exist.\nExisting profiles: "
Expand Down
21 changes: 14 additions & 7 deletions conans/model/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import OrderedDict
from conans.util.config_parser import ConfigParser
from conans.model.scope import Scopes, _root
from conans.errors import ConanException


class Profile(object):
Expand All @@ -19,18 +20,24 @@ def loads(text):
obj = Profile()
doc = ConfigParser(text, allowed_fields=["settings", "env", "scopes"])

for setting in doc.settings.split("\n"):
if setting:
for setting in doc.settings.splitlines():
setting = setting.strip()
if setting and not setting.startswith("#"):
if "=" not in setting:
raise ConanException("Invalid setting line '%s'" % setting)
name, value = setting.split("=")
obj.settings[name] = value
obj.settings[name.strip()] = value.strip()

if doc.scopes:
obj.scopes = Scopes.from_list(doc.scopes.split("\n"))
obj.scopes = Scopes.from_list(doc.scopes.splitlines())

for env in doc.env.split("\n"):
if env:
for env in doc.env.splitlines():
env = env.strip()
if env and not env.startswith("#"):
if "=" not in env:
raise ConanException("Invalid env line '%s'" % env)
varname, value = env.split("=")
obj.env[varname] = value
obj.env[varname.strip()] = value.strip()

obj._order()
return obj
Expand Down
72 changes: 72 additions & 0 deletions conans/test/integration/profile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,78 @@ class ProfileTest(unittest.TestCase):
def setUp(self):
self.client = TestClient()

def bad_syntax_test(self):
self.client.save({CONANFILE: conanfile_scope_env})
self.client.run("export lasote/stable")

profile = '''
[settings
'''
save(self.client.client_cache.profile_path("clang"), profile)
self.client.run("install Hello0/0.1@lasote/stable --build missing -pr clang", ignore_error=True)
self.assertIn("Error reading 'clang' profile", self.client.user_io.out)
self.assertIn("Bad syntax", self.client.user_io.out)

profile = '''
[settings]
[invented]
'''
save(self.client.client_cache.profile_path("clang"), profile)
self.client.run("install Hello0/0.1@lasote/stable --build missing -pr clang", ignore_error=True)
self.assertIn("Unrecognized field 'invented'", self.client.user_io.out)
self.assertIn("Error reading 'clang' profile", self.client.user_io.out)

profile = '''
[settings]
as
'''
save(self.client.client_cache.profile_path("clang"), profile)
self.client.run("install Hello0/0.1@lasote/stable --build missing -pr clang", ignore_error=True)
self.assertIn("Error reading 'clang' profile: Invalid setting line 'as'", self.client.user_io.out)

profile = '''
[env]
as
'''
save(self.client.client_cache.profile_path("clang"), profile)
self.client.run("install Hello0/0.1@lasote/stable --build missing -pr clang", ignore_error=True)
self.assertIn("Error reading 'clang' profile: Invalid env line 'as'", self.client.user_io.out)

profile = '''
[scopes]
as
'''
save(self.client.client_cache.profile_path("clang"), profile)
self.client.run("install Hello0/0.1@lasote/stable --build missing -pr clang", ignore_error=True)
self.assertIn("Error reading 'clang' profile: Bad scope as", self.client.user_io.out)

profile = '''
[settings]
os = a value
'''
save(self.client.client_cache.profile_path("clang"), profile)
self.client.run("install Hello0/0.1@lasote/stable --build missing -pr clang", ignore_error=True)
# stripped "a value"
self.assertIn("'a value' is not a valid 'settings.os'", self.client.user_io.out)

profile = '''
[env]
ENV_VAR = a value
'''
save(self.client.client_cache.profile_path("clang"), profile)
self.client.run("install Hello0/0.1@lasote/stable --build missing -pr clang", ignore_error=True)
self._assert_env_variable_printed("ENV_VAR", "a value")

profile = '''
# Line with comments is not a problem
[env]
# Not even here
ENV_VAR = a value
'''
save(self.client.client_cache.profile_path("clang"), profile)
self.client.run("install Hello0/0.1@lasote/stable --build -pr clang", ignore_error=True)
self._assert_env_variable_printed("ENV_VAR", "a value")

def build_with_profile_test(self):
self._create_profile("scopes_env", {},
{}, # undefined scope do not apply to my packages
Expand Down

0 comments on commit 2d22f5f

Please sign in to comment.