discover_tabs 0.5 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f2c20999e5198d37a1ebb13f5081eb2fd86f203
4
- data.tar.gz: 0334b4fb74dce96269f5ce0b1c3c99a9278379bd
3
+ metadata.gz: e13fe304b1c8b92808bcd20b17a994774143f4b1
4
+ data.tar.gz: 1e649395b0c028c1470f383348e7e73c6ef0a669
5
5
  SHA512:
6
- metadata.gz: ea0b7aeb673d91868f75f2475f1afc5a2c9797bf50c4447ecc3f6eec2ff5d63a54e88700351823288b5785f3112a2923180bcf5ef14e12733432f9f6f80a7eca
7
- data.tar.gz: da59df03c9963dc13a94b99fbbbf4a7b8d8cc92f105d7692909d077862c7a777bb8462eda68e3a8830b610112d157b124c75094b4f2e48d598420dbf1a461c62
6
+ metadata.gz: b673f98cdbf7819267b04701e2174d352209d9ca704bef66e4f92a98090436dcbfca5d2ecd9387bc53f3ba42efa6f957f9e98ba97f5af886a5a6223204135181
7
+ data.tar.gz: 086de983bfc6e27dbba21af102424313903470c935fbf58a077b9cf042c74d48336a3d568a0db8f9c837300a5913a73d80b8102a3333e3118ddaf3d3699231e9
data/README.md CHANGED
@@ -1,4 +1,23 @@
1
1
  discover_tabs
2
2
  =============
3
3
 
4
- Discover which files use tabs
4
+ Discover which files use tabs (and potentially replace them with spaces if you like)
5
+
6
+ ### Usage
7
+
8
+ After installation, there should be a command line app called "discover_tabs" on your path.
9
+
10
+ usage: discover_tabs [options] [filename|directory]
11
+ -r N Replace tabs intending with N spaces
12
+ -h, --help Show this message
13
+
14
+ It will output all of the files that have indentation with tabs. The -r options will replace them with spaces.
15
+
16
+ examples:
17
+
18
+ * discover_tabs *.cpp
19
+ * discover_tabs *.cpp -r4
20
+
21
+ ### Installation
22
+
23
+ gem discover_tabs
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ require File.expand_path('../lib/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'discover_tabs'
6
+ gem.version = DiscoverTabs::VERSION
7
+ gem.date = Date.today.to_s
8
+ gem.license = 'MIT'
9
+ gem.executables = [ "discover_tabs" ]
10
+
11
+ gem.summary = "Discover which files are tab indented"
12
+ gem.description = "Library and Command line for discovering files that contain tab indentation"
13
+
14
+ gem.authors = ['Bas Vodde']
15
+ gem.email = 'basv@odd-e.com'
16
+ gem.homepage = 'https://github.com/basvodde/discover_tabs'
17
+
18
+ gem.add_runtime_dependency( 'rake', '~> 0')
19
+ gem.add_runtime_dependency( 'ptools', '~> 1.1')
20
+ gem.add_development_dependency('rspec', '~> 2.0', '>= 2.0.0')
21
+
22
+ gem.files = `git ls-files -- {.,test,spec,lib,bin}/*`.split("\n")
23
+ end
@@ -1,45 +1,112 @@
1
1
  require 'pathname'
2
2
  require 'ptools'
3
+ require 'optparse'
3
4
 
4
5
  module DiscoverTabs
5
6
 
6
- def self.in_dir(dir, pattern = "*")
7
- files_with_tabs = false
8
- Dir["#{dir}/**/#{pattern}"].each do |file|
9
- begin
10
- has_tabs = false
11
- unless File.directory?(file) || File.binary?(file)
12
- has_tabs = in_file(file)
7
+ class << self
8
+
9
+ def files_with_tab_indenting(full_path)
10
+ filename = File.basename(full_path)
11
+ dirname = File.dirname(full_path)
12
+
13
+ files_with_tabs = []
14
+ if File.directory?(full_path)
15
+ files_with_tabs += files_with_tab_indenting_in_dir(full_path)
16
+ elsif filename.include?("*")
17
+ files_with_tabs += files_with_tab_indenting_in_dir(dirname, filename)
18
+ else
19
+ files_with_tabs << full_path if file_has_tabs_indenting?(full_path)
20
+ end
21
+ files_with_tabs
22
+ end
23
+
24
+ def files_with_tab_indenting_in_dir(dir, pattern = "*")
25
+ files_with_tabs = []
26
+ Dir["#{dir}/**/#{pattern}"].each do |file|
27
+ begin
28
+ unless File.directory?(file) || File.binary?(file)
29
+ files_with_tabs << file if file_has_tabs_indenting?(file)
30
+ end
31
+ rescue ArgumentError
13
32
  end
14
- rescue ArgumentError
15
33
  end
16
- files_with_tabs ||= has_tabs
34
+ files_with_tabs
17
35
  end
18
- files_with_tabs
19
- end
20
36
 
21
- def self.in_file(filename)
22
- File.readlines(filename).each do |line|
23
- if line =~ /^\t+/
24
- puts Pathname.new(filename).cleanpath.to_s
25
- return true
37
+ def file_has_tabs_indenting?(filename)
38
+ File.readlines(filename).each do |line|
39
+ return true if line =~ /^\t+/
26
40
  end
41
+ false
27
42
  end
28
- false
29
- end
30
43
 
31
- def self.cmdline_run(argv)
32
- full_path = argv[0] || "."
33
- filename = File.basename(full_path)
34
- dirname = File.dirname(full_path)
44
+ def parse_argv(argv)
45
+ options = {}
46
+
47
+ opt_parser = OptionParser.new do |opts|
48
+ opts.banner = "usage: discover_tabs [options] [filename|directory]"
49
+
50
+ opts.on("-r N", Integer, "Replace tabs intending with N spaces") do |n|
51
+ options[:replace_tabs] = n
52
+ end
53
+
54
+ opts.on_tail("-h", "--help", "Show this message") do
55
+ puts opts.to_s
56
+ exit(0)
57
+ end
58
+ end
59
+ opt_parser.parse!(argv)
60
+
61
+ options[:files] = argv
62
+ options[:files] = ["."] if argv.empty?
63
+
64
+
65
+ options
66
+ rescue OptionParser::InvalidOption => error
67
+ puts error.message
68
+ exit 1
69
+ end
70
+
71
+ def cmdline_run(argv)
72
+ options = parse_argv(argv)
73
+
74
+ files_with_tabs = []
75
+ options[:files].each do |full_path|
76
+ files_with_tabs += files_with_tab_indenting(full_path)
77
+ end
78
+
79
+ files_with_tabs.each { |file| puts Pathname.new(file).cleanpath.to_s }
80
+
81
+ if options[:replace_tabs]
82
+ replace_tabs_in_files(files_with_tabs, options[:replace_tabs])
83
+ end
84
+
85
+ files_with_tabs.empty? ? 1 : 0
86
+ end
87
+
88
+ def replace_tabs_in_files(files, amount_of_spaces_per_tab)
89
+ files.each do |file|
90
+ content = File.read(file)
91
+ new_content = replace_tabs_with_spaces(content, amount_of_spaces_per_tab)
92
+ File.write(file, new_content)
93
+ end
94
+ end
35
95
 
36
- found_tabs = if File.directory?(full_path)
37
- in_dir(full_path)
38
- elsif filename.include?("*")
39
- in_dir(dirname, filename)
40
- else
41
- in_file(full_path)
96
+ def replace_tabs_with_spaces(content, amount_of_spaces_per_tab)
97
+ new_content = ""
98
+ content.lines.each { |line| new_content += replace_tabs_with_spaces_on_line(line, amount_of_spaces_per_tab)}
99
+ new_content
100
+ end
101
+
102
+ def replace_tabs_with_spaces_on_line(string, amount_of_spaces_per_tab)
103
+ new_string = string
104
+ if string =~ /^(\t+)/
105
+ replacement_spaces = " " * amount_of_spaces_per_tab * $1.size
106
+ new_string = string.gsub(/^#{$1}/, replacement_spaces)
107
+ end
108
+ new_string
42
109
  end
43
- found_tabs ? 0 : 1
44
110
  end
45
111
  end
112
+
@@ -1,4 +1,4 @@
1
1
 
2
2
  module DiscoverTabs
3
- VERSION = "0.5"
3
+ VERSION = "0.7"
4
4
  end
@@ -2,67 +2,133 @@ require 'spec_helper.rb'
2
2
 
3
3
  describe "It can discover tabs" do
4
4
 
5
- it "Can discover tabs in one file" do
6
- DiscoverTabs.should_receive(:puts).with(/file_with_tabs.rb/)
7
- DiscoverTabs.in_file(test_data("file_with_tabs.rb")).should== true
8
- end
5
+ context "Discovering files with tab indentation" do
9
6
 
10
- it "Won't discover tabs in one file without tabs" do
11
- DiscoverTabs.should_not_receive(:puts)
12
- DiscoverTabs.in_file(test_data("file_without_tabs.rb")).should== false
13
- end
7
+ it "Can discover tabs in one file" do
8
+ DiscoverTabs.file_has_tabs_indenting?(test_data("file_with_tabs.rb")).should== true
9
+ end
14
10
 
15
- it "Won't discover any files with tabs in a directory when there are none" do
16
- DiscoverTabs.should_not_receive(:puts)
17
- DiscoverTabs.in_dir(test_data("dir_without_tabbled_files")).should== false
18
- end
11
+ it "Won't discover tabs in one file without tabs" do
12
+ DiscoverTabs.file_has_tabs_indenting?(test_data("file_without_tabs.rb")).should== false
13
+ end
19
14
 
20
- it "Won't discover any files with tabs in a directory when there are none" do
21
- DiscoverTabs.should_receive(:puts).with(/dir_with_some_tabbed_files\/1.rb/)
22
- DiscoverTabs.should_receive(:puts).with(/dir_with_some_tabbed_files\/8\/1.rb/)
23
- DiscoverTabs.should_receive(:puts).with(/dir_with_some_tabbed_files\/dir\/4.rb/)
24
- DiscoverTabs.in_dir(test_data("dir_with_some_tabbed_files")).should== true
25
- end
15
+ it "Won't discover any files with tabs in a directory when there are none" do
16
+ DiscoverTabs.files_with_tab_indenting_in_dir(test_data("dir_without_tabbled_files")).should be_empty
17
+ end
26
18
 
27
- it "Can deal with the current dir" do
28
- DiscoverTabs.should_receive(:puts).at_least(1).times
29
- DiscoverTabs.in_dir(".").should== true
30
- end
19
+ it "Won't discover any files with tabs in a directory when there are none" do
20
+ files_with_tabs = DiscoverTabs.files_with_tab_indenting_in_dir(test_data("dir_with_some_tabbed_files"))
21
+ files_with_tabs[0].should== test_data("dir_with_some_tabbed_files/1.rb")
22
+ files_with_tabs[1].should== test_data("dir_with_some_tabbed_files/8/1.rb")
23
+ files_with_tabs[2].should== test_data("dir_with_some_tabbed_files/dir/4.rb")
24
+ files_with_tabs.size.should== 3
25
+ end
31
26
 
32
- it "Should swallow ArgumentError expections" do
33
- DiscoverTabs.stub(:in_file).and_raise(ArgumentError)
34
- DiscoverTabs.in_dir(".").should== false
35
- end
27
+ it "Can deal with the current dir" do
28
+ DiscoverTabs.files_with_tab_indenting_in_dir(".").should_not be_empty
29
+ end
36
30
 
37
- it "Makes sure the pathname is clean" do
38
- DiscoverTabs.should_receive(:puts).with(/dir_with_some_tabbed_files_and_different_extensions\/tabs.rb/)
39
- DiscoverTabs.should_receive(:puts)
40
- DiscoverTabs.in_dir(test_data("dir_with_some_tabbed_files_and_different_extensions/")).should== true
41
- end
31
+ it "Should swallow ArgumentError expections" do
32
+ DiscoverTabs.stub(:file_has_tabs_indenting?).and_raise(ArgumentError)
33
+ DiscoverTabs.files_with_tab_indenting_in_dir(".").should== []
34
+ end
42
35
 
43
- it "will only find the files with a certain extension" do
44
- DiscoverTabs.should_receive(:puts).with(/dir_with_some_tabbed_files_and_different_extensions\/tabs.rb/)
45
- DiscoverTabs.should_not_receive(:puts).with(/dir_with_some_tabbed_files_and_different_extensions\tabs.cpp/)
46
- DiscoverTabs.in_dir(test_data("dir_with_some_tabbed_files_and_different_extensions"), "*.rb").should== true
47
- end
36
+ it "will only find the files with a certain extension" do
37
+ files_with_tabs = DiscoverTabs.files_with_tab_indenting_in_dir(test_data("dir_with_some_tabbed_files_and_different_extensions"), "*rb")
38
+ files_with_tabs.size.should== 1
39
+ files_with_tabs[0].should== test_data("dir_with_some_tabbed_files_and_different_extensions/tabs.rb")
40
+ end
41
+
42
+ it "will discover tabs in a file when a filename it passed (success)" do
43
+ DiscoverTabs.should_receive(:file_has_tabs_indenting?).with(test_data("file_with_tabs.rb")).and_return(true)
44
+ DiscoverTabs.files_with_tab_indenting(test_data("file_with_tabs.rb")).should== [test_data("file_with_tabs.rb")]
45
+ end
48
46
 
49
- it "will discover tabs in a file when a filename it passed (success)" do
50
- DiscoverTabs.should_receive(:in_file).with(test_data("file_with_tabs.rb")).and_return(true)
51
- DiscoverTabs.cmdline_run([ test_data("file_with_tabs.rb") ]).should== 0
47
+ it "will discover tabs in a dir when a directory it passed (failed)" do
48
+ DiscoverTabs.should_receive(:files_with_tab_indenting_in_dir).with(test_data("dir_with_some_tabbed_files")).and_return([])
49
+ DiscoverTabs.files_with_tab_indenting(test_data("dir_with_some_tabbed_files")).should== []
50
+ end
51
+
52
+ it "will discover a pattern (based on having an * in the filename) and it passes the pattern (success)" do
53
+ DiscoverTabs.should_receive(:files_with_tab_indenting_in_dir).with(test_data("dir_with_some_tabbed_files"), "*").and_return(["file"])
54
+ DiscoverTabs.files_with_tab_indenting(test_data("dir_with_some_tabbed_files/*")).should== ["file"]
55
+ end
52
56
  end
53
57
 
54
- it "will discover tabs in a dir when a directory it passed (failed)" do
55
- DiscoverTabs.should_receive(:in_dir).with(test_data("dir_with_some_tabbed_files")).and_return(false)
56
- DiscoverTabs.cmdline_run([ test_data("dir_with_some_tabbed_files") ]).should== 1
58
+ context "tab replacement" do
59
+
60
+ it "will return the same line if there is no tab on the line" do
61
+ DiscoverTabs.replace_tabs_with_spaces_on_line("string", 4).should== "string"
62
+ end
63
+
64
+ it "will convert one tab at the beginning to spaces" do
65
+ DiscoverTabs.replace_tabs_with_spaces_on_line("\tstring", 2).should== " string"
66
+ end
67
+
68
+ it "will convert multiple tabs at the beginning to spaces" do
69
+ DiscoverTabs.replace_tabs_with_spaces_on_line("\t\t\tstring", 3).should== " string"
70
+ end
71
+
72
+ it "will not convert tabs that are not at the beginning" do
73
+ DiscoverTabs.replace_tabs_with_spaces_on_line(" \tstri\tng\t", 3).should== " \tstri\tng\t"
74
+ end
75
+
76
+ it "will replace all tabs in a multi-line file" do
77
+ DiscoverTabs.replace_tabs_with_spaces("\t\thello\nworld\n\t", 2).should== " hello\nworld\n "
78
+ end
79
+
80
+ it "will read and re-write the files which it replaces spaces" do
81
+ File.should_receive(:read).with("file").and_return("content")
82
+ DiscoverTabs.should_receive(:replace_tabs_with_spaces).with("content", 3).and_return("new_content")
83
+ File.should_receive(:write).with("file", "new_content")
84
+ DiscoverTabs.replace_tabs_in_files(["file"], 3)
85
+ end
57
86
  end
58
87
 
59
- it "will discover a pattern (based on having an * in the filename) and it passes the pattern (success)" do
60
- DiscoverTabs.should_receive(:in_dir).with(test_data("dir_with_some_tabbed_files"), "*").and_return(true)
61
- DiscoverTabs.cmdline_run([ test_data("dir_with_some_tabbed_files/*") ]).should== 0
88
+ context "Parsing parameters" do
89
+ it "Can print out an error on error" do
90
+ DiscoverTabs.should_receive(:puts).with("invalid option: --wrong_parameter")
91
+ DiscoverTabs.should_receive(:exit).with(1)
92
+ DiscoverTabs.parse_argv(["--wrong_parameter"])
93
+ end
94
+
95
+ it "Should receive the help text on -h" do
96
+ DiscoverTabs.should_receive(:puts).with("usage: discover_tabs [options] [filename|directory]\n -r N Replace tabs intending with N spaces\n -h, --help\
97
+ Show this message\n")
98
+ DiscoverTabs.should_receive(:exit).with(0)
99
+ DiscoverTabs.parse_argv(["-h"])
100
+ end
101
+
102
+ it "Should parse the -r4 and pass it back properly" do
103
+ DiscoverTabs.parse_argv(["-r4", "file"])[:replace_tabs].should == 4
104
+ end
105
+
106
+ it "Should parse the filename|directory" do
107
+ DiscoverTabs.parse_argv(["file"])[:files].should == ["file"]
108
+ end
109
+
110
+ it "Should default the filename|directory to current dir" do
111
+ DiscoverTabs.parse_argv([])[:files].should == ["."]
112
+ end
113
+
114
+ it "Options also set the files" do
115
+ DiscoverTabs.parse_argv(["-r4"])[:files].should == ["."]
116
+ end
62
117
  end
63
118
 
64
- it "Defaults to the current dir" do
65
- DiscoverTabs.should_receive(:in_dir).with(".").and_return(true)
66
- DiscoverTabs.cmdline_run([])
119
+ context "Command line main entry point" do
120
+ it "Should print all the file to stdout" do
121
+ DiscoverTabs.should_receive(:files_with_tab_indenting).with("file").and_return(["file"])
122
+ DiscoverTabs.should_receive(:files_with_tab_indenting).with("file2").and_return([])
123
+ DiscoverTabs.should_receive(:puts).with("file")
124
+ DiscoverTabs.cmdline_run(["file", "file2"]).should== 0
125
+ end
126
+
127
+ it "Makes sure the pathname is clean when printing a file" do
128
+ DiscoverTabs.should_receive(:files_with_tab_indenting).and_return(["file"])
129
+ DiscoverTabs.should_receive(:puts)
130
+ DiscoverTabs.should_receive(:replace_tabs_in_files).with(["file"], 4)
131
+ DiscoverTabs.cmdline_run(["-r4", "file"]).should== 0
132
+ end
67
133
  end
68
134
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
  class SomeClass
3
- def tabbed method
3
+ def tabbed method
4
4
 
5
- end
5
+ end
6
6
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
  class SomeClass
3
- def tabbed method
3
+ def tabbed method
4
4
 
5
- end
5
+ end
6
6
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
  class SomeClass
3
- def tabbed method
3
+ def tabbed method
4
4
 
5
- end
5
+ end
6
6
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
  class SomeClass
3
- def tabbed method
3
+ def tabbed method
4
4
 
5
- end
5
+ end
6
6
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
  class SomeClass
3
- def tabbed method
3
+ def tabbed method
4
4
 
5
- end
5
+ end
6
6
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
  class SomeClass
3
- def tabbed method
3
+ def tabbed method
4
4
 
5
- end
5
+ end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discover_tabs
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.5'
4
+ version: '0.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bas Vodde
@@ -70,6 +70,7 @@ files:
70
70
  - README.md
71
71
  - Rakefile
72
72
  - bin/discover_tabs
73
+ - discover_tabs.gemspec
73
74
  - lib/discover_tabs.rb
74
75
  - lib/version.rb
75
76
  - spec/discover_tabs_spec.rb
@@ -84,6 +85,7 @@ files:
84
85
  - spec/test_data/dir_with_some_tabbed_files_and_different_extensions/tabs.rb
85
86
  - spec/test_data/dir_without_tabbed_files/3.rb
86
87
  - spec/test_data/dir_without_tabbed_files/9.rb
88
+ - spec/test_data/dir_without_tabbed_files/binary_file
87
89
  - spec/test_data/dir_without_tabbed_files/file_without_tabs.rb
88
90
  - spec/test_data/file_with_tabs.rb
89
91
  - spec/test_data/file_without_tabs.rb