Skip to content

Commit

Permalink
CLI#edit edits a file and returns the contents
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Dec 8, 2011
1 parent 68ae61c commit 84a6f4d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/springpad.rb
@@ -1,6 +1,7 @@
require_relative "springpad/version"
require_relative "springpad/api"
require_relative "springpad/blocks"
require_relative "springpad/cli"

module Springpad
end
15 changes: 15 additions & 0 deletions lib/springpad/cli.rb
@@ -0,0 +1,15 @@
require 'tempfile'

module Springpad
class CLI
# Public: Edits a file with $EDITOR and returns the contents.
#
# Returns an Array with the first line and an array of the other lines.
def edit
temp_file = Tempfile.new('block')
system("$EDITOR #{temp_file.path}")
contents = temp_file.read.chomp.split("\n").reject(&:empty?).map(&:strip)
[contents.first, contents[1..-1]]
end
end
end
24 changes: 24 additions & 0 deletions test/springpad/cli_test.rb
@@ -0,0 +1,24 @@
require 'test_helper'

module Springpad
describe CLI do
describe "#edit" do
it 'edits a file and returns the contents' do
cli = CLI.new

Tempfile.stubs(:new).returns file = stub_everything
file.stubs(:path).returns "/tmp/something"
file.stubs(:read).returns """Name of the task \n\n This is a description of the task\nThis is more body for the task\n"
cli.expects(:system).with("$EDITOR /tmp/something")

cli.edit.must_equal [
"Name of the task",
[
"This is a description of the task",
"This is more body for the task",
]
]
end
end
end
end

0 comments on commit 84a6f4d

Please sign in to comment.