From 4241d44de87d8f04f593f683e913980bc746b3d6 Mon Sep 17 00:00:00 2001 From: Samantha McVey Date: Wed, 9 Nov 2016 18:06:38 -0800 Subject: [PATCH 1/4] Add support for checking YAML validity using Travis CI --- .travis.yml | 3 +++ Gemfile | 3 +++ Rakefile | 5 +++++ tests/yaml.rb | 21 +++++++++++++++++++++ 4 files changed, 32 insertions(+) create mode 100644 .travis.yml create mode 100644 Gemfile create mode 100644 Rakefile create mode 100644 tests/yaml.rb diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..ab89cb23 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: ruby +rvm: + - 2.2 diff --git a/Gemfile b/Gemfile new file mode 100644 index 00000000..eb73fd89 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +group :test do + gem 'rake' +end diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..49c90d5e --- /dev/null +++ b/Rakefile @@ -0,0 +1,5 @@ +task default: %w[test] + +task :test do + ruby "tests/yaml.rb" +end diff --git a/tests/yaml.rb b/tests/yaml.rb new file mode 100644 index 00000000..bf0c3797 --- /dev/null +++ b/tests/yaml.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env ruby +require 'yaml'; +$file_count = 0; +markdown_files = Dir["./**/*.html.markdown"] +markdown_files.each do |file| + begin + YAML.load_file(file) + $file_count = $file_count + 1 + rescue Exception => msg + puts msg + end +end +files_failed = markdown_files.length - $file_count +if files_failed != 0 + puts "FAILURE!!! #{files_failed} files were unable to be parsed!" + puts "Please check the YAML headers for the documents that failed!" + exit 1 +else + puts "Success. All #{$file_count} were checked" + exit 0 +end From 58ffb4057fcd67e5c0ba33f76344cc8ad927c72b Mon Sep 17 00:00:00 2001 From: Samantha McVey Date: Wed, 9 Nov 2016 20:24:10 -0800 Subject: [PATCH 2/4] Add UTF-8 check test --- Rakefile | 4 +++- tests/encoding.rb | 25 +++++++++++++++++++++++++ tests/yaml.rb | 22 +++++++++++----------- 3 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 tests/encoding.rb diff --git a/Rakefile b/Rakefile index 49c90d5e..b41beab5 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,7 @@ task default: %w[test] task :test do - ruby "tests/yaml.rb" + Dir["./tests/*.rb"].each do |test_file| + ruby test_file + end end diff --git a/tests/encoding.rb b/tests/encoding.rb new file mode 100644 index 00000000..c4d41d19 --- /dev/null +++ b/tests/encoding.rb @@ -0,0 +1,25 @@ +#!/usr/bin/env ruby +$file_count = 0; +markdown_files = Dir["./**/*.html.markdown"] +markdown_files.each do |file| + begin + file_bin = File.open(file, "rb") + contents = file_bin.read + if ! contents.valid_encoding? + puts "#{file} has an invalid encoding! Please save the file in UTF-8!" + else + $file_count = $file_count + 1 + end + rescue Exception => msg + puts msg + end +end +files_failed = markdown_files.length - $file_count +if files_failed != 0 + puts "FAILURE!!! #{files_failed} files were unable to be validated as UTF-8!" + puts "Please resave the file as UTF-8." + exit 1 +else + puts "Success. All #{$file_count} files passed UTF-8 validity checks" + exit 0 +end diff --git a/tests/yaml.rb b/tests/yaml.rb index bf0c3797..9f21016f 100644 --- a/tests/yaml.rb +++ b/tests/yaml.rb @@ -3,19 +3,19 @@ require 'yaml'; $file_count = 0; markdown_files = Dir["./**/*.html.markdown"] markdown_files.each do |file| - begin - YAML.load_file(file) - $file_count = $file_count + 1 - rescue Exception => msg - puts msg - end + begin + YAML.load_file(file) + $file_count = $file_count + 1 + rescue Exception => msg + puts msg + end end files_failed = markdown_files.length - $file_count if files_failed != 0 - puts "FAILURE!!! #{files_failed} files were unable to be parsed!" - puts "Please check the YAML headers for the documents that failed!" - exit 1 + puts "FAILURE!!! #{files_failed} files were unable to be parsed!" + puts "Please check the YAML headers for the documents that failed!" + exit 1 else - puts "Success. All #{$file_count} were checked" - exit 0 + puts "Success. All #{$file_count} files were verified valid YAML" + exit 0 end From 70d6977ccc07b667da0ed165b7706afbb5190816 Mon Sep 17 00:00:00 2001 From: Samantha McVey Date: Wed, 9 Nov 2016 20:57:13 -0800 Subject: [PATCH 3/4] Use charlock_holmes to do encoding detection. In my tests it has properly identified incorrect encodings that used to be present on older commits. This will help ensure this won't happen again, giving people instant feedback and allowing all pull requests to be checked --- Gemfile | 1 + Rakefile | 6 +++++- tests/encoding.rb | 16 ++++++++++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index eb73fd89..018aeb17 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ group :test do gem 'rake' + gem 'charlock_holmes' end diff --git a/Rakefile b/Rakefile index b41beab5..0193d816 100644 --- a/Rakefile +++ b/Rakefile @@ -2,6 +2,10 @@ task default: %w[test] task :test do Dir["./tests/*.rb"].each do |test_file| - ruby test_file + begin + ruby test_file + rescue + puts "FAILED #{test_file}!" + end end end diff --git a/tests/encoding.rb b/tests/encoding.rb index c4d41d19..ae7e495f 100644 --- a/tests/encoding.rb +++ b/tests/encoding.rb @@ -1,14 +1,18 @@ #!/usr/bin/env ruby +require 'charlock_holmes' $file_count = 0; markdown_files = Dir["./**/*.html.markdown"] markdown_files.each do |file| begin - file_bin = File.open(file, "rb") - contents = file_bin.read - if ! contents.valid_encoding? - puts "#{file} has an invalid encoding! Please save the file in UTF-8!" - else + contents = File.read(file) + detection = CharlockHolmes::EncodingDetector.detect(contents) + case detection[:encoding] + when 'UTF-8' $file_count = $file_count + 1 + when 'ISO-8859-1' + $file_count = $file_count + 1 + else + puts "#{file} was detected as #{detection[:encoding]} encoding! Please save the file in UTF-8!" end rescue Exception => msg puts msg @@ -20,6 +24,6 @@ if files_failed != 0 puts "Please resave the file as UTF-8." exit 1 else - puts "Success. All #{$file_count} files passed UTF-8 validity checks" + puts "Success. All #{$file_count} files Ruby's UTF-8 validity checks. This won't catch most problems." exit 0 end From 0a0acadf06661b004dd9bbf3797cdd3d15edab96 Mon Sep 17 00:00:00 2001 From: Samantha McVey Date: Wed, 9 Nov 2016 21:02:04 -0800 Subject: [PATCH 4/4] Add rubygems.org as gem source so Travis CI can use charlock_holmes --- Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile b/Gemfile index 018aeb17..c30a6497 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ +source 'http://rubygems.org' group :test do gem 'rake' gem 'charlock_holmes'