Added validation for project working environment names

This commit is contained in:
Ivan Kravets committed 2023-03-20 15:00:22 -06:00
1 parent 331ff2dc9c
commit 269d5e0a3e
5 files changed
+45 -9

No files matched your search

+17 -5
View File
@@ -39,6 +39,7 @@ CONFIG_HEADER = """
class ProjectConfigBase:
ENVNAME_RE = re.compile(r"^[a-z\d\_\-]+$", flags=re.I)
INLINE_COMMENT_RE = re.compile(r"\s+;.*$")
VARTPL_RE = re.compile(r"\$\{([^\.\}\()]+)\.([^\}]+)\}")
@@ -388,16 +389,27 @@ class ProjectConfigBase:
def validate(self, envs=None, silent=False):
if not os.path.isfile(self.path):
raise exception.NotPlatformIOProjectError(os.path.dirname(self.path))
known_envs = set(self.envs())
# check envs
known = set(self.envs())
if not known:
if not known_envs:
raise exception.ProjectEnvsNotAvailableError()
unknown = set(list(envs or []) + self.default_envs()) - known
if unknown:
raise exception.UnknownEnvNamesError(", ".join(unknown), ", ".join(known))
unknown_envs = set(list(envs or []) + self.default_envs()) - known_envs
if unknown_envs:
raise exception.UnknownEnvNamesError(
", ".join(unknown_envs), ", ".join(known_envs)
)
# check envs names
for env in known_envs:
if not self.ENVNAME_RE.match(env):
raise exception.InvalidEnvNameError(env)
if not silent:
for warning in self.warnings:
click.secho("Warning! %s" % warning, fg="yellow")
return True
+7
View File
@@ -43,5 +43,12 @@ class UnknownEnvNamesError(ProjectError, UserSideException):
MESSAGE = "Unknown environment names '{0}'. Valid names are '{1}'"
class InvalidEnvNameError(ProjectError, UserSideException):
MESSAGE = (
"Invalid environment name '{0}'. The name can contain "
"alphanumeric, underscore, and hyphen characters (a-z, 0-9, -, _)"
)
class ProjectOptionValueError(ProjectError, UserSideException):
MESSAGE = "{0} for option `{1}` in section [{2}]"