robot-vim 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -26,23 +26,33 @@ Or create an instance with a specific Vim
26
26
  ### Running commands
27
27
  Commands are passed in as a string with one command per line.
28
28
 
29
+ The input file can be specified as a path to an existing input file, or as a string which RobotVim will turn into an short lived file backed buffer.
30
+
29
31
  commands = <<-COMMANDS
30
32
  RunSomeCoolCommand
31
33
  SomeOtherCoolCommand
32
34
  COMMANDS
33
35
 
36
+
34
37
  buffer_text = robot.run(:input_file => "some/file.txt", :commands => commands)
35
38
 
39
+ input = <<-CONTENT
40
+ This text will be used
41
+ as the contents of my input file
42
+ CONTENT
43
+
44
+ buffer_text = robot.run(:input_file => input, :commands => commands)
45
+
36
46
 
37
47
  ### Making Assertions
38
48
  Use your preferred Ruby testing library to make assertions about the buffer text string returned by RobotVim::Runner#run
39
49
 
40
- See spec/integration\_spec.rb for an example of sorting a file and making an assertion.
50
+ spec/integration\_spec.rb contains examples of asserting with Rspec
41
51
 
42
52
  ## TODO
43
53
  - automatically save buffer to an output file after running the last command(done)
44
54
  - automatically close Vim after running the last command(done)
45
- - take a string for input and write out a temporary file that Vim will run against
55
+ - take a string for input and write out a temporary file that Vim will run against(done)
46
56
  - figure out if there is a way to specify a .vimrc file without disabling the normal Vim initialization process
47
57
 
48
58
  ## Author
@@ -1,3 +1,5 @@
1
1
  require 'uuid'
2
2
  require "robot-vim/runner"
3
3
  require "robot-vim/script_file"
4
+ require "robot-vim/input_file"
5
+ require "robot-vim/file_name_generator"
@@ -0,0 +1,7 @@
1
+ module RobotVim
2
+ class FileNameGenerator
3
+ def self.generate
4
+ UUID.new.generate(:compact)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ module RobotVim
2
+ class InputFile
3
+ def self.path_for(input, &block)
4
+ if File.exists?(input)
5
+ yield_existing_file_path(input, block)
6
+ else
7
+ yield_on_demand_file_path(input, block)
8
+ end
9
+ end
10
+
11
+
12
+ def self.yield_existing_file_path(input, block)
13
+ block.call(input)
14
+ end
15
+
16
+ def self.yield_on_demand_file_path(input, block)
17
+ file_name = FileNameGenerator.generate
18
+ File.open(file_name, "w+") do |file|
19
+ file << input
20
+ end
21
+ block.call(file_name)
22
+ ensure
23
+ File.delete(file_name)
24
+ end
25
+ end
26
+ end
@@ -11,21 +11,36 @@ module RobotVim
11
11
  end
12
12
 
13
13
  def run(args={})
14
- output_file_name = UUID.new.generate(:compact)
15
- commands = args[:commands] + ":w #{output_file_name}"
16
- commands = commands + "\n:%bd!\n:q!\n"
17
- invoke_vim(commands, args[:input_file])
18
- return File.read(output_file_name)
14
+ output_file_name = RobotVim::FileNameGenerator.generate
15
+ commands = args[:commands] + output_write_command(output_file_name) + vim_close_commands
16
+
17
+ InputFile.path_for(args[:input_file]) do |input_file_path|
18
+ ScriptFile.open(commands) do |script_file_path|
19
+ invoke_vim(script_file_path, input_file_path)
20
+ end
21
+ end
22
+
23
+ return read_output_file_contents(output_file_name)
19
24
  ensure
20
25
  File.delete(output_file_name) if File.exists?(output_file_name)
21
26
  end
22
27
 
23
28
  private
24
-
25
- def invoke_vim(commands, input_file)
26
- ScriptFile.open(commands) do |script_file_path|
27
- Kernel.send(:`, "#{self.vim_binary} -n -s #{script_file_path} #{input_file}")
28
- end
29
+
30
+ def invoke_vim(script_file_path, input_file)
31
+ Kernel.send(:`, "#{self.vim_binary} -n -s #{script_file_path} #{input_file}")
32
+ end
33
+
34
+ def output_write_command(output_file_name)
35
+ ":w #{output_file_name}"
36
+ end
37
+
38
+ def vim_close_commands
39
+ "\n:%bd!\n:q!\n"
40
+ end
41
+
42
+ def read_output_file_contents(output_file_name)
43
+ File.read(output_file_name)
29
44
  end
30
45
  end
31
46
  end
@@ -2,7 +2,7 @@ module RobotVim
2
2
  class ScriptFile
3
3
 
4
4
  def self.open(commands)
5
- file_name = UUID.new.generate(:compact)
5
+ file_name = FileNameGenerator.generate
6
6
  script_file = File.new(file_name, "w")
7
7
  script_file << commands
8
8
  script_file.flush
@@ -1,5 +1,5 @@
1
1
  module Robot
2
2
  module Vim
3
- VERSION = "0.9.0"
3
+ VERSION = "0.10.0"
4
4
  end
5
5
  end
@@ -1,16 +1,33 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "Automating Vim with RobotVim" do
4
- it "can sort a file using vim" do
5
- input_path = File.join(File.dirname(__FILE__), "fixtures", "unsorted_file.txt")
6
- unsorted_text = File.read(input_path)
4
+ let(:runner){RobotVim::Runner.new}
7
5
 
8
- commands = <<-COMMANDS
6
+ context "When using an existing input file" do
7
+ it "can sort a file using vim" do
8
+ input_path = File.join(File.dirname(__FILE__), "fixtures", "unsorted_file.txt")
9
+ unsorted_text = File.read(input_path)
10
+
11
+ commands = <<-COMMANDS
9
12
  :%!sort
10
- COMMANDS
13
+ COMMANDS
14
+
15
+ result = runner.run(:commands => commands, :input_file => input_path)
16
+ result.should == unsorted_text.split("\n").sort.join("\n") + "\n"
17
+ end
18
+ end
11
19
 
12
- runner = RobotVim::Runner.new
13
- result = runner.run(:commands => commands, :input_file => input_path)
14
- result.should eql(unsorted_text.split("\n").sort.join("\n") + "\n")
20
+ context "When using a string for input" do
21
+ it "can uppercase the first line" do
22
+ text_to_uppercase = "this line should be uppercased"
23
+
24
+ commands = <<-COMMANDS
25
+ :normal 0|gU$
26
+ COMMANDS
27
+
28
+ result = runner.run(:commands => commands, :input_file => text_to_uppercase)
29
+ result.should == text_to_uppercase.upcase + "\n"
30
+ end
15
31
  end
32
+
16
33
  end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe RobotVim::FileNameGenerator do
4
+ describe "generating a file name" do
5
+ it "returns a unique name each time" do
6
+ names = (1..5).map{|x| RobotVim::FileNameGenerator.generate}
7
+ names.uniq.size.should eql(names.size)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe RobotVim::InputFile do
4
+ describe "path_for" do
5
+ context "given a file path that exists" do
6
+ it "yields the file path" do
7
+ File.stub(:exists?).and_return(true)
8
+ expected_path = "/some/path/file.txt"
9
+ RobotVim::InputFile.path_for(expected_path) do |path|
10
+ path.should == expected_path
11
+ end
12
+ end
13
+ end
14
+
15
+ context "given contents for a file" do
16
+ let(:file_contents) do
17
+ <<-CONTENTS
18
+ Hi there.
19
+ This will serve as our input file.
20
+ CONTENTS
21
+ end
22
+
23
+ it "creates a new file with those contents" do
24
+ RobotVim::InputFile.path_for(file_contents) do |path|
25
+ File.read(path).should == file_contents
26
+ end
27
+ end
28
+
29
+ it "cleans up the file when we are done with it" do
30
+ file_name = "some_file_name"
31
+ RobotVim::FileNameGenerator.stub(:generate).and_return(file_name)
32
+ RobotVim::InputFile.path_for(file_contents) do |path|
33
+ File.exists?(file_name).should be_true
34
+ end
35
+ File.exists?(file_name).should be_false
36
+ end
37
+ end
38
+ end
39
+ end
@@ -21,11 +21,11 @@ describe RobotVim::Runner do
21
21
  let(:runner){RobotVim::Runner.new(:vim => vim_path)}
22
22
  let(:commands){"some vim commands\n"}
23
23
  let(:input_file){"some/path/to/a/file"}
24
- let(:output_file_name){"output_file_name"}
25
24
 
26
25
  before do
27
26
  Kernel.stub(:`)
28
- File.stub(:read)
27
+ runner.stub(:read_output_file_contents)
28
+ RobotVim::InputFile.stub(:path_for).and_yield(input_file)
29
29
  end
30
30
 
31
31
  def run_robot
@@ -73,20 +73,25 @@ describe RobotVim::Runner do
73
73
  RobotVim::ScriptFile.should_receive(:open).with(/:%bd!\n:q!/)
74
74
  run_robot
75
75
  end
76
-
77
- it "returns the contents of the output file" do
78
- UUID.stub_chain(:new, :generate => output_file_name)
79
- expected_result = "awesome buffer"
80
- File.stub(:read).with(output_file_name).and_return(expected_result)
81
- result = run_robot
82
- result.should == expected_result
83
- end
84
76
 
85
- it "deletes the output file" do
86
- output_file_name = "somefile"
87
- UUID.stub_chain(:new, :generate => output_file_name)
88
- run_robot
89
- File.exists?(output_file_name).should be_false
77
+ describe "output file management" do
78
+ let(:output_file_name){"output_file_name"}
79
+
80
+ before do
81
+ RobotVim::FileNameGenerator.stub(:generate).and_return(output_file_name)
82
+ end
83
+
84
+ it "returns the contents of the output file" do
85
+ expected_result = "awesome buffer"
86
+ runner.stub(:read_output_file_contents).with(output_file_name).and_return(expected_result)
87
+ result = run_robot
88
+ result.should == expected_result
89
+ end
90
+
91
+ it "deletes the output file" do
92
+ run_robot
93
+ File.exists?(output_file_name).should be_false
94
+ end
90
95
  end
91
96
  end
92
97
 
@@ -3,15 +3,18 @@ require 'spec_helper'
3
3
  describe RobotVim::ScriptFile do
4
4
  describe "when creating a script file from a string" do
5
5
  let(:commands_string){"some string of vim commands"}
6
+ let(:expected_file_name){"some_file_name"}
7
+
8
+ before do
9
+ RobotVim::FileNameGenerator.stub(:generate).and_return(expected_file_name)
10
+ end
6
11
 
7
12
  it "yields the path of the script file" do
8
- expected_path = "/some/path/somewhere"
9
- UUID.stub_chain(:new, :generate => expected_path)
10
13
  file = stub("file").as_null_object
11
14
  File.stub(:new).and_return(file)
12
15
 
13
16
  RobotVim::ScriptFile.open(commands_string) do |file_path|
14
- file_path.should == expected_path
17
+ file_path.should == expected_file_name
15
18
  end
16
19
  end
17
20
 
@@ -22,11 +25,9 @@ describe RobotVim::ScriptFile do
22
25
  end
23
26
 
24
27
  it "deletes the file after we are done using it" do
25
- file_name = "somefile"
26
- UUID.stub_chain(:new, :generate => file_name)
27
28
  RobotVim::ScriptFile.open(commands_string) do |file_path|
28
29
  end
29
- File.exists?(file_name).should be_false
30
+ File.exists?(expected_file_name).should be_false
30
31
  end
31
32
 
32
33
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: robot-vim
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.9.0
5
+ version: 0.10.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Matt Margolis
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-03-26 00:00:00 -05:00
13
+ date: 2011-03-27 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -75,12 +75,16 @@ files:
75
75
  - README.md
76
76
  - Rakefile
77
77
  - lib/robot-vim.rb
78
+ - lib/robot-vim/file_name_generator.rb
79
+ - lib/robot-vim/input_file.rb
78
80
  - lib/robot-vim/runner.rb
79
81
  - lib/robot-vim/script_file.rb
80
82
  - lib/robot-vim/version.rb
81
83
  - robot-vim.gemspec
82
84
  - spec/fixtures/unsorted_file.txt
83
85
  - spec/integration_spec.rb
86
+ - spec/robot-vim/file_name_generator_spec.rb
87
+ - spec/robot-vim/input_file_spec.rb
84
88
  - spec/robot-vim/runner_spec.rb
85
89
  - spec/robot-vim/script_file_spec.rb
86
90
  - spec/spec_helper.rb