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