Commit 380f63bf authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve remote CI/CD config file extension validation

parent 7977a20b
...@@ -10,7 +10,7 @@ module Gitlab ...@@ -10,7 +10,7 @@ module Gitlab
attr_reader :location, :opts, :errors attr_reader :location, :opts, :errors
YAML_WHITELIST_EXTENSION = /(yml|yaml)$/i.freeze YAML_WHITELIST_EXTENSION = /.+\.(yml|yaml)$/i.freeze
def initialize(location, opts = {}) def initialize(location, opts = {})
@location = location @location = location
...@@ -21,7 +21,7 @@ module Gitlab ...@@ -21,7 +21,7 @@ module Gitlab
end end
def invalid_extension? def invalid_extension?
!location.match(YAML_WHITELIST_EXTENSION) !::File.basename(location).match(YAML_WHITELIST_EXTENSION)
end end
def valid? def valid?
......
# frozen_string_literal: true
require 'fast_spec_helper'
describe Gitlab::Ci::Config::External::File::Base do
subject { described_class.new(location) }
before do
allow_any_instance_of(described_class)
.to receive(:content).and_return('key: value')
end
describe '#valid?' do
context 'when location is not a YAML file by extension' do
let(:location) { 'some/file.txt' }
it { is_expected.not_to be_valid }
end
context 'when location is not an invalid YAML extension' do
let(:location) { 'some/file/.yml' }
it { is_expected.not_to be_valid }
end
context 'when location is not an valid YAML extension' do
let(:location) { 'some/file/config.yml' }
it { is_expected.to be_valid }
end
context 'when location is not an valid YAML extension' do
let(:location) { 'some/file/config.yaml' }
it { is_expected.to be_valid }
end
context 'when there are YAML syntax errors' do
let(:location) { 'some/file/config.yml' }
before do
allow_any_instance_of(described_class)
.to receive(:content).and_return('invalid_syntax')
end
it 'is not a valid file' do
expect(subject).not_to be_valid
expect(subject.error_message).to match /does not have valid YAML syntax/
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment