Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frozen strings. Drop support for Ruby 2.2 and 2.3; add 2.6. #29

Merged
merged 5 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: ruby
rvm:
- 2.5.0
- 2.4.0
- 2.3.3
- 2.2.6
- 2.6.3
- 2.5.5
- 2.4.6
addons:
apt:
packages:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
-------------------------------------------------------------------
Wed Jun 12 08:43:33 UTC 2019 - Martin Vidner <mvidner@suse.com>

- Drop support for Ruby 2.2 and 2.3; add 2.6.

-------------------------------------------------------------------
Thu Nov 8 12:51:22 UTC 2018 - jreidinger@suse.com

Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "ruby-augeas"
Expand All @@ -8,5 +10,6 @@ group :development, :test do
end

group :development do
gem "rake"
gem "rubocop"
end
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "rake"
require "rspec/core/rake_task"

Expand Down
2 changes: 2 additions & 0 deletions cfa.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Gem::Specification.new do |s|
s.name = "cfa"
s.version = "0.7.0"
Expand Down
2 changes: 2 additions & 0 deletions lib/cfa/augeas_parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "set"
require "augeas"
require "forwardable"
Expand Down
2 changes: 2 additions & 0 deletions lib/cfa/augeas_parser/keys_cache.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module CFA
# A cache that holds all avaiable keys in an Augeas tree. It is used to
# prevent too many `aug.match` calls which are expensive.
Expand Down
2 changes: 2 additions & 0 deletions lib/cfa/augeas_parser/reader.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "cfa/augeas_parser/keys_cache"
require "cfa/augeas_parser"

Expand Down
2 changes: 2 additions & 0 deletions lib/cfa/augeas_parser/writer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module CFA
# The goal of this class is to write the data stored in {AugeasTree}
# back to Augeas.
Expand Down
30 changes: 24 additions & 6 deletions lib/cfa/base_model.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "cfa/matcher"
require "cfa/placer"
# FIXME: tree should be generic and not augeas specific,
Expand Down Expand Up @@ -172,43 +174,59 @@ def add_new(key, value, tree)
end
end

# Representing boolean value switcher in default grub configuration file.
# Represents a boolean value switcher in default grub configuration file.
# Allows easy switching and questioning for boolean value, even if
# represented by text in config file
# represented by text in config file.
# It's tristate: if unset, {#enabled?} and {#disabled?} return `nil`
# (but once set, we cannot return to an unset state).
class BooleanValue
# @param name [String]
# @param model [BaseModel]
# @param true_value [String]
# @param false_value [String]
def initialize(name, model, true_value: "true", false_value: "false")
@name = name
@model = model
@true_value = true_value
@false_value = false_value
end

# Set to *true*
def enable
@model.generic_set(@name, @true_value)
end

# Set to *false*
def disable
@model.generic_set(@name, @false_value)
end

# @return [Boolean,nil] true, false, (nil if undefined)
def enabled?
return nil unless data
d = data
return nil unless d

data == @true_value
d == @true_value
end

# @return [Boolean,nil] true, false, (nil if undefined)
def disabled?
return nil unless data
d = data
return nil unless d

data != @true_value
d != @true_value
end

# @return [Boolean]
# true if the key has a value;
# false if {#enabled?} and {#disabled?} return `nil`.
def defined?
!data.nil?
end

# sets boolean value, recommend to use for generic boolean setter.
# for constants prefer to use enable/disable
# @param value [Boolean]
def value=(value)
@model.generic_set(@name, value ? @true_value : @false_value)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/cfa/matcher.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module CFA
# The Matcher is used as a predicate on {AugeasElement}.
#
Expand Down
2 changes: 2 additions & 0 deletions lib/cfa/memory_file.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module CFA
# memory file is used when string is stored only in memory.
# Useful for testing. For remote read or socket read, own File class
Expand Down
4 changes: 3 additions & 1 deletion lib/cfa/placer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module CFA
# Places a new {AugeasElement} into an {AugeasTree}.
# @abstract Subclasses implement different ways **where**
Expand All @@ -10,7 +12,7 @@ class Placer
# documents its structure.
def new_element(_tree)
raise NotImplementedError,
"Subclasses of #{Module.nesting.first} must override #{__method__}"
"Subclasses of #{Module.nesting.first} must override #{__method__}"
end

protected
Expand Down
2 changes: 2 additions & 0 deletions performance_test/augeas_loader_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# use with `time ruby <path>` to test changes in library
# goal of this tests are to measure time, for correctness is used
# tests in spec directory
Expand Down
2 changes: 2 additions & 0 deletions spec/augeas_collection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "spec_helper"

require "cfa/augeas_parser"
Expand Down
2 changes: 2 additions & 0 deletions spec/augeas_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "spec_helper"

require "cfa/augeas_parser"
Expand Down
2 changes: 2 additions & 0 deletions spec/augeas_tree_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "spec_helper"

require "cfa/augeas_parser"
Expand Down
2 changes: 2 additions & 0 deletions spec/augeas_writer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "spec_helper"

require "cfa/augeas_parser"
Expand Down
105 changes: 104 additions & 1 deletion spec/base_model_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# frozen_string_literal: true

require_relative "spec_helper"
require "cfa/augeas_parser"
require "cfa/base_model"
require "cfa/memory_file"

# A testing model
class TestModel < CFA::BaseModel
PARSER = CFA::AugeasParser.new("postgresql.lns")
PATH = "/var/lib/pgsql/postgresql.conf".freeze
PATH = "/var/lib/pgsql/postgresql.conf"

attributes(
port: "port",
Expand All @@ -20,6 +23,7 @@ def initialize(file_handler: nil)
# Most models will use the AugeasParser that we supply but we
# must not rely on that. Test with a non-standard parser.
class NonAugeasModel < CFA::BaseModel
# Non-Augeas parser
class Parser
def parse(raw_string)
raw_string.size
Expand Down Expand Up @@ -99,3 +103,102 @@ def initialize(file_handler: nil)
end
end
end

describe CFA::BooleanValue do
let(:name) { "my_bool" }
let(:model) { TestModel.new }

subject { described_class.new(name, model) }

describe "#enabled?" do
it "returns nil when unset" do
expect(subject.enabled?).to be_nil
end

it "returns true when 'true'" do
expect(subject).to receive(:data).and_return("true")
expect(subject.enabled?).to eq(true)
end

it "returns false when 'false'" do
expect(subject).to receive(:data).and_return("false")
expect(subject.enabled?).to eq(false)
end

it "returns false when 'other'" do
expect(subject).to receive(:data).and_return("other")
expect(subject.enabled?).to eq(false)
end
end

describe "#disabled?" do
it "returns nil when unset" do
expect(subject.disabled?).to be_nil
end

it "returns false when 'true'" do
expect(subject).to receive(:data).and_return("true")
expect(subject.disabled?).to eq(false)
end

it "returns true when 'false'" do
expect(subject).to receive(:data).and_return("false")
expect(subject.disabled?).to eq(true)
end

it "returns true when 'other'" do
expect(subject).to receive(:data).and_return("other")
expect(subject.disabled?).to eq(true)
end
end

describe "#defined?" do
it "returns false when unset" do
expect(subject.defined?).to eq(false)
end

it "returns false when set" do
expect(subject).to receive(:data).and_return("whatever")
expect(subject.defined?).to eq(true)
end
end

describe "#value=" do
it "sets a true value" do
subject.value = true
expect(subject.enabled?).to eq(true)
end

it "sets a false value" do
subject.value = false
expect(subject.enabled?).to eq(false)
end
end

describe "#enable" do
it "sets a true value" do
subject.enable
expect(subject.enabled?).to eq(true)
end
end

describe "#disable" do
it "sets a false value" do
subject.disable
expect(subject.enabled?).to eq(false)
end
end

describe "#inspect" do
it "produces a nice description" do
expect(subject.inspect)
.to match(/#<
CFA::BooleanValue:0x.* \s
name=\"my_bool\", \s
data=nil, \s
true_value=\"true\", \s
false_value=\"false\"
>/x)
end
end
end
2 changes: 2 additions & 0 deletions spec/matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "spec_helper"
require "cfa/matcher"

Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)

def load_data(path)
Expand Down