From 9362a4f4014c5d9a9e3629d1275d25fc34414e23 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Wed, 29 May 2024 12:19:16 +0200 Subject: [PATCH 1/2] Make symbolize keys optional rspec-puppet-facts has a setting to stringify keys again. Converting to symbols and then converting back to strings is inefficient. This parameter allows skipping the step. --- lib/facterdb.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/facterdb.rb b/lib/facterdb.rb index 4b623c38..661e794d 100644 --- a/lib/facterdb.rb +++ b/lib/facterdb.rb @@ -137,13 +137,17 @@ def self.valid_filters?(filters) # @return [Array[Hash[Symbol, Any]]] array of hashes of facts # @param filter [Object] The filter to convert to jgrep string - def self.get_facts(filter = nil, cache = true) + # @param symbolize_keys [Boolean] + # Whether to symbolize the keys. Note this is only on the top level and not + # on nested values. + def self.get_facts(filter = nil, cache = true, symbolize_keys: true) if cache && filter && filter == Thread.current[:facterdb_last_filter_seen] return Thread.current[:facterdb_last_facts_seen] end filter_str = generate_filter_str(filter) - result = JGrep.jgrep(database, filter_str).map { |hash| hash.map { |k, v| [k.to_sym, v] }.to_h } + result = JGrep.jgrep(database, filter_str) + result = result.map { |hash| hash.transform_keys(&:to_sym) } if symbolize_keys if cache Thread.current[:facterdb_last_filter_seen] = filter Thread.current[:facterdb_last_facts_seen] = result From 880b135cdb1c1821cab138bc27c05b88830ae72f Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Fri, 7 Jun 2024 14:06:00 +0200 Subject: [PATCH 2/2] Add tests for symbol conversion --- .rubocop_todo.yml | 39 ++++++++------------------------------- spec/facter_db_spec.rb | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e6dd8238..f2cb0d34 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-12-12 12:48:14 UTC using RuboCop version 1.57.2. +# on 2024-06-08 21:54:16 UTC using RuboCop version 1.63.5. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -20,11 +20,12 @@ Lint/SuppressedException: # Offense count: 3 # This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: AutoCorrect. Lint/UselessAssignment: Exclude: - 'spec/facts_spec.rb' -# Offense count: 2 +# Offense count: 3 # Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. # AllowedNames: as, at, by, cc, db, id, if, in, io, ip, of, on, os, pp, to Naming/MethodParameterName: @@ -38,7 +39,7 @@ Performance/MapCompact: Exclude: - 'lib/facterdb.rb' -# Offense count: 7 +# Offense count: 8 # This cop supports unsafe autocorrection (--autocorrect-all). Performance/StringInclude: Exclude: @@ -64,7 +65,7 @@ RSpec/DescribeClass: # Offense count: 12 # This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: SkipBlocks, EnforcedStyle. +# Configuration parameters: SkipBlocks, EnforcedStyle, OnlyStaticConstants. # SupportedStyles: described_class, explicit RSpec/DescribedClass: Exclude: @@ -79,7 +80,7 @@ RSpec/ExampleLength: RSpec/MultipleExpectations: Max: 7 -# Offense count: 11 +# Offense count: 19 # Configuration parameters: AllowSubject. RSpec/MultipleMemoizedHelpers: Max: 6 @@ -114,7 +115,7 @@ Style/Documentation: - 'lib/facterdb.rb' - 'lib/facterdb/bin.rb' -# Offense count: 13 +# Offense count: 12 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle. # SupportedStyles: always, always_true, never @@ -141,24 +142,6 @@ Style/HashEachMethods: Exclude: - 'spec/facts_spec.rb' -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -Style/HashTransformKeys: - Exclude: - - 'lib/facterdb.rb' - -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Style/IfUnlessModifier: - Exclude: - - 'lib/facterdb.rb' - -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -Style/MapToHash: - Exclude: - - 'lib/facterdb.rb' - # Offense count: 1 Style/MixinUsage: Exclude: @@ -189,12 +172,6 @@ Style/OptionalBooleanParameter: Exclude: - 'lib/facterdb.rb' -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -Style/SlicingWithRange: - Exclude: - - 'Rakefile' - # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: Mode. @@ -215,4 +192,4 @@ Style/SymbolProc: # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. # URISchemes: http, https Layout/LineLength: - Max: 179 + Max: 154 diff --git a/spec/facter_db_spec.rb b/spec/facter_db_spec.rb index 82b01626..cff6a28d 100644 --- a/spec/facter_db_spec.rb +++ b/spec/facter_db_spec.rb @@ -319,14 +319,35 @@ end describe '.get_facts' do - subject(:result) { FacterDB.get_facts(filter) } + subject(:result) { FacterDB.get_facts(filter, symbolize_keys: symbolize_keys) } let(:filter) { nil } + let(:symbolize_keys) { nil } context 'without parameters' do include_examples 'returns a result' end + context 'with stringified output' do + let(:symbolize_keys) { false } + + it 'returns strings as keys in factsets' do + result.each do |factset| + expect(factset.keys).to all(be_an_instance_of(String)) + end + end + end + + context 'with symbolized output' do + let(:symbolize_keys) { true } + + it 'returns strings as keys in factsets' do + result.each do |factset| + expect(factset.keys).to all(be_an_instance_of(Symbol)) + end + end + end + context 'with an Array filter' do let(:filter) { [osfamily: 'Debian'] }