test/signore/repo_test.rb in signore-0.6.0 vs test/signore/repo_test.rb in signore-0.7.0
- old
+ new
@@ -1,7 +1,5 @@
-# frozen_string_literal: true
-
require 'fileutils'
require 'pathname'
require 'tempfile'
require 'tmpdir'
require_relative '../test_helper'
@@ -9,12 +7,32 @@
require_relative '../../lib/signore/signature'
require_relative '../../lib/signore/tags'
module Signore
describe Repo do
- let(:path) { Pathname.new(Tempfile.new('').path) }
+ let(:path) { Pathname.new(Tempfile.new.path) }
+ describe '.default_path' do
+ it 'honours XDG_DATA_HOME if it’s set' do
+ old_xdg = ENV.delete('XDG_DATA_HOME')
+ ENV['XDG_DATA_HOME'] = Dir.mktmpdir
+ path = "#{ENV['XDG_DATA_HOME']}/signore/signatures.yml"
+ _(Repo.default_path).must_equal Pathname.new(path)
+ ensure
+ FileUtils.rmtree ENV['XDG_DATA_HOME']
+ old_xdg ? ENV['XDG_DATA_HOME'] = old_xdg : ENV.delete('XDG_DATA_HOME')
+ end
+
+ it 'defaults XDG_DATA_HOME to ~/.local/share if it’s not set' do
+ old_xdg = ENV.delete('XDG_DATA_HOME')
+ path = File.expand_path('~/.local/share/signore/signatures.yml')
+ _(Repo.default_path).must_equal Pathname.new(path)
+ ensure
+ ENV['XDG_DATA_HOME'] = old_xdg if old_xdg
+ end
+ end
+
describe '.new' do
it 'rewrites legacy file to hashes on first access' do
FileUtils.cp Pathname.new('test/fixtures/signatures.legacy.yml'), path
Repo.new(path: path)
_(path.read).wont_include 'Signore::Signature'
@@ -46,53 +64,33 @@
FileUtils.cp Pathname.new('test/fixtures/signatures.legacy.yml'), path
refute Repo.new(path: path).empty?
end
end
- describe '#find' do
- let(:path) { Pathname.new('test/fixtures/signatures.yml') }
- let(:repo) { Repo.new(path: path, sig_finder: sig_finder) }
- let(:sig_finder) { fake(SigFinder) }
- let(:sigs) { repo.sigs }
- let(:store) { YAML::Store.new(path) }
-
- it 'returns a random signature by default' do
- stub(sig_finder).find(sigs, tags: Tags.new) { sigs.last }
- _(repo.find).must_equal sigs.last
- end
-
- it 'returns a random signature based on required and forbidden tags' do
- tags = Tags.new(forbidden: %w(programming security), required: %w(tech))
- stub(sig_finder).find(sigs, tags: tags) { sigs.first }
- _(repo.find(tags: tags)).must_equal sigs.first
- end
-
- it 'doesn’t blow up if the path is missing' do
- begin
- tempdir = Dir.mktmpdir
- path = Pathname.new("#{tempdir}/some_intermediate_dir/sigs.yml")
- _(Repo.new(path: path).find(tags: Tags.new)).must_equal Signature.new
- ensure
- FileUtils.rmtree tempdir
- end
- end
-
- it 'keeps working with legacy YAML files' do
- path = Pathname.new(Tempfile.new('').path)
- FileUtils.cp Pathname.new('test/fixtures/signatures.legacy.yml'), path
- repo = Repo.new(path: path, sig_finder: sig_finder)
- stub(sig_finder).find(sigs, tags: Tags.new) { sigs.last }
- _(repo.find).must_equal sigs.last
- end
- end
-
describe '#sigs' do
it 'returns all the Signatures from the Repo' do
path = Pathname.new('test/fixtures/signatures.yml')
sigs = Repo.new(path: path).sigs
_(sigs.size).must_equal 6
_(sigs.first.author).must_equal 'Gary Barnes'
_(sigs.last.subject).must_equal 'Star Wars ending explained'
+ end
+
+ it 'keeps working with legacy YAML files' do
+ legacy_path = Pathname.new('test/fixtures/signatures.legacy.yml')
+ temp_path = Pathname.new(Tempfile.new.path)
+ FileUtils.cp legacy_path, temp_path
+ legacy_repo = Repo.new(path: temp_path)
+ new_repo = Repo.new(path: Pathname.new('test/fixtures/signatures.yml'))
+ _(legacy_repo.sigs).must_equal new_repo.sigs
+ end
+
+ it 'doesn’t blow up if the path is missing' do
+ tempdir = Dir.mktmpdir
+ path = Pathname.new("#{tempdir}/some_intermediate_dir/sigs.yml")
+ _(Repo.new(path: path).sigs).must_equal []
+ ensure
+ FileUtils.rmtree tempdir
end
end
end
end