bad_encodings-ruby19 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ BadEncodings
2
+ ================
3
+
4
+ Small gem that tries to make the task of finding bad ruby encodings in your project a little easier. e.g. 'find_bad_encodings .'
5
+
6
+ It basically iterates over the regular 'ruby' files it can find and tries each line for 'valid_encoding' and then returns a list of all lines that failed.
7
+
8
+ Installation
9
+ =======
10
+
11
+ Install gem as normal
12
+
13
+ gem install adamsalter-bad_encodings-ruby19
14
+
15
+
16
+ Usage
17
+ -----
18
+
19
+ You can either use it as a binary:
20
+
21
+ ~/my_unencoded_dir/$ find_bad_encodings .
22
+ # => various wrongly encoded files.
23
+
24
+ or add it as a rake task to your (rails) project:
25
+
26
+ # put this in your project Rakefile
27
+ require 'bad_encodings/tasks'
28
+
29
+ ~/my_unencoded_project/$ rake find_bad_encodings
30
+
31
+
32
+
33
+ Known Bugs
34
+ ========
35
+
36
+ None. (well actually the detection of ruby encoding is pretty simple and probably not 100% accurate, certainly works for everything I've tried it on... patches welcome.)
37
+
38
+
39
+ Follow me on:
40
+ -------
41
+
42
+ > Twitter: [twitter.com/adamsalter](http://twitter.com/adamsalter)
43
+ > Github: [github.com/adamsalter](http://github.com/adamsalter)
44
+
45
+ Copyright (c) 2009 Adam @ [Codebright.net][cb], released under the MIT license
46
+
47
+ [cb]:http://codebright.net "http://codebright.net"
@@ -0,0 +1,14 @@
1
+ require 'rake/testtask'
2
+ require 'find'
3
+
4
+ desc 'Default: run unit tests.'
5
+ task :default => :test
6
+
7
+ desc 'Test ActiveScaffold.'
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'lib'
10
+ t.pattern = 'test/**/*_test.rb'
11
+ t.verbose = true
12
+ end
13
+
14
+ Dir['tasks/**/*.rake'].each { |t| load t }
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 2
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{bad_encodings-ruby19}
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Adam Salter"]
12
+ s.date = %q{2009-09-11}
13
+ s.default_executable = %q{find_bad_encodings}
14
+ s.description = %q{Small gem that tries to make the task of finding bad ruby encodings in your project a little easier.}
15
+ s.email = %q{adam@codebright.net}
16
+ s.executables = ["find_bad_encodings"]
17
+ s.extra_rdoc_files = [
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ "README.md",
22
+ "Rakefile",
23
+ "VERSION.yml",
24
+ "bad_encodings-ruby19.gemspec",
25
+ "bin/find_bad_encodings",
26
+ "lib/bad_encodings.rb",
27
+ "lib/bad_encodings/tasks.rb",
28
+ "tasks/gemspec.rake",
29
+ "test/bad_encodings/bad1.rb",
30
+ "test/bad_encodings/bad2.rb",
31
+ "test/bad_encodings/bad3.rb",
32
+ "test/bad_encodings/ok.rb",
33
+ "test/bad_encodings/ok.yml",
34
+ "test/bad_encodings_test.rb",
35
+ "test/test_helper.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/adamsalter/bad_encodings-ruby19}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.5}
41
+ s.summary = %q{Small gem that tries to make the task of finding bad ruby encodings in your project a little easier.}
42
+ s.test_files = [
43
+ "test/bad_encodings/bad1.rb",
44
+ "test/bad_encodings/bad2.rb",
45
+ "test/bad_encodings/bad3.rb",
46
+ "test/bad_encodings/ok.rb",
47
+ "test/bad_encodings_test.rb",
48
+ "test/test_helper.rb"
49
+ ]
50
+
51
+ if s.respond_to? :specification_version then
52
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
53
+ s.specification_version = 3
54
+
55
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
56
+ s.add_runtime_dependency(%q<optiflag>, ["~> 0.6.5"])
57
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
58
+ else
59
+ s.add_dependency(%q<optiflag>, ["~> 0.6.5"])
60
+ s.add_dependency(%q<shoulda>, [">= 0"])
61
+ end
62
+ else
63
+ s.add_dependency(%q<optiflag>, ["~> 0.6.5"])
64
+ s.add_dependency(%q<shoulda>, [">= 0"])
65
+ end
66
+ end
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+ require 'bad_encodings'
4
+
5
+ if ARGV[0] && File.exists?(ARGV[0])
6
+ dir = ARGV[0]
7
+ else
8
+ raise ArgumentError, "must supply a directory"
9
+ end
10
+
11
+ raise ArgumentError, "Requires Ruby 1.9" unless RUBY_VERSION > '1.9'
12
+ dir = File.expand_path(dir)
13
+ bad_lines = BadEncodings.find_lines_in_path(dir)
14
+ unless bad_lines.empty?
15
+ puts 'The following bad encodings were found:'
16
+ bad_lines.each do |line|
17
+ puts "[%s:%d]" % [line[0].sub(dir,ARGV[0]), line[1]]
18
+ end
19
+ else
20
+ puts 'No bad encodings found'
21
+ end
@@ -0,0 +1,68 @@
1
+ require 'find'
2
+
3
+ class BadEncodings
4
+ class << self
5
+ def find_lines_in_path(dir, includes = nil, dir_excludes = nil)
6
+ includes ||= /(\.rb|\.rake|\.haml|\.sass|\.erb)$/
7
+ files = []
8
+ Find.find(dir) do |path|
9
+ if FileTest.directory?(path)
10
+ if !dir_excludes.nil? && path =~ dir_excludes
11
+ Find.prune # Don't look any further into this directory.
12
+ else
13
+ next
14
+ end
15
+ elsif path =~ includes
16
+ files << path
17
+ end
18
+ end
19
+ find_lines_in_files(files.reverse)
20
+ end
21
+
22
+ def find_lines_in_files(files)
23
+ bad_lines = []
24
+ files.each do |file|
25
+ bad_lines += find_lines_in_file(file)
26
+ end
27
+ bad_lines
28
+ end
29
+
30
+ def find_lines_in_file(file)
31
+ bad_lines = []
32
+ file = File.open(file, "r:US-ASCII")
33
+ file.each_line do |line|
34
+ begin
35
+ if (file.lineno == 1 || file.lineno == 2) && line =~ /^#.*coding:\s([\w-]+)/
36
+ file.set_encoding($1)
37
+ end
38
+ rescue ArgumentError
39
+ # regex match will fail with 'invalid byte sequence in US-ASCII'
40
+ # if invalid byte sequence on first line of file
41
+ end
42
+ next if line.valid_encoding?
43
+ bad_lines << [file.path, file.lineno]
44
+ end
45
+ bad_lines
46
+ end
47
+
48
+ def get_rb_file_encoding(file_path)
49
+ file = File.open(file_path, 'r:US-ASCII')
50
+ begin
51
+ file.each_line do |line|
52
+ if (file.lineno == 1 || file.lineno == 2) && line =~ /^#.*coding:\s([\w-]+)/
53
+ return $1
54
+ elsif file.lineno > 2
55
+ return 'US-ASCII'
56
+ end
57
+ end
58
+ return 'US-ASCII'
59
+ rescue ArgumentError
60
+ # rescue invalid byte sequence on first line of file
61
+ return 'US-ASCII'
62
+ ensure
63
+ file.close
64
+ end
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ require 'bad_encodings'
3
+
4
+ desc "find all bad encodïngs in app"
5
+ task 'find_bad_encodings' => :environment do
6
+ raise ArgumentError, "Requires Ruby 1.9" unless RUBY_VERSION > '1.9'
7
+ dir = RAILS_ROOT
8
+ bad_lines = BadEncodings.find_in_path(dir)
9
+ unless bad_lines.empty?
10
+ puts 'The following bad encodings were found:'
11
+ bad_lines.each do |line|
12
+ puts "[%s:%d]" % [line[0].sub(dir,'RAILS_ROOT'), line[1]]
13
+ end
14
+ else
15
+ puts 'No bad encodings found'
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |s|
4
+ s.name = 'bad_encodings-ruby19'
5
+ s.executables = ['find_bad_encodings']
6
+ s.summary = 'Small gem that tries to make the task of finding bad ruby encodings in your project a little easier.'
7
+ s.email = 'adam@codebright.net'
8
+ s.homepage = 'http://github.com/adamsalter/bad_encodings-ruby19'
9
+ s.description = 'Small gem that tries to make the task of finding bad ruby encodings in your project a little easier.'
10
+ s.authors = ['Adam Salter']
11
+ s.files = FileList['*', '{bin,lib,test,tasks}/**/*']
12
+ s.add_dependency 'optiflag', '~> 0.6.5'
13
+ s.add_development_dependency 'shoulda'
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ # line1
2
+ # encoding: US-ASCII
3
+
4
+ puts "my bad encodïïïng - line 4"
@@ -0,0 +1,3 @@
1
+ # shoüld give error
2
+ # should not
3
+ # shöuld
@@ -0,0 +1,4 @@
1
+ # bad encoding lïne 1
2
+ # encoding: utf-8
3
+
4
+ # good encoding lïne 4
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+
3
+ # shoüld bé fïne
@@ -0,0 +1,4 @@
1
+ # shouldn't give error since yaml files are 'OK'
2
+
3
+ test:
4
+ tëst: 'here'
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class BadEncodingsTest < Test::Unit::TestCase
4
+ context 'A file with bad encodings' do
5
+ should 'fail' do
6
+ path = File.dirname(__FILE__)
7
+ bad_file = path + '/bad_encodings/bad1.rb'
8
+ bad_lines = BadEncodings.find_lines_in_file(bad_file)
9
+ assert_equal bad_lines, [[path+"/bad_encodings/bad1.rb", 4]]
10
+ end
11
+ end
12
+
13
+ context 'A path with bad encodings' do
14
+ should 'fail' do
15
+ path = File.dirname(__FILE__)
16
+ bad_lines = BadEncodings.find_lines_in_path(path)
17
+ puts bad_lines.inspect
18
+ assert_equal bad_lines, [[path+"/bad_encodings/bad1.rb", 4], [path+"/bad_encodings/bad2.rb", 1], [path+"/bad_encodings/bad2.rb", 3], [path+"/bad_encodings/bad3.rb", 1]]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ # require 'test/unit'
2
+ require 'shoulda'
3
+ require 'bad_encodings'
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bad_encodings-ruby19
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Adam Salter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-11 00:00:00 +10:00
13
+ default_executable: find_bad_encodings
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: optiflag
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.6.5
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: shoulda
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Small gem that tries to make the task of finding bad ruby encodings in your project a little easier.
36
+ email: adam@codebright.net
37
+ executables:
38
+ - find_bad_encodings
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.md
43
+ files:
44
+ - README.md
45
+ - Rakefile
46
+ - VERSION.yml
47
+ - bad_encodings-ruby19.gemspec
48
+ - bin/find_bad_encodings
49
+ - lib/bad_encodings.rb
50
+ - lib/bad_encodings/tasks.rb
51
+ - tasks/gemspec.rake
52
+ - test/bad_encodings/bad1.rb
53
+ - test/bad_encodings/bad2.rb
54
+ - test/bad_encodings/bad3.rb
55
+ - test/bad_encodings/ok.rb
56
+ - test/bad_encodings/ok.yml
57
+ - test/bad_encodings_test.rb
58
+ - test/test_helper.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/adamsalter/bad_encodings-ruby19
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.5
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Small gem that tries to make the task of finding bad ruby encodings in your project a little easier.
87
+ test_files:
88
+ - test/bad_encodings/bad1.rb
89
+ - test/bad_encodings/bad2.rb
90
+ - test/bad_encodings/bad3.rb
91
+ - test/bad_encodings/ok.rb
92
+ - test/bad_encodings_test.rb
93
+ - test/test_helper.rb