guideline 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/guideline.gemspec CHANGED
@@ -23,4 +23,5 @@ Gem::Specification.new do |gem|
23
23
  gem.add_dependency "active_support"
24
24
  gem.add_development_dependency "rspec", ">=2.12.0"
25
25
  gem.add_development_dependency "pry"
26
+ gem.add_development_dependency "simplecov"
26
27
  end
@@ -89,7 +89,7 @@ module Guideline
89
89
 
90
90
  class MethodCallChecker < MethodChecker
91
91
  interesting_files /.*\.rb/
92
- interesting_nodes :call, :fcall, :vcall, :var_ref
92
+ interesting_nodes :call, :fcall, :vcall
93
93
 
94
94
  add_callback :start_call do |node|
95
95
  call(node.message.to_s)
@@ -102,10 +102,6 @@ module Guideline
102
102
  add_callback :start_vcall do |node|
103
103
  call(node.to_s)
104
104
  end
105
-
106
- add_callback :start_var_ref do |node|
107
- call(node.to_s)
108
- end
109
105
  end
110
106
 
111
107
  class Definition < OpenStruct
@@ -16,7 +16,7 @@ module Guideline
16
16
  end
17
17
 
18
18
  def parse
19
- generate_default_config_file if @hash[:init]
19
+ before_hook
20
20
  @hash[:config] = load_config
21
21
  @hash.delete(:help)
22
22
  @hash
@@ -24,6 +24,20 @@ module Guideline
24
24
 
25
25
  private
26
26
 
27
+ def before_hook
28
+ case
29
+ when @hash[:init]
30
+ generate_default_config_file
31
+ when @hash[:version]
32
+ show_version
33
+ end
34
+ end
35
+
36
+ def show_version
37
+ puts VERSION
38
+ exit
39
+ end
40
+
27
41
  def load_config
28
42
  YAML.load_file(config_path)
29
43
  rescue Errno::ENOENT
@@ -74,6 +88,7 @@ module Guideline
74
88
  banner "Usage: guideline [directory] [options]"
75
89
  on :c=, :config=, "Path to config YAML file."
76
90
  on :i, :init, "Generate config YAML template into current directory."
91
+ on :v, :version, "Show version number"
77
92
  end
78
93
  end
79
94
  end
@@ -1,3 +1,3 @@
1
1
  module Guideline
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -70,18 +70,14 @@ module Guideline
70
70
 
71
71
  def excepted_paths
72
72
  if except_pattern
73
- Pathname.glob(only_pattern)
73
+ Pathname.glob(except_pattern)
74
74
  else
75
75
  []
76
76
  end
77
77
  end
78
78
 
79
79
  def only_pattern
80
- if options[:only]
81
- "#{options[:only]}/**/*.rb"
82
- else
83
- "**/*.rb"
84
- end
80
+ options[:only] || "**/*.rb"
85
81
  end
86
82
 
87
83
  def except_pattern
@@ -166,6 +166,60 @@ module Guideline
166
166
  end
167
167
  it { should have_error }
168
168
  end
169
+
170
+ context "when there is module definition" do
171
+ let(:script) do
172
+ <<-EOF
173
+ module ModuleName
174
+ def a
175
+ b.c
176
+ end
177
+ end
178
+ EOF
179
+ end
180
+ it "reports error method with module name" do
181
+ checker.should_receive(:report) do |args|
182
+ args[:message].should include("ModuleName#a")
183
+ end
184
+ subject
185
+ end
186
+ end
187
+
188
+ context "when there is class definition" do
189
+ let(:script) do
190
+ <<-EOF
191
+ class ClassName
192
+ def a
193
+ b.c
194
+ end
195
+ end
196
+ EOF
197
+ end
198
+ it "reports error method with class name" do
199
+ checker.should_receive(:report) do |args|
200
+ args[:message].should include("ClassName#a")
201
+ end
202
+ subject
203
+ end
204
+ end
205
+
206
+ context "when there is class method definition" do
207
+ let(:script) do
208
+ <<-EOF
209
+ class ClassName
210
+ def self.a
211
+ b.c
212
+ end
213
+ end
214
+ EOF
215
+ end
216
+ it "reports error class method with class name" do
217
+ checker.should_receive(:report) do |args|
218
+ args[:message].should include("ClassName.a")
219
+ end
220
+ subject
221
+ end
222
+ end
169
223
  end
170
224
  end
171
225
  end
@@ -28,10 +28,12 @@ module Guideline
28
28
  end
29
29
  end
30
30
 
31
- context "when there is last comma in sigleline hash" do
31
+ context "when there is last comma" do
32
32
  let(:script) do
33
33
  <<-EOF
34
- { :a => 1, }
34
+ {
35
+ :a => 1,
36
+ }
35
37
  EOF
36
38
  end
37
39
 
@@ -40,7 +42,7 @@ module Guideline
40
42
  end
41
43
  end
42
44
 
43
- context "when there is no last comma in multiline hash" do
45
+ context "when there is no last comma" do
44
46
  let(:script) do
45
47
  <<-EOF
46
48
  {
@@ -54,7 +56,7 @@ module Guideline
54
56
  end
55
57
  end
56
58
 
57
- context "when there is no last comma in multiline hash of new syntax" do
59
+ context "when there is no last comma with new hash syntax" do
58
60
  let(:script) do
59
61
  <<-EOF
60
62
  {
@@ -68,7 +70,7 @@ module Guideline
68
70
  end
69
71
  end
70
72
 
71
- context "when there is no last comma in multiline hash with array" do
73
+ context "when there is no last comma with array" do
72
74
  let(:script) do
73
75
  <<-EOF
74
76
  {
@@ -82,27 +84,27 @@ module Guideline
82
84
  end
83
85
  end
84
86
 
85
- context "when there is last comma in multiline hash" do
87
+ context "when there is no last comma with nested hash" do
86
88
  let(:script) do
87
89
  <<-EOF
88
90
  {
89
- :a => 1,
91
+ :a => {
92
+ :b => 1,
93
+ }
90
94
  }
91
95
  EOF
92
96
  end
93
97
 
94
- it "does not detect error" do
95
- should_not have_error
98
+ it "detects error" do
99
+ should have_error
96
100
  end
97
101
  end
98
102
 
99
- context "when there is no last comma in multiline nested hash" do
103
+ context "when there is no last comma with embedded expansion" do
100
104
  let(:script) do
101
- <<-EOF
105
+ <<-'EOF'
102
106
  {
103
- :a => {
104
- :b => 1,
105
- }
107
+ :a => "#{b(1, 2)}"
106
108
  }
107
109
  EOF
108
110
  end
@@ -112,17 +114,33 @@ module Guideline
112
114
  end
113
115
  end
114
116
 
115
- context "when there is embedded expansion brace" do
117
+ context "when there is no last comma with round braces" do
116
118
  let(:script) do
117
119
  <<-'EOF'
118
120
  {
119
- :a => "#{b}",
121
+ :a => b(1, 2)
120
122
  }
121
123
  EOF
122
124
  end
123
125
 
124
- it "does not detect error" do
125
- should_not have_error
126
+ it "detects error" do
127
+ should have_error
128
+ end
129
+ end
130
+
131
+ context "when there is no last comma with do-end args" do
132
+ let(:script) do
133
+ <<-'EOF'
134
+ {
135
+ :a => b do |c, d|
136
+ 1
137
+ end
138
+ }
139
+ EOF
140
+ end
141
+
142
+ it "detects error" do
143
+ should have_error
126
144
  end
127
145
  end
128
146
  end
@@ -0,0 +1,16 @@
1
+ require "spec_helper"
2
+
3
+ module Guideline
4
+ describe Error do
5
+ let(:error) do
6
+ described_class.new(:line => 1, :message => "message")
7
+ end
8
+
9
+ describe "#render" do
10
+ it "renders its line and message" do
11
+ error.should_receive(:puts).with(" 1: message")
12
+ error.render
13
+ end
14
+ end
15
+ end
16
+ end
@@ -8,13 +8,17 @@ module Guideline
8
8
  end
9
9
 
10
10
  before do
11
- described_class.any_instance.stub(:puts)
11
+ runner.stub(:puts)
12
12
  end
13
13
 
14
14
  let(:argv) do
15
15
  []
16
16
  end
17
17
 
18
+ let(:runner) do
19
+ described_class.any_instance
20
+ end
21
+
18
22
  it { should be_a HashWithIndifferentAccess }
19
23
 
20
24
  context "when given --init option" do
@@ -24,7 +28,7 @@ module Guideline
24
28
 
25
29
  context "when config file already exists" do
26
30
  before do
27
- described_class.any_instance.stub(:config_file_exist? => true)
31
+ File.stub(:exist? => true)
28
32
  end
29
33
 
30
34
  it "does not generate config file" do
@@ -35,7 +39,7 @@ module Guideline
35
39
 
36
40
  context "when config file does not exist" do
37
41
  before do
38
- described_class.any_instance.stub(:config_file_exist? => false)
42
+ File.stub(:exist? => false)
39
43
  end
40
44
 
41
45
  it "generates config file" do
@@ -45,6 +49,17 @@ module Guideline
45
49
  end
46
50
  end
47
51
 
52
+ context "when given --version option" do
53
+ let(:argv) do
54
+ ["--version"]
55
+ end
56
+
57
+ it "shows current version number and exit" do
58
+ runner.should_receive(:puts).with(VERSION)
59
+ expect { subject }.to raise_error(SystemExit)
60
+ end
61
+ end
62
+
48
63
  context "when not given --config option" do
49
64
  it "returns default config parsed from guideline.yml" do
50
65
  should == {
@@ -60,6 +75,7 @@ module Guideline
60
75
  },
61
76
  },
62
77
  "init" => nil,
78
+ "version" => nil,
63
79
  }
64
80
  end
65
81
  end
@@ -79,6 +95,7 @@ module Guideline
79
95
  "a" => "b",
80
96
  },
81
97
  "init" => nil,
98
+ "version" => nil,
82
99
  }
83
100
  end
84
101
  end
@@ -2,30 +2,54 @@ require "spec_helper"
2
2
 
3
3
  module Guideline
4
4
  describe Visitor do
5
- before do
6
- visitor.stub(:paths).and_return(paths)
5
+ let(:visitor) do
6
+ described_class.new(options)
7
7
  end
8
8
 
9
- let(:visitor) do
10
- described_class.new(:checker => [checker])
9
+ let(:options) do
10
+ { :checker => [checker] }
11
11
  end
12
12
 
13
13
  let(:checker) do
14
14
  mock(:has_error? => false)
15
15
  end
16
16
 
17
- let(:paths) do
18
- [path]
19
- end
17
+ describe "#visit" do
18
+ let(:path) do
19
+ Pathname.new("spec/guideline/visitor_spec.rb")
20
+ end
20
21
 
21
- let(:path) do
22
- mock
23
- end
22
+ context "when :only option is not specified" do
23
+ it "visits all paths" do
24
+ called = false
25
+ checker.should_receive(:check).at_least(1) do |args|
26
+ called = true if args == path
27
+ end
28
+ visitor.visit
29
+ called.should be_true
30
+ end
31
+ end
24
32
 
25
- describe "#visit" do
26
- it "calls checker#check with path" do
27
- checker.should_receive(:check).with(path)
28
- visitor.visit
33
+ context "when :only option is specified" do
34
+ before do
35
+ options[:only] = "lib/**/*.rb"
36
+ end
37
+
38
+ it "does not visit paths which are not specified" do
39
+ checker.should_not_receive(:check).with(path)
40
+ visitor.visit
41
+ end
42
+ end
43
+
44
+ context "when :except option is specified" do
45
+ before do
46
+ options[:except] = "spec/**/*.rb"
47
+ end
48
+
49
+ it "does not visit paths which are specified" do
50
+ checker.should_not_receive(:check).with(path)
51
+ visitor.visit
52
+ end
29
53
  end
30
54
  end
31
55
 
@@ -36,7 +60,7 @@ module Guideline
36
60
  end
37
61
 
38
62
  it "calls checker#prepare with path" do
39
- checker.should_receive(:prepare).with(path)
63
+ checker.should_receive(:prepare).at_least(1)
40
64
  visitor.prepare
41
65
  end
42
66
  end
@@ -52,5 +76,25 @@ module Guideline
52
76
  end
53
77
  end
54
78
  end
79
+
80
+ describe "#render" do
81
+ before do
82
+ visitor.instance_variable_set(:@errors, errors)
83
+ visitor.stub(:puts)
84
+ end
85
+
86
+ let(:errors) do
87
+ [error]
88
+ end
89
+
90
+ let(:error) do
91
+ mock(:path => "path")
92
+ end
93
+
94
+ it "calls #render of each error" do
95
+ error.should_receive(:render)
96
+ visitor.render
97
+ end
98
+ end
55
99
  end
56
100
  end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,5 @@
1
+ require "simplecov"
2
+ SimpleCov.start
3
+
1
4
  $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
5
  require "guideline"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guideline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-12-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: slop
16
- requirement: &70154114004860 !ruby/object:Gem::Requirement
16
+ requirement: &70231125798820 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70154114004860
24
+ version_requirements: *70231125798820
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: code_analyzer
27
- requirement: &70154114004440 !ruby/object:Gem::Requirement
27
+ requirement: &70231125798400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70154114004440
35
+ version_requirements: *70231125798400
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: active_support
38
- requirement: &70154114004020 !ruby/object:Gem::Requirement
38
+ requirement: &70231125797980 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70154114004020
46
+ version_requirements: *70231125797980
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70154114003520 !ruby/object:Gem::Requirement
49
+ requirement: &70231125797480 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.12.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70154114003520
57
+ version_requirements: *70231125797480
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: pry
60
- requirement: &70154114003100 !ruby/object:Gem::Requirement
60
+ requirement: &70231125797060 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,18 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70154114003100
68
+ version_requirements: *70231125797060
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: &70231125796600 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70231125796600
69
80
  description: Guideline.gem checks that your code is protectingthe rule of coding guideline
70
81
  email:
71
82
  - r7kamura@gmail.com
@@ -103,6 +114,7 @@ files:
103
114
  - spec/guideline/checkers/long_line_checker_spec.rb
104
115
  - spec/guideline/checkers/long_method_checker_spec.rb
105
116
  - spec/guideline/checkers/unused_method_checker_spec.rb
117
+ - spec/guideline/error_spec.rb
106
118
  - spec/guideline/runner_spec.rb
107
119
  - spec/guideline/visitor_spec.rb
108
120
  - spec/guideline_spec.rb
@@ -140,6 +152,7 @@ test_files:
140
152
  - spec/guideline/checkers/long_line_checker_spec.rb
141
153
  - spec/guideline/checkers/long_method_checker_spec.rb
142
154
  - spec/guideline/checkers/unused_method_checker_spec.rb
155
+ - spec/guideline/error_spec.rb
143
156
  - spec/guideline/runner_spec.rb
144
157
  - spec/guideline/visitor_spec.rb
145
158
  - spec/guideline_spec.rb