diff options
Diffstat (limited to 'spec/lib/gitlab/static_site_editor/config')
7 files changed, 578 insertions, 13 deletions
diff --git a/spec/lib/gitlab/static_site_editor/config/file_config/entry/global_spec.rb b/spec/lib/gitlab/static_site_editor/config/file_config/entry/global_spec.rb new file mode 100644 index 00000000000..9ce6007165b --- /dev/null +++ b/spec/lib/gitlab/static_site_editor/config/file_config/entry/global_spec.rb @@ -0,0 +1,245 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Global do + let(:global) { described_class.new(hash) } + let(:default_image_upload_path_value) { 'source/images' } + + let(:default_mounts_value) do + [ + { + source: 'source', + target: '' + } + ] + end + + let(:default_static_site_generator_value) { 'middleman' } + + shared_examples_for 'valid default configuration' do + describe '#compose!' do + before do + global.compose! + end + + it 'creates nodes hash' do + expect(global.descendants).to be_an Array + end + + it 'creates node object for each entry' do + expect(global.descendants.count).to eq 3 + end + + it 'creates node object using valid class' do + expect(global.descendants.map(&:class)).to match_array(expected_node_object_classes) + end + + it 'sets a description containing "Static Site Editor" for all nodes' do + expect(global.descendants.map(&:description)).to all(match(/Static Site Editor/)) + end + + describe '#leaf?' do + it 'is not leaf' do + expect(global).not_to be_leaf + end + end + end + + context 'when not composed' do + describe '#static_site_generator_value' do + it 'returns nil' do + expect(global.static_site_generator_value).to be nil + end + end + + describe '#leaf?' do + it 'is leaf' do + expect(global).to be_leaf + end + end + end + + context 'when composed' do + before do + global.compose! + end + + describe '#errors' do + it 'has no errors' do + expect(global.errors).to be_empty + end + end + + describe '#image_upload_path_value' do + it 'returns correct values' do + expect(global.image_upload_path_value).to eq(default_image_upload_path_value) + end + end + + describe '#mounts_value' do + it 'returns correct values' do + expect(global.mounts_value).to eq(default_mounts_value) + end + end + + describe '#static_site_generator_value' do + it 'returns correct values' do + expect(global.static_site_generator_value).to eq(default_static_site_generator_value) + end + end + end + end + + describe '.nodes' do + it 'returns a hash' do + expect(described_class.nodes).to be_a(Hash) + end + + context 'when filtering all the entry/node names' do + it 'contains the expected node names' do + expected_node_names = %i[ + image_upload_path + mounts + static_site_generator + ] + expect(described_class.nodes.keys).to match_array(expected_node_names) + end + end + end + + context 'when configuration is valid' do + context 'when some entries defined' do + let(:expected_node_object_classes) do + [ + Gitlab::StaticSiteEditor::Config::FileConfig::Entry::ImageUploadPath, + Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Mounts, + Gitlab::StaticSiteEditor::Config::FileConfig::Entry::StaticSiteGenerator + ] + end + + let(:hash) do + { + image_upload_path: default_image_upload_path_value, + mounts: default_mounts_value, + static_site_generator: default_static_site_generator_value + } + end + + it_behaves_like 'valid default configuration' + end + end + + context 'when value is an empty hash' do + let(:expected_node_object_classes) do + [ + Gitlab::Config::Entry::Unspecified, + Gitlab::Config::Entry::Unspecified, + Gitlab::Config::Entry::Unspecified + ] + end + + let(:hash) { {} } + + it_behaves_like 'valid default configuration' + end + + context 'when configuration is not valid' do + before do + global.compose! + end + + context 'when a single entry is invalid' do + let(:hash) do + { image_upload_path: { not_a_string: true } } + end + + describe '#errors' do + it 'reports errors' do + expect(global.errors) + .to include 'image_upload_path config should be a string' + end + end + end + + context 'when a multiple entries are invalid' do + let(:hash) do + { + image_upload_path: { not_a_string: true }, + static_site_generator: { not_a_string: true } + } + end + + describe '#errors' do + it 'reports errors' do + expect(global.errors) + .to match_array([ + 'image_upload_path config should be a string', + 'static_site_generator config should be a string', + "static_site_generator config should be 'middleman'" + ]) + end + end + end + + context 'when there is an invalid key' do + let(:hash) do + { invalid_key: true } + end + + describe '#errors' do + it 'reports errors' do + expect(global.errors) + .to include 'global config contains unknown keys: invalid_key' + end + end + end + end + + context 'when value is not a hash' do + let(:hash) { [] } + + describe '#valid?' do + it 'is not valid' do + expect(global).not_to be_valid + end + end + + describe '#errors' do + it 'returns error about invalid type' do + expect(global.errors.first).to match /should be a hash/ + end + end + end + + describe '#specified?' do + it 'is concrete entry that is defined' do + expect(global.specified?).to be true + end + end + + describe '#[]' do + before do + global.compose! + end + + let(:hash) do + { static_site_generator: default_static_site_generator_value } + end + + context 'when entry exists' do + it 'returns correct entry' do + expect(global[:static_site_generator]) + .to be_an_instance_of Gitlab::StaticSiteEditor::Config::FileConfig::Entry::StaticSiteGenerator + expect(global[:static_site_generator].value).to eq default_static_site_generator_value + end + end + + context 'when entry does not exist' do + it 'always return unspecified node' do + expect(global[:some][:unknown][:node]) + .not_to be_specified + end + end + end +end diff --git a/spec/lib/gitlab/static_site_editor/config/file_config/entry/image_upload_path_spec.rb b/spec/lib/gitlab/static_site_editor/config/file_config/entry/image_upload_path_spec.rb new file mode 100644 index 00000000000..c2b7fbf6f98 --- /dev/null +++ b/spec/lib/gitlab/static_site_editor/config/file_config/entry/image_upload_path_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::ImageUploadPath do + subject(:image_upload_path_entry) { described_class.new(config) } + + describe 'validations' do + context 'with a valid config' do + let(:config) { 'an-image-upload-path' } + + it { is_expected.to be_valid } + + describe '#value' do + it 'returns a image_upload_path key' do + expect(image_upload_path_entry.value).to eq config + end + end + end + + context 'with an invalid config' do + let(:config) { { not_a_string: true } } + + it { is_expected.not_to be_valid } + + it 'reports errors about wrong type' do + expect(image_upload_path_entry.errors) + .to include 'image upload path config should be a string' + end + end + end + + describe '.default' do + it 'returns default image_upload_path' do + expect(described_class.default).to eq 'source/images' + end + end +end diff --git a/spec/lib/gitlab/static_site_editor/config/file_config/entry/mount_spec.rb b/spec/lib/gitlab/static_site_editor/config/file_config/entry/mount_spec.rb new file mode 100644 index 00000000000..04248fc60a5 --- /dev/null +++ b/spec/lib/gitlab/static_site_editor/config/file_config/entry/mount_spec.rb @@ -0,0 +1,101 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Mount do + subject(:entry) { described_class.new(config) } + + describe 'validations' do + context 'with a valid config' do + context 'and target is a non-empty string' do + let(:config) do + { + source: 'source', + target: 'sub-site' + } + end + + it { is_expected.to be_valid } + + describe '#value' do + it 'returns mount configuration' do + expect(entry.value).to eq config + end + end + end + + context 'and target is an empty string' do + let(:config) do + { + source: 'source', + target: '' + } + end + + it { is_expected.to be_valid } + + describe '#value' do + it 'returns mount configuration' do + expect(entry.value).to eq config + end + end + end + end + + context 'with an invalid config' do + context 'when source is not a string' do + let(:config) { { source: 123, target: 'target' } } + + it { is_expected.not_to be_valid } + + it 'reports error' do + expect(entry.errors) + .to include 'mount source should be a string' + end + end + + context 'when source is not present' do + let(:config) { { target: 'target' } } + + it { is_expected.not_to be_valid } + + it 'reports error' do + expect(entry.errors) + .to include "mount source can't be blank" + end + end + + context 'when target is not a string' do + let(:config) { { source: 'source', target: 123 } } + + it { is_expected.not_to be_valid } + + it 'reports error' do + expect(entry.errors) + .to include 'mount target should be a string' + end + end + + context 'when there is an unknown key present' do + let(:config) { { test: 100 } } + + it { is_expected.not_to be_valid } + + it 'reports error' do + expect(entry.errors) + .to include 'mount config contains unknown keys: test' + end + end + end + end + + describe '.default' do + it 'returns default mount' do + expect(described_class.default) + .to eq({ + source: 'source', + target: '' + }) + end + end +end diff --git a/spec/lib/gitlab/static_site_editor/config/file_config/entry/mounts_spec.rb b/spec/lib/gitlab/static_site_editor/config/file_config/entry/mounts_spec.rb new file mode 100644 index 00000000000..0ae2ece9474 --- /dev/null +++ b/spec/lib/gitlab/static_site_editor/config/file_config/entry/mounts_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::Mounts do + subject(:entry) { described_class.new(config) } + + describe 'validations' do + context 'with a valid config' do + let(:config) do + [ + { + source: 'source', + target: '' + }, + { + source: 'sub-site/source', + target: 'sub-site' + } + ] + end + + it { is_expected.to be_valid } + + describe '#value' do + it 'returns mounts configuration' do + expect(entry.value).to eq config + end + end + end + + context 'with an invalid config' do + let(:config) { { not_an_array: true } } + + it { is_expected.not_to be_valid } + + it 'reports errors about wrong type' do + expect(entry.errors) + .to include 'mounts config should be a array' + end + end + end + + describe '.default' do + it 'returns default mounts' do + expect(described_class.default) + .to eq([{ + source: 'source', + target: '' + }]) + end + end +end diff --git a/spec/lib/gitlab/static_site_editor/config/file_config/entry/static_site_generator_spec.rb b/spec/lib/gitlab/static_site_editor/config/file_config/entry/static_site_generator_spec.rb new file mode 100644 index 00000000000..a9c730218cf --- /dev/null +++ b/spec/lib/gitlab/static_site_editor/config/file_config/entry/static_site_generator_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig::Entry::StaticSiteGenerator do + let(:static_site_generator) { described_class.new(config) } + + describe 'validations' do + context 'when value is valid' do + let(:config) { 'middleman' } + + describe '#value' do + it 'returns a static_site_generator key' do + expect(static_site_generator.value).to eq config + end + end + + describe '#valid?' do + it 'is valid' do + expect(static_site_generator).to be_valid + end + end + end + + context 'when value is invalid' do + let(:config) { 'not-a-valid-generator' } + + describe '#valid?' do + it 'is not valid' do + expect(static_site_generator).not_to be_valid + end + end + end + + context 'when value has a wrong type' do + let(:config) { { not_a_string: true } } + + it 'reports errors about wrong type' do + expect(static_site_generator.errors) + .to include 'static site generator config should be a string' + end + end + end + + describe '.default' do + it 'returns default static_site_generator' do + expect(described_class.default).to eq 'middleman' + end + end +end diff --git a/spec/lib/gitlab/static_site_editor/config/file_config_spec.rb b/spec/lib/gitlab/static_site_editor/config/file_config_spec.rb index 594425c2dab..d444d4f1df7 100644 --- a/spec/lib/gitlab/static_site_editor/config/file_config_spec.rb +++ b/spec/lib/gitlab/static_site_editor/config/file_config_spec.rb @@ -3,13 +3,85 @@ require 'spec_helper' RSpec.describe Gitlab::StaticSiteEditor::Config::FileConfig do - subject(:config) { described_class.new } + let(:config) do + described_class.new(yml) + end + + context 'when config is valid' do + context 'when config has valid values' do + let(:yml) do + <<-EOS + static_site_generator: middleman + EOS + end + + describe '#to_hash_with_defaults' do + it 'returns hash created from string' do + expect(config.to_hash_with_defaults.fetch(:static_site_generator)).to eq 'middleman' + end + end + + describe '#valid?' do + it 'is valid' do + expect(config).to be_valid + end + + it 'has no errors' do + expect(config.errors).to be_empty + end + end + end + end + + context 'when a config entry has an empty value' do + let(:yml) { 'static_site_generator: ' } + + describe '#to_hash' do + it 'returns default value' do + expect(config.to_hash_with_defaults.fetch(:static_site_generator)).to eq 'middleman' + end + end + + describe '#valid?' do + it 'is valid' do + expect(config).to be_valid + end + + it 'has no errors' do + expect(config.errors).to be_empty + end + end + end + + context 'when config is invalid' do + context 'when yml is incorrect' do + let(:yml) { '// invalid' } + + describe '.new' do + it 'raises error' do + expect { config }.to raise_error(described_class::ConfigError, /Invalid configuration format/) + end + end + end + + context 'when config value exists but is not a valid value' do + let(:yml) { 'static_site_generator: "unsupported-generator"' } + + describe '#valid?' do + it 'is not valid' do + expect(config).not_to be_valid + end - describe '#data' do - subject { config.data } + it 'has errors' do + expect(config.errors).not_to be_empty + end + end - it 'returns hardcoded data for now' do - is_expected.to match(static_site_generator: 'middleman') + describe '#errors' do + it 'returns an array of strings' do + expect(config.errors).to all(be_an_instance_of(String)) + end + end end end end diff --git a/spec/lib/gitlab/static_site_editor/config/generated_config_spec.rb b/spec/lib/gitlab/static_site_editor/config/generated_config_spec.rb index 3433a54be9c..2f761b69e60 100644 --- a/spec/lib/gitlab/static_site_editor/config/generated_config_spec.rb +++ b/spec/lib/gitlab/static_site_editor/config/generated_config_spec.rb @@ -29,7 +29,7 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::GeneratedConfig do project: 'project', project_id: project.id, return_url: 'http://example.com', - is_supported_content: 'true', + is_supported_content: true, base_url: '/namespace/project/-/sse/master%2FREADME.md', merge_requests_illustration_path: %r{illustrations/merge_requests} }) @@ -65,7 +65,7 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::GeneratedConfig do stub_feature_flags(sse_erb_support: project) end - it { is_expected.to include(is_supported_content: 'true') } + it { is_expected.to include(is_supported_content: true) } end context 'when feature flag is disabled' do @@ -75,7 +75,7 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::GeneratedConfig do stub_feature_flags(sse_erb_support: false) end - it { is_expected.to include(is_supported_content: 'false') } + it { is_expected.to include(is_supported_content: false) } end end @@ -88,31 +88,31 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::GeneratedConfig do context 'when branch is not master' do let(:ref) { 'my-branch' } - it { is_expected.to include(is_supported_content: 'false') } + it { is_expected.to include(is_supported_content: false) } end context 'when file does not have a markdown extension' do let(:path) { 'README.txt' } - it { is_expected.to include(is_supported_content: 'false') } + it { is_expected.to include(is_supported_content: false) } end context 'when file does not have an extension' do let(:path) { 'README' } - it { is_expected.to include(is_supported_content: 'false') } + it { is_expected.to include(is_supported_content: false) } end context 'when file does not exist' do let(:path) { 'UNKNOWN.md' } - it { is_expected.to include(is_supported_content: 'false') } + it { is_expected.to include(is_supported_content: false) } end context 'when repository is empty' do let(:repository) { create(:project_empty_repo).repository } - it { is_expected.to include(is_supported_content: 'false') } + it { is_expected.to include(is_supported_content: false) } end context 'when return_url is not a valid URL' do @@ -132,5 +132,11 @@ RSpec.describe Gitlab::StaticSiteEditor::Config::GeneratedConfig do it { is_expected.to include(return_url: nil) } end + + context 'when a commit for the ref cannot be found' do + let(:ref) { 'nonexistent-ref' } + + it { is_expected.to include(commit_id: nil) } + end end end |