Back to Repositories

Testing Path Normalization Filter Implementation in DevDocs

This test suite validates the path normalization functionality in the DevDocs documentation system, focusing on URL handling and path transformations. It ensures proper handling of index pages, subpaths, and various URL formats for consistent documentation rendering.

Test Coverage Overview

The test suite provides comprehensive coverage of path normalization scenarios in the DevDocs documentation system.

Key areas tested include:
  • Root page path generation
  • Subpath handling and normalization
  • Store path generation for HTML files
  • URL rewriting for relative and absolute paths
  • Special URL handling (mailto, ftp)
  • Query string and fragment retention

Implementation Analysis

The testing approach utilizes RSpec’s behavior-driven development patterns with extensive use of stubs and mocks for isolation.

Technical implementation includes:
  • FilterTestHelper module integration
  • Minitest::Spec framework usage
  • Mock object patterns for dependency isolation
  • Hierarchical test organization with nested describes

Technical Details

Testing infrastructure:
  • Minitest as the primary testing framework
  • Custom FilterTestHelper for shared functionality
  • Mock/stub utilities for test isolation
  • Relative path resolution through test_helper

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through thorough edge case coverage and clean organization.

Notable practices include:
  • Isolated test cases with clear assertions
  • Comprehensive URL format handling
  • Consistent test naming conventions
  • Effective use of test hooks (before blocks)

freecodecamp/devdocs

test/lib/docs/filters/core/normalize_paths_test.rb

            
require_relative '../../../../test_helper'
require_relative '../../../../../lib/docs'

class NormalizePathsFilterTest < Minitest::Spec
  include FilterTestHelper
  self.filter_class = Docs::NormalizePathsFilter

  describe "#path" do
    it "returns 'index' when the page is the root page" do
      mock(filter).root_page? { true }
      assert_equal 'index', filter.path
    end

    it "returns 'test/index' when #subpath is 'test/'" do
      stub(filter).subpath { 'test/' }
      assert_equal 'test/index', filter.path
    end

    it "returns 'test' when #subpath is '/test'" do
      stub(filter).subpath { '/test' }
      assert_equal 'test', filter.path
    end
  end

  describe "#store_path" do
    it "returns 'index.html' when #path is 'index'" do
      stub(filter).path { 'index' }
      assert_equal 'index.html', filter.store_path
    end

    it "returns 'index.html' when #path is 'index.html'" do
      stub(filter).path { 'index.html' }
      assert_equal 'index.html', filter.store_path
    end

    it "returns 'page.ext.html' when #path is 'page.ext'" do
      stub(filter).path { 'page.ext' }
      assert_equal 'page.ext.html', filter.store_path
    end
  end

  describe "#normalize_path" do
    it "returns 'index' with '.'" do
      assert_equal 'index', filter.normalize_path('.')
    end

    it "returns 'test' with 'TEST'" do
      assert_equal 'test', filter.normalize_path('TEST')
    end

    it "returns 'test/index' with 'test/'" do
      assert_equal 'test/index', filter.normalize_path('test/')
    end

    it "returns 'test' with 'test.html'" do
      assert_equal 'test', filter.normalize_path('test.html')
    end
  end

  before do
    stub(filter).subpath { '' }
  end

  it "rewrites relative urls" do
    @body = link_to 'TEST/'
    assert_equal link_to('test/index'), filter_output_string
  end

  it "doesn't rewrite absolute urls" do
    @body = link_to 'http://example.com'
    assert_equal @body, filter_output_string
  end

  it "retains query strings" do
    @body = link_to 'TEST/?query'
    assert_equal link_to('test/index?query'), filter_output_string
  end

  it "retains fragments" do
    @body = link_to 'TEST/#frag'
    assert_equal link_to('test/index#frag'), filter_output_string
  end

  it "doesn't rewrite mailto urls" do
    @body = link_to 'mailto:'
    assert_equal @body, filter_output_string
  end

  it "doesn't rewrite ftp urls" do
    @body = link_to 'ftp://example.com'
    assert_equal @body, filter_output_string
  end

  it "doesn't rewrite invalid urls" do
    @body = link_to '.%'
    assert_equal @body, filter_output_string
  end
end