Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ lib/bundler/man
pkg
rdoc
spec/reports
spec/source
test/tmp
test/version_tmp
tmp
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: ruby
rvm:
- 2.2
- 2.1
- 2.0
script: script/cibuild
sudo: false
cache: bundler
8 changes: 0 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
require "bundler/gem_tasks"
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end

task :default => :test
4 changes: 2 additions & 2 deletions jekyll-compose.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'jekyll/compose/version'
require 'jekyll-compose/version'

Gem::Specification.new do |spec|
spec.name = "jekyll-compose"
Expand All @@ -20,5 +20,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.5"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "jekyll", "~> 2.0"
spec.add_development_dependency "shoulda"
spec.add_development_dependency "rspec", "~> 3.0"
end
10 changes: 10 additions & 0 deletions lib/jekyll-compose.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "jekyll-compose/version"

module Jekyll
module Compose
end
end

%w{draft post publish}.each do |file|
require File.expand_path("jekyll/commands/#{file}.rb", File.dirname(__FILE__))
end
File renamed without changes.
18 changes: 9 additions & 9 deletions lib/jekyll/commands/draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def self.init_with_program(prog)
end
end

def self.process(args, options = {})
def self.process(args = [], options = {})
raise ArgumentError.new('You must specify a name.') if args.empty?
type = options["type"].nil? ? "markdown" : options["type"]
layout = options["layout"].nil? ? "post" : options["layout"]

type = options["type"] || "markdown"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider:

type = options.fetch("type", "markdown")

layout = options["layout"] || "post"

title = args.shift
name = title.gsub(' ', '-').downcase
Expand All @@ -34,7 +34,7 @@ def self.process(args, options = {})
end

puts "New draft created at ./#{draft_path}.\n"
end
end
# Internal: Gets the filename of the draft to be created
#
# Returns the filename of the draft, as a String
Expand All @@ -43,10 +43,10 @@ def self.draft_name(name, ext='markdown')
end

def self.front_matter(layout, title)
"---
layout: #{layout}
title: #{title}
---"
{
"layout" => layout,
"title" => title,
}.to_yaml + "\n---\n"
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/jekyll/commands/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def self.init_with_program(prog)
end
end

def self.process(args, options = {})
def self.process(args = [], options = {})
raise ArgumentError.new('You must specify a name.') if args.empty?

type = options["type"].nil? ? "markdown" : options["type"]
layout = options["layout"].nil? ? "post" : options["layout"]

Expand All @@ -37,7 +37,7 @@ def self.process(args, options = {})
end

puts "New post created at ./#{post_path}.\n"
end
end
# Internal: Gets the filename of the draft to be created
#
# Returns the filename of the draft, as a String
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll/commands/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.init_with_program(prog)
end
end

def self.process(args, options = {})
def self.process(args = [], options = {})
raise ArgumentError.new('You must specify a draft path.') if args.empty?

date = options["date"].nil? ? Date.today : Date.parse(options["date"])
Expand Down
10 changes: 0 additions & 10 deletions lib/jekyll/compose.rb

This file was deleted.

2 changes: 2 additions & 0 deletions script/bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
bundle install --jobs=8 --retry=3
2 changes: 2 additions & 0 deletions script/cibuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
script/test --format progress $@
2 changes: 2 additions & 0 deletions script/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
bundle exec rspec --color --require spec_helper $@
58 changes: 58 additions & 0 deletions spec/draft_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
RSpec.describe(Jekyll::Commands::Draft) do
let(:name) { 'A test post' }
let(:args) { [name] }
let(:drafts_dir) { Pathname.new source_dir('_drafts') }
let(:path) { drafts_dir.join('a-test-post.markdown') }

before(:all) do
FileUtils.mkdir_p source_dir unless File.directory? source_dir
Dir.chdir source_dir
end

before(:each) do
FileUtils.mkdir_p drafts_dir unless File.directory? drafts_dir
end

after(:each) do
FileUtils.rm_r drafts_dir if File.directory? drafts_dir
end

it 'creates a new draft' do
expect(path).not_to exist
capture_stdout { described_class.process(args) }
expect(path).to exist
end

it 'writes a helpful success message' do
output = capture_stdout { described_class.process(args) }
expect(output).to eql("New draft created at ./_drafts/a-test-post.markdown.\n")
end

it 'errors with no arguments' do
expect(-> {
capture_stdout { described_class.process }
}).to raise_error('You must specify a name.')
end

context 'when the draft already exists' do
let(:name) { 'An existing draft' }
let(:path) { drafts_dir.join('an-existing-draft.markdown') }

before(:each) do
FileUtils.touch path
end

it 'raises an error' do
expect(-> {
capture_stdout { described_class.process(args) }
}).to raise_error("A draft already exists at ./_drafts/an-existing-draft.markdown")
end

it 'overwrites if --force is given' do
expect(-> {
capture_stdout { described_class.process(args, 'force' => true) }
}).not_to raise_error
expect(File.read(path)).to match(/layout: post/)
end
end
end
59 changes: 59 additions & 0 deletions spec/post_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
RSpec.describe(Jekyll::Commands::Post) do
let(:name) { 'A test post' }
let(:args) { [name] }
let(:posts_dir) { Pathname.new source_dir('_posts') }
let(:filename) { "#{Time.now.strftime('%Y-%m-%d')}-a-test-post.markdown" }
let(:path) { posts_dir.join(filename) }

before(:all) do
FileUtils.mkdir_p source_dir unless File.directory? source_dir
Dir.chdir source_dir
end

before(:each) do
FileUtils.mkdir_p posts_dir unless File.directory? posts_dir
end

after(:each) do
FileUtils.rm_r posts_dir if File.directory? posts_dir
end

it 'creates a new post' do
expect(path).not_to exist
capture_stdout { described_class.process(args) }
expect(path).to exist
end

it 'should write a helpful message when successful' do
output = capture_stdout { described_class.process(args) }
expect(output).to eql("New post created at ./_posts/#{filename}.\n")
end

it 'errors with no arguments' do
expect(-> {
capture_stdout { described_class.process }
}).to raise_error('You must specify a name.')
end

context 'when the post already exists' do
let(:name) { 'An existing post' }
let(:filename) { "#{Time.now.strftime('%Y-%m-%d')}-an-existing-post.markdown" }

before(:each) do
FileUtils.touch path
end

it 'raises an error' do
expect(-> {
capture_stdout { described_class.process(args) }
}).to raise_error("A post already exists at ./_posts/#{filename}")
end

it 'overwrites if --force is given' do
expect(-> {
capture_stdout { described_class.process(args, 'force' => true) }
}).not_to raise_error
expect(File.read(path)).to match(/layout: post/)
end
end
end
55 changes: 55 additions & 0 deletions spec/publish_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
RSpec.describe(Jekyll::Commands::Publish) do
let(:drafts_dir) { source_dir('_drafts') }
let(:posts_dir) { source_dir('_posts') }
let(:draft_to_publish) { 'a-test-post.markdown' }
let(:post_filename) { "#{Time.now.strftime('%Y-%m-%d')}-#{draft_to_publish}" }
let(:args) { ["_drafts/#{draft_to_publish}"] }

let(:draft_path) { Pathname.new(File.join(drafts_dir, draft_to_publish)) }
let(:post_path) { Pathname.new(File.join(posts_dir, post_filename))}

before(:all) do
FileUtils.mkdir_p source_dir unless File.directory? source_dir
Dir.chdir source_dir
end

before(:each) do
FileUtils.mkdir_p drafts_dir unless File.directory? drafts_dir
FileUtils.mkdir_p posts_dir unless File.directory? posts_dir
FileUtils.touch draft_path
end

after(:each) do
FileUtils.rm_r drafts_dir if File.directory? drafts_dir
FileUtils.rm_r posts_dir if File.directory? posts_dir
FileUtils.rm_r draft_path if File.file? draft_path
FileUtils.rm_r post_path if File.file? post_path
end

it 'publishes a draft post' do
expect(Pathname.new(post_path)).not_to exist
expect(Pathname.new(draft_path)).to exist
capture_stdout { described_class.process(args) }
expect(Pathname.new(post_path)).to exist
end

it 'writes a helpful message on success' do
expect(Pathname.new(draft_path)).to exist
output = capture_stdout { described_class.process(args) }
expect(output).to eql("Draft _drafts/#{draft_to_publish} was published to ./_posts/#{post_filename}\n")
end

it 'errors if there is no argument' do
expect(-> {
capture_stdout { described_class.process }
}).to raise_error('You must specify a draft path.')
end

it 'errors if no file exists at given path' do
weird_path = '_drafts/i-do-not-exist.markdown'
expect(-> {
capture_stdout { described_class.process [weird_path] }
}).to raise_error("There was no draft found at '_drafts/i-do-not-exist.markdown'.")
end

end
57 changes: 57 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'jekyll'
require File.expand_path('../../lib/jekyll-compose.rb', __FILE__)

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.filter_run :focus
config.run_all_when_everything_filtered = true

config.disable_monkey_patching!

config.warnings = true

if config.files_to_run.one?
config.default_formatter = 'doc'
end

config.profile_examples = 3

config.order = :random

Kernel.srand config.seed

###
### Helper methods
###
TEST_DIR = File.expand_path('../', __FILE__)
def test_dir(*files)
File.expand_path(File.join(TEST_DIR, *files))
end

def source_dir(*files)
test_dir('source', *files)
end

def fixture_site
Jekyll::Site.new(Jekyll::Utils.deep_merge_hashes(
Jekyll::Configuration::DEFAULTS,
{ 'source' => source_dir, 'destination' => test_dir('dest') }
))
end

def capture_stdout
$old_stdout = $stdout
$stdout = StringIO.new
yield
$stdout.rewind
return $stdout.string
ensure
$stdout = $old_stdout
end
end
Loading