Skip to content

Commit b2df067

Browse files
author
remi Taylor
committed
Fix style violations and update RuboCop config
1 parent c7ca10c commit b2df067

4 files changed

Lines changed: 80 additions & 23 deletions

File tree

.rubocop.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,61 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# RubyCop configuration file
16+
#
17+
# RuboCop
18+
# http://batsov.com/rubocop/
19+
#
20+
# Style cops
21+
# http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style
22+
#
23+
# Lint cops
24+
# http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Lint
25+
26+
# Prefer double quotes for consistency (regardless of interpolation)
1527
Style/StringLiterals:
1628
EnforcedStyle: double_quotes
29+
30+
# Prefer no parentheses for method definitions and invocations
1731
Style/MethodDefParentheses:
1832
EnforcedStyle: require_no_parentheses
33+
34+
# Use %[ ] for string arrays
35+
# Use %{ } for other delimiters, eg. %{ } sometimes used for multiline strings
36+
Style/PercentLiteralDelimiters:
37+
Enabled: false
38+
39+
# Always use 2 spaces for indentation
40+
# Sometimes method invokations might be additionally indented for clarity
41+
Style/MultilineMethodCallIndentation:
42+
Enabled: false
43+
44+
# Unused block arguments are OK
45+
# They can add clarity to code to demonstrate available arguments
46+
Lint/UnusedBlockArgument:
47+
Enabled: false
48+
49+
# Usage of // or %r{ } is acceptable for regular expressions
50+
# Please use // unless %r{ } adds clarity, eg. slashes used in expression
51+
Style/RegexpLiteral:
52+
Enabled: false
53+
54+
# Use of parenthesis is optional in nested method calls
55+
# This cop is optional mostly to support RSpec expectations
56+
# If the code sample is: service.add "variable"
57+
# Then the spec can use: expect { service.add "variable" }
58+
Style/NestedParenthesizedCalls:
59+
Enabled: false
60+
61+
# In production code, yield is prefered over block.call
62+
# For code samples, explicitly calling a block by name can add clarity
63+
Performance/RedundantBlockCall:
64+
Enabled: false
65+
66+
# Empty line at top of block is sometimes OK, eg. for RSpec describe blocks
67+
Style/EmptyLinesAroundBlockBody:
68+
Enabled: false
69+
1970
Style/NumericLiterals:
2071
Enabled: false
2172
Style/SpaceAroundOperators:

logging/Gemfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ GEM
88
thread_safe (~> 0.3, >= 0.3.4)
99
tzinfo (~> 1.1)
1010
addressable (2.4.0)
11-
ast (2.2.0)
11+
ast (2.3.0)
1212
autoparse (0.3.3)
1313
addressable (>= 2.3.1)
1414
extlib (>= 0.9.15)
@@ -63,7 +63,7 @@ GEM
6363
multi_json (1.12.1)
6464
multipart-post (2.0.0)
6565
os (0.9.6)
66-
parser (2.3.1.1)
66+
parser (2.3.1.2)
6767
ast (~> 2.2)
6868
powerpack (0.1.1)
6969
rack (1.6.4)
@@ -85,8 +85,8 @@ GEM
8585
diff-lcs (>= 1.2.0, < 2.0)
8686
rspec-support (~> 3.4.0)
8787
rspec-support (3.4.1)
88-
rubocop (0.40.0)
89-
parser (>= 2.3.1.0, < 3.0)
88+
rubocop (0.42.0)
89+
parser (>= 2.3.1.1, < 3.0)
9090
powerpack (~> 0.1)
9191
rainbow (>= 1.99.1, < 3.0)
9292
ruby-progressbar (~> 1.7)
@@ -100,7 +100,7 @@ GEM
100100
thread_safe (0.3.5)
101101
tzinfo (1.2.2)
102102
thread_safe (~> 0.1)
103-
unicode-display_width (1.0.5)
103+
unicode-display_width (1.1.0)
104104
zonefile (1.04)
105105

106106
PLATFORMS

logging/sample.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ def write_log_entry
113113
end
114114

115115
def delete_log
116-
# [START delete_log]
116+
# [START delete_log]
117117
require "gcloud"
118118

119119
gcloud = Gcloud.new "my-gcp-project-id"
120120
logging = gcloud.logging
121121

122122
logging.delete_log "my_application_log"
123-
# [END delete_log]
123+
# [END delete_log]
124124
end
125125

126126
def write_log_entry_using_ruby_logger
@@ -129,8 +129,10 @@ def write_log_entry_using_ruby_logger
129129

130130
gcloud = Gcloud.new "my-gcp-project-id"
131131
logging = gcloud.logging
132-
resource = logging.resource "gae_app", module_id: "default", version_id: "20160101t163030"
133-
logger = logging.logger "my_application_log", resource
132+
resource = logging.resource "gae_app", module_id: "default",
133+
version_id: "20160101t163030"
134+
135+
logger = logging.logger "my_application_log", resource
134136

135137
logger.info "Log message"
136138
# [END write_log_entry_using_ruby_logger]

logging/spec/sample_spec.rb

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ def wait_until times: 5, delay: 1, &condition
2727
raise "Condition not met. Waited #{times} times with #{delay} sec delay"
2828
end
2929

30+
# Frequently used full path to "my_application_log" for test project
31+
def my_application_log_name
32+
"projects/#{@project_id}/logs/my_application_log"
33+
end
34+
3035
# Returns entries logged to "my_application_log" in the test project
3136
def my_application_log_entries
3237
@logging.entries(
33-
filter: %{logName = "projects/#{@project_id}/logs/my_application_log"},
38+
filter: %{logName = "#{my_application_log_name}"},
3439
order: "timestamp desc"
3540
)
3641
end
@@ -73,7 +78,8 @@ def my_application_log_entries
7378

7479
# Delete log sink used by code samples if the test created one
7580
def cleanup!
76-
@logging.sink("my-sink")&.delete
81+
test_sink = @logging.sink "my-sink"
82+
test_sink.delete if test_sink
7783
end
7884

7985
it "can create logging client" do
@@ -91,14 +97,14 @@ def cleanup!
9197

9298
it "can create log sink" do
9399
expect(@logging.sink "my-sink").to be nil
94-
100+
95101
create_log_sink
96102

97103
expect(@logging.sink "my-sink").not_to be nil
98104
end
99105

100106
it "can update log sink" do
101-
original_destination = "storage.googleapis.com/#{@bucket.id}"
107+
original_destination = "storage.googleapis.com/#{@bucket.id}"
102108
updated_destination = "storage.googleapis.com/#{@alt_bucket.id}"
103109

104110
create_log_sink
@@ -130,17 +136,15 @@ def cleanup!
130136
with(filter: %{resource.type = "gae_app"}).
131137
and_wrap_original do |m, *args|
132138
m.call(
133-
filter: %{logName = "projects/#{@project_id}/logs/my_application_log"},
139+
filter: %{logName = "#{my_application_log_name}"},
134140
order: "timestamp desc"
135141
)
136-
end
142+
end
137143

138144
timestamp = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} [^\\\\]+"
139-
log_name = "projects/#{@project_id}/logs/my_application_log"
140-
payload = '"Log message"'
141145

142146
expect { list_log_entries }.to output(
143-
%r{\[#{timestamp}\] #{log_name} #{payload}}
147+
%r{\[#{timestamp}\] #{my_application_log_name} "Log message"}
144148
).to_stdout
145149
end
146150

@@ -167,7 +171,7 @@ def cleanup!
167171
end
168172

169173
entries = my_application_log_entries
170-
entry = entries.detect {|e| e.payload.include? "time #{current_time}" }
174+
entry = entries.detect { |e| e.payload.include? "time #{current_time}" }
171175
expect(entry).to be nil
172176

173177
write_log_entry
@@ -180,7 +184,7 @@ def cleanup!
180184
end
181185

182186
entries = my_application_log_entries
183-
entry = entries.detect {|e| e.payload.include? "time #{current_time}" }
187+
entry = entries.detect { |e| e.payload.include? "time #{current_time}" }
184188
expect(entry).not_to be nil
185189
expect(entry.payload).to eq "Log message - current time #{current_time}"
186190
expect(entry.severity).to eq :NOTICE
@@ -200,7 +204,7 @@ def cleanup!
200204
filter: %{logName = "projects/#{@project_id}/logs/my_application_log"},
201205
order: "timestamp desc"
202206
)
203-
entry = entries.detect {|e| e.payload.include? "time #{current_time}" }
207+
entry = entries.detect { |e| e.payload.include? "time #{current_time}" }
204208
expect(entry).to be nil
205209

206210
# Hooked up to real test project
@@ -227,7 +231,7 @@ def cleanup!
227231
m.call message
228232
end
229233

230-
write_log_entry_using_ruby_logger
234+
write_log_entry_using_ruby_logger
231235

232236
# Wait for entry to be queryable
233237
wait_until do
@@ -237,7 +241,7 @@ def cleanup!
237241
end
238242

239243
entries = my_application_log_entries
240-
entry = entries.detect {|e| e.payload.include? "time #{current_time}" }
244+
entry = entries.detect { |e| e.payload.include? "time #{current_time}" }
241245
expect(entry).not_to be nil
242246
expect(entry.payload).to eq "Log message - current time #{current_time}"
243247
expect(entry.severity).to eq :INFO

0 commit comments

Comments
 (0)