add dirty checking and version support to HarpBlog

This commit is contained in:
Sami Samhuri 2014-10-18 15:33:52 -07:00
parent 9028d805ae
commit 3546ab2952
5 changed files with 112 additions and 12 deletions

1
public/version.txt Normal file
View file

@ -0,0 +1 @@
890b1508c132cd5ee5db1c28e9dced538b504d96

View file

@ -1,6 +1,7 @@
require 'fileutils'
require 'json'
require './web_title_finder'
require './web_version_finder'
class HarpBlog
@ -100,10 +101,31 @@ class HarpBlog
end # Post
def initialize(path, dry_run = true, title_finder = nil)
def initialize(path, dry_run = true, title_finder = nil, version_finder = nil)
@path = path
@dry_run = dry_run
@title_finder = title_finder || WebTitleFinder.new
@version_finder = version_finder || WebVersionFinder.new
end
def local_version
git_sha
end
def remote_version
@version_finder.find_version
end
def dirty?
local_version != remote_version
end
def status
{
'local-version' => local_version,
'remote-version' => remote_version,
'dirty' => dirty?,
}
end
def years
@ -353,6 +375,14 @@ class HarpBlog
end
end
def git_sha
if output = run('git log -n1 | head -n1 | cut -d" " -f2')
output.strip
else
'fake-sha'
end
end
def git_commit(title, *files)
quoted_files = files.map { |f| "\"#{quote(f)}\"" }
message = "linked '#{quote(title)}'"

View file

@ -143,7 +143,10 @@ RSpec.describe HarpBlog do
before :each do
@test_blog_ref = git_sha(TEST_BLOG_PATH)
dry_run = false
@blog = HarpBlog.new(TEST_BLOG_PATH, dry_run)
@mock_title = 'fancy title'
@mock_title_finder = mock_title_finder(@mock_title)
@mock_version_finder = mock_version_finder(@test_blog_ref)
@blog = HarpBlog.new(TEST_BLOG_PATH, dry_run, @mock_title_finder, @mock_version_finder)
end
after :each do
@ -161,6 +164,40 @@ RSpec.describe HarpBlog do
end
end
describe '#local_version' do
it "should expose the local version" do
expect(@blog.local_version).to eq(@test_blog_ref)
end
end
describe '#remote_version' do
it "should expose the remote version" do
expect(@blog.remote_version).to eq(@test_blog_ref)
end
end
describe '#dirty?' do
it "should specify whether or not there are unpublished changes" do
expect(@blog.dirty?).to be_falsy
@blog.create_post('title', 'body', nil)
expect(@blog.dirty?).to be_truthy
@blog.publish
@mock_version_finder.version = @blog.local_version
expect(@blog.dirty?).to be_falsy
end
end
describe '#status' do
it "should expose the local and remote versions, and dirty state" do
status = @blog.status
expect(status['local-version']).to eq(@blog.local_version)
expect(status['remote-version']).to eq(@blog.remote_version)
expect(status['dirty']).to eq(@blog.dirty?)
end
end
describe '#years' do
it "should return all of the years with posts" do
# yup, if I don't blog for an entire year that's a bug!
@ -247,16 +284,11 @@ RSpec.describe HarpBlog do
end
it "should fetch titles if necessary" do
class TitleFinder
def find_title(url) 'fancy title' end
end
dry_run = false
blog = HarpBlog.new(TEST_BLOG_PATH, dry_run, TitleFinder.new)
post = blog.create_post(nil, nil, 'http://samhuri.net')
expect(post.title).to eq('fancy title')
blog.delete_post(post.time.year.to_s, post.padded_month, post.slug)
post = blog.create_post(" \t\n", nil, 'http://samhuri.net')
expect(post.title).to eq('fancy title')
post = @blog.create_post(nil, nil, 'http://samhuri.net')
expect(post.title).to eq(@mock_title)
@blog.delete_post(post.time.year.to_s, post.padded_month, post.slug)
post = @blog.create_post(" \t\n", nil, 'http://samhuri.net')
expect(post.title).to eq(@mock_title)
end
it "should push the new post to the origin repo" do

View file

@ -42,4 +42,30 @@ module Helpers
end
end
class TitleFinder
attr_accessor :title
def initialize(title)
@title = title
end
def find_title(url) @title end
end
def mock_title_finder(title)
TitleFinder.new(title)
end
class VersionFinder
attr_accessor :version
def initialize(version)
@version = version
end
def find_version(url = nil)
@version
end
end
def mock_version_finder(version)
VersionFinder.new(version)
end
end

View file

@ -0,0 +1,11 @@
require 'open-uri'
class WebVersionFinder
DEFAULT_URL = 'http://samhuri.net/version.txt'
def find_version(url = nil)
open(url || DEFAULT_URL).read.strip
end
end