From 43b66db7bb5f0f195ac4969f42854cebdd1059ec Mon Sep 17 00:00:00 2001 From: Arkadiy Butermanov Date: Sun, 8 Apr 2018 20:42:16 +0300 Subject: [PATCH] Remove all windows-illegal characters from dirnames --- lib/rspec_api_documentation/views/markup_example.rb | 11 ++++++++--- spec/views/html_example_spec.rb | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/rspec_api_documentation/views/markup_example.rb b/lib/rspec_api_documentation/views/markup_example.rb index df2c1fdd..682075bf 100644 --- a/lib/rspec_api_documentation/views/markup_example.rb +++ b/lib/rspec_api_documentation/views/markup_example.rb @@ -3,6 +3,8 @@ module RspecApiDocumentation module Views class MarkupExample < Mustache + SPECIAL_CHARS = /[<>:"\/\\|?*]/.freeze + def initialize(example, configuration) @example = example @host = configuration.curl_host @@ -19,12 +21,11 @@ def respond_to?(method, include_private = false) end def dirname - resource_name.to_s.downcase.gsub(/\s+/, '_').gsub(":", "_") + sanitize(resource_name.to_s.downcase) end def filename - special_chars = /[<>:"\/\\|?*]/ - basename = description.downcase.gsub(/\s+/, '_').gsub(special_chars, '') + basename = sanitize(description.downcase) basename = Digest::MD5.new.update(description).to_s if basename.blank? "#{basename}.#{extension}" end @@ -87,6 +88,10 @@ def format_scope(unformatted_scope) def content_type(headers) headers && headers.fetch("Content-Type", nil) end + + def sanitize(name) + name.gsub(/\s+/, '_').gsub(SPECIAL_CHARS, '') + end end end end diff --git a/spec/views/html_example_spec.rb b/spec/views/html_example_spec.rb index 5f3bb1e3..55bb2e2f 100644 --- a/spec/views/html_example_spec.rb +++ b/spec/views/html_example_spec.rb @@ -28,6 +28,14 @@ end end + context "when resource name contains special characters for Windows OS" do + let(:metadata) { { :resource_name => 'foo<>:"/\|?*bar' } } + + it "removes them" do + expect(html_example.dirname).to eq("foobar") + end + end + describe "multi-character example name" do let(:metadata) { { :resource_name => "オーダ" } } let(:label) { "Coffee / Teaが順番で並んでいること" }