Skip to content

Commit

Permalink
attempt to placate the wild and dangerous rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
ledbettj authored and John Ledbetter committed Jan 13, 2016
1 parent d8665df commit aafc07d
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SignalException:
Enabled: false

RaiseArgs:
Enabled: false
Enabled: true

SpaceInsideBlockBraces:
Enabled: true
Expand Down
2 changes: 1 addition & 1 deletion lib/systemd/ffi_size_t.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.monkey_patch_type_i_need!(which)
method_defined?("read_#{name}") if t == type
end

raise RuntimeError, "Unable to patch in reader/writer for #{which}" if type.nil?
raise "Unable to patch in reader/writer for #{which}" if type.nil?

alias_method "read_#{which}", "read_#{type}"
alias_method "write_#{which}", "write_#{type}"
Expand Down
2 changes: 1 addition & 1 deletion lib/systemd/id128.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def self.random
def self.read_id128(func)
ptr = FFI::MemoryPointer.new(Native::Id128, 1)
rc = Native.send(func, ptr)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
Native::Id128.new(ptr).to_s
end

Expand Down
30 changes: 17 additions & 13 deletions lib/systemd/journal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def initialize(opts = {})
opts[:files] = opts[:file] if opts[:file]

rc = open_journal(ptr, opts, flags)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0

@ptr = ptr.read_pointer
ObjectSpace.define_finalizer(self, self.class.finalize(@ptr))
Expand Down Expand Up @@ -84,7 +84,7 @@ def read_field(field)
field = field.to_s.upcase
rc = Native.sd_journal_get_data(@ptr, field, out_ptr, len_ptr)

raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0

len = len_ptr.read_size_t
string_from_out_ptr(out_ptr, len).split('=', 2).last
Expand Down Expand Up @@ -123,7 +123,7 @@ def current_catalog
out_ptr = FFI::MemoryPointer.new(:pointer, 1)

rc = Native.sd_journal_get_catalog(@ptr, out_ptr)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0

Journal.read_and_free_outstr(out_ptr.read_pointer)
end
Expand All @@ -135,7 +135,7 @@ def self.catalog_for(message_id)
Systemd::Id128::Native::Id128.from_s(message_id),
out_ptr
)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0

read_and_free_outstr(out_ptr.read_pointer)
end
Expand All @@ -152,7 +152,7 @@ def query_unique(field)
Native.sd_journal_restart_unique(@ptr)

rc = Native.sd_journal_query_unique(@ptr, field.to_s.upcase)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0

while (kvpair = enumerate_helper(:sd_journal_enumerate_unique))
results << kvpair.last
Expand All @@ -170,7 +170,7 @@ def disk_usage
size_ptr = FFI::MemoryPointer.new(:uint64)
rc = Native.sd_journal_get_usage(@ptr, size_ptr)

raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
size_ptr.read_uint64
end

Expand All @@ -180,7 +180,7 @@ def disk_usage
def data_threshold
size_ptr = FFI::MemoryPointer.new(:size_t, 1)
if (rc = Native.sd_journal_get_data_threshold(@ptr, size_ptr)) < 0
raise JournalError.new(rc)
raise JournalError, rc
end

size_ptr.read_size_t
Expand All @@ -190,7 +190,7 @@ def data_threshold
# Fields longer than this will be truncated.
def data_threshold=(threshold)
if (rc = Native.sd_journal_set_data_threshold(@ptr, threshold)) < 0
raise JournalError.new(rc)
raise JournalError, rc
end
end

Expand All @@ -210,6 +210,11 @@ def inspect
def open_journal(ptr, opts, flags)
@open_flags = 0

if opts[:container] && !Native.open_container?
raise ArgumentError,
'This native library version does not support opening containers'
end

case
when opts[:path]
@open_target = "path:#{opts[:path]}"
Expand All @@ -219,7 +224,6 @@ def open_journal(ptr, opts, flags)
@open_target = "file#{files.one? ? '' : 's'}:#{files.join(',')}"
Native.sd_journal_open_files(ptr, array_to_ptrs(files), 0)
when opts[:container]
raise ArgumentError.new('This libsystemd-journal version does not support sd_journal_open_container') unless Native.open_container?
@open_flags = flags
@open_target = "container:#{opts[:container]}"
Native.sd_journal_open_container(ptr, opts[:container], flags)
Expand All @@ -233,7 +237,7 @@ def open_journal(ptr, opts, flags)
def read_realtime
out = FFI::MemoryPointer.new(:uint64, 1)
rc = Native.sd_journal_get_realtime_usec(@ptr, out)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0

out.read_uint64
end
Expand All @@ -243,7 +247,7 @@ def read_monotonic
boot = FFI::MemoryPointer.new(Systemd::Id128::Native::Id128, 1)

rc = Native.sd_journal_get_monotonic_usec(@ptr, out, boot)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0

[out.read_uint64, Systemd::Id128::Native::Id128.new(boot).to_s]
end
Expand All @@ -261,7 +265,7 @@ def validate_options!(opts)
exclusive = [:path, :files, :container, :file]
provided = (opts.keys & exclusive)
if provided.length > 1
raise ArgumentError.new("#{provided} are conflicting options")
raise ArgumentError, "#{provided} are conflicting options"
end
end

Expand All @@ -274,7 +278,7 @@ def enumerate_helper(enum_function)
out_ptr = FFI::MemoryPointer.new(:pointer, 1)

rc = Native.send(enum_function, @ptr, out_ptr, len_ptr)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
return nil if rc == 0

len = len_ptr.read_size_t
Expand Down
6 changes: 3 additions & 3 deletions lib/systemd/journal/filterable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def filter(*conditions)
def add_filter(field, value)
match = "#{field.to_s.upcase}=#{value}"
rc = Native.sd_journal_add_match(@ptr, match, match.length)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
end

# Add a set of filters to the journal, such that only entries where the
Expand Down Expand Up @@ -71,7 +71,7 @@ def add_filters(filters)
# end
def add_disjunction
rc = Native.sd_journal_add_disjunction(@ptr)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
end

# Add an AND condition to the filter. All previously added terms will be
Expand All @@ -89,7 +89,7 @@ def add_disjunction
# end
def add_conjunction
rc = Native.sd_journal_add_conjunction(@ptr)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
end

# Remove all filters and conjunctions/disjunctions.
Expand Down
6 changes: 3 additions & 3 deletions lib/systemd/journal/native.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ module Native
ffi_lib %w( libsystemd.so.0 libsystemd.so
libsystemd-journal.so.0 libsystemd-journal.so)

@@has_open_container = true
@has_open_container = true

def self.open_container?
@@has_open_container
@has_open_container
end

# setup/teardown
Expand All @@ -28,7 +28,7 @@ def self.open_container?
begin
attach_function :sd_journal_open_container, [:pointer, :string, :int], :int
rescue FFI::NotFoundError
@@has_open_container = false
@has_open_container = false
end

# navigation
Expand Down
20 changes: 10 additions & 10 deletions lib/systemd/journal/navigable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Navigable
def cursor
out_ptr = FFI::MemoryPointer.new(:pointer, 1)
if (rc = Native.sd_journal_get_cursor(@ptr, out_ptr)) < 0
raise JournalError.new(rc)
raise JournalError, rc
end

Journal.read_and_free_outstr(out_ptr.read_pointer)
Expand All @@ -20,7 +20,7 @@ def cursor
# provided cursor, False otherwise.
def cursor?(c)
if (rc = Native.sd_journal_test_cursor(@ptr, c)) < 0
raise JournalError.new(rc)
raise JournalError, rc
end

rc > 0
Expand All @@ -41,7 +41,7 @@ def move(offset = 1)
# that the pointer has reached the end of the journal.
def move_next
rc = Native.sd_journal_next(@ptr)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
rc > 0
end

Expand All @@ -51,7 +51,7 @@ def move_next
# pointer has reached the end of the journal.
def move_next_skip(amount)
rc = Native.sd_journal_next_skip(@ptr, amount)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
rc
end

Expand All @@ -61,7 +61,7 @@ def move_next_skip(amount)
# indicating that the pointer has reached the beginning of the journal.
def move_previous
rc = Native.sd_journal_previous(@ptr)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
rc > 0
end

Expand All @@ -71,7 +71,7 @@ def move_previous
# read pointer has reached the beginning of the journal.
def move_previous_skip(amount)
rc = Native.sd_journal_previous_skip(@ptr, amount)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
rc
end

Expand All @@ -94,16 +94,16 @@ def seek(where)
Native.sd_journal_seek_tail(@ptr)
when where.is_a?(Time)
Native.sd_journal_seek_realtime_usec(
@ptr,
where.to_i * 1_000_000
@ptr,
where.to_i * 1_000_000
)
when where.is_a?(String)
Native.sd_journal_seek_cursor(@ptr, where)
else
raise ArgumentError.new("Unknown seek type: #{where.class}")
raise ArgumentError, "Unknown seek type: #{where.class}"
end

raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0

true
end
Expand Down
6 changes: 3 additions & 3 deletions lib/systemd/journal/waitable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def wait(timeout_usec = -1, opts = {})
wait_select(timeout_usec)
else
rc = Native.sd_journal_wait(@ptr, timeout_usec)
raise JournalError.new(rc) if rc.is_a?(Fixnum) && rc < 0
raise JournalError, rc if rc.is_a?(Fixnum) && rc < 0
rc == :nop ? nil : rc
end
end
Expand Down Expand Up @@ -61,13 +61,13 @@ def io_object

def file_descriptor
fd = Native.sd_journal_get_fd(@ptr)
raise JournalError.new(rc) if fd < 0
raise JournalError, rc if fd < 0
fd
end

def reason_for_wakeup
rc = Native.sd_journal_process(@ptr)
raise JournalError.new(rc) if rc.is_a?(Fixnum) && rc < 0
raise JournalError, rc if rc.is_a?(Fixnum) && rc < 0
rc == :nop ? nil : rc
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/systemd/journal/writable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def log_stream(identifier, priority, opts = {})
priority,
!opts[:prefix].nil?
)
raise JournalError.new(fd) if fd < 0
raise JournalError, fd if fd < 0

IO.new(fd, File::WRONLY, encoding: Encoding::UTF_8)
end
Expand All @@ -58,7 +58,7 @@ def log_stream(identifier, priority, opts = {})
# @param [String] message the text to prefix the error message with.
def perror(message)
rc = Native.sd_journal_perror(message)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
end

# write a simple message to the systemd journal.
Expand All @@ -67,7 +67,7 @@ def perror(message)
# @param [String] message the content of the message to write.
def print(level, message)
rc = Native.sd_journal_print(level, message)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
end

# write an event to the systemd journal.
Expand All @@ -79,7 +79,7 @@ def message(contents)
# add a null pointer to terminate the varargs
items += [:string, nil]
rc = Native.sd_journal_send(*items)
raise JournalError.new(rc) if rc < 0
raise JournalError, rc if rc < 0
end
end
end
Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def entry_field(index, name)
require 'systemd/journal'

RSpec.configure do |config|

config.disable_monkey_patching!
config.include SpecHelper
end
2 changes: 1 addition & 1 deletion spec/systemd/id128_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

RSpec.describe Systemd::Id128 do
subject(:id128){ Systemd::Id128 }
subject(:id128) { Systemd::Id128 }

describe 'machine_id' do
it 'should be a 128 bit hexadecimal string' do
Expand Down
11 changes: 6 additions & 5 deletions spec/systemd/journal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
expect { j.new(container: '', path: '/') }.to raise_error(ArgumentError)
end

it 'throws an ArgumentError when attempting to open a container without support' do
allow(Systemd::Journal::Native).to receive(:open_container?).and_return(false)
it 'raises ArgumentError on attempt to open a container without support' do
allow(Systemd::Journal::Native).to receive(:open_container?)
.and_return(false)

expect { j.new(container: 'test') }.to raise_error(ArgumentError)
end
end
Expand Down Expand Up @@ -359,13 +361,13 @@
end

it 'can use select' do
pending "not available on JRUBY" if Systemd::Journal::IS_JRUBY
pending 'not available on JRUBY' if Systemd::Journal::IS_JRUBY
expect(Systemd::Journal::Native).to_not receive(:sd_journal_wait)
j.wait(1, select: true)
end

it 'ignores request to use select on JRuby' do
pending "not necessary on MRI" unless Systemd::Journal::IS_JRUBY
pending 'not necessary on MRI' unless Systemd::Journal::IS_JRUBY
expect(Systemd::Journal::Native).to receive(:sd_journal_wait)
j.wait(1, select: true)
end
Expand All @@ -380,5 +382,4 @@
expect([true, false]).to include(j.wait_select_reliable?)
end
end

end
1 change: 0 additions & 1 deletion spec/systemd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@
Systemd.boot_id
end
end

end
Loading

0 comments on commit aafc07d

Please sign in to comment.