From 2f81fbe21f09ae7dd80ceebf7a70903b0febcfe2 Mon Sep 17 00:00:00 2001 From: Evan Read Date: Mon, 27 Jun 2022 14:04:48 +1000 Subject: Match spec location to implementation location --- spec/gitlab/content_spec.rb | 18 ---- spec/gitlab/docs/link_spec.rb | 148 ---------------------------- spec/gitlab/docs/page_spec.rb | 102 ------------------- spec/gitlab/navigation/category_spec.rb | 85 ---------------- spec/gitlab/navigation/doc_spec.rb | 84 ---------------- spec/gitlab/navigation/section_spec.rb | 77 --------------- spec/gitlab/navigation_spec.rb | 70 ------------- spec/helpers/edit_on_gitlab_spec.rb | 49 --------- spec/lib/gitlab/content_spec.rb | 18 ++++ spec/lib/gitlab/docs/link_spec.rb | 148 ++++++++++++++++++++++++++++ spec/lib/gitlab/docs/page_spec.rb | 102 +++++++++++++++++++ spec/lib/gitlab/navigation/category_spec.rb | 85 ++++++++++++++++ spec/lib/gitlab/navigation/doc_spec.rb | 84 ++++++++++++++++ spec/lib/gitlab/navigation/section_spec.rb | 77 +++++++++++++++ spec/lib/gitlab/navigation_spec.rb | 70 +++++++++++++ spec/lib/helpers/edit_on_gitlab_spec.rb | 49 +++++++++ 16 files changed, 633 insertions(+), 633 deletions(-) delete mode 100644 spec/gitlab/content_spec.rb delete mode 100644 spec/gitlab/docs/link_spec.rb delete mode 100644 spec/gitlab/docs/page_spec.rb delete mode 100644 spec/gitlab/navigation/category_spec.rb delete mode 100644 spec/gitlab/navigation/doc_spec.rb delete mode 100644 spec/gitlab/navigation/section_spec.rb delete mode 100644 spec/gitlab/navigation_spec.rb delete mode 100644 spec/helpers/edit_on_gitlab_spec.rb create mode 100644 spec/lib/gitlab/content_spec.rb create mode 100644 spec/lib/gitlab/docs/link_spec.rb create mode 100644 spec/lib/gitlab/docs/page_spec.rb create mode 100644 spec/lib/gitlab/navigation/category_spec.rb create mode 100644 spec/lib/gitlab/navigation/doc_spec.rb create mode 100644 spec/lib/gitlab/navigation/section_spec.rb create mode 100644 spec/lib/gitlab/navigation_spec.rb create mode 100644 spec/lib/helpers/edit_on_gitlab_spec.rb diff --git a/spec/gitlab/content_spec.rb b/spec/gitlab/content_spec.rb deleted file mode 100644 index c8538121..00000000 --- a/spec/gitlab/content_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -# See: https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/4726#note_459473659 -describe 'Content directory size' do - subject { Dir.glob('content/**/*').sum { |f| File.size(f) } } - - let(:megabyte) { 1024**2 } - - # This limit can be increased after checking that Omnibus package build does not fail - let(:maximum_size) { 2 * megabyte } - - # `content` directory is included to the Omnibus package - # We want to make sure that the size of the directory is small enough - # to prevent accidental Omnibus pipeline failures. - it { is_expected.to be < maximum_size } -end diff --git a/spec/gitlab/docs/link_spec.rb b/spec/gitlab/docs/link_spec.rb deleted file mode 100644 index 4484936e..00000000 --- a/spec/gitlab/docs/link_spec.rb +++ /dev/null @@ -1,148 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe Gitlab::Docs::Link do - let(:page) { instance_double(Gitlab::Docs::Page) } - let(:href) { '../some/page.html#some-anchor' } - - subject { described_class.new(href, page) } - - describe '#to_anchor?' do - context 'when link contains anchor name' do - let(:href) { '../some/page.html' } - - it { is_expected.not_to be_to_anchor } - end - - context 'when link does not contain anchor name' do - let(:href) { '../some/page.html#some-anchor' } - - it { is_expected.to be_to_anchor } - end - - context 'when link contains an empty anchor' do - let(:href) { '../some/page.html#' } - - it { is_expected.not_to be_to_anchor } - end - end - - describe '#internal_anchor?' do - context 'when link contains internal anchor name' do - let(:href) { '#some-internal-anchor' } - - it { is_expected.to be_internal_anchor } - end - - context 'when link does not contain internal anchor' do - let(:href) { '../some/page.html#some-anchor' } - - it { is_expected.not_to be_internal_anchor } - end - end - - describe '#internal?' do - context 'when link is an external link' do - let(:href) { 'https://gitlab.com/some/page.html' } - - it { is_expected.not_to be_internal } - end - - context 'when link is an internal link' do - let(:href) { '../some/page.html' } - - it { is_expected.to be_internal } - end - end - - describe '#absolute_path' do - before do - allow(page).to receive(:directory).and_return('/my/path') - allow(Gitlab::Docs::Nanoc).to receive(:output_dir).and_return('/nanoc') - end - - context 'when link is an external link' do - let(:href) { 'https://gitlab.com/some/page.html' } - - it 'raises an error' do - expect { subject.absolute_path }.to raise_error(RuntimeError) - end - end - - context 'when link is relative' do - let(:href) { '../some/page.html' } - - it 'expands relative link' do - expect(subject.absolute_path).to eq '/my/some/page.html' - end - end - - context 'when link is absolute' do - let(:href) { '/some/page.html' } - - it 'expands absolute path' do - expect(subject.absolute_path).to eq '/nanoc/some/page.html' - end - end - end - - describe '#destination_page' do - context 'when the link is an internal link' do - let(:href) { '#some-anchor' } - - it 'returns the page it is assigned by itself' do - expect(subject.destination_page).to eq page - end - end - - context 'when the link is an external link' do - let(:href) { '../some/page.html#my-anchor' } - let(:destination) { instance_double(Gitlab::Docs::Page) } - - before do - allow(page).to receive(:directory).and_return('/my/docs/page') - end - - it 'builds a new page' do - expect(Gitlab::Docs::Page).to receive(:build) - .with('/my/docs/some/page.html') - .and_return(destination) - - expect(subject.destination_page).to eq destination - end - end - end - - describe '#source_file' do - it 'returns page file' do - expect(page).to receive(:file) - - subject.source_file - end - end - - describe '#destination_page_not_found?' do - before do - allow(page).to receive(:directory).and_return('/my/dir') - allow(subject).to receive(:destination_file) - .and_return(instance_double(Gitlab::Docs::Page, exists?: false)) - end - - it 'returns false if page does not exist' do - expect(subject.destination_page_not_found?).to be true - end - end - - describe '#destination_anchor_not_found?' do - before do - allow(page).to receive(:directory).and_return('/my/dir') - allow(subject).to receive(:destination_file) - .and_return(instance_double(Gitlab::Docs::Page, has_anchor?: false)) - end - - it 'returns false if anchor does not exist' do - expect(subject.destination_anchor_not_found?).to be true - end - end -end diff --git a/spec/gitlab/docs/page_spec.rb b/spec/gitlab/docs/page_spec.rb deleted file mode 100644 index 44454385..00000000 --- a/spec/gitlab/docs/page_spec.rb +++ /dev/null @@ -1,102 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -require 'nokogiri' -require 'gitlab/docs/page' -require 'gitlab/docs/link' -require 'gitlab/docs/element' -require 'gitlab/docs/document' - -describe Gitlab::Docs::Page do - subject do - described_class.new('some/file.html') - end - - context 'when file exists' do - before do - allow(File).to receive(:exist?) - .with('some/file.html') - .and_return(true) - - allow(File).to receive(:read) - .with('some/file.html') - .and_return <<~HTML - - - See external file - See internal section - See internal section broken - See utf-8 anchor - See utf-8 anchor 2 - -

Some section

-

Some section 2

- - - HTML - end - - describe '#links' do - it 'collects links on a page' do - expect(subject.links.count).to eq 4 - end - end - - describe '#hrefs' do - it 'collects all hrefs' do - expect(subject.hrefs).to match_array %w[../link.html#my-anchor - #internal-anchor - #internal-anchor-2 - #utf-8-id-✔] - end - end - - describe '#ids' do - it 'collects all ids' do - expect(subject.ids).to match_array %w[internal-anchor - utf-8-id-✔] - end - end - - describe '#has_anchor?' do - it 'returns true when anchor exists on a page' do - expect(subject).to have_anchor('internal-anchor') - end - - it 'returns false when anchors does not exist' do - expect(subject).not_to have_anchor('internal-anchor-2') - end - - it 'returns true when UTF-8 encoded anchors are compared' do - expect(subject).to have_anchor('utf-8-id-✔') - expect(subject).to have_anchor('utf-8-id-%E2%9C%94') - expect(subject).to have_anchor('utf-8-id-%e2%9c%94') - end - end - end - - describe '#directory' do - it 'returns base directory of a file' do - expect(subject.directory).to eq 'some' - end - end - - describe '.build' do - context 'when path does not lead to the HTML file' do - it 'builds an index.html page object' do - page = described_class.build('some/path') - - expect(page.file).to eq 'some/path/index.html' - end - end - - context 'when path leads to the HTML file' do - it 'builds an object representing that file' do - page = described_class.build('some/path/file.html') - - expect(page.file).to eq 'some/path/file.html' - end - end - end -end diff --git a/spec/gitlab/navigation/category_spec.rb b/spec/gitlab/navigation/category_spec.rb deleted file mode 100644 index f0424171..00000000 --- a/spec/gitlab/navigation/category_spec.rb +++ /dev/null @@ -1,85 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' -require 'gitlab/navigation/category' -require 'gitlab/navigation/doc' - -describe Gitlab::Navigation::Category do - subject(:category) { described_class.new(element) } - - let(:element) do - { - category_title: title, - external_url: external_url, - category_url: url, - ee_only: ee_only, - ee_tier: ee_tier, - docs: docs - } - end - - let(:title) { 'Title' } - let(:external_url) { 'http://example.com' } - let(:url) { 'README.html' } - let(:ee_only) { true } - let(:ee_tier) { 'GitLab Premium' } - let(:docs) { [{ doc_title: 'Doc Title' }] } - - describe '#title' do - subject { category.title } - - it { is_expected.to eq(title) } - end - - describe '#external_url' do - subject { category.external_url } - - it { is_expected.to eq(external_url) } - end - - describe '#url' do - subject { category.url } - - it { is_expected.to eq(url) } - end - - describe '#ee_only?' do - subject { category.ee_only? } - - it { is_expected.to eq(ee_only) } - end - - describe '#ee_tier' do - subject { category.ee_tier } - - it { is_expected.to eq(ee_tier) } - end - - describe '#has_children?' do - subject { category.has_children? } - - it { is_expected.to be_truthy } - - context 'when docs are empty' do - let(:docs) { [] } - - it { is_expected.to be_falsey } - end - end - - describe '#children' do - subject { category.children } - - it 'returns a list of children' do - children = subject - - expect(children.first.title).to eq('Doc Title') - end - - context 'when docs are empty' do - let(:docs) { [] } - - it { is_expected.to eq([]) } - end - end -end diff --git a/spec/gitlab/navigation/doc_spec.rb b/spec/gitlab/navigation/doc_spec.rb deleted file mode 100644 index 600edb44..00000000 --- a/spec/gitlab/navigation/doc_spec.rb +++ /dev/null @@ -1,84 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' -require 'gitlab/navigation/doc' - -describe Gitlab::Navigation::Doc do - subject(:doc) { described_class.new(element) } - - let(:element) do - { - doc_title: title, - external_url: external_url, - doc_url: url, - ee_only: ee_only, - ee_tier: ee_tier, - docs: docs - } - end - - let(:title) { 'Title' } - let(:external_url) { 'http://example.com' } - let(:url) { 'README.html' } - let(:ee_only) { true } - let(:ee_tier) { 'GitLab Premium' } - let(:docs) { [{ doc_title: 'Doc Title' }] } - - describe '#title' do - subject { doc.title } - - it { is_expected.to eq(title) } - end - - describe '#external_url' do - subject { doc.external_url } - - it { is_expected.to eq(external_url) } - end - - describe '#url' do - subject { doc.url } - - it { is_expected.to eq(url) } - end - - describe '#ee_only?' do - subject { doc.ee_only? } - - it { is_expected.to eq(ee_only) } - end - - describe '#ee_tier' do - subject { doc.ee_tier } - - it { is_expected.to eq(ee_tier) } - end - - describe '#has_children?' do - subject { doc.has_children? } - - it { is_expected.to be_truthy } - - context 'when docs are empty' do - let(:docs) { [] } - - it { is_expected.to be_falsey } - end - end - - describe '#children' do - subject { doc.children } - - it 'returns a list of children' do - children = subject - - expect(children.first.title).to eq('Doc Title') - end - - context 'when docs are empty' do - let(:docs) { [] } - - it { is_expected.to eq([]) } - end - end -end diff --git a/spec/gitlab/navigation/section_spec.rb b/spec/gitlab/navigation/section_spec.rb deleted file mode 100644 index f71f60f2..00000000 --- a/spec/gitlab/navigation/section_spec.rb +++ /dev/null @@ -1,77 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' -require 'gitlab/navigation/section' -require 'gitlab/navigation/category' - -describe Gitlab::Navigation::Section do - subject(:section) { described_class.new(element) } - - let(:element) do - { - section_title: title, - section_url: url, - ee_only: ee_only, - ee_tier: ee_tier, - section_categories: categories - } - end - - let(:title) { 'Title' } - let(:url) { 'README.html' } - let(:ee_only) { true } - let(:ee_tier) { 'GitLab Premium' } - let(:categories) { [{ category_title: 'Category Title' }] } - - describe '#title' do - subject { section.title } - - it { is_expected.to eq(title) } - end - - describe '#url' do - subject { section.url } - - it { is_expected.to eq(url) } - end - - describe '#ee_only?' do - subject { section.ee_only? } - - it { is_expected.to eq(ee_only) } - end - - describe '#ee_tier' do - subject { section.ee_tier } - - it { is_expected.to eq(ee_tier) } - end - - describe '#has_children?' do - subject { section.has_children? } - - it { is_expected.to be_truthy } - - context 'when categories are empty' do - let(:categories) { [] } - - it { is_expected.to be_falsey } - end - end - - describe '#children' do - subject { section.children } - - it 'returns a list of children' do - children = subject - - expect(children.first.title).to eq('Category Title') - end - - context 'when categories are empty' do - let(:categories) { [] } - - it { is_expected.to eq([]) } - end - end -end diff --git a/spec/gitlab/navigation_spec.rb b/spec/gitlab/navigation_spec.rb deleted file mode 100644 index 425a5428..00000000 --- a/spec/gitlab/navigation_spec.rb +++ /dev/null @@ -1,70 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -require 'gitlab/navigation' -require 'gitlab/navigation/section' - -describe Gitlab::Navigation do - subject(:navigation) { described_class.new(items, item) } - - let(:item) { instance_double(Nanoc::Core::CompilationItemView, path: '/omnibus/user/README.html', identifier: instance_double(Nanoc::Core::Identifier, to_s: '/omnibus/user/README.md')) } - let(:items) do - { - '/_data/navigation.yaml' => { sections: [Gitlab::Navigation::Section.new(section_title: 'Default Section')] } - } - end - - describe '#nav_items' do - subject { navigation.nav_items } - - context 'when yaml configuration for project does not exist' do - let(:item) { instance_double(Nanoc::Core::CompilationItemView, path: '/ee/user/README.html', identifier: instance_double(Nanoc::Core::Identifier, to_s: '/ee/user/README.md')) } - - it 'returns default sections' do - sections = subject[:sections] - section = sections.first - - expect(section.title).to eq('Default Section') - end - end - end - - describe '#element_href' do - subject { navigation.element_href(element) } - - let(:element) { Gitlab::Navigation::Section.new(section_url: url) } - let(:url) { 'omnibus/user/README.html' } - - it { is_expected.to eq('/omnibus/user/README.html') } - - context 'when yaml configuration for project does not exist' do - let(:item) { instance_double(Nanoc::Core::CompilationItemView, path: '/ee/user/README.html', identifier: instance_double(Nanoc::Core::Identifier, to_s: '/ee/user/README.md')) } - - it { is_expected.to eq('/omnibus/user/README.html') } - end - end - - describe '#show_element?' do - subject { navigation.show_element?(element) } - - let(:element) { Gitlab::Navigation::Section.new(section_url: url) } - let(:url) { 'omnibus/user/README.html' } - - it { is_expected.to be_truthy } - - context 'when url does not match item path' do - let(:url) { 'project/README.html' } - - it { is_expected.to be_falsey } - end - end - - describe '#id_for' do - subject { navigation.id_for(element) } - - let(:element) { Gitlab::Navigation::Section.new(section_title: 'Section Example') } - - it { is_expected.to eq 'SectionExample' } - end -end diff --git a/spec/helpers/edit_on_gitlab_spec.rb b/spec/helpers/edit_on_gitlab_spec.rb deleted file mode 100644 index a813e66c..00000000 --- a/spec/helpers/edit_on_gitlab_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -# 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/lib/gitlab/content_spec.rb b/spec/lib/gitlab/content_spec.rb new file mode 100644 index 00000000..c8538121 --- /dev/null +++ b/spec/lib/gitlab/content_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'spec_helper' + +# See: https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/4726#note_459473659 +describe 'Content directory size' do + subject { Dir.glob('content/**/*').sum { |f| File.size(f) } } + + let(:megabyte) { 1024**2 } + + # This limit can be increased after checking that Omnibus package build does not fail + let(:maximum_size) { 2 * megabyte } + + # `content` directory is included to the Omnibus package + # We want to make sure that the size of the directory is small enough + # to prevent accidental Omnibus pipeline failures. + it { is_expected.to be < maximum_size } +end diff --git a/spec/lib/gitlab/docs/link_spec.rb b/spec/lib/gitlab/docs/link_spec.rb new file mode 100644 index 00000000..4484936e --- /dev/null +++ b/spec/lib/gitlab/docs/link_spec.rb @@ -0,0 +1,148 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Docs::Link do + let(:page) { instance_double(Gitlab::Docs::Page) } + let(:href) { '../some/page.html#some-anchor' } + + subject { described_class.new(href, page) } + + describe '#to_anchor?' do + context 'when link contains anchor name' do + let(:href) { '../some/page.html' } + + it { is_expected.not_to be_to_anchor } + end + + context 'when link does not contain anchor name' do + let(:href) { '../some/page.html#some-anchor' } + + it { is_expected.to be_to_anchor } + end + + context 'when link contains an empty anchor' do + let(:href) { '../some/page.html#' } + + it { is_expected.not_to be_to_anchor } + end + end + + describe '#internal_anchor?' do + context 'when link contains internal anchor name' do + let(:href) { '#some-internal-anchor' } + + it { is_expected.to be_internal_anchor } + end + + context 'when link does not contain internal anchor' do + let(:href) { '../some/page.html#some-anchor' } + + it { is_expected.not_to be_internal_anchor } + end + end + + describe '#internal?' do + context 'when link is an external link' do + let(:href) { 'https://gitlab.com/some/page.html' } + + it { is_expected.not_to be_internal } + end + + context 'when link is an internal link' do + let(:href) { '../some/page.html' } + + it { is_expected.to be_internal } + end + end + + describe '#absolute_path' do + before do + allow(page).to receive(:directory).and_return('/my/path') + allow(Gitlab::Docs::Nanoc).to receive(:output_dir).and_return('/nanoc') + end + + context 'when link is an external link' do + let(:href) { 'https://gitlab.com/some/page.html' } + + it 'raises an error' do + expect { subject.absolute_path }.to raise_error(RuntimeError) + end + end + + context 'when link is relative' do + let(:href) { '../some/page.html' } + + it 'expands relative link' do + expect(subject.absolute_path).to eq '/my/some/page.html' + end + end + + context 'when link is absolute' do + let(:href) { '/some/page.html' } + + it 'expands absolute path' do + expect(subject.absolute_path).to eq '/nanoc/some/page.html' + end + end + end + + describe '#destination_page' do + context 'when the link is an internal link' do + let(:href) { '#some-anchor' } + + it 'returns the page it is assigned by itself' do + expect(subject.destination_page).to eq page + end + end + + context 'when the link is an external link' do + let(:href) { '../some/page.html#my-anchor' } + let(:destination) { instance_double(Gitlab::Docs::Page) } + + before do + allow(page).to receive(:directory).and_return('/my/docs/page') + end + + it 'builds a new page' do + expect(Gitlab::Docs::Page).to receive(:build) + .with('/my/docs/some/page.html') + .and_return(destination) + + expect(subject.destination_page).to eq destination + end + end + end + + describe '#source_file' do + it 'returns page file' do + expect(page).to receive(:file) + + subject.source_file + end + end + + describe '#destination_page_not_found?' do + before do + allow(page).to receive(:directory).and_return('/my/dir') + allow(subject).to receive(:destination_file) + .and_return(instance_double(Gitlab::Docs::Page, exists?: false)) + end + + it 'returns false if page does not exist' do + expect(subject.destination_page_not_found?).to be true + end + end + + describe '#destination_anchor_not_found?' do + before do + allow(page).to receive(:directory).and_return('/my/dir') + allow(subject).to receive(:destination_file) + .and_return(instance_double(Gitlab::Docs::Page, has_anchor?: false)) + end + + it 'returns false if anchor does not exist' do + expect(subject.destination_anchor_not_found?).to be true + end + end +end diff --git a/spec/lib/gitlab/docs/page_spec.rb b/spec/lib/gitlab/docs/page_spec.rb new file mode 100644 index 00000000..44454385 --- /dev/null +++ b/spec/lib/gitlab/docs/page_spec.rb @@ -0,0 +1,102 @@ +# frozen_string_literal: true + +require 'spec_helper' + +require 'nokogiri' +require 'gitlab/docs/page' +require 'gitlab/docs/link' +require 'gitlab/docs/element' +require 'gitlab/docs/document' + +describe Gitlab::Docs::Page do + subject do + described_class.new('some/file.html') + end + + context 'when file exists' do + before do + allow(File).to receive(:exist?) + .with('some/file.html') + .and_return(true) + + allow(File).to receive(:read) + .with('some/file.html') + .and_return <<~HTML + + + See external file + See internal section + See internal section broken + See utf-8 anchor + See utf-8 anchor 2 + +

Some section

+

Some section 2

+ + + HTML + end + + describe '#links' do + it 'collects links on a page' do + expect(subject.links.count).to eq 4 + end + end + + describe '#hrefs' do + it 'collects all hrefs' do + expect(subject.hrefs).to match_array %w[../link.html#my-anchor + #internal-anchor + #internal-anchor-2 + #utf-8-id-✔] + end + end + + describe '#ids' do + it 'collects all ids' do + expect(subject.ids).to match_array %w[internal-anchor + utf-8-id-✔] + end + end + + describe '#has_anchor?' do + it 'returns true when anchor exists on a page' do + expect(subject).to have_anchor('internal-anchor') + end + + it 'returns false when anchors does not exist' do + expect(subject).not_to have_anchor('internal-anchor-2') + end + + it 'returns true when UTF-8 encoded anchors are compared' do + expect(subject).to have_anchor('utf-8-id-✔') + expect(subject).to have_anchor('utf-8-id-%E2%9C%94') + expect(subject).to have_anchor('utf-8-id-%e2%9c%94') + end + end + end + + describe '#directory' do + it 'returns base directory of a file' do + expect(subject.directory).to eq 'some' + end + end + + describe '.build' do + context 'when path does not lead to the HTML file' do + it 'builds an index.html page object' do + page = described_class.build('some/path') + + expect(page.file).to eq 'some/path/index.html' + end + end + + context 'when path leads to the HTML file' do + it 'builds an object representing that file' do + page = described_class.build('some/path/file.html') + + expect(page.file).to eq 'some/path/file.html' + end + end + end +end diff --git a/spec/lib/gitlab/navigation/category_spec.rb b/spec/lib/gitlab/navigation/category_spec.rb new file mode 100644 index 00000000..f0424171 --- /dev/null +++ b/spec/lib/gitlab/navigation/category_spec.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'gitlab/navigation/category' +require 'gitlab/navigation/doc' + +describe Gitlab::Navigation::Category do + subject(:category) { described_class.new(element) } + + let(:element) do + { + category_title: title, + external_url: external_url, + category_url: url, + ee_only: ee_only, + ee_tier: ee_tier, + docs: docs + } + end + + let(:title) { 'Title' } + let(:external_url) { 'http://example.com' } + let(:url) { 'README.html' } + let(:ee_only) { true } + let(:ee_tier) { 'GitLab Premium' } + let(:docs) { [{ doc_title: 'Doc Title' }] } + + describe '#title' do + subject { category.title } + + it { is_expected.to eq(title) } + end + + describe '#external_url' do + subject { category.external_url } + + it { is_expected.to eq(external_url) } + end + + describe '#url' do + subject { category.url } + + it { is_expected.to eq(url) } + end + + describe '#ee_only?' do + subject { category.ee_only? } + + it { is_expected.to eq(ee_only) } + end + + describe '#ee_tier' do + subject { category.ee_tier } + + it { is_expected.to eq(ee_tier) } + end + + describe '#has_children?' do + subject { category.has_children? } + + it { is_expected.to be_truthy } + + context 'when docs are empty' do + let(:docs) { [] } + + it { is_expected.to be_falsey } + end + end + + describe '#children' do + subject { category.children } + + it 'returns a list of children' do + children = subject + + expect(children.first.title).to eq('Doc Title') + end + + context 'when docs are empty' do + let(:docs) { [] } + + it { is_expected.to eq([]) } + end + end +end diff --git a/spec/lib/gitlab/navigation/doc_spec.rb b/spec/lib/gitlab/navigation/doc_spec.rb new file mode 100644 index 00000000..600edb44 --- /dev/null +++ b/spec/lib/gitlab/navigation/doc_spec.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'gitlab/navigation/doc' + +describe Gitlab::Navigation::Doc do + subject(:doc) { described_class.new(element) } + + let(:element) do + { + doc_title: title, + external_url: external_url, + doc_url: url, + ee_only: ee_only, + ee_tier: ee_tier, + docs: docs + } + end + + let(:title) { 'Title' } + let(:external_url) { 'http://example.com' } + let(:url) { 'README.html' } + let(:ee_only) { true } + let(:ee_tier) { 'GitLab Premium' } + let(:docs) { [{ doc_title: 'Doc Title' }] } + + describe '#title' do + subject { doc.title } + + it { is_expected.to eq(title) } + end + + describe '#external_url' do + subject { doc.external_url } + + it { is_expected.to eq(external_url) } + end + + describe '#url' do + subject { doc.url } + + it { is_expected.to eq(url) } + end + + describe '#ee_only?' do + subject { doc.ee_only? } + + it { is_expected.to eq(ee_only) } + end + + describe '#ee_tier' do + subject { doc.ee_tier } + + it { is_expected.to eq(ee_tier) } + end + + describe '#has_children?' do + subject { doc.has_children? } + + it { is_expected.to be_truthy } + + context 'when docs are empty' do + let(:docs) { [] } + + it { is_expected.to be_falsey } + end + end + + describe '#children' do + subject { doc.children } + + it 'returns a list of children' do + children = subject + + expect(children.first.title).to eq('Doc Title') + end + + context 'when docs are empty' do + let(:docs) { [] } + + it { is_expected.to eq([]) } + end + end +end diff --git a/spec/lib/gitlab/navigation/section_spec.rb b/spec/lib/gitlab/navigation/section_spec.rb new file mode 100644 index 00000000..f71f60f2 --- /dev/null +++ b/spec/lib/gitlab/navigation/section_spec.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'gitlab/navigation/section' +require 'gitlab/navigation/category' + +describe Gitlab::Navigation::Section do + subject(:section) { described_class.new(element) } + + let(:element) do + { + section_title: title, + section_url: url, + ee_only: ee_only, + ee_tier: ee_tier, + section_categories: categories + } + end + + let(:title) { 'Title' } + let(:url) { 'README.html' } + let(:ee_only) { true } + let(:ee_tier) { 'GitLab Premium' } + let(:categories) { [{ category_title: 'Category Title' }] } + + describe '#title' do + subject { section.title } + + it { is_expected.to eq(title) } + end + + describe '#url' do + subject { section.url } + + it { is_expected.to eq(url) } + end + + describe '#ee_only?' do + subject { section.ee_only? } + + it { is_expected.to eq(ee_only) } + end + + describe '#ee_tier' do + subject { section.ee_tier } + + it { is_expected.to eq(ee_tier) } + end + + describe '#has_children?' do + subject { section.has_children? } + + it { is_expected.to be_truthy } + + context 'when categories are empty' do + let(:categories) { [] } + + it { is_expected.to be_falsey } + end + end + + describe '#children' do + subject { section.children } + + it 'returns a list of children' do + children = subject + + expect(children.first.title).to eq('Category Title') + end + + context 'when categories are empty' do + let(:categories) { [] } + + it { is_expected.to eq([]) } + end + end +end diff --git a/spec/lib/gitlab/navigation_spec.rb b/spec/lib/gitlab/navigation_spec.rb new file mode 100644 index 00000000..425a5428 --- /dev/null +++ b/spec/lib/gitlab/navigation_spec.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require 'spec_helper' + +require 'gitlab/navigation' +require 'gitlab/navigation/section' + +describe Gitlab::Navigation do + subject(:navigation) { described_class.new(items, item) } + + let(:item) { instance_double(Nanoc::Core::CompilationItemView, path: '/omnibus/user/README.html', identifier: instance_double(Nanoc::Core::Identifier, to_s: '/omnibus/user/README.md')) } + let(:items) do + { + '/_data/navigation.yaml' => { sections: [Gitlab::Navigation::Section.new(section_title: 'Default Section')] } + } + end + + describe '#nav_items' do + subject { navigation.nav_items } + + context 'when yaml configuration for project does not exist' do + let(:item) { instance_double(Nanoc::Core::CompilationItemView, path: '/ee/user/README.html', identifier: instance_double(Nanoc::Core::Identifier, to_s: '/ee/user/README.md')) } + + it 'returns default sections' do + sections = subject[:sections] + section = sections.first + + expect(section.title).to eq('Default Section') + end + end + end + + describe '#element_href' do + subject { navigation.element_href(element) } + + let(:element) { Gitlab::Navigation::Section.new(section_url: url) } + let(:url) { 'omnibus/user/README.html' } + + it { is_expected.to eq('/omnibus/user/README.html') } + + context 'when yaml configuration for project does not exist' do + let(:item) { instance_double(Nanoc::Core::CompilationItemView, path: '/ee/user/README.html', identifier: instance_double(Nanoc::Core::Identifier, to_s: '/ee/user/README.md')) } + + it { is_expected.to eq('/omnibus/user/README.html') } + end + end + + describe '#show_element?' do + subject { navigation.show_element?(element) } + + let(:element) { Gitlab::Navigation::Section.new(section_url: url) } + let(:url) { 'omnibus/user/README.html' } + + it { is_expected.to be_truthy } + + context 'when url does not match item path' do + let(:url) { 'project/README.html' } + + it { is_expected.to be_falsey } + end + end + + describe '#id_for' do + subject { navigation.id_for(element) } + + let(:element) { Gitlab::Navigation::Section.new(section_title: 'Section Example') } + + it { is_expected.to eq 'SectionExample' } + end +end diff --git a/spec/lib/helpers/edit_on_gitlab_spec.rb b/spec/lib/helpers/edit_on_gitlab_spec.rb new file mode 100644 index 00000000..a813e66c --- /dev/null +++ b/spec/lib/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 -- cgit v1.2.3