Done with tests. TODO: Message management, bundler, codecov badge (v. important)

This commit is contained in:
Netscape Navigator 2019-10-09 20:27:48 -05:00
parent 40a8a46aa4
commit fd517e6117
2 changed files with 56 additions and 7 deletions

View File

@ -26,6 +26,7 @@ module Pigeon
def set_conf(key, value)
path = conf_path_for(key)
maybe_touch(path)
File.write(path, value.to_s)
end
@ -50,11 +51,6 @@ module Pigeon
File.file?(f) ? File.read(f) : nil
end
def urlsafe_base64(identity)
Base64
.urlsafe_decode64(KeyPair.strip_headers(identity))
end
def add_peer(identity)
path = KeyPair.strip_headers(identity)
FileUtils.mkdir_p(File.join(peer_dir, path))
@ -80,6 +76,12 @@ module Pigeon
.map { |x| KeyPair.add_headers(x) }
end
def all_blocks
Dir[File.join(block_dir, "*")]
.map { |x| File.split(x).last }
.map { |x| KeyPair.add_headers(x) }
end
private
def initialized?
@ -112,6 +114,10 @@ module Pigeon
File.join(first_part, hex_hash_string[2..-1])
end
def maybe_touch(path)
FileUtils.touch(path) unless File.file?(path)
end
def conf_path_for(key)
File.join(conf_dir, key.to_s)
end

View File

@ -2,9 +2,52 @@ require "spec_helper"
RSpec.describe Pigeon::Storage do
include FakeFS::SpecHelpers
it "does something to the filesystem" do
LOGO_BLOB = File.read("./logo.png")
IDS = %w(@_TlC2z3FT4fimecC4eytrBhOwhLUZsVBZEZriBO9cWs=.ed25519
@28FyT7evjcYrrwngr8G2V1HZ0ODK0VPsFctDEZwfZJc=.ed25519
@ExA5Fmld-vMDjROfN30G5pmSp_261QILFP3qe64iDn8=.ed25519
@galdahnB3L2DE2cTU0Me54IpIUKVEgKmBwvZVtWJccg=.ed25519
@I6cN_IE9iPmH05xXnlI_WyLqnrAoKv1plUKWfiGSSK4=.ed25519
@JnCKDs5tIzY9OF--GFT94Qj5jHtK7lTxqCt1tmPcwjM=.ed25519
@q-_9BTnTThvW2ZGkmy8D3j-hW9ON2PNa3nwbCQgRw-g=.ed25519
@VIim19-PzaavRICicQg4c4z08SoWTa1tr2e-kfhmm0Y=.ed25519)
def test_fs
FakeFS.with_fresh do
expect(2 + 2).to eq(4)
yield(Pigeon::Storage.new)
end
end
it "manages configs" do
test_fs do |s|
s.set_conf("FOO", "BAR")
value = s.get_conf("FOO")
expect(value).to eq("BAR")
end
end
it "manages blobs" do
test_fs do |s|
logo_hash = s.set_blob(LOGO_BLOB)
expect(s.get_blob(logo_hash)).to eq(LOGO_BLOB)
end
end
it "manages peers" do
test_fs do |s|
s.add_peer(IDS[0])
s.add_peer(IDS[1])
expect(s.all_peers).to include(IDS[0])
expect(s.all_peers).to include(IDS[1])
s.remove_peer(IDS[0])
expect(s.all_peers).not_to include(IDS[0])
expect(s.all_blocks).not_to include(IDS[0])
s.block_peer(IDS[1])
expect(s.all_peers).not_to include(IDS[1])
expect(s.all_blocks).to include(IDS[1])
expect(s.all_blocks.count).to eq(1)
end
end
end