Skip to content

Commit 99b32e2

Browse files
authored
Merge pull request #21 from platformsh/local-vars
Refactor dynamic property access to be more permissive, too.
2 parents 963ec98 + efe2d3b commit 99b32e2

2 files changed

Lines changed: 47 additions & 43 deletions

File tree

platformshconfig/config.py

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -492,58 +492,32 @@ def __getattr__(self, config_property):
492492
493493
"""
494494

495-
if not self.is_valid_platform():
496-
raise NotValidPlatformException(
497-
'You are not running on Platform.sh, so the {0} variable is not available.'.format(config_property)
498-
)
499495
is_build_var = config_property in self._directVariables.keys()
500496
is_runtime_var = config_property in self._directVariablesRuntime.keys()
501497

502498
# For now, all unprefixed variables are also runtime variables. If that ever changes this logic will change
503499
# with it.
504500
is_unprefixed_var = config_property in self._unPrefixedVariablesRuntime.keys()
505501

506-
if self.in_build() and is_runtime_var:
507-
raise BuildTimeVariableAccessException(
508-
'The {0} variable is not available during build time.'.format(config_property)
509-
)
510502
if is_build_var:
511-
return self[self._directVariables[config_property]]
512-
if is_runtime_var:
513-
return self[self._directVariablesRuntime[config_property]]
514-
if is_unprefixed_var:
515-
return self._environmentVariables.get(self._unPrefixedVariablesRuntime[config_property])
516-
raise AttributeError('No such variable defined: '.format(config_property))
517-
518-
def isset(self, config_property):
519-
"""Checks whether a configuration property is set.
520-
521-
Args:
522-
config_property:
523-
A (magic) property name.
524-
525-
Returns:
526-
bool:
527-
True if the property exists and is not None, False otherwise.
528-
529-
"""
530-
531-
if not self.is_valid_platform():
532-
return False
533-
534-
is_build_var = config_property in self._directVariables.keys()
535-
is_runtime_var = config_property in self._directVariablesRuntime.keys()
536-
537-
# For now, all unprefixed variables are also runtime variables. If that ever changes this logic will change
538-
# with it.
539-
is_unprefixed_var = config_property in self._unPrefixedVariablesRuntime.keys()
540-
541-
if self.in_build():
542-
return is_build_var and config_property is not None
543-
if is_build_var or is_runtime_var or is_unprefixed_var:
544-
return config_property is not None
545-
return False
503+
value = self[self._directVariables[config_property]]
504+
elif is_runtime_var:
505+
value = self[self._directVariablesRuntime[config_property]]
506+
elif is_unprefixed_var:
507+
value = self._environmentVariables.get(self._unPrefixedVariablesRuntime[config_property])
508+
else:
509+
raise AttributeError('No such variable defined: {}'.format(config_property))
510+
511+
if not value:
512+
if self.in_build() and (is_runtime_var or is_unprefixed_var):
513+
raise BuildTimeVariableAccessException(
514+
'The {} variable is not available during build time.'.format(config_property)
515+
)
516+
raise NotValidPlatformException(
517+
'The {} variable is not defined. Are you sure you\'re running on Platform.sh?'.format(config_property)
518+
)
546519

520+
return value
547521

548522
def pymongo_formatter(credentials):
549523
"""Returns a DSN for a pymongo-MongoDB connection.

tests/test_config.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,36 @@ def test_build_and_deploy_properties_in_deploy_exists(self):
308308
self.assertTrue(hasattr(config, 'port'))
309309
self.assertTrue(hasattr(config, 'socket'))
310310

311+
def test_build_and_deploy_properties_mocked_in_local_exist(self):
312+
env = self.mockEnvironmentDeploy
313+
env.pop('PLATFORM_APPLICATION', None)
314+
env.pop('PLATFORM_ENVIRONMENT', None)
315+
env.pop('PLATFORM_BRANCH', None)
316+
317+
config = Config(env)
318+
319+
self.assertEqual('/app', config.appDir)
320+
self.assertEqual('app', config.applicationName)
321+
self.assertEqual('test-project', config.project)
322+
self.assertEqual('abc123', config.treeID)
323+
self.assertEqual('def789', config.projectEntropy)
324+
325+
self.assertEqual('/app/web', config.documentRoot)
326+
self.assertEqual('1.2.3.4', config.smtpHost)
327+
self.assertEqual('8080', config.port)
328+
self.assertEqual('unix://tmp/blah.sock', config.socket)
329+
330+
self.assertTrue(hasattr(config, 'appDir'))
331+
self.assertTrue(hasattr(config, 'applicationName'))
332+
self.assertTrue(hasattr(config, 'project'))
333+
self.assertTrue(hasattr(config, 'treeID'))
334+
self.assertTrue(hasattr(config, 'projectEntropy'))
335+
336+
self.assertTrue(hasattr(config, 'documentRoot'))
337+
self.assertTrue(hasattr(config, 'smtpHost'))
338+
self.assertTrue(hasattr(config, 'port'))
339+
self.assertTrue(hasattr(config, 'socket'))
340+
311341
def test_deploy_property_in_build_throws(self):
312342

313343
env = self.mockEnvironmentBuild

0 commit comments

Comments
 (0)