Tests for `delete_config`

This commit is contained in:
Netscape Navigator 2019-10-26 18:13:06 -05:00
parent d71f1d1498
commit b083b7f855
6 changed files with 53 additions and 26 deletions

View File

@ -5,13 +5,7 @@ module Pigeon
NAME_OF_DRAFT = "HEAD.draft"
OUTBOX_PATH = File.join(".pigeon", "user")
attr_reader :author,
:kind,
:prev,
:body,
:depth,
:timestamp, # Maybe not?
:signature # Maybe not?
attr_reader :author, :kind, :prev, :body, :depth, :signature
def self.create(kind:, prev: nil, body: {})
self.new(author: KeyPair.current.public_key,
@ -20,16 +14,13 @@ module Pigeon
body: body).save
end
def initialize(author:,
kind:,
prev: nil,
body: [],
timestamp: Time.now.to_i)
def initialize(author:, kind:, prev: nil, body: {})
@author = author
@kind = kind
@prev = prev
@body = body
@timestamp = timestamp
@depth = calculate_depth
@prev = previous_message ? previous_message.signature : "NONE"
end
def self.current
@ -54,13 +45,11 @@ module Pigeon
end
def sign
# Set @depth
@depth = calculate_depth
@prev = previous_message ? previous_message.signature : "NONE"
@signature = calculate_signature
file_path = path_to_message_number(@depth)
self.freeze
File.write(file_path, Marshal.dump(self))
Pigeon::Storage.current.delete_config(NAME_OF_DRAFT)
self
end
@ -72,7 +61,8 @@ module Pigeon
private
def calculate_signature
string = Template.new(self).render_without_signature
template = Template.new(self)
string = template.render_without_signature
KeyPair.current.sign(string)
end
@ -81,15 +71,27 @@ module Pigeon
end
def previous_message
raise "Could not load @depth" unless @depth
return @previous_message if @previous_message
return @previous_message = nil if @depth == 1
if @depth.nil?
raise "Could not load @depth"
end
if @previous_message
puts "~~ Previous message set to " + @previous_message.inspect
return @previous_message
end
if @depth == 1
puts "~~ depth is 1!"
return @previous_message = nil
end
puts "~~ TADA!"
path = path_to_message_number(@depth - 1)
return @previous_message = Marshal.load(File.read(path))
@previous_message = Marshal.load(File.read(path))
end
def calculate_depth
Dir[OUTBOX_PATH].count
Dir[OUTBOX_PATH].count.tap { |x| "~~~ x is " + x.to_s }
end
def message_id # I need this to calculate `prev`.

View File

@ -31,8 +31,13 @@ module Pigeon
File.write(path, value.to_s)
end
def delete_config(key)
File.delete(conf_path_for(key))
end
def get_config(key)
File.read(conf_path_for(key))
f = conf_path_for(key)
File.read(f) if File.file?(f)
end
def set_blob(data)

View File

@ -43,6 +43,7 @@ echo "Making a new `scratch_pad` log entry"
echo "Appending values..."
echo "...string via pipe"
echo "my_value" | ./pigeon-cli message append key1
echo "...string with no quotes"
@ -68,11 +69,12 @@ echo "== sign (publish, save, commit, etc) draft message"
echo "=== add a second message to the db"
./pigeon-cli message create second_test
echo "=== append hello:'world' to message:"
./pigeon-cli message append hello "world"
./pigeon-cli message sign
echo "=== Sign message #2"
./pigeon-cli message show
./pigeon-cli message sign
echo "=== getting status:"
./pigeon-cli status

View File

@ -1,7 +1,7 @@
require "spec_helper"
RSpec.describe Pigeon::Config do
it "has a `foo`" do
it "has a `::VERSION`" do
expect(Pigeon::Config::VERSION).to eq("0.0.1")
end
end

View File

@ -0,0 +1,7 @@
require "spec_helper"
RSpec.describe Pigeon::Message do
include FakeFS::SpecHelpers
it "??"
end

View File

@ -12,6 +12,17 @@ RSpec.describe Pigeon::Storage do
end
end
it "deletes a config" do
test_fs do |s|
s.set_config("FOO", "BAR")
value = s.get_config("FOO")
expect(value).to eq("BAR")
s.delete_config("FOO")
value = s.get_config("FOO")
expect(value).to eq(nil)
end
end
it "manages configs" do
test_fs do |s|
s.set_config("FOO", "BAR")