adamsalter-bad_encodings-ruby19 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = BadEncodings
2
+
3
+ http://github.com/adamsalter/bad_encodings-ruby19/tree/master
4
+
5
+ Small gem that tries to make the task of finding bad ruby encodings in your project a little easier. 'find_bad_encodings .'
data/Rakefile ADDED
@@ -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 }
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
@@ -0,0 +1,64 @@
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.0"
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-10}
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.rdoc"
19
+ ]
20
+ s.files = [
21
+ "README.rdoc",
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/ok.rb",
32
+ "test/bad_encodings/ok.yml",
33
+ "test/bad_encodings_test.rb",
34
+ "test/test_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/adamsalter/bad_encodings-ruby19}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.5}
40
+ s.summary = %q{Small gem that tries to make the task of finding bad ruby encodings in your project a little easier.}
41
+ s.test_files = [
42
+ "test/bad_encodings/bad1.rb",
43
+ "test/bad_encodings/bad2.rb",
44
+ "test/bad_encodings/ok.rb",
45
+ "test/bad_encodings_test.rb",
46
+ "test/test_helper.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ s.add_runtime_dependency(%q<optiflag>, ["~> 0.6.5"])
55
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<optiflag>, ["~> 0.6.5"])
58
+ s.add_dependency(%q<shoulda>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<optiflag>, ["~> 0.6.5"])
62
+ s.add_dependency(%q<shoulda>, [">= 0"])
63
+ end
64
+ end
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fileutils'
3
+ require '../lib/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,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,61 @@
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)
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_encoding = get_rb_file_encoding(file)
33
+ file = File.open(file, "r:#{file_encoding}")
34
+ file.each_line do |line|
35
+ next if line.valid_encoding?
36
+ bad_lines << [file.path, file.lineno]
37
+ end
38
+ bad_lines
39
+ end
40
+
41
+ def get_rb_file_encoding(file_path)
42
+ file = File.open(file_path, 'r:US-ASCII')
43
+ begin
44
+ file.each_line do |line|
45
+ if (file.lineno == 1 || file.lineno == 2) && line =~ /^#.*coding:\s([\w-]+)/
46
+ return $1
47
+ elsif file.lineno > 2
48
+ return 'US-ASCII'
49
+ end
50
+ end
51
+ return 'US-ASCII'
52
+ rescue ArgumentError
53
+ # rescue invalid byte sequence on first line of file
54
+ return 'US-ASCII'
55
+ ensure
56
+ file.close
57
+ end
58
+ end
59
+
60
+ end
61
+ 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,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,22 @@
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
+ bad_file = File.dirname(__FILE__) + '/bad_encodings/bad1.rb'
7
+ bad_lines = BadEncodings.find_lines_in_file(bad_file)
8
+ assert_equal bad_lines.class, Array
9
+ assert_equal bad_lines[0][1], 4
10
+ end
11
+ end
12
+
13
+ context 'A path with bad encodings' do
14
+ should 'fail' do
15
+ bad_lines = BadEncodings.find_lines_in_path(File.dirname(__FILE__))
16
+ assert_equal bad_lines.class, Array
17
+ assert_equal bad_lines[0][1], 1
18
+ assert_equal bad_lines[1][1], 3
19
+ assert_equal bad_lines[2][1], 4
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ # require 'test/unit'
2
+ require 'shoulda'
3
+ require 'bad_encodings'
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adamsalter-bad_encodings-ruby19
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Salter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-10 00:00:00 -07: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.rdoc
43
+ files:
44
+ - README.rdoc
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/ok.rb
55
+ - test/bad_encodings/ok.yml
56
+ - test/bad_encodings_test.rb
57
+ - test/test_helper.rb
58
+ has_rdoc: false
59
+ homepage: http://github.com/adamsalter/bad_encodings-ruby19
60
+ licenses:
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.5
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Small gem that tries to make the task of finding bad ruby encodings in your project a little easier.
85
+ test_files:
86
+ - test/bad_encodings/bad1.rb
87
+ - test/bad_encodings/bad2.rb
88
+ - test/bad_encodings/ok.rb
89
+ - test/bad_encodings_test.rb
90
+ - test/test_helper.rb