From 191656f65f0dc1bb47d644285088c4bd5367fe61 Mon Sep 17 00:00:00 2001 From: Piotr Orzechowski Date: Wed, 18 Jan 2023 17:14:34 +0100 Subject: [PATCH] Disallow dots in identifiers (#1267) * Disallow leading dot in identifiers This is to avoid `alr printenv` printing invalid environment variable names, i.e. with leading dot. * Fix typo in run_alr error message * Simplify crate name validation test code * Use two letters for min crate name length verification * Test init with valid crate name * Add more test cases for bad index name * Use constant instead of dot literal * Disallow dots in identifiers Dots in identifiers are allowed by mistake. This commit forbids them completely, instead of just disallowing dots as the first character in crate name. --- src/alire/alire-crates.adb | 4 +-- src/alire/alire.ads | 2 +- testsuite/drivers/alr.py | 2 +- testsuite/tests/index/bad-name/test.py | 21 +++++++++--- .../tests/init/crate-name-validation/test.py | 34 +++++++++++++++++++ .../init/crate-name-validation/test.yaml | 4 +++ 6 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 testsuite/tests/init/crate-name-validation/test.py create mode 100644 testsuite/tests/init/crate-name-validation/test.yaml diff --git a/src/alire/alire-crates.adb b/src/alire/alire-crates.adb index aefce46d..31197b94 100644 --- a/src/alire/alire-crates.adb +++ b/src/alire/alire-crates.adb @@ -19,8 +19,8 @@ package body Alire.Crates is (AAA.Strings.Empty_Vector .Append ("Identifiers for crates and indexes must use " & "lowercase alphanumeric characters from the latin " - & "alphabet. Underscores can also be used except as " - & "the first character.") + & "alphabet. Underscores can also be used, except as" + & " the first character.") .New_Line .Append ("Length must be of" & Alire.Min_Name_Length'Img & " to" & Alire.Max_Name_Length'Img diff --git a/src/alire/alire.ads b/src/alire/alire.ads index b26e4845..79966aeb 100644 --- a/src/alire/alire.ads +++ b/src/alire/alire.ads @@ -63,7 +63,7 @@ package Alire with Preelaborate is subtype Crate_Character is Character with Static_Predicate => Crate_Character in - 'a' .. 'z' | '0' .. '9' | '_' | Extension_Separator; + 'a' .. 'z' | '0' .. '9' | '_'; -------------------- -- Crate Naming -- diff --git a/testsuite/drivers/alr.py b/testsuite/drivers/alr.py index 97a4d06c..e5120eb8 100644 --- a/testsuite/drivers/alr.py +++ b/testsuite/drivers/alr.py @@ -105,7 +105,7 @@ def run_alr(*args, **kwargs): if (p.status != 0 and complain_on_error) or (p.status == 0 and not complain_on_error): print('The following command:') print(' {}'.format(' '.join(quote_arg(arg) for arg in argv))) - print('Exitted with status code {}'.format(p.status)) + print('Exited with status code {}'.format(p.status)) print('Output:') print(p.out) if complain_on_error: diff --git a/testsuite/tests/index/bad-name/test.py b/testsuite/tests/index/bad-name/test.py index e0d3b563..0e01d085 100644 --- a/testsuite/tests/index/bad-name/test.py +++ b/testsuite/tests/index/bad-name/test.py @@ -1,13 +1,24 @@ -""" +''' Check that specifying a malformed index name is properly reported -""" +''' from drivers.alr import run_alr from drivers.asserts import assert_match +def assert_that(name, fails_with): + p = run_alr('index', '--add', name, '--name', name, complain_on_error=False, debug=False) + assert_match(fails_with, p.out) -p = run_alr('index', '--add', 'xx', '--name', 'xx', - complain_on_error=False, debug=False) -assert_match('.*Identifier too short.*', p.out) +# < min length +assert_that(name='aa', fails_with='.*Identifier too short.*') + +# > max length +assert_that(name='a' * 65, fails_with='.*Identifier too long.*') + +# Leading underscore +assert_that(name='_aaa', fails_with='.*Identifiers must not begin with an underscore.*') + +# Non lowercase ASCII alnum +assert_that(name='aaą', fails_with='.*Identifiers must be lowercase ASCII alphanumerical.*') print('SUCCESS') diff --git a/testsuite/tests/init/crate-name-validation/test.py b/testsuite/tests/init/crate-name-validation/test.py new file mode 100644 index 00000000..8da9c23e --- /dev/null +++ b/testsuite/tests/init/crate-name-validation/test.py @@ -0,0 +1,34 @@ +''' +Check that the crate name is validated properly +''' + +import string + +from drivers.alr import run_alr +from drivers.asserts import assert_match + +CRATE_TYPES = ['bin', 'lib'] +VALID_NAME = f"{string.ascii_lowercase}{string.digits}_" + +def assert_that(name, fails_with): + for crate_type in CRATE_TYPES: + p = run_alr('init', f"--{crate_type}", name, complain_on_error=False) + assert_match(fails_with, p.out) + +# < min length +assert_that(name='aa', fails_with='.*Identifier too short.*') + +# > max length +assert_that(name='a' * 65, fails_with='.*Identifier too long.*') + +# Leading underscore +assert_that(name='_aaa', fails_with='.*Identifiers must not begin with an underscore.*') + +# Non lowercase ASCII alnum +assert_that(name='aaą', fails_with='.*Identifiers must be lowercase ASCII alphanumerical.*') + +# Valid name +for crate_type in CRATE_TYPES: + run_alr('init', f"--{crate_type}", f"{VALID_NAME}{crate_type}") + +print('SUCCESS') diff --git a/testsuite/tests/init/crate-name-validation/test.yaml b/testsuite/tests/init/crate-name-validation/test.yaml new file mode 100644 index 00000000..e7e3556b --- /dev/null +++ b/testsuite/tests/init/crate-name-validation/test.yaml @@ -0,0 +1,4 @@ +driver: python-script +indexes: + basic_index: # needed to avoid cloning the community index + in_fixtures: true -- 2.39.5