From 0639709e28c172e8a924c6d6b0fd263c6948d0ca Mon Sep 17 00:00:00 2001 From: Simon Neutert Date: Sun, 1 Sep 2024 14:13:43 +0200 Subject: [PATCH] rubocopping --- app.rb | 2 +- lib/file_operations/file_upload.rb | 2 +- .../files_sort_by_latest_modified.rb | 2 +- .../meta_data_param_deserializer.rb | 6 +- lib/helper/simple_uid_generator.rb | 2 +- test/default_memos_test.rb | 2 +- test/endpoints_api_v1_test.rb | 39 ++++++------ test/endpoints_get_test.rb | 17 +++-- test/meta_data_serializer_test.rb | 62 +++++++++---------- test/simple_uid_generator_test.rb | 2 +- 10 files changed, 74 insertions(+), 62 deletions(-) diff --git a/app.rb b/app.rb index 0494228..1696ee8 100644 --- a/app.rb +++ b/app.rb @@ -160,7 +160,7 @@ class App < Roda @media_files = Dir.glob(".#{@current_path_memo}/**") .filter { |filename| MEDIA_WHITELIST.any? { |t| filename.downcase.end_with?(t.downcase) } } - .reject { |filename| filename.end_with?('memo.md') || filename.end_with?('meta.yaml') } + .reject { |filename| filename.end_with?('memo.md', 'meta.yaml') } r.on 'destroy' do FileOperations::DeleteMemo.new(memo_path, @current_path_memo).run diff --git a/lib/file_operations/file_upload.rb b/lib/file_operations/file_upload.rb index f112977..e41c4a9 100644 --- a/lib/file_operations/file_upload.rb +++ b/lib/file_operations/file_upload.rb @@ -18,7 +18,7 @@ def store filename = @params_file_data[:filename].encode('utf-8', invalid: :replace, undef: :replace, replace: '_') validate!(filename) - pathy_filename = "#{Time.now.to_i}-#{filename}"[1..].gsub('/', '_').gsub(' ', '_') + pathy_filename = "#{Time.now.to_i}-#{filename}"[1..].tr('/', '_').tr(' ', '_') file_content = File.read(@params_file_data[:tempfile]) File.write("#{@root_path}#{@params_path}/#{pathy_filename}", diff --git a/lib/file_operations/files_sort_by_latest_modified.rb b/lib/file_operations/files_sort_by_latest_modified.rb index 45b14f6..44e78ad 100644 --- a/lib/file_operations/files_sort_by_latest_modified.rb +++ b/lib/file_operations/files_sort_by_latest_modified.rb @@ -28,7 +28,7 @@ def latest_n_memos_by_file_modified(num) # def all_files_order_from_least_to_newest_modified Dir.glob('memos/**/**') - .filter { |paths| paths.match(/\.md/) } + .filter { |paths| paths.include?('.md') } .sort_by { |f| File.mtime(f) } end end diff --git a/lib/file_operations/meta_data_param_deserializer.rb b/lib/file_operations/meta_data_param_deserializer.rb index 429fef2..5ec1991 100644 --- a/lib/file_operations/meta_data_param_deserializer.rb +++ b/lib/file_operations/meta_data_param_deserializer.rb @@ -18,7 +18,7 @@ def read(params) private def markdown_url_escape(url) - return url unless url.start_with?('[') + return url unless markdown_style_url?(url) begin url.split('](').last.split(')').first @@ -27,6 +27,10 @@ def markdown_url_escape(url) url end end + + def markdown_style_url?(url) + url.include?('](') && url.end_with?(')') + end end end end diff --git a/lib/helper/simple_uid_generator.rb b/lib/helper/simple_uid_generator.rb index 8ff9069..d3b7558 100644 --- a/lib/helper/simple_uid_generator.rb +++ b/lib/helper/simple_uid_generator.rb @@ -4,7 +4,7 @@ module Helper class SimpleUidGenerator class << self def generate - 8.times.map do + Array.new(8) do ('a'..'z').to_a.sample end.join.insert(4, '-') end diff --git a/test/default_memos_test.rb b/test/default_memos_test.rb index 6ff40ec..933785f 100644 --- a/test/default_memos_test.rb +++ b/test/default_memos_test.rb @@ -5,6 +5,6 @@ class TestMemosOnFilesystem < Minitest::Test def test_memos_default - assert_equal Dir.glob('.defaults/memos/**/*.md').size, 2 + assert_equal 2, Dir.glob('.defaults/memos/**/*.md').size end end diff --git a/test/endpoints_api_v1_test.rb b/test/endpoints_api_v1_test.rb index 6bd300c..17260bf 100644 --- a/test/endpoints_api_v1_test.rb +++ b/test/endpoints_api_v1_test.rb @@ -25,7 +25,8 @@ def test_memos_tantiny_reload post '/api/v1/memos/reload' json = JSON.parse(last_response.body) - assert_equal json['status'], 'success' + + assert_equal 'success', json['status'] end # rubocop:disable Layout/LineLength @@ -35,17 +36,17 @@ def test_memos_tantiny_search post('/api/v1/memos/search', { search: 'pug' }) json = JSON.parse(last_response.body) - assert_equal last_response.status, 200 + + assert_equal 200, last_response.status assert_kind_of Array, json - assert_equal json.size, 2 - assert_equal json.first.size, 3 + assert_equal 2, json.size + assert_equal 3, json.first.size first_result_triplet = json.first assert_equal '/memos/2021/08/21/hgfe-dcba', first_result_triplet.first assert_equal 'Facts about Pugs!', first_result_triplet[1] - assert_equal ["The Pug is a breed of dog originally from China, with physically distinctive features of a wrinkly, short-muzzled face and curled tail. The breed has a fine, glossy coat that comes in a variety of colors, most often light brown (fawn) or black, and a compact, square body with well developed and thick muscles all over the body.\r", - "![](/memos/2021/08/21/hgfe-dcba/665507228-pug.jpg)\r"], first_result_triplet.last + assert_equal ['The Pug is a breed of dog originally from China, with physically distinctive features of a wrinkly, short-muzzled face and curled tail. The breed has a fine, glossy coat that comes in a variety of colors, most often light brown (fawn) or black, and a compact, square body with well developed and thick muscles all over the body.', '![](/memos/2021/08/21/hgfe-dcba/665507228-pug.jpg)', '[Wikipedia](https://en.wikipedia.org/wiki/Pug)'], first_result_triplet.last assert_kind_of Array, first_result_triplet.last end # rubocop:enable Layout/LineLength @@ -56,34 +57,36 @@ def test_memos_tantiny_search_without_match post('/api/v1/memos/search', { search: 'pugxxx' }) json = JSON.parse(last_response.body) - assert_equal last_response.status, 200 + + assert_equal 200, last_response.status assert_kind_of Array, json - assert_equal json.size, 0 + assert_equal 0, json.size assert_nil json.first - assert json.empty? + assert_empty json end def test_memos_markdown_to_html_preview post('/api/v1/memos/preview', { md: '' }) json = JSON.parse(last_response.body) - assert_equal json['md'], '' + assert_equal '', json['md'] post('/api/v1/memos/preview', { md: '# Labradorite' }) json = JSON.parse(last_response.body) - assert_equal json['md'], "

Labradorite

\n" + assert_equal "

Labradorite

\n", json['md'] end # rubocop:disable Layout/LineLength def test_memos_update_memo - post('/api/v1/memos/2021/08/21/hgfe-dcba/update', { 'title' => 'Facts about Pugsies!', - 'tags' => 'dog,pug,pet', - 'content' => - "The Pug is a breed of dog originally from China, with physically distinctive features of a wrinkly, short-muzzled face and curled tail. The breed has a fine, glossy coat that comes in a variety of colors, most often light brown (fawn) or black, and a compact, square body with well developed and thick muscles all over the body.\r\n\r\n![](/memos/2021/08/21/hgfe-dcba/665507228-pug.jpg)\r\n\r\n## Second\r\n\r\nSecond paragraph with a link to a [GitHub](http://localhost:9292/memos/2022/09/26/abcd-efgh/edit) repository.\r\n\r\n### Thirds\r\n\r\nhere are some links to help you\r\n\r\nhttp://localhost:9292/memos/2022/09/25/abcd-efgh/edit\r\n\r\nhttp://localhost:9292/memos/2022/09/24/abcd-efgh/edit\r\n" }) - - assert_equal last_response.status, 200 - assert_equal JSON.parse(last_response.body)['status'], 'success' + post('/api/v1/memos/2021/08/21/hgfe-dcba/update', { + 'title' => 'Facts about Pugsies!', + 'tags' => 'dog,pug,pet', + 'content' => "The Pug is a breed of dog originally from China, with physically distinctive features of a wrinkly, short-muzzled face and curled tail. The breed has a fine, glossy coat that comes in a variety of colors, most often light brown (fawn) or black, and a compact, square body with well developed and thick muscles all over the body.\r\n\r\n![](/memos/2021/08/21/hgfe-dcba/665507228-pug.jpg)\r\n\r\n## Second\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nSecond paragraph with a link to a [GitHub](http://localhost:9292/memos/2022/09/26/abcd-efgh/edit) repository.\r\n\r\n### Thirds\r\n\r\nhere are some links to help you\r\n\r\nhttp://localhost:9292/memos/2022/09/25/abcd-efgh/edit\r\n\r\nhttp://localhost:9292/memos/2022/09/24/abcd-efgh/edit\r\n" + }) + + assert_equal 200, last_response.status + assert_equal 'success', JSON.parse(last_response.body)['status'] end # rubocop:enable Layout/LineLength end diff --git a/test/endpoints_get_test.rb b/test/endpoints_get_test.rb index 21876e9..5ed30fb 100644 --- a/test/endpoints_get_test.rb +++ b/test/endpoints_get_test.rb @@ -23,27 +23,32 @@ def after_teardown def test_index_redirect get '/' - assert_equal last_response.status, 302 + + assert_equal 302, last_response.status end def test_memos_index get '/memos' - assert_equal last_response.status, 200 + + assert_equal 200, last_response.status end def test_memos_show get '/memos/2021/08/21/hgfe-dcba' - assert_equal last_response.status, 200 + + assert_equal 200, last_response.status end def test_memos_edit get '/memos/2021/08/21/hgfe-dcba/edit' - assert_equal last_response.status, 200 + + assert_equal 200, last_response.status end def test_memos_delete_or_destroy get '/memos/2021/08/21/hgfe-dcba/destroy' - assert_equal last_response.status, 302 - assert_equal Dir.glob('memos/**/*/').size, 6 + + assert_equal 302, last_response.status + assert_equal 6, Dir.glob('memos/**/*/').size end end diff --git a/test/meta_data_serializer_test.rb b/test/meta_data_serializer_test.rb index 6f11cd0..4986fe4 100644 --- a/test/meta_data_serializer_test.rb +++ b/test/meta_data_serializer_test.rb @@ -33,43 +33,43 @@ def after_teardown end def test_meta_struct - assert(@yaml_data_as_struct.public_methods.include?(:id)) - assert(@yaml_data_as_struct.public_methods.include?(:title)) - assert(@yaml_data_as_struct.public_methods.include?(:tags)) - assert(@yaml_data_as_struct.public_methods.include?(:urls)) - assert(@yaml_data_as_struct.public_methods.include?(:updated_at)) - assert(@yaml_data_as_struct.is_a?(FileOperations::MetaStruct)) - assert_equal(@yaml_data_as_struct.id, '2021/08/21/hgfe-dcba') - assert_equal(@yaml_data_as_struct.title, 'Facts about Pugs!') - assert_equal(@yaml_data_as_struct.tags, 'dog,pug,pet') - assert(@yaml_data_as_struct.urls.is_a?(Array)) - assert_equal(@yaml_data_as_struct.urls.first, 'http://localhost:9292/memos/2022/09/26/abcd-efgh/edit') - assert(@yaml_data_as_struct.updated_at.is_a?(DateTime)) + assert_includes(@yaml_data_as_struct.public_methods, :id) + assert_includes(@yaml_data_as_struct.public_methods, :title) + assert_includes(@yaml_data_as_struct.public_methods, :tags) + assert_includes(@yaml_data_as_struct.public_methods, :urls) + assert_includes(@yaml_data_as_struct.public_methods, :updated_at) + assert_kind_of(FileOperations::MetaStruct, @yaml_data_as_struct) + assert_equal('2021/08/21/hgfe-dcba', @yaml_data_as_struct.id) + assert_equal('Facts about Pugs!', @yaml_data_as_struct.title) + assert_equal('dog,pug,pet', @yaml_data_as_struct.tags) + assert_kind_of(Array, @yaml_data_as_struct.urls) + assert_equal('http://localhost:9292/memos/2022/09/26/abcd-efgh/edit', @yaml_data_as_struct.urls.first) + assert_kind_of(DateTime, @yaml_data_as_struct.updated_at) end def test_yaml_serialiazation_from_string - assert(@yaml_data_from_string.is_a?(Hash)) + assert_kind_of(Hash, @yaml_data_from_string) assert(@yaml_data_from_string['id'], '2021/08/21/hgfe-dcba') - assert_equal(@yaml_data_from_string['title'], 'Facts about Pugs!') - assert_equal(@yaml_data_from_string['tags'], 'dog,pug,pet') - assert(@yaml_data_from_string['urls'].is_a?(Array)) - assert_equal(@yaml_data_from_string['urls'].first, 'http://localhost:9292/memos/2022/09/26/abcd-efgh/edit') - assert(@yaml_data_from_string['updated_at'].is_a?(DateTime)) - assert_equal(@yaml_data_from_string['updated_at'].year, 2021) - assert_equal(@yaml_data_from_string['updated_at'].month, 8) - assert_equal(@yaml_data_from_string['updated_at'].day, 21) + assert_equal('Facts about Pugs!', @yaml_data_from_string['title']) + assert_equal('dog,pug,pet', @yaml_data_from_string['tags']) + assert_kind_of(Array, @yaml_data_from_string['urls']) + assert_equal('http://localhost:9292/memos/2022/09/26/abcd-efgh/edit', @yaml_data_from_string['urls'].first) + assert_kind_of(DateTime, @yaml_data_from_string['updated_at']) + assert_equal(2021, @yaml_data_from_string['updated_at'].year) + assert_equal(8, @yaml_data_from_string['updated_at'].month) + assert_equal(21, @yaml_data_from_string['updated_at'].day) end def test_yaml_serialiazation_from_file - assert(@yaml_data_from_demo_file.is_a?(Hash)) - assert_equal(@yaml_data_from_demo_file['id'], '2022/09/26/abcd-efgh') - assert_equal(@yaml_data_from_demo_file['title'], 'All about Pugs!!!') - assert_equal(@yaml_data_from_demo_file['tags'], 'dog,pug,pet') - assert(@yaml_data_from_demo_file['urls'].is_a?(Array)) - assert(@yaml_data_from_demo_file['urls'].empty?) - assert(@yaml_data_from_demo_file['updated_at'].is_a?(DateTime)) - assert_equal(@yaml_data_from_demo_file['updated_at'].year, 2022) - assert_equal(@yaml_data_from_demo_file['updated_at'].month, 9) - assert_equal(@yaml_data_from_demo_file['updated_at'].day, 29) + assert_kind_of(Hash, @yaml_data_from_demo_file) + assert_equal('2022/09/26/abcd-efgh', @yaml_data_from_demo_file['id']) + assert_equal('All about Pugs!!!', @yaml_data_from_demo_file['title']) + assert_equal('dog,pug,pet', @yaml_data_from_demo_file['tags']) + assert_kind_of(Array, @yaml_data_from_demo_file['urls']) + assert_empty(@yaml_data_from_demo_file['urls']) + assert_kind_of(DateTime, @yaml_data_from_demo_file['updated_at']) + assert_equal(2022, @yaml_data_from_demo_file['updated_at'].year) + assert_equal(9, @yaml_data_from_demo_file['updated_at'].month) + assert_equal(29, @yaml_data_from_demo_file['updated_at'].day) end end diff --git a/test/simple_uid_generator_test.rb b/test/simple_uid_generator_test.rb index 000a559..d719e06 100644 --- a/test/simple_uid_generator_test.rb +++ b/test/simple_uid_generator_test.rb @@ -9,6 +9,6 @@ def setup end def test_uid_pattern - assert @simple_uid.match?(/^\w{4}-\w{4}$/) + assert_match(/^\w{4}-\w{4}$/, @simple_uid) end end