borrower 0.7.0 → 0.8.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.
@@ -8,16 +8,29 @@ module Borrower
8
8
  # @param [String] from the path to the file
9
9
  # @return [String] contents of the file
10
10
  def take from
11
- Borrower::Content.get from
11
+ Content.get from
12
12
  end
13
13
 
14
14
  # write the content to a destination file
15
15
  #
16
16
  # @param [String] content content for the file
17
- # @param [String] to path to write contents to
17
+ # @param [String] destination path to write contents to
18
+ # @param [Symbol] on_conflict what to do if the destination exists
18
19
  # @return [Void]
19
- def put content, to
20
- Borrower::Content.put content, to
20
+ def put content, destination, on_conflict=:overwrite
21
+
22
+ if on_conflict != :overwrite && Content::Item.new( destination ).exists?
23
+ case on_conflict
24
+ when :skip then return
25
+ when :prompt then
26
+ input = Util.get_input "a file already exists at #{destination}\noverwrite? (y|n): "
27
+ return unless input.downcase == "y"
28
+ when :raise_error then
29
+ raise "File already exists at #{destination}"
30
+ end
31
+ end
32
+
33
+ Content.put content, destination
21
34
  end
22
35
 
23
36
  # parse through the content and merge
@@ -0,0 +1,14 @@
1
+ module Borrower
2
+ module Util
3
+ class << self
4
+
5
+ # get input from the command line
6
+ # @return [String]
7
+ def get_input message
8
+ print message
9
+ $stdin.gets.chomp
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module Borrower
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
data/lib/borrower.rb CHANGED
@@ -5,6 +5,7 @@ require "net/https"
5
5
  require "uri"
6
6
 
7
7
  # borrower
8
+ require 'borrower/util'
8
9
  require 'borrower/version'
9
10
  require 'borrower/content'
10
11
  require 'borrower/manifest'
@@ -35,6 +36,7 @@ module Borrower::DSL
35
36
  # @return [Void]
36
37
  def borrow path, options={}, &block
37
38
  destination = options.delete(:to) { raise ArgumentError, "missing 'to:' argument" }
39
+ on_conflict = options.delete(:on_conflict) { nil }
38
40
  content = Borrower.take(path)
39
41
 
40
42
  if content.ascii_only?
@@ -42,7 +44,7 @@ module Borrower::DSL
42
44
  content = yield content if block_given?
43
45
  end
44
46
 
45
- Borrower.put content, destination
47
+ Borrower.put *[content, destination, on_conflict].compact
46
48
  end
47
49
 
48
50
  end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe Borrower::PublicAPI do
4
+
5
+ after :each do
6
+ cleanup_tmp
7
+ end
8
+
9
+ describe "#put" do
10
+ context "when no file exists at destination" do
11
+ [ :overwrite, :prompt, :skip, :raise_error ].each do |on_conflict|
12
+ context "when on_conflict is set to :#{on_conflict}" do
13
+ it "puts the contents in a file at the destination" do
14
+ destination = File.join( TMP, 'destination/file.txt' )
15
+ Borrower.put "Hello World", destination, on_conflict
16
+
17
+ expect( Borrower.take(destination) ).to eq "Hello World"
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ context "when a file exists at destination" do
24
+
25
+ let(:destination) { File.join( TMP, "file.txt" ) }
26
+ before :each do
27
+ given_file "file.txt", "I'm conflicted"
28
+ end
29
+
30
+ context "when on_conflict is set to :overwrite" do
31
+ it "overwrites the contents of the file" do
32
+ Borrower.put "Hello World", destination, :overwrite
33
+
34
+ expect( Borrower.take(destination) ).not_to eq "I'm conflicted"
35
+ expect( Borrower.take(destination) ).to eq "Hello World"
36
+ end
37
+ end
38
+
39
+ context "when on_confict is set to :prompt" do
40
+ it "overwrites to file if response is y" do
41
+ fake_stdin("y") do
42
+ Borrower.put "Hello World", destination, :prompt
43
+ end
44
+
45
+ expect( Borrower.take(destination) ).not_to eq "I'm conflicted"
46
+ expect( Borrower.take(destination) ).to eq "Hello World"
47
+ end
48
+
49
+ it "skips file if response is no" do
50
+ fake_stdin("n") do
51
+ Borrower.put "Hello World", destination, :prompt
52
+ end
53
+
54
+ expect( Borrower.take(destination) ).not_to eq "Hello World"
55
+ expect( Borrower.take(destination) ).to eq "I'm conflicted"
56
+ end
57
+ end
58
+
59
+ context "when on_conflict is set to :skip" do
60
+ it "doesn't overwrite the contents of the file" do
61
+ Borrower.put "Hello World", destination, :skip
62
+
63
+ expect( Borrower.take(destination) ).not_to eq "Hello World"
64
+ expect( Borrower.take(destination) ).to eq "I'm conflicted"
65
+ end
66
+ end
67
+
68
+ context "when on_conflict is set to :raise_error" do
69
+ it "raises an error" do
70
+ expect{
71
+ Borrower.put "Hello World", destination, :raise_error
72
+ }.to raise_error
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ end
@@ -94,4 +94,18 @@ describe Borrower do
94
94
 
95
95
  end
96
96
 
97
+ describe "on_conflict options" do
98
+ it "doesn't pass an option to put if none is set" do
99
+ expect( Borrower ).to receive(:put).with( "Hi I'm a file", file_destination )
100
+
101
+ borrow local_file, to: file_destination
102
+ end
103
+
104
+ it "passes an option if set" do
105
+ expect( Borrower ).to receive(:put).with( "Hi I'm a file", file_destination, :skip )
106
+
107
+ borrow local_file, to: file_destination, on_conflict: :skip
108
+ end
109
+ end
110
+
97
111
  end
data/spec/spec_helper.rb CHANGED
@@ -1,36 +1,29 @@
1
1
  require 'rspec'
2
2
  require 'fileutils'
3
3
 
4
- begin
5
- require 'pry-debugger'
6
- rescue LoadError
7
- puts "no biggie, but pry debugger isn't available"
8
- end
9
-
10
- TMP = File.join( Dir.pwd, "tmp" )
11
- ROOT = Dir.pwd
4
+ unless ENV['TRAVIS']
12
5
 
13
- def given_files list
14
- FileUtils.mkdir_p TMP
15
- Array(list).each do |f|
16
- file_path = File.join( TMP, f )
17
- FileUtils.mkdir_p( File.dirname(file_path) )
18
- FileUtils.touch( file_path )
6
+ # try and load pry
7
+ begin
8
+ require 'pry'
9
+ rescue LoadError
10
+ puts "no biggie, but pry isn't available"
19
11
  end
20
- end
21
12
 
22
- def given_file name, content
23
- file_path = File.join( TMP, name )
24
- FileUtils.mkdir_p( File.dirname(file_path) )
25
- File.open( file_path, 'w' ) do |file|
26
- file.write content
27
- end
13
+ # Silence $stdout
14
+ $stdout = StringIO.open('','w+')
15
+
28
16
  end
29
17
 
30
- def given_config content
31
- File.open( File.join( TMP, 'manifest.borrower' ), 'w' ) do |file|
32
- file.write content
33
- end
18
+ require_relative 'support/fake_stdin'
19
+ require_relative 'support/given'
20
+
21
+ TMP = File.join( Dir.pwd, "tmp" )
22
+ ROOT = Dir.pwd
23
+
24
+ RSpec.configure do |config|
25
+ config.include FakeStdin
26
+ config.include Given
34
27
  end
35
28
 
36
29
  def cleanup_tmp
@@ -0,0 +1,14 @@
1
+ module FakeStdin
2
+
3
+ # Patch STDIN for a block
4
+ def fake_stdin *args
5
+ $stdin = StringIO.new
6
+ $stdin.puts(args.shift) until args.empty?
7
+ $stdin.rewind
8
+
9
+ yield
10
+ ensure
11
+ $stdin = STDIN
12
+ end
13
+
14
+ end
@@ -0,0 +1,24 @@
1
+ module Given
2
+ def given_files list
3
+ FileUtils.mkdir_p TMP
4
+ Array(list).each do |f|
5
+ file_path = File.join( TMP, f )
6
+ FileUtils.mkdir_p( File.dirname(file_path) )
7
+ FileUtils.touch( file_path )
8
+ end
9
+ end
10
+
11
+ def given_file name, content
12
+ file_path = File.join( TMP, name )
13
+ FileUtils.mkdir_p( File.dirname(file_path) )
14
+ File.open( file_path, 'w' ) do |file|
15
+ file.write content
16
+ end
17
+ end
18
+
19
+ def given_config content
20
+ File.open( File.join( TMP, 'manifest.borrower' ), 'w' ) do |file|
21
+ file.write content
22
+ end
23
+ end
24
+ end
metadata CHANGED
@@ -1,78 +1,87 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: borrower
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.7.0
4
+ version: 0.8.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Steven Sloan
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-24 00:00:00.000000000 Z
12
+ date: 2013-11-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: net-ssh
16
- version_requirements: !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
17
18
  requirements:
18
19
  - - ~>
19
20
  - !ruby/object:Gem::Version
20
21
  version: 2.6.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
21
25
  none: false
22
- requirement: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - ~>
25
28
  - !ruby/object:Gem::Version
26
29
  version: 2.6.5
27
- none: false
28
- prerelease: false
29
- type: :runtime
30
- description: ' For borrowing little snippets of the web, or files, or really any snippet
31
- of string. '
30
+ description: ! ' For borrowing little snippets of the web, or files, or really any
31
+ snippet of string. '
32
32
  email:
33
33
  - stevenosloan@gmail.com
34
34
  executables: []
35
35
  extensions: []
36
36
  extra_rdoc_files: []
37
37
  files:
38
- - lib/borrower.rb
39
38
  - lib/borrower/content.rb
39
+ - lib/borrower/manifest/config_file.rb
40
40
  - lib/borrower/manifest.rb
41
41
  - lib/borrower/merge.rb
42
42
  - lib/borrower/public_api.rb
43
+ - lib/borrower/util.rb
43
44
  - lib/borrower/version.rb
44
- - lib/borrower/manifest/config_file.rb
45
- - spec/borrower_spec.rb
46
- - spec/spec_helper.rb
45
+ - lib/borrower.rb
47
46
  - spec/borrower/manifest_spec.rb
48
47
  - spec/borrower/merge_spec.rb
48
+ - spec/borrower/public_api_spec.rb
49
+ - spec/borrower_spec.rb
50
+ - spec/spec_helper.rb
51
+ - spec/support/fake_stdin.rb
52
+ - spec/support/given.rb
49
53
  homepage: http://github.com/stevenosloan/borrower
50
54
  licenses:
51
55
  - MIT
52
- post_install_message:
56
+ post_install_message:
53
57
  rdoc_options: []
54
58
  require_paths:
55
59
  - lib
56
60
  required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
57
62
  requirements:
58
- - - '>='
63
+ - - ! '>='
59
64
  - !ruby/object:Gem::Version
60
65
  version: '0'
61
- none: false
62
66
  required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
63
68
  requirements:
64
- - - '>='
69
+ - - ! '>='
65
70
  - !ruby/object:Gem::Version
66
71
  version: '0'
67
- none: false
68
72
  requirements: []
69
- rubyforge_project:
70
- rubygems_version: 1.8.24
71
- signing_key:
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.25
75
+ signing_key:
72
76
  specification_version: 3
73
- summary: For borrowing little snippets of the web, or files, or really any snippet of string.
77
+ summary: For borrowing little snippets of the web, or files, or really any snippet
78
+ of string.
74
79
  test_files:
75
- - spec/borrower_spec.rb
76
- - spec/spec_helper.rb
77
80
  - spec/borrower/manifest_spec.rb
78
81
  - spec/borrower/merge_spec.rb
82
+ - spec/borrower/public_api_spec.rb
83
+ - spec/borrower_spec.rb
84
+ - spec/spec_helper.rb
85
+ - spec/support/fake_stdin.rb
86
+ - spec/support/given.rb
87
+ has_rdoc: