tty-file 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -0
  3. data/lib/tty/file.rb +75 -85
  4. data/lib/tty/file/create_file.rb +5 -5
  5. data/lib/tty/file/differ.rb +5 -4
  6. data/lib/tty/file/digest_file.rb +4 -4
  7. data/lib/tty/file/download_file.rb +5 -5
  8. data/lib/tty/file/version.rb +1 -1
  9. metadata +19 -56
  10. data/Rakefile +0 -10
  11. data/bin/console +0 -14
  12. data/bin/setup +0 -8
  13. data/spec/fixtures/cli_app/%name%_cli.rb +0 -2
  14. data/spec/fixtures/cli_app/commands/subcommand.rb +0 -2
  15. data/spec/fixtures/cli_app/excluded/%name%_cli.rb +0 -2
  16. data/spec/fixtures/templates/%file_name%.rb +0 -1
  17. data/spec/fixtures/templates/unit_test.rb +0 -1
  18. data/spec/spec_helper.rb +0 -94
  19. data/spec/unit/append_to_file_spec.rb +0 -110
  20. data/spec/unit/binary_spec.rb +0 -230
  21. data/spec/unit/checksum_file_spec.rb +0 -48
  22. data/spec/unit/chmod_spec.rb +0 -92
  23. data/spec/unit/copy_directory_spec.rb +0 -120
  24. data/spec/unit/copy_file_spec.rb +0 -172
  25. data/spec/unit/create_directory_spec.rb +0 -93
  26. data/spec/unit/create_file_spec.rb +0 -130
  27. data/spec/unit/diff_spec.rb +0 -107
  28. data/spec/unit/differ/call_spec.rb +0 -101
  29. data/spec/unit/download_file_spec.rb +0 -68
  30. data/spec/unit/escape_glob_path_spec.rb +0 -14
  31. data/spec/unit/inject_into_file_spec.rb +0 -176
  32. data/spec/unit/prepend_to_file_spec.rb +0 -124
  33. data/spec/unit/read_to_char_spec.rb +0 -24
  34. data/spec/unit/remove_file_spec.rb +0 -67
  35. data/spec/unit/replace_in_file_spec.rb +0 -140
  36. data/spec/unit/tail_file_spec.rb +0 -77
  37. data/tasks/console.rake +0 -11
  38. data/tasks/coverage.rake +0 -11
  39. data/tasks/spec.rake +0 -29
  40. data/tty-file.gemspec +0 -33
@@ -1,172 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'ostruct'
4
-
5
- RSpec.describe TTY::File, "#copy_file" do
6
- def exists_and_identical?(source, dest)
7
- dest_path = File.join(tmp_path, dest)
8
- expect(::File.exist?(dest_path)).to be(true)
9
-
10
- source_path = File.join(fixtures_path, source)
11
- expect(::FileUtils).to be_identical(source_path, dest_path)
12
- end
13
-
14
- shared_context 'copying files' do
15
- it "copies file without destination" do
16
- src = path_factory.call('Gemfile')
17
-
18
- TTY::File.copy_file(src, verbose: false)
19
-
20
- exists_and_identical?('Gemfile', 'Gemfile')
21
- end
22
-
23
- it "copies file to the destination" do
24
- src = path_factory.call('Gemfile')
25
- dest = path_factory.call('app/Makefile')
26
-
27
- TTY::File.copy_file(src, dest, verbose: false)
28
-
29
- exists_and_identical?('Gemfile', 'app/Makefile')
30
- end
31
-
32
- it "copies file to existing destination" do
33
- src = path_factory.call('Gemfile')
34
- dest = path_factory.call('app/Gemfile')
35
-
36
- TTY::File.copy_file(src, dest, verbose: false)
37
-
38
- exists_and_identical?('Gemfile', 'app/Gemfile')
39
- end
40
-
41
- it "copies file with block content" do
42
- src = path_factory.call('Gemfile')
43
- dest = path_factory.call('app/Gemfile')
44
-
45
- TTY::File.copy_file(src, dest, verbose: false) do |content|
46
- "https://rubygems.org\n" + content
47
- end
48
- expect(File.read(dest)).to eq("https://rubygems.org\ngem 'nokogiri'\ngem 'rails', '5.0.0'\ngem 'rack', '>=1.0'\n")
49
- end
50
-
51
- it "copies file and preservs metadata" do
52
- src = path_factory.call('Gemfile')
53
- dest = path_factory.call('app/Gemfile')
54
-
55
- expect {
56
- TTY::File.copy_file(src, dest, verbose: false, preserve: true)
57
- }.to output('').to_stdout_from_any_process
58
-
59
- expect(File.stat(src)).to eq(File.stat(dest))
60
- end
61
-
62
- it "doesn't copy file if :noop is true" do
63
- src = path_factory.call('Gemfile')
64
- dest = path_factory.call('app/Gemfile')
65
-
66
- TTY::File.copy_file(src, dest, verbose: false, noop: true)
67
-
68
- expect(File.exist?(dest)).to eq(false)
69
- end
70
-
71
- it "logs status" do
72
- src = path_factory.call('Gemfile')
73
- dest = path_factory.call('app/Gemfile')
74
-
75
- expect {
76
- TTY::File.copy_file(src, dest)
77
- }.to output(/\e\[32mcreate\e\[0m.*Gemfile/).to_stdout_from_any_process
78
- end
79
-
80
- it "logs status without color" do
81
- src = path_factory.call('Gemfile')
82
- dest = path_factory.call('app/Gemfile')
83
-
84
- expect {
85
- TTY::File.copy_file(src, dest, color: false)
86
- }.to output(/\s+create.*Gemfile/).to_stdout_from_any_process
87
- end
88
-
89
- it "removes template .erb extension" do
90
- src = path_factory.call('templates/application.html.erb')
91
-
92
- TTY::File.copy_file(src, verbose: false)
93
-
94
- exists_and_identical?('templates/application.html.erb',
95
- 'templates/application.html')
96
- end
97
-
98
- it "converts filename based on context" do
99
- src = path_factory.call('templates/%file_name%.rb')
100
- dest = path_factory.call('app/%file_name%.rb')
101
-
102
- variables = OpenStruct.new
103
- variables[:foo] = 'bar'
104
- variables[:file_name] = 'expected'
105
-
106
- TTY::File.copy_file(src, dest, context: variables, verbose: false)
107
-
108
- expected = tmp_path('app/expected.rb')
109
- expect(File.read(expected)).to eq("bar\n")
110
- end
111
-
112
- it "converts filename based on class context" do
113
- src = path_factory.call('templates/%file_name%.rb')
114
- dest = path_factory.call('templates/expected.rb')
115
-
116
- stub_const('TestCase', Class.new {
117
- def foo
118
- 'bar'
119
- end
120
-
121
- def file_name
122
- 'expected'
123
- end
124
- })
125
- TestCase.send(:include, TTY::File)
126
-
127
- TestCase.new.send(:copy_file, src, verbose: false)
128
-
129
- expect(File.read(dest)).to eq("bar\n")
130
- end
131
-
132
- it "copies file with custom class context" do
133
- src = path_factory.call('templates/unit_test.rb')
134
- dest = path_factory.call('test/unit_test.rb')
135
-
136
- stub_const('TestCase', Class.new {
137
- def self.class_name
138
- 'Example'
139
- end
140
- })
141
- TestCase.extend(TTY::File)
142
-
143
- TestCase.send(:copy_file, src, dest, verbose: false)
144
-
145
- expect(File.read(dest)).to eq("class ExampleTest; end\n")
146
- end
147
-
148
- it "copies template with custom context binding" do
149
- src = path_factory.call('templates/unit_test.rb')
150
- dest = path_factory.call('test/unit_test.rb')
151
-
152
- variables = OpenStruct.new
153
- variables[:class_name] = 'Example'
154
-
155
- TTY::File.copy_file(src, dest, context: variables, verbose: false)
156
-
157
- expect(File.read(dest)).to eq("class ExampleTest; end\n")
158
- end
159
- end
160
-
161
- context "when passed String instances for the file arguments" do
162
- let(:path_factory) { method(:tmp_path) }
163
-
164
- include_context "copying files"
165
- end
166
-
167
- context "when passed Pathname instances for the file arguments" do
168
- let(:path_factory) { method(:tmp_pathname) }
169
-
170
- include_context "copying files"
171
- end
172
- end
@@ -1,93 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::File, "#create_directory" do
4
- shared_context "creating directories" do
5
- it "creates empty directory" do
6
- app_dir = tmp_path('app')
7
-
8
- TTY::File.create_directory(app_dir, verbose: false)
9
-
10
- expect(File.exist?(app_dir)).to eq(true)
11
- end
12
-
13
- it "logs status" do
14
- doc_dir = tmp_path('doc')
15
-
16
- expect {
17
- TTY::File.create_dir(doc_dir, verbose: true)
18
- }.to output(%r{ \e\[32mcreate\e\[0m(.*)doc\n}).to_stdout_from_any_process
19
- end
20
-
21
- it "logs status wihtout color" do
22
- doc_dir = tmp_path('doc')
23
-
24
- expect {
25
- TTY::File.create_dir(doc_dir, verbose: true, color: false)
26
- }.to output(%r{ create(.*)doc\n}).to_stdout_from_any_process
27
- end
28
-
29
- it "creates tree of dirs and files" do
30
- app_dir = tmp_path('app')
31
-
32
- tree = {
33
- app_dir => [
34
- 'empty_file',
35
- ['full_file', 'File with contents'],
36
- 'subdir' => [
37
- 'empty_file_subdir',
38
- ['full_file_subdir', 'File with contents']
39
- ],
40
- 'empty' => []
41
- ]
42
- }
43
-
44
- TTY::File.create_directory(tree, verbose: false)
45
-
46
- expect(Find.find(app_dir.to_s).to_a).to eq([
47
- tmp_path('app'),
48
- tmp_path('app/empty'),
49
- tmp_path('app/empty_file'),
50
- tmp_path('app/full_file'),
51
- tmp_path('app/subdir'),
52
- tmp_path('app/subdir/empty_file_subdir'),
53
- tmp_path('app/subdir/full_file_subdir'),
54
- ])
55
-
56
- expect(::File.read(tmp_path('app/subdir/full_file_subdir'))).to eq('File with contents')
57
- end
58
-
59
- it "creates tree of dirs in parent directory" do
60
- app_dir = tmp_path('parent')
61
-
62
- tree = {
63
- 'app' => [
64
- ['file', "File multi\nline contents"],
65
- 'subdir' => ['file1', 'file2']
66
- ]
67
- }
68
-
69
- TTY::File.create_dir(tree, app_dir, verbose: false)
70
-
71
- expect(Find.find(app_dir.to_s).to_a).to eq([
72
- tmp_path('parent'),
73
- tmp_path('parent/app'),
74
- tmp_path('parent/app/file'),
75
- tmp_path('parent/app/subdir'),
76
- tmp_path('parent/app/subdir/file1'),
77
- tmp_path('parent/app/subdir/file2')
78
- ])
79
- end
80
- end
81
-
82
- context "when passed a String instance for the directory argument" do
83
- let(:path_factory) { method(:tmp_path) }
84
-
85
- include_context "creating directories"
86
- end
87
-
88
- context "when passed a Pathname instance for the directory argument" do
89
- let(:path_factory) { method(:tmp_pathname) }
90
-
91
- include_context "creating directories"
92
- end
93
- end
@@ -1,130 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::File, "#create_file" do
4
- shared_context "creating files" do
5
- context 'when new file' do
6
- it "creates file" do
7
- expect {
8
- TTY::File.create_file(path_factory.call('doc/README.md'))
9
- }.to output(/create/).to_stdout_from_any_process
10
-
11
- expect(File.exist?(tmp_path('doc/README.md'))).to eq(true)
12
- end
13
-
14
- it "creates file with content" do
15
- file = path_factory.call('doc/README.md')
16
- TTY::File.create_file(file, '# Title', verbose: false)
17
-
18
- expect(File.read(file)).to eq('# Title')
19
- end
20
-
21
- it "creates file with content in a block" do
22
- file = path_factory.call('doc/README.md')
23
- TTY::File.create_file(file, verbose: false) do
24
- "# Title"
25
- end
26
-
27
- expect(File.read(file)).to eq('# Title')
28
- end
29
-
30
- it "doesn't create file if :noop is true" do
31
- file = path_factory.call('doc/README.md')
32
- TTY::File.create_file(file, '# Title', noop: true, verbose: false)
33
-
34
- expect(File.exist?(file)).to eq(false)
35
- end
36
- end
37
-
38
- context 'when file exists' do
39
- context 'and is identical' do
40
- it "logs identical status" do
41
- file = path_factory.call('README.md')
42
- TTY::File.create_file(file, '# Title', verbose: false)
43
- expect {
44
- TTY::File.create_file(file, '# Title', verbose: true)
45
- }.to output(/identical/).to_stdout_from_any_process
46
- end
47
- end
48
-
49
- context 'and is not identical' do
50
- context 'and :force is true' do
51
- it "logs forced status to stdout" do
52
- file = path_factory.call('README.md')
53
- TTY::File.create_file(file, '# Title', verbose: false)
54
- expect {
55
- TTY::File.create_file(file, '# Header', verbose: true, force: true)
56
- }.to output(/force/).to_stdout_from_any_process
57
- end
58
-
59
- it 'overrides the previous file' do
60
- file = path_factory.call('README.md')
61
- TTY::File.create_file(file, '# Title', verbose: false)
62
- TTY::File.create_file(file, '# Header', force: true, verbose: false)
63
- content = File.read(file)
64
- expect(content).to eq('# Header')
65
- end
66
- end
67
-
68
- it "displays collision menu and overwrites" do
69
- test_prompt = TTY::TestPrompt.new
70
- test_prompt.input << "\n"
71
- test_prompt.input.rewind
72
- allow(TTY::Prompt).to receive(:new).and_return(test_prompt)
73
-
74
- file = path_factory.call('README.md')
75
- TTY::File.create_file(file, '# Title', verbose: false)
76
-
77
- expect {
78
- TTY::File.create_file(file, '# Header', verbose: true)
79
- }.to output(/collision/).to_stdout_from_any_process
80
-
81
- expect(File.read(file)).to eq('# Header')
82
- end
83
-
84
- it "displays collision menu and doesn't overwrite" do
85
- test_prompt = TTY::TestPrompt.new
86
- test_prompt.input << "n\n"
87
- test_prompt.input.rewind
88
- allow(TTY::Prompt).to receive(:new).and_return(test_prompt)
89
-
90
- file = path_factory.call('README.md')
91
- TTY::File.create_file(file, '# Title', verbose: false)
92
-
93
- expect {
94
- TTY::File.create_file(file, '# Header', verbose: true)
95
- }.to output(/collision/).to_stdout_from_any_process
96
-
97
- expect(File.read(file)).to eq('# Title')
98
- end
99
-
100
- it "displays collision menu and aborts" do
101
- test_prompt = TTY::TestPrompt.new
102
- test_prompt.input << "q\n"
103
- test_prompt.input.rewind
104
- allow(TTY::Prompt).to receive(:new).and_return(test_prompt)
105
-
106
- file = path_factory.call('README.md')
107
- TTY::File.create_file(file, '# Title', verbose: false)
108
-
109
- expect {
110
- TTY::File.create_file(file, '# Header', verbose: false)
111
- }.to raise_error(SystemExit)
112
-
113
- expect(File.read(file)).to eq('# Title')
114
- end
115
- end
116
- end
117
- end
118
-
119
- context 'when passed a String instance as the file argument' do
120
- let(:path_factory) { method(:tmp_path) }
121
-
122
- include_context "creating files"
123
- end
124
-
125
- context 'when passed a Pathname instance as the file argument' do
126
- let(:path_factory) { method(:tmp_pathname) }
127
-
128
- include_context "creating files"
129
- end
130
- end
@@ -1,107 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::File, "#diff" do
4
- shared_context "diffing files" do
5
- it "diffs two files" do
6
- file_a = path_factory.call('diff/file_a')
7
- file_b = path_factory.call('diff/file_b')
8
-
9
- diff = TTY::File.diff(file_a, file_b, verbose: false)
10
-
11
- expect(diff).to eq(strip_heredoc(<<-EOS
12
- @@ -1,4 +1,4 @@
13
- aaa
14
- -bbb
15
- +xxx
16
- ccc
17
- EOS
18
- ))
19
- end
20
-
21
- it "diffs identical files" do
22
- src_a = path_factory.call('diff/file_a')
23
-
24
- expect(TTY::File.diff(src_a, src_a, verbose: false)).to eq('')
25
- end
26
-
27
- it "diffs a file and a string" do
28
- src_a = path_factory.call('diff/file_a')
29
- src_b = "aaa\nxxx\nccc\n"
30
-
31
- diff = TTY::File.diff(src_a, src_b, verbose: false)
32
-
33
- expect(diff).to eq(strip_heredoc(<<-EOS
34
- @@ -1,4 +1,4 @@
35
- aaa
36
- -bbb
37
- +xxx
38
- ccc
39
- EOS
40
- ))
41
- end
42
-
43
- it "diffs two strings" do
44
- file_a = "aaa\nbbb\nccc\n"
45
- file_b = "aaa\nxxx\nccc\n"
46
-
47
- diff = TTY::File.diff(file_a, file_b, verbose: false)
48
-
49
- expect(diff).to eq(strip_heredoc(<<-EOS
50
- @@ -1,4 +1,4 @@
51
- aaa
52
- -bbb
53
- +xxx
54
- ccc
55
- EOS
56
- ))
57
- end
58
-
59
- it "logs status" do
60
- file_a = path_factory.call('diff/file_a')
61
- file_b = path_factory.call('diff/file_b')
62
-
63
- expect {
64
- TTY::File.diff_files(file_a, file_b, verbose: true)
65
- }.to output(%r{diff(.*)/diff/file_a(.*)/diff/file_b}).to_stdout_from_any_process
66
- end
67
-
68
- it "doesn't diff files when :noop option is given" do
69
- file_a = path_factory.call('diff/file_a')
70
- file_b = path_factory.call('diff/file_b')
71
-
72
- diff = TTY::File.diff(file_a, file_b, verbose: false, noop: true)
73
-
74
- expect(diff).to eq('')
75
- end
76
-
77
- it "doesn't diff if first file is too large" do
78
- file_a = path_factory.call('diff/file_a')
79
- file_b = path_factory.call('diff/file_b')
80
-
81
- expect {
82
- TTY::File.diff(file_a, file_b, threshold: 10)
83
- }.to raise_error(ArgumentError, /file size of (.*) exceeds 10 bytes/)
84
- end
85
-
86
- it "doesn't diff binary files" do
87
- file_a = path_factory.call('blackhole.png')
88
- file_b = path_factory.call('diff/file_b')
89
-
90
- expect {
91
- TTY::File.diff(file_a, file_b)
92
- }.to raise_error(ArgumentError, /is binary, diff output suppressed/)
93
- end
94
- end
95
-
96
- context "when passed String instances for the file arguments" do
97
- let(:path_factory) { method(:tmp_path) }
98
-
99
- include_context "diffing files"
100
- end
101
-
102
- context "when passed Pathname instances for the file arguments" do
103
- let(:path_factory) { method(:tmp_pathname) }
104
-
105
- include_context "diffing files"
106
- end
107
- end