From 9595c4c149869ba81e37a3df690ae5d81e64ecad Mon Sep 17 00:00:00 2001 From: alpaca-tc Date: Thu, 9 May 2024 15:58:13 +0900 Subject: [PATCH] When saving module_store, sort by source name. Fixed: #26 --- lib/diver_down/web/module_store.rb | 8 +++++- spec/diver_down/web/module_store_spec.rb | 32 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/diver_down/web/module_store.rb b/lib/diver_down/web/module_store.rb index 3665725..8b98f7e 100644 --- a/lib/diver_down/web/module_store.rb +++ b/lib/diver_down/web/module_store.rb @@ -31,7 +31,13 @@ def get(source_name) # @return [Hash] def to_h - @store.dup + sorted_store = {} + + @store.keys.sort.each do |key| + sorted_store[key] = @store[key] + end + + sorted_store end # Write store to file diff --git a/spec/diver_down/web/module_store_spec.rb b/spec/diver_down/web/module_store_spec.rb index ad2ca75..8eccd41 100644 --- a/spec/diver_down/web/module_store_spec.rb +++ b/spec/diver_down/web/module_store_spec.rb @@ -46,6 +46,38 @@ described_class.new(tempfile.path).get('a.rb') }.from([]).to(['A', 'B']) end + + it 'sorts by source name' do + tempfile = Tempfile.new(['test', '.yaml']) + instance = described_class.new(tempfile.path) + + sources = ['a.rb', 'b.rb', 'c.rb'] + + sources.shuffle.each do |source| + instance.set(source, ['A']) + end + + expect { + instance.flush + }.to change { + described_class.new(tempfile.path).instance_variable_get(:@store).keys + }.from([]).to(sources) + end + end + + describe '#to_h' do + it 'returns sorted hash' do + tempfile = Tempfile.new(['test', '.yaml']) + instance = described_class.new(tempfile.path) + + sources = ['a.rb', 'b.rb', 'c.rb'] + + sources.shuffle.each do |source| + instance.set(source, ['A']) + end + + expect(instance.to_h.keys).to eq(sources) + end end end end