diff options
author | Brian Williams <bwilliams@gitlab.com> | 2022-06-23 15:41:06 +0300 |
---|---|---|
committer | Brian Williams <bwilliams@gitlab.com> | 2022-06-23 15:42:48 +0300 |
commit | 769ea6ff714849aea4c3ac68f9f7efcdcb4bf8ec (patch) | |
tree | 4c14c638fb831e03260167dad68aa84ba2b2273c | |
parent | cb654d1504769109f688b5d2d5636c9a2141008f (diff) |
Add tests for edit_on_gitlab helper
-rw-r--r-- | Gemfile | 1 | ||||
-rw-r--r-- | Gemfile.lock | 15 | ||||
-rw-r--r-- | lib/helpers/edit_on_gitlab.rb | 6 | ||||
-rw-r--r-- | spec/helpers/edit_on_gitlab_spec.rb | 49 | ||||
-rw-r--r-- | spec/spec_helper.rb | 1 |
5 files changed, 69 insertions, 3 deletions
@@ -22,6 +22,7 @@ end group :test, :development do gem 'highline', '~> 2.0.0' gem 'rspec', '~> 3.11.0' + gem 'rspec-parameterized', '~> 0.5.1' gem 'pry-byebug', '~> 3.9.0', require: false gem 'gitlab-styles', '~> 7.1.0', require: false end diff --git a/Gemfile.lock b/Gemfile.lock index 8bbbed48..ad014e7f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -20,6 +20,7 @@ GEM asciidoctor-plantuml (0.0.15) asciidoctor (>= 1.5.6, < 3.0.0) ast (2.4.2) + binding_ninja (0.2.3) builder (3.2.4) byebug (11.1.3) claide (1.1.0) @@ -182,6 +183,10 @@ GEM ast (~> 2.4.1) pastel (0.8.0) tty-color (~> 0.5) + proc_to_ast (0.1.0) + coderay + parser + unparser pry (0.13.1) coderay (~> 1.1) method_source (~> 1.0) @@ -216,6 +221,12 @@ GEM rspec-mocks (3.11.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.11.0) + rspec-parameterized (0.5.1) + binding_ninja (>= 0.2.3) + parser + proc_to_ast + rspec (>= 2.13, < 4) + unparser rspec-support (3.11.0) rubocop (0.93.1) parallel (~> 1.10) @@ -266,6 +277,9 @@ GEM tzinfo (2.0.4) concurrent-ruby (~> 1.0) unicode-display_width (1.8.0) + unparser (0.6.5) + diff-lcs (~> 1.3) + parser (>= 3.1.0) zeitwerk (2.6.0) PLATFORMS @@ -282,6 +296,7 @@ DEPENDENCIES pry-byebug (~> 3.9.0) rake (~> 13.0.0) rspec (~> 3.11.0) + rspec-parameterized (~> 0.5.1) sass (= 3.7.4) sassc (~> 2.4.0) diff --git a/lib/helpers/edit_on_gitlab.rb b/lib/helpers/edit_on_gitlab.rb index 7188cd1e..47d94d26 100644 --- a/lib/helpers/edit_on_gitlab.rb +++ b/lib/helpers/edit_on_gitlab.rb @@ -4,7 +4,7 @@ module Nanoc::Helpers module EditOnGitLab def edit_on_gitlab(item, editor: :simple) # Make an array out of the content's source path. - content_filename_array = @item.identifier.to_s.split('/') + content_filename_array = item.identifier.to_s.split('/') # remove first empty item content_filename_array.shift # Get the product name. @@ -35,8 +35,8 @@ module Nanoc::Helpers gitlab_ide_url = "https://gitlab.com/-/ide/project/gitlab-org/gitlab/edit/master/-/doc/#{docs_content_filename}" else # gitlab-docs pages - gitlab_url = "https://gitlab.com/gitlab-org/gitlab-docs/blob/main/#{@item[:content_filename]}" - gitlab_ide_url = "https://gitlab.com/-/ide/project/gitlab-org/gitlab-docs/edit/main/-/#{@item[:content_filename]}" + gitlab_url = "https://gitlab.com/gitlab-org/gitlab-docs/blob/main/#{item[:content_filename]}" + gitlab_ide_url = "https://gitlab.com/-/ide/project/gitlab-org/gitlab-docs/edit/main/-/#{item[:content_filename]}" end case editor diff --git a/spec/helpers/edit_on_gitlab_spec.rb b/spec/helpers/edit_on_gitlab_spec.rb new file mode 100644 index 00000000..77114593 --- /dev/null +++ b/spec/helpers/edit_on_gitlab_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'nanoc' +require 'helpers/edit_on_gitlab' + +RSpec.describe Nanoc::Helpers::EditOnGitLab do + let(:mock_class) { Class.new { extend Nanoc::Helpers::EditOnGitLab } } + let(:identifier) { "/content/404.html" } + let(:content_filename) { "content/404.html" } + + let(:mock_item) do + item = Struct.new(:identifier, :content_filename) + item.new(identifier, content_filename) + end + + subject { mock_class.edit_on_gitlab(mock_item, editor: editor) } + + describe '#edit_on_gitlab' do + using RSpec::Parameterized::TableSyntax + + where(:identifier, :editor, :expected_url) do + "/omnibus/index.md" | :simple | "https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/index.md" + "/omnibus/index.md" | :webide | "https://gitlab.com/-/ide/project/gitlab-org/omnibus-gitlab/edit/master/-/doc/index.md" + "/runner/index.md" | :simple | "https://gitlab.com/gitlab-org/gitlab-runner/blob/main/docs/index.md" + "/runner/index.md" | :webide | "https://gitlab.com/-/ide/project/gitlab-org/gitlab-runner/edit/main/-/docs/index.md" + "/charts/index.md" | :simple | "https://gitlab.com/gitlab-org/charts/gitlab/blob/master/doc/index.md" + "/charts/index.md" | :webide | "https://gitlab.com/-/ide/project/gitlab-org/charts/gitlab/edit/master/-/doc/index.md" + "/operator/index.md" | :simple | "https://gitlab.com/gitlab-org/cloud-native/gitlab-operator/blob/master/doc/index.md" + "/operator/index.md" | :webide | "https://gitlab.com/-/ide/project/gitlab-org/cloud-native/gitlab-operator/edit/master/-/doc/index.md" + "/ee/user/ssh.md" | :simple | "https://gitlab.com/gitlab-org/gitlab/blob/master/doc/user/ssh.md" + "/ee/user/ssh.md" | :webide | "https://gitlab.com/-/ide/project/gitlab-org/gitlab/edit/master/-/doc/user/ssh.md" + "/content/404.html" | :simple | "https://gitlab.com/gitlab-org/gitlab-docs/blob/main/content/404.html" + "/content/404.html" | :webide | "https://gitlab.com/-/ide/project/gitlab-org/gitlab-docs/edit/main/-/content/404.html" + end + + with_them do + it 'returns correct url for identifier and editor' do + expect(subject).to eq(expected_url) + end + end + + context 'with unknown editor' do + let(:editor) { :word } + + it { expect { subject }.to raise_error("Unknown editor: word") } + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 43596af8..762c465b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,6 +2,7 @@ $LOAD_PATH << 'lib/' +require 'rspec-parameterized' require 'gitlab/docs' RSpec.configure do |config| |