diff options
Diffstat (limited to 'spec/lib/gitlab/regex_spec.rb')
-rw-r--r-- | spec/lib/gitlab/regex_spec.rb | 120 |
1 files changed, 34 insertions, 86 deletions
diff --git a/spec/lib/gitlab/regex_spec.rb b/spec/lib/gitlab/regex_spec.rb index b4c1f3b689b..d48e8183650 100644 --- a/spec/lib/gitlab/regex_spec.rb +++ b/spec/lib/gitlab/regex_spec.rb @@ -1,7 +1,11 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' +require_relative '../../../lib/gitlab/regex' + +# All specs that can be run with fast_spec_helper only +# See regex_requires_app_spec for tests that require the full spec_helper RSpec.describe Gitlab::Regex do shared_examples_for 'project/group name chars regex' do it { is_expected.to match('gitlab-ce') } @@ -401,35 +405,6 @@ RSpec.describe Gitlab::Regex do it { is_expected.not_to match('%2e%2e%2f1.2.3') } end - describe '.npm_package_name_regex' do - subject { described_class.npm_package_name_regex } - - it { is_expected.to match('@scope/package') } - it { is_expected.to match('unscoped-package') } - it { is_expected.not_to match('@first-scope@second-scope/package') } - it { is_expected.not_to match('scope-without-at-symbol/package') } - it { is_expected.not_to match('@not-a-scoped-package') } - it { is_expected.not_to match('@scope/sub/package') } - it { is_expected.not_to match('@scope/../../package') } - it { is_expected.not_to match('@scope%2e%2e%2fpackage') } - it { is_expected.not_to match('@%2e%2e%2f/package') } - - context 'capturing group' do - [ - ['@scope/package', 'scope'], - ['unscoped-package', nil], - ['@not-a-scoped-package', nil], - ['@scope/sub/package', nil], - ['@inv@lid-scope/package', nil] - ].each do |package_name, extracted_scope_name| - it "extracts the scope name for #{package_name}" do - match = package_name.match(described_class.npm_package_name_regex) - expect(match&.captures&.first).to eq(extracted_scope_name) - end - end - end - end - describe '.nuget_version_regex' do subject { described_class.nuget_version_regex } @@ -595,8 +570,7 @@ RSpec.describe Gitlab::Regex do # nothing after colon in version number it { is_expected.not_to match('2:') } # revision number is empty - # Note: we are less strict here - # it { is_expected.not_to match('1.0-') } + it { is_expected.not_to match('1.0-') } # version number is empty it { is_expected.not_to match('-1') } it { is_expected.not_to match('2:-1') } @@ -618,63 +592,12 @@ RSpec.describe Gitlab::Regex do it { is_expected.not_to match('1.0 ') } # dpkg accepts multiple colons it { is_expected.not_to match('1:2:3') } + # we limit the number of dashes + it { is_expected.to match('1-2-3-4-5-6-7-8-9-10-11-12-13-14-15') } + it { is_expected.not_to match('1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16') } end end - describe '.debian_architecture_regex' do - subject { described_class.debian_architecture_regex } - - it { is_expected.to match('amd64') } - it { is_expected.to match('kfreebsd-i386') } - - # may not be empty string - it { is_expected.not_to match('') } - # must start with an alphanumeric - it { is_expected.not_to match('-a') } - it { is_expected.not_to match('+a') } - it { is_expected.not_to match('.a') } - it { is_expected.not_to match('_a') } - # only letters, digits and characters '-' - it { is_expected.not_to match('a+b') } - it { is_expected.not_to match('a.b') } - it { is_expected.not_to match('a_b') } - it { is_expected.not_to match('a~') } - it { is_expected.not_to match('aé') } - - # More strict - # Enforce lowercase - it { is_expected.not_to match('AMD64') } - it { is_expected.not_to match('Amd64') } - it { is_expected.not_to match('aMD64') } - end - - describe '.debian_distribution_regex' do - subject { described_class.debian_distribution_regex } - - it { is_expected.to match('buster') } - it { is_expected.to match('buster-updates') } - it { is_expected.to match('Debian10.5') } - - # Do not allow slash, even if this exists in the wild - it { is_expected.not_to match('jessie/updates') } - - # Do not allow Unicode - it { is_expected.not_to match('hé') } - end - - describe '.debian_component_regex' do - subject { described_class.debian_component_regex } - - it { is_expected.to match('main') } - it { is_expected.to match('non-free') } - - # Do not allow slash - it { is_expected.not_to match('non/free') } - - # Do not allow Unicode - it { is_expected.not_to match('hé') } - end - describe '.helm_channel_regex' do subject { described_class.helm_channel_regex } @@ -1020,4 +943,29 @@ RSpec.describe Gitlab::Regex do it { is_expected.not_to match('a' * 63 + '#') } it { is_expected.not_to match('') } end + + describe '.sep_by_1' do + subject { %r{\A #{described_class.sep_by_1(/\.+/, /[abcdef]{3}/)} \z}x } + + it { is_expected.to match('abc') } + it { is_expected.to match('abc.def') } + it { is_expected.to match('abc.def.caf') } + it { is_expected.to match('abc..def') } + it { is_expected.to match('abc..def..caf') } + it { is_expected.to match('abc...def') } + it { is_expected.to match('abc....def........caf') } + it { is_expected.to match((['abc'] * 100).join('.')) } + + it { is_expected.not_to match('') } + it { is_expected.not_to match('a') } + it { is_expected.not_to match('aaaa') } + it { is_expected.not_to match('foo') } + it { is_expected.not_to match('.abc') } + it { is_expected.not_to match('abc.') } + it { is_expected.not_to match('.abc.def') } + it { is_expected.not_to match('abc.def.') } + it { is_expected.not_to match('abc.defe.caf') } + it { is_expected.not_to match('abc!abc') } + it { is_expected.not_to match((['abc'] * 100).join('.') + '!') } + end end |