tty-file 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,54 +1,68 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RSpec.describe TTY::File, '#download_file' do
4
- it "downloads a file from remote uri" do
5
- body = "##Header1\nCopy text.\n"
6
- stub_request(:get, "https://example.com/README.md").to_return(body: body)
7
- path = tmp_path('doc/README.md')
4
+ shared_context "downloading a file" do
5
+ it "downloads a file from remote uri" do
6
+ body = "##Header1\nCopy text.\n"
7
+ stub_request(:get, "https://example.com/README.md").to_return(body: body)
8
+ path = path_factory.call('doc/README.md')
8
9
 
9
- TTY::File.download_file('https://example.com/README.md', path, verbose: false)
10
+ TTY::File.download_file('https://example.com/README.md', path, verbose: false)
10
11
 
11
- expect(File.read(path)).to eq(body)
12
- end
12
+ expect(File.read(path)).to eq(body)
13
+ end
13
14
 
14
- it "yields content from remoe uri" do
15
- body = "##Header1\nCopy text.\n"
16
- stub_request(:get, "https://example.com/README.md").to_return(body: body)
17
- path = tmp_path('doc/README.md')
15
+ it "yields content from remoe uri" do
16
+ body = "##Header1\nCopy text.\n"
17
+ stub_request(:get, "https://example.com/README.md").to_return(body: body)
18
+ path = path_factory.call('doc/README.md')
18
19
 
19
- TTY::File.download_file('https://example.com/README.md', path, verbose: false) do |content|
20
- expect(a_request(:get, 'https://example.com/README.md')).to have_been_made
21
- expect(content).to eq(body)
20
+ TTY::File.download_file('https://example.com/README.md', path, verbose: false) do |content|
21
+ expect(a_request(:get, 'https://example.com/README.md')).to have_been_made
22
+ expect(content).to eq(body)
23
+ end
22
24
  end
23
- end
24
25
 
25
- it "logs file operation" do
26
- body = "##Header1\nCopy text.\n"
27
- stub_request(:get, "https://example.com/README.md").to_return(body: body)
28
- path = tmp_path('doc/README.md')
26
+ it "logs file operation" do
27
+ body = "##Header1\nCopy text.\n"
28
+ stub_request(:get, "https://example.com/README.md").to_return(body: body)
29
+ path = path_factory.call('doc/README.md')
29
30
 
30
- expect {
31
- TTY::File.download_file('https://example.com/README.md', path)
32
- }.to output(/create(.*)doc\/README.md/).to_stdout_from_any_process
33
- end
31
+ expect {
32
+ TTY::File.download_file('https://example.com/README.md', path)
33
+ }.to output(/create(.*)doc\/README.md/).to_stdout_from_any_process
34
+ end
35
+
36
+ it "specifies limit on redirects" do
37
+ stub_request(:get, "https://example.com/wrong").to_return(status: 302, headers: { location: 'https://example.com/wrong_again'})
38
+ stub_request(:get, "https://example.com/wrong_again").to_return(status: 302, headers: { location: 'https://example.com/README.md'})
34
39
 
35
- it "specifies limit on redirects" do
36
- stub_request(:get, "https://example.com/wrong").to_return(status: 302, headers: { location: 'https://example.com/wrong_again'})
37
- stub_request(:get, "https://example.com/wrong_again").to_return(status: 302, headers: { location: 'https://example.com/README.md'})
40
+ path = path_factory.call('doc/README.md')
38
41
 
39
- path = tmp_path('doc/README.md')
42
+ expect {
43
+ TTY::File.download_file('https://example.com/wrong', path, verbose: false, limit: 1)
44
+ }.to raise_error(TTY::File::DownloadError)
45
+ end
46
+
47
+ it "copies the file from relative location if not URI" do
48
+ src_path = path_factory.call('Gemfile')
49
+ dest_path = path_factory.call('app/Gemfile')
50
+
51
+ TTY::File.get_file(src_path, dest_path, verbose: false)
40
52
 
41
- expect {
42
- TTY::File.download_file('https://example.com/wrong', path, verbose: false, limit: 1)
43
- }.to raise_error(TTY::File::DownloadError)
53
+ exists_and_identical?('Gemfile', 'app/Gemfile')
54
+ end
44
55
  end
45
56
 
46
- it "copies the file from relative location if not URI" do
47
- src_path = tmp_path('Gemfile')
48
- dest_path = tmp_path('app/Gemfile')
57
+ context 'when passed a String instance for the file argument' do
58
+ let(:path_factory) { method(:tmp_path) }
59
+
60
+ include_context "downloading a file"
61
+ end
49
62
 
50
- TTY::File.get_file(src_path, dest_path, verbose: false)
63
+ context 'when passed a Pathname instance for the file argument' do
64
+ let(:path_factory) { method(:tmp_pathname) }
51
65
 
52
- exists_and_identical?('Gemfile', 'app/Gemfile')
66
+ include_context "downloading a file"
53
67
  end
54
68
  end
@@ -1,162 +1,176 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RSpec.describe TTY::File, '#inject_into_file' do
4
- it "injects content into file :before" do
5
- file = tmp_path('Gemfile')
6
- TTY::File.inject_into_file(file, "gem 'tty'\n",
7
- before: "gem 'rack', '>=1.0'\n", verbose: false)
8
-
9
- expect(File.read(file)).to eq([
10
- "gem 'nokogiri'\n",
11
- "gem 'rails', '5.0.0'\n",
12
- "gem 'tty'\n",
13
- "gem 'rack', '>=1.0'\n",
14
- ].join)
15
- end
16
-
17
- it "injects content into file :after" do
18
- file = tmp_path('Gemfile')
19
-
20
- expect {
21
- TTY::File.inject_into_file(file, "gem 'tty'", after: "gem 'rack', '>=1.0'\n")
22
- }.to output(/inject/).to_stdout_from_any_process
23
-
24
- expect(File.read(file)).to eq([
25
- "gem 'nokogiri'\n",
26
- "gem 'rails', '5.0.0'\n",
27
- "gem 'rack', '>=1.0'\n",
28
- "gem 'tty'"
29
- ].join)
30
- end
31
-
32
- it "accepts content in block" do
33
- file = tmp_path('Gemfile')
34
-
35
- expect {
36
- TTY::File.insert_into_file(file, after: "gem 'rack', '>=1.0'\n") do
4
+ shared_context "injecting into file" do
5
+ it "injects content into file :before" do
6
+ file = path_factory.call('Gemfile')
7
+ TTY::File.inject_into_file(file, "gem 'tty'\n",
8
+ before: "gem 'rack', '>=1.0'\n", verbose: false)
9
+
10
+ expect(File.read(file)).to eq([
11
+ "gem 'nokogiri'\n",
12
+ "gem 'rails', '5.0.0'\n",
13
+ "gem 'tty'\n",
14
+ "gem 'rack', '>=1.0'\n",
15
+ ].join)
16
+ end
17
+
18
+ it "injects content into file :after" do
19
+ file = path_factory.call('Gemfile')
20
+
21
+ expect {
22
+ TTY::File.inject_into_file(file, "gem 'tty'", after: "gem 'rack', '>=1.0'\n")
23
+ }.to output(/inject/).to_stdout_from_any_process
24
+
25
+ expect(File.read(file)).to eq([
26
+ "gem 'nokogiri'\n",
27
+ "gem 'rails', '5.0.0'\n",
28
+ "gem 'rack', '>=1.0'\n",
37
29
  "gem 'tty'"
38
- end
39
- }.to output(/inject/).to_stdout_from_any_process
40
-
41
- expect(File.read(file)).to eq([
42
- "gem 'nokogiri'\n",
43
- "gem 'rails', '5.0.0'\n",
44
- "gem 'rack', '>=1.0'\n",
45
- "gem 'tty'"
46
- ].join)
47
- end
48
-
49
- it "accepts many lines" do
50
- file = tmp_path('Gemfile')
51
-
52
- TTY::File.inject_into_file(file, "gem 'tty'\n", "gem 'loaf'",
53
- after: "gem 'rack', '>=1.0'\n", verbose: false)
54
-
55
- expect(File.read(file)).to eq([
56
- "gem 'nokogiri'\n",
57
- "gem 'rails', '5.0.0'\n",
58
- "gem 'rack', '>=1.0'\n",
59
- "gem 'tty'\n",
60
- "gem 'loaf'"
61
- ].join)
62
- end
63
-
64
- it "logs action" do
65
- file = tmp_path('Gemfile')
66
-
67
- expect {
68
- TTY::File.inject_into_file(file, "gem 'tty'\n", "gem 'loaf'",
69
- after: "gem 'rack', '>=1.0'\n", verbose: true)
70
- }.to output(/\e\[32minject.*Gemfile/).to_stdout_from_any_process
71
- end
72
-
73
- it "logs action without color" do
74
- file = tmp_path('Gemfile')
75
-
76
- expect {
77
- TTY::File.inject_into_file(file, "gem 'tty'\n", "gem 'loaf'",
78
- after: "gem 'rack', '>=1.0'\n", verbose: true, color: false)
79
- }.to output(/\s+inject.*Gemfile/).to_stdout_from_any_process
80
- end
81
-
82
- it "doesn't inject new content if already present" do
83
- file = tmp_path('Gemfile')
84
- TTY::File.inject_into_file(file, "gem 'tty'",
85
- after: "gem 'rack', '>=1.0'\n", verbose: false)
86
-
87
- expect(File.read(file)).to eq([
88
- "gem 'nokogiri'\n",
89
- "gem 'rails', '5.0.0'\n",
90
- "gem 'rack', '>=1.0'\n",
91
- "gem 'tty'"
92
- ].join)
93
-
94
- TTY::File.inject_into_file(file, "gem 'tty'",
95
- after: "gem 'rack', '>=1.0'\n",
96
- force: false, verbose: false)
97
-
98
- expect(File.read(file)).to eq([
99
- "gem 'nokogiri'\n",
100
- "gem 'rails', '5.0.0'\n",
101
- "gem 'rack', '>=1.0'\n",
102
- "gem 'tty'"
103
- ].join)
104
- end
30
+ ].join)
31
+ end
32
+
33
+ it "accepts content in block" do
34
+ file = path_factory.call('Gemfile')
35
+
36
+ expect {
37
+ TTY::File.insert_into_file(file, after: "gem 'rack', '>=1.0'\n") do
38
+ "gem 'tty'"
39
+ end
40
+ }.to output(/inject/).to_stdout_from_any_process
41
+
42
+ expect(File.read(file)).to eq([
43
+ "gem 'nokogiri'\n",
44
+ "gem 'rails', '5.0.0'\n",
45
+ "gem 'rack', '>=1.0'\n",
46
+ "gem 'tty'"
47
+ ].join)
48
+ end
49
+
50
+ it "accepts many lines" do
51
+ file = path_factory.call('Gemfile')
52
+
53
+ TTY::File.inject_into_file(file, "gem 'tty'\n", "gem 'loaf'",
54
+ after: "gem 'rack', '>=1.0'\n", verbose: false)
55
+
56
+ expect(File.read(file)).to eq([
57
+ "gem 'nokogiri'\n",
58
+ "gem 'rails', '5.0.0'\n",
59
+ "gem 'rack', '>=1.0'\n",
60
+ "gem 'tty'\n",
61
+ "gem 'loaf'"
62
+ ].join)
63
+ end
64
+
65
+ it "logs action" do
66
+ file = path_factory.call('Gemfile')
67
+
68
+ expect {
69
+ TTY::File.inject_into_file(file, "gem 'tty'\n", "gem 'loaf'",
70
+ after: "gem 'rack', '>=1.0'\n", verbose: true)
71
+ }.to output(/\e\[32minject.*Gemfile/).to_stdout_from_any_process
72
+ end
73
+
74
+ it "logs action without color" do
75
+ file = path_factory.call('Gemfile')
76
+
77
+ expect {
78
+ TTY::File.inject_into_file(file, "gem 'tty'\n", "gem 'loaf'",
79
+ after: "gem 'rack', '>=1.0'\n", verbose: true, color: false)
80
+ }.to output(/\s+inject.*Gemfile/).to_stdout_from_any_process
81
+ end
82
+
83
+ it "doesn't inject new content if already present" do
84
+ file = path_factory.call('Gemfile')
85
+ TTY::File.inject_into_file(file, "gem 'tty'",
86
+ after: "gem 'rack', '>=1.0'\n", verbose: false)
87
+
88
+ expect(File.read(file)).to eq([
89
+ "gem 'nokogiri'\n",
90
+ "gem 'rails', '5.0.0'\n",
91
+ "gem 'rack', '>=1.0'\n",
92
+ "gem 'tty'"
93
+ ].join)
105
94
 
106
- it "checks if a content can be safely injected" do
107
- file = tmp_path('Gemfile')
108
- TTY::File.safe_inject_into_file(file, "gem 'tty'",
109
- after: "gem 'rack', '>=1.0'\n", verbose: false)
110
- expect(::File.read(file)).to eq([
111
- "gem 'nokogiri'\n",
112
- "gem 'rails', '5.0.0'\n",
113
- "gem 'rack', '>=1.0'\n",
114
- "gem 'tty'"
115
- ].join)
116
- end
95
+ TTY::File.inject_into_file(file, "gem 'tty'",
96
+ after: "gem 'rack', '>=1.0'\n",
97
+ force: false, verbose: false)
117
98
 
118
- it "changes content already present if :force flag is true" do
119
- file = tmp_path('Gemfile')
120
-
121
- TTY::File.inject_into_file(file, "gem 'tty'\n",
122
- before: "gem 'nokogiri'", verbose: false)
123
-
124
- expect(File.read(file)).to eq([
125
- "gem 'tty'\n",
126
- "gem 'nokogiri'\n",
127
- "gem 'rails', '5.0.0'\n",
128
- "gem 'rack', '>=1.0'\n",
129
- ].join)
130
-
131
- TTY::File.inject_into_file(file, "gem 'tty'\n",
132
- before: "gem 'nokogiri'", verbose: false, force: true)
133
-
134
- expect(File.read(file)).to eq([
135
- "gem 'tty'\n",
136
- "gem 'tty'\n",
137
- "gem 'nokogiri'\n",
138
- "gem 'rails', '5.0.0'\n",
139
- "gem 'rack', '>=1.0'\n",
140
- ].join)
99
+ expect(File.read(file)).to eq([
100
+ "gem 'nokogiri'\n",
101
+ "gem 'rails', '5.0.0'\n",
102
+ "gem 'rack', '>=1.0'\n",
103
+ "gem 'tty'"
104
+ ].join)
105
+ end
106
+
107
+ it "checks if a content can be safely injected" do
108
+ file = path_factory.call('Gemfile')
109
+ TTY::File.safe_inject_into_file(file, "gem 'tty'",
110
+ after: "gem 'rack', '>=1.0'\n", verbose: false)
111
+ expect(::File.read(file)).to eq([
112
+ "gem 'nokogiri'\n",
113
+ "gem 'rails', '5.0.0'\n",
114
+ "gem 'rack', '>=1.0'\n",
115
+ "gem 'tty'"
116
+ ].join)
117
+ end
118
+
119
+ it "changes content already present if :force flag is true" do
120
+ file = path_factory.call('Gemfile')
121
+
122
+ TTY::File.inject_into_file(file, "gem 'tty'\n",
123
+ before: "gem 'nokogiri'", verbose: false)
124
+
125
+ expect(File.read(file)).to eq([
126
+ "gem 'tty'\n",
127
+ "gem 'nokogiri'\n",
128
+ "gem 'rails', '5.0.0'\n",
129
+ "gem 'rack', '>=1.0'\n",
130
+ ].join)
131
+
132
+ TTY::File.inject_into_file(file, "gem 'tty'\n",
133
+ before: "gem 'nokogiri'", verbose: false, force: true)
134
+
135
+ expect(File.read(file)).to eq([
136
+ "gem 'tty'\n",
137
+ "gem 'tty'\n",
138
+ "gem 'nokogiri'\n",
139
+ "gem 'rails', '5.0.0'\n",
140
+ "gem 'rack', '>=1.0'\n",
141
+ ].join)
142
+ end
143
+
144
+ it "fails to inject into non existent file" do
145
+ file = path_factory.call('unknown')
146
+
147
+ expect {
148
+ TTY::File.inject_into_file(file, "gem 'tty'", after: "gem 'rack', '>=1.0'\n")
149
+ }.to raise_error(ArgumentError, /File path (.)* does not exist/)
150
+ end
151
+
152
+ it "doesn't change content when :noop flag is true" do
153
+ file = path_factory.call('Gemfile')
154
+ TTY::File.inject_into_file(file, "gem 'tty'\n",
155
+ before: "gem 'nokogiri'", verbose: false, noop: true)
156
+
157
+ expect(File.read(file)).to eq([
158
+ "gem 'nokogiri'\n",
159
+ "gem 'rails', '5.0.0'\n",
160
+ "gem 'rack', '>=1.0'\n",
161
+ ].join)
162
+ end
141
163
  end
142
164
 
143
- it "fails to inject into non existent file" do
144
- file = tmp_path('unknown')
165
+ context "when passed a String instance for the file argument" do
166
+ let(:path_factory) { method(:tmp_path) }
145
167
 
146
- expect {
147
- TTY::File.inject_into_file(file, "gem 'tty'", after: "gem 'rack', '>=1.0'\n")
148
- }.to raise_error(ArgumentError, /File path (.)* does not exist/)
168
+ include_context "injecting into file"
149
169
  end
150
170
 
151
- it "doesn't change content when :noop flag is true" do
152
- file = tmp_path('Gemfile')
153
- TTY::File.inject_into_file(file, "gem 'tty'\n",
154
- before: "gem 'nokogiri'", verbose: false, noop: true)
171
+ context "when passed a Pathname instance for the file argument" do
172
+ let(:path_factory) { method(:tmp_pathname) }
155
173
 
156
- expect(File.read(file)).to eq([
157
- "gem 'nokogiri'\n",
158
- "gem 'rails', '5.0.0'\n",
159
- "gem 'rack', '>=1.0'\n",
160
- ].join)
174
+ include_context "injecting into file"
161
175
  end
162
176
  end
@@ -1,98 +1,124 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RSpec.describe TTY::File, '#prepend_to_file' do
4
- it "appends to file" do
5
- file = tmp_path('Gemfile')
6
- result = TTY::File.prepend_to_file(file, "gem 'tty'\n", verbose: false)
7
- expect(result).to eq(true)
8
- expect(File.read(file)).to eq([
9
- "gem 'tty'\n",
10
- "gem 'nokogiri'\n",
11
- "gem 'rails', '5.0.0'\n",
12
- "gem 'rack', '>=1.0'\n",
13
- ].join)
14
- end
4
+ shared_context "prepending to a file" do
5
+ it "appends to file" do
6
+ file = path_factory.call('Gemfile')
15
7
 
16
- it "prepends multiple lines to file" do
17
- file = tmp_path('Gemfile')
18
- TTY::File.prepend_to_file(file, "gem 'tty'\n", "gem 'rake'\n", verbose: false)
19
- expect(File.read(file)).to eq([
20
- "gem 'tty'\n",
21
- "gem 'rake'\n",
22
- "gem 'nokogiri'\n",
23
- "gem 'rails', '5.0.0'\n",
24
- "gem 'rack', '>=1.0'\n",
25
- ].join)
26
- end
8
+ result = TTY::File.prepend_to_file(file, "gem 'tty'\n", verbose: false)
27
9
 
28
- it "prepends content in a block" do
29
- file = tmp_path('Gemfile')
30
- TTY::File.prepend_to_file(file, verbose: false) { "gem 'tty'\n"}
31
- expect(File.read(file)).to eq([
32
- "gem 'tty'\n",
33
- "gem 'nokogiri'\n",
34
- "gem 'rails', '5.0.0'\n",
35
- "gem 'rack', '>=1.0'\n",
36
- ].join)
37
- end
10
+ expect(result).to eq(true)
11
+ expect(File.read(file)).to eq([
12
+ "gem 'tty'\n",
13
+ "gem 'nokogiri'\n",
14
+ "gem 'rails', '5.0.0'\n",
15
+ "gem 'rack', '>=1.0'\n",
16
+ ].join)
17
+ end
38
18
 
39
- it "doesn't prepend if already present" do
40
- file = tmp_path('Gemfile')
41
- TTY::File.prepend_to_file(file, "gem 'nokogiri'\n", force: false, verbose: false)
42
- expect(::File.read(file)).to eq([
43
- "gem 'nokogiri'\n",
44
- "gem 'rails', '5.0.0'\n",
45
- "gem 'rack', '>=1.0'\n",
46
- ].join)
47
- end
19
+ it "prepends multiple lines to file" do
20
+ file = path_factory.call('Gemfile')
48
21
 
49
- it "checks if a content can be safely prepended" do
50
- file = tmp_path('Gemfile')
51
- TTY::File.safe_prepend_to_file(file, "gem 'nokogiri'\n", verbose: false)
52
- expect(::File.read(file)).to eq([
53
- "gem 'nokogiri'\n",
54
- "gem 'rails', '5.0.0'\n",
55
- "gem 'rack', '>=1.0'\n",
56
- ].join)
57
- end
22
+ TTY::File.prepend_to_file(file, "gem 'tty'\n", "gem 'rake'\n", verbose: false)
58
23
 
59
- it "doesn't prepend if already present for multiline content" do
60
- file = tmp_path('Gemfile')
61
- TTY::File.prepend_to_file(file, "gem 'nokogiri'\n", verbose: false)
62
- TTY::File.prepend_to_file(file, "gem 'nokogiri'\n", "gem 'nokogiri'\n", force: false, verbose: false)
63
- expect(::File.read(file)).to eq([
64
- "gem 'nokogiri'\n",
65
- "gem 'nokogiri'\n",
66
- "gem 'rails', '5.0.0'\n",
67
- "gem 'rack', '>=1.0'\n",
68
- ].join)
69
- end
24
+ expect(File.read(file)).to eq([
25
+ "gem 'tty'\n",
26
+ "gem 'rake'\n",
27
+ "gem 'nokogiri'\n",
28
+ "gem 'rails', '5.0.0'\n",
29
+ "gem 'rack', '>=1.0'\n",
30
+ ].join)
31
+ end
32
+
33
+ it "prepends content in a block" do
34
+ file = path_factory.call('Gemfile')
35
+
36
+ TTY::File.prepend_to_file(file, verbose: false) { "gem 'tty'\n"}
37
+
38
+ expect(File.read(file)).to eq([
39
+ "gem 'tty'\n",
40
+ "gem 'nokogiri'\n",
41
+ "gem 'rails', '5.0.0'\n",
42
+ "gem 'rack', '>=1.0'\n",
43
+ ].join)
44
+ end
45
+
46
+ it "doesn't prepend if already present" do
47
+ file = path_factory.call('Gemfile')
48
+
49
+ TTY::File.prepend_to_file(file, "gem 'nokogiri'\n", force: false, verbose: false)
70
50
 
71
- it "prepends multiple times if forced" do
72
- file = tmp_path('Gemfile')
73
- TTY::File.prepend_to_file(file, "gem 'nokogiri'\n", force: true, verbose: false)
74
- TTY::File.prepend_to_file(file, "gem 'nokogiri'\n", "gem 'nokogiri'\n", force: true, verbose: false)
75
- expect(::File.read(file)).to eq([
76
- "gem 'nokogiri'\n",
77
- "gem 'nokogiri'\n",
78
- "gem 'nokogiri'\n",
79
- "gem 'nokogiri'\n",
80
- "gem 'rails', '5.0.0'\n",
81
- "gem 'rack', '>=1.0'\n"
82
- ].join)
51
+ expect(::File.read(file)).to eq([
52
+ "gem 'nokogiri'\n",
53
+ "gem 'rails', '5.0.0'\n",
54
+ "gem 'rack', '>=1.0'\n",
55
+ ].join)
56
+ end
57
+
58
+ it "checks if a content can be safely prepended" do
59
+ file = path_factory.call('Gemfile')
60
+ TTY::File.safe_prepend_to_file(file, "gem 'nokogiri'\n", verbose: false)
61
+ expect(::File.read(file)).to eq([
62
+ "gem 'nokogiri'\n",
63
+ "gem 'rails', '5.0.0'\n",
64
+ "gem 'rack', '>=1.0'\n",
65
+ ].join)
66
+ end
67
+
68
+ it "doesn't prepend if already present for multiline content" do
69
+ file = path_factory.call('Gemfile')
70
+
71
+ TTY::File.prepend_to_file(file, "gem 'nokogiri'\n", verbose: false)
72
+ TTY::File.prepend_to_file(file, "gem 'nokogiri'\n", "gem 'nokogiri'\n", force: false, verbose: false)
73
+
74
+ expect(::File.read(file)).to eq([
75
+ "gem 'nokogiri'\n",
76
+ "gem 'nokogiri'\n",
77
+ "gem 'rails', '5.0.0'\n",
78
+ "gem 'rack', '>=1.0'\n",
79
+ ].join)
80
+ end
81
+
82
+ it "prepends multiple times if forced" do
83
+ file = path_factory.call('Gemfile')
84
+
85
+ TTY::File.prepend_to_file(file, "gem 'nokogiri'\n", force: true, verbose: false)
86
+ TTY::File.prepend_to_file(file, "gem 'nokogiri'\n", "gem 'nokogiri'\n", force: true, verbose: false)
87
+
88
+ expect(::File.read(file)).to eq([
89
+ "gem 'nokogiri'\n",
90
+ "gem 'nokogiri'\n",
91
+ "gem 'nokogiri'\n",
92
+ "gem 'nokogiri'\n",
93
+ "gem 'rails', '5.0.0'\n",
94
+ "gem 'rack', '>=1.0'\n"
95
+ ].join)
96
+ end
97
+
98
+ it "logs action" do
99
+ file = path_factory.call('Gemfile')
100
+ expect {
101
+ TTY::File.prepend_to_file(file, "gem 'tty'")
102
+ }.to output(/\e\[32mprepend\e\[0m.*Gemfile/).to_stdout_from_any_process
103
+ end
104
+
105
+ it "logs action without color" do
106
+ file = path_factory.call('Gemfile')
107
+ expect {
108
+ TTY::File.prepend_to_file(file, "gem 'tty'", color: false)
109
+ }.to output(/\s+prepend.*Gemfile/).to_stdout_from_any_process
110
+ end
83
111
  end
84
112
 
85
- it "logs action" do
86
- file = tmp_path('Gemfile')
87
- expect {
88
- TTY::File.prepend_to_file(file, "gem 'tty'")
89
- }.to output(/\e\[32mprepend\e\[0m.*Gemfile/).to_stdout_from_any_process
113
+ context "when passed a String instance for the file argument" do
114
+ let(:path_factory) { method(:tmp_path) }
115
+
116
+ include_context "prepending to a file"
90
117
  end
91
118
 
92
- it "logs action without color" do
93
- file = tmp_path('Gemfile')
94
- expect {
95
- TTY::File.prepend_to_file(file, "gem 'tty'", color: false)
96
- }.to output(/\s+prepend.*Gemfile/).to_stdout_from_any_process
119
+ context "when passed a Pathname instance for the file argument" do
120
+ let(:path_factory) { method(:tmp_pathname) }
121
+
122
+ include_context "prepending to a file"
97
123
  end
98
124
  end