Skip to content

Commit 29bb8f5

Browse files
committed
New test. RuboCop-induced style improvements.
- RegExpPattern felt fragile, so I added a unit test. - A bunch of auto-fixes. - Some re-configuration where I didn't like the defaults. - Actually simplifies some methods. - Moves debug printing to a separate class.
1 parent 64feb6d commit 29bb8f5

21 files changed

+357
-176
lines changed

.rubocop.yml

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
AllCops:
2-
TargetRubyVersion: 2.3
2+
TargetRubyVersion: 2.3
3+
4+
# The default, 80, is a nice-to-have but not really all that relevant
5+
# on today's wide screens. Use 120 as a "hard" cap and try to be shorter.
6+
Metrics/LineLength:
7+
Max: 120
8+
9+
# The default, 10, seems silly here.
10+
Metrics/MethodLength:
11+
Max: 25
12+
13+
Style/RegexpLiteral:
14+
EnforcedStyle: mixed
15+
16+
# I'd rather have empty else branches than forget one.
17+
Style/EmptyElse:
18+
Enabled: false

Gemfile

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
source "https://rubygems.org"
2-
gemspec
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
gemspec

Rakefile

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'rake/testtask'
24
require 'yard'
35

@@ -10,8 +12,8 @@ end
1012

1113
desc 'Build documentation'
1214
YARD::Rake::YardocTask.new do |t|
13-
#t.files = ['lib/**/*.rb', OTHER_PATHS] # optional
14-
#t.options = ['--any', '--extra', '--opts'] # optional
15+
# t.files = ['lib/**/*.rb', OTHER_PATHS] # optional
16+
# t.options = ['--any', '--extra', '--opts'] # optional
1517
end
1618

17-
task :default => :test
19+
task default: :test

bin/texlogparser

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

34
require 'tex_log_parser'
5+
require 'tex_log_parser/version'
46
require 'optparse'
57
require 'json'
68

79
# Defaults
810
options = {
9-
debug: false,
1011
format: :file_line,
1112
input: STDIN,
1213
output: STDOUT
1314
}
14-
formats = [:file_line, :json]
15+
formats = %i[file_line json]
1516

1617
OptionParser.new do |opts|
1718
opts.banner = 'Usage: texlogparser [options]'
1819
opts.on('-d', '--debug', 'Output debug information') do
19-
options[:debug] = true
20+
Logger.debugging = true
2021
end
2122
opts.on('-f ENUM', '--format ENUM', formats,
22-
'Output format', "One of: #{formats.map { |e| e.to_s }.join(", ")}") do |format|
23+
'Output format', "One of: #{formats.map(&:to_s).join(', ')}") do |format|
2324
options[:format] = format
2425
end
2526
opts.on('-i', '--input PATH', 'Read input from PATH') do |path|
@@ -44,7 +45,7 @@ OptionParser.new do |opts|
4445

4546
begin
4647
opts.parse!
47-
rescue => e
48+
rescue StandardError => e
4849
STDERR.puts e
4950
exit 1
5051
end
@@ -64,9 +65,9 @@ output = STDOUT
6465
output = File.open(options[:output], 'w') if options[:output].is_a?(String)
6566
case options[:format]
6667
when :file_line
67-
output.write(messages.map { |m| m.to_s }.join("\n\n"))
68+
output.write(messages.map(&:to_s).join("\n\n"))
6869
# TODO: print summary?
6970
when :json
7071
output.write(JSON.pretty_generate(messages))
7172
end
72-
output.close
73+
output.close

lib/logger.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
# A simple helper, probably to be replaced by a proper logging library
4+
# at some point.
5+
#
6+
# @attr [true,false] debug
7+
class Logger
8+
class << self
9+
attr_accessor :debugging
10+
11+
# Logs the given message to STDOUT if `debug` is true.
12+
# @param [String] message
13+
def debug(message)
14+
puts message if debugging || !ENV['DEBUG'].nil?
15+
end
16+
end
17+
end

lib/tex_log_parser.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
require 'logger'
4+
require 'tex_log_parser/log_buffer'
15
require 'tex_log_parser/log_message'
26
require 'tex_log_parser/log_parser'
3-
require "tex_log_parser/log_pattern"
7+
require 'tex_log_parser/log_pattern'
48
Dir["#{File.expand_path(__dir__)}/tex_log_parser/patterns/*.rb"].each { |p| require p }
59

610
# TODO: document
@@ -17,22 +21,22 @@ def scope_changes(line)
1721
# A scope opened and closed immediately -- log it, then
1822
# continue with rest of the line (there can be multiple such
1923
# things in one line, see e.g. 000.log:656)
20-
[$1, :pop] + ($2.strip.empty? ? [] : scope_changes($2))
24+
[Regexp.last_match(1), :pop] + (Regexp.last_match(2).strip.empty? ? [] : scope_changes(Regexp.last_match(2)))
2125
when /^\s*\(([^()]+?)\s*$/
2226
# A scope opened and will be closed later.
2327
# Happens on a dedicated line
24-
[$1]
28+
[Regexp.last_match(1)]
2529
when /^\s*(\)+)(.*)$/
2630
# Scopes close on a dedicated line, except if they don't (cf 000.log:624)
2731
# So we have to continue on the rest of the line. Uh oh.
28-
([:pop] * $1.length) + scope_changes($2)
32+
([:pop] * Regexp.last_match(1).length) + scope_changes(Regexp.last_match(2))
2933
when /\([^)]*$/
3034
# BROKEN_BY_LINEBREAKS
3135
# Bad linebreaks can cause trailing ) to spill over. Narf.
3236
# e.g. 000.log:502-503
33-
["dummy"] # Compensate the bad pop that will happen next line.
37+
['dummy'] # Compensate the bad pop that will happen next line.
3438
else
3539
[]
3640
end
3741
end
38-
end
42+
end

lib/tex_log_parser/log_buffer.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class LogBuffer
24
# @param [Array<String>,IO] log
35
def initialize(log)
@@ -12,6 +14,10 @@ def empty?
1214
@buffer.empty? && stream_is_done?
1315
end
1416

17+
def buffer_size
18+
@buffer.size
19+
end
20+
1521
def first
1622
self[0]
1723
end
@@ -53,4 +59,4 @@ def close
5359
def stream_is_done?
5460
@stream.nil? || @stream.closed? || @stream.eof?
5561
end
56-
end
62+
end

lib/tex_log_parser/log_message.rb

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'json'
24

35
# @attr [String] message
@@ -17,8 +19,6 @@ def initialize(message:, source_file: nil, source_lines: nil, log_lines: nil,
1719
@preformatted = preformatted
1820
@level = level
1921
@pattern = pattern
20-
21-
@debug = false # TODO: get option here
2222
end
2323

2424
attr_accessor :message, :source_file, :source_lines, :log_lines,
@@ -33,24 +33,25 @@ def to_s
3333
end
3434

3535
message = @message
36-
message = message.split("\n").map {|l| l.strip }.join("") unless @preformatted
37-
message += "\nLog pattern: '#{@pattern}'" if @debug
36+
message = message.split("\n").map(&:strip).join(' ') unless @preformatted
37+
message += "\nLog pattern: '#{@pattern}'" if Logger.debugging
3838

3939
<<~MSG
4040
#{@source_file}#{lines}: #{@level.to_s.upcase}
4141
#{message}
4242
MSG
4343
end
4444

45-
def to_json(options={})
45+
def to_json(_options = {})
4646
hash = {
47-
level: @level,
48-
source_file: @source_file,
49-
source_lines: @source_lines,
50-
message: @message,
51-
log_lines: @log_lines,
52-
preformatted: @preformatted
47+
level: @level,
48+
source_file: @source_file,
49+
source_lines: @source_lines,
50+
message: @message,
51+
log_lines: @log_lines,
52+
preformatted: @preformatted
5353
}
54+
hash[:pattern] = @pattern if Logger.debugging
5455
JSON.pretty_generate hash
5556
end
56-
end
57+
end

0 commit comments

Comments
 (0)