Skip to content

Commit

Permalink
add rubocop autofixes for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaffen committed Apr 6, 2023
1 parent 4c7a8e7 commit 472b187
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 66 deletions.
8 changes: 4 additions & 4 deletions app/models/data_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def self.search(environment, key)
result = {}
HieraData.new(environment.name).files_including(key.name).each do |file|
hierarchies[file[:hierarchy_name]] ||= Hierarchy.new(
environment: environment,
environment:,
name: file[:hierarchy_name],
backend: file[:hierarchy_backend]
)
data_file = new(
hierarchy: hierarchies[file[:hierarchy_name]],
path: file[:path]
)
value = Value.new(data_file: data_file, key: key, value: file[:value])
value = Value.new(data_file:, key:, value: file[:value])
result[hierarchies[file[:hierarchy_name]]] ||= {}
result[hierarchies[file[:hierarchy_name]]][data_file] = value
end
Expand All @@ -38,7 +38,7 @@ def initialize(attributes = {})

def keys
@keys ||= hiera_data.keys_in_file(hierarchy.name, path).map do |key_name|
Key.new(environment: environment, name: key_name)
Key.new(environment:, name: key_name)
end
end

Expand All @@ -48,7 +48,7 @@ def has_key?(key)

def value_for(key:)
raw_value = (hiera_data.value_in_file(hierarchy.name, path, key.name, facts: node&.facts) if has_key?(key)) # rubocop:disable Style/PreferredHashMethods
Value.new(data_file: self, key: key, value: raw_value)
Value.new(data_file: self, key:, value: raw_value)
end

def writable?
Expand Down
22 changes: 11 additions & 11 deletions app/models/hiera_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def initialize(environment)
def all_keys(facts)
keys = []
config.hierarchies.each do |hierarchy|
hierarchy.resolved_paths(facts: facts).each do |path|
file = DataFile.new(path: hierarchy.datadir.join(path), facts: facts)
hierarchy.resolved_paths(facts:).each do |path|
file = DataFile.new(path: hierarchy.datadir.join(path), facts:)
keys.concat(file.keys)
end
end
Expand All @@ -36,12 +36,12 @@ def all_keys(facts)

def files_for(hierarchy_name, facts: {})
hierarchy = find_hierarchy(hierarchy_name)
hierarchy.resolved_paths(facts: facts)
hierarchy.resolved_paths(facts:)
end

def file_attributes(hierarchy_name, path, facts: nil)
hierarchy = find_hierarchy(hierarchy_name)
file = DataFile.new(path: hierarchy.datadir.join(path), facts: facts)
file = DataFile.new(path: hierarchy.datadir.join(path), facts:)
{
exist: file.exist?,
writable: file.writable?,
Expand All @@ -56,16 +56,16 @@ def keys_in_file(hierarchy_name, path)

def value_in_file(hierarchy_name, path, key, facts: {})
hierarchy = find_hierarchy(hierarchy_name)
file = DataFile.new(path: hierarchy.datadir.join(path), facts: facts)
file = DataFile.new(path: hierarchy.datadir.join(path), facts:)
file.content_for_key(key)
end

def search_key(hierarchy_name, key, facts: nil)
hierarchy = find_hierarchy(hierarchy_name)
files = facts ? hierarchy.resolved_paths(facts: facts) : hierarchy.candidate_files
files = facts ? hierarchy.resolved_paths(facts:) : hierarchy.candidate_files
search_results = {}
files.each do |path|
file = DataFile.new(path: hierarchy.datadir.join(path), facts: facts)
file = DataFile.new(path: hierarchy.datadir.join(path), facts:)
search_results[path] = {
file_present: file.exist?,
file_writable: file.writable?,
Expand All @@ -84,7 +84,7 @@ def files_including(key)
next unless search_result[:key_present]

{
path: path,
path:,
hierarchy_name: hierarchy.name,
hierarchy_backend: hierarchy.backend,
value: search_result[:value]
Expand All @@ -95,13 +95,13 @@ def files_including(key)

def write_key(hierarchy_name, path, key, value, facts: {})
hierarchy = find_hierarchy(hierarchy_name)
read_file = DataFile.new(path: hierarchy.datadir.join(path), facts: facts)
read_file = DataFile.new(path: hierarchy.datadir.join(path), facts:)
read_file.write_key(key, value)
end

def remove_key(hierarchy_name, path, key, facts: {})
hierarchy = find_hierarchy(hierarchy_name)
read_file = DataFile.new(path: hierarchy.datadir.join(path), facts: facts)
read_file = DataFile.new(path: hierarchy.datadir.join(path), facts:)
read_file.remove_key(key)
end

Expand All @@ -127,7 +127,7 @@ def encrypt_value(hierarchy_name, value)
def lookup_options(facts)
result = {}
config.hierarchies.each do |hierarchy|
hierarchy.resolved_paths(facts: facts).each do |path|
hierarchy.resolved_paths(facts:).each do |path|
file = DataFile.new(path: hierarchy.datadir.join(path))
result = (file["lookup_options"] || {}).merge(result)
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/hiera_data/hierarchy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def paths

def resolved_paths(facts:)
paths.flat_map do |path|
resolved_path = Interpolation.interpolate_facts(path: path, facts: facts)
resolved_path = Interpolation.interpolate_facts(path:, facts:)
if uses_globs?
resolved_path = Interpolation
.interpolate_globs(path: resolved_path, datadir: datadir)
.interpolate_globs(path: resolved_path, datadir:)
end
resolved_path
end
Expand All @@ -80,7 +80,7 @@ def resolved_paths(facts:)
def candidate_files
paths.flat_map do |path|
globbed_path = Interpolation.replace_variables_with_globs(path)
Interpolation.interpolate_globs(path: globbed_path, datadir: datadir)
Interpolation.interpolate_globs(path: globbed_path, datadir:)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/hiera_data/interpolation.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class HieraData
module Interpolation
VARIABLE_REGEXP = /%{(::)?(facts|trusted)\.([^}]+)}/.freeze
VARIABLE_REGEXP = /%{(::)?(facts|trusted)\.([^}]+)}/

module_function

Expand Down
4 changes: 2 additions & 2 deletions app/models/hierarchy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Hierarchy < HieraModel
def self.all(environment)
HieraData.new(environment.name).hierarchies.map do |hierarchy|
new(name: hierarchy.name,
environment: environment,
environment:,
backend: hierarchy.backend,
encryptable: hierarchy.encryptable?)
end
Expand All @@ -19,7 +19,7 @@ def self.find(environment, name)

def files_for(node:)
hiera_data.files_for(name, facts: node.facts).map do |data_file|
DataFile.new(hierarchy: self, path: data_file, node: node)
DataFile.new(hierarchy: self, path: data_file, node:)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.all_for(node, environment: node.environment)
keys = []
HieraData.new(environment.name).all_keys(facts)
.each do |name|
key = new(environment: environment, name: name)
key = new(environment:, name:)
if name == "lookup_options"
keys.unshift(key)
else
Expand Down
4 changes: 2 additions & 2 deletions app/models/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize
end

def authenticate(email, password)
ldap.bind_as(filter: "(mail=#{email})", password: password) if email.present? && password.present?
ldap.bind_as(filter: "(mail=#{email})", password:) if email.present? && password.present?
end

private
Expand All @@ -25,7 +25,7 @@ def ldap
host: @host,
port: @port,
base: @base_dn,
encryption: encryption
encryption:
)
ldap.authenticate @bind_dn, @bind_dn_password if @bind_dn
ldap
Expand Down
4 changes: 2 additions & 2 deletions app/models/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def encrypted?
def update(new_value, node: nil)
parsed_value = YAML.safe_load(new_value)
facts = node ? node.facts : {}
hiera_data.write_key(data_file.hierarchy.name, data_file.path, key.name, parsed_value, facts: facts)
hiera_data.write_key(data_file.hierarchy.name, data_file.path, key.name, parsed_value, facts:)
end

def destroy(node: nil)
facts = node ? node.facts : {}
hiera_data.remove_key(data_file.hierarchy.name, data_file.path, key.name, facts: facts)
hiera_data.remove_key(data_file.hierarchy.name, data_file.path, key.name, facts:)
end
end
2 changes: 1 addition & 1 deletion test/models/environment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class EnvironmentTest < ActiveSupport::TestCase

test "#hierarchies loads all hierarchies" do
environment = Environment.new(name: "development")
hierarchies = [Hierarchy.new(environment: environment, name: "test", backend: :yaml)]
hierarchies = [Hierarchy.new(environment:, name: "test", backend: :yaml)]
Hierarchy.stub(:all, hierarchies) do
assert_equal hierarchies, environment.hierarchies
end
Expand Down
40 changes: 20 additions & 20 deletions test/models/hiera_data/hierarchy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ class HieraData::HierarchyTest < ActiveSupport::TestCase
test "#uses_globs? returns true if `glob` key present" do
glob = "/*/singular/glob"
raw_hash = {"name" => "Singular path", "glob" => glob}
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert hierarchy.uses_globs?
end

test "#uses_globs? returns true if `globs` key present" do
globs = ["/*/array/globs", "/test/**/globs"]
raw_hash = {"name" => "Singular path", "globs" => globs}
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert hierarchy.uses_globs?
end

test "#paths supports the singular `path` setting" do
path = "/test/singular/path"
raw_hash = {"name" => "Singular path", "path" => path}
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert_equal [path], hierarchy.paths
end

test "#paths returns all non-interpolated path names" do
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
expected_paths = [
"nodes/%{::facts.fqdn}.yaml",
"role/%{::facts.role}-%{::facts.env}.yaml",
Expand All @@ -42,19 +42,19 @@ class HieraData::HierarchyTest < ActiveSupport::TestCase
test "#paths supports the singular `glob` setting" do
glob = "/*/singular/glob"
raw_hash = {"name" => "Singular path", "glob" => glob}
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert_equal [glob], hierarchy.paths
end

test "#paths supports the `globs` array setting" do
globs = ["/*/array/globs", "/test/**/globs"]
raw_hash = {"name" => "Singular path", "globs" => globs}
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert_equal globs, hierarchy.paths
end

test "#resolved_paths uses facts to resolve paths" do
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
facts = {
"fqdn" => "testhost",
"role" => "hdm_test",
Expand All @@ -68,32 +68,32 @@ class HieraData::HierarchyTest < ActiveSupport::TestCase
"zone/internal.yaml",
"common.yaml"
]
assert_equal expected_resolved_paths, hierarchy.resolved_paths(facts: facts)
assert_equal expected_resolved_paths, hierarchy.resolved_paths(facts:)
end

test "#resolved_paths resolves globs" do
base_path = Rails.root.join("test/fixtures/files/puppet/environments/globs")
globs = ["common/*.yaml"]
raw_hash = {"name" => "Common", "datadir" => "data", "globs" => globs}
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: base_path)
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path:)
facts = {"fqdn" => "testhost"}
expected_resolved_paths = [
"common/foobar.yaml",
"common/hdm.yaml"
]
assert_equal expected_resolved_paths, hierarchy.resolved_paths(facts: facts)
assert_equal expected_resolved_paths, hierarchy.resolved_paths(facts:)
end

test "#name returns the existing name" do
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert_equal "Yaml hierarchy", hierarchy.name
end

test "#candidate_files returns files matching paths with variables replaced by globs" do
base_path = Rails.root.join("test/fixtures/files/puppet/environments/multiple_hierarchies")
hierarchy = HieraData::Hierarchy.new(
raw_hash: raw_hash.merge("datadir" => "data"),
base_path: base_path
base_path:
)
expected_candidate_files = [
"nodes/4msusyei.betadots.training.yaml",
Expand Down Expand Up @@ -121,7 +121,7 @@ def raw_hash

class HieraData::HierarchyForYamlDataTest < ActiveSupport::TestCase
test "data_hash specified and yaml" do
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert_equal :yaml, hierarchy.backend
end

Expand All @@ -135,7 +135,7 @@ def raw_hash

class HieraData::HierarchyForJSONDataTest < ActiveSupport::TestCase
test "data_hash specified and json" do
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert_equal :json, hierarchy.backend
end

Expand All @@ -158,30 +158,30 @@ class HieraData::HierarchyForEyamlDataTest < ActiveSupport::TestCase
end

test "lookup function is not data_hash" do
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert_equal :eyaml, hierarchy.backend
end

test "#private_key returns path from options" do
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert_equal "private.key", hierarchy.private_key.to_s
end

test "#public_key returns path from options" do
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: ".")
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: ".")
assert_equal "public.key", hierarchy.public_key.to_s
end

test "#encryptable? is true if all keys present and readable" do
tmpdir_path = Pathname.new(@tmpdir)
%w(private.key public.key).each { |f| FileUtils.touch(tmpdir_path.join(f)) }
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: @tmpdir)
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: @tmpdir)

assert hierarchy.encryptable?
end

test "#encryptable? is false if a key is missing" do
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: @tmpdir)
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: @tmpdir)

refute hierarchy.encryptable?
end
Expand All @@ -193,7 +193,7 @@ class HieraData::HierarchyForEyamlDataTest < ActiveSupport::TestCase
FileUtils.touch(path)
File.chmod(0000, path)
end
hierarchy = HieraData::Hierarchy.new(raw_hash: raw_hash, base_path: @tmpdir)
hierarchy = HieraData::Hierarchy.new(raw_hash:, base_path: @tmpdir)

refute hierarchy.encryptable?

Expand Down
Loading

0 comments on commit 472b187

Please sign in to comment.