tty-file 0.8.0 → 0.9.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.
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,230 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::File, '#binary?' do
4
- let(:ascii) { "This is a text file.\nWith more than one line.\nAnd a \tTab.\nAnd other printable chars too: ~!@\#$%^&*()`:\"<>?{}|_+,./;'[]\\-=\n" }
5
-
6
- let(:utf_8) { "Testing utf-8 unicode...\n\n\non a new line: \xE2\x80\x93\n" }
7
-
8
- let(:latin_1) { "Testing latin chars...\nsuch as #{0xFD.chr}mlaut.\n" }
9
-
10
- let(:shift_jis) { "And some kanjis:\n #{0x83.chr}#{0x80.chr}.\n" }
11
-
12
- it "identifies zero-length file as non-binary" do
13
- Tempfile.open('tty-file-binary-spec') do |file|
14
- expect(TTY::File.binary?(file)).to eq(false)
15
- end
16
- end
17
-
18
- it "indentifies text with hex as binary" do
19
- Tempfile.open('tty-file-binary-spec') do |file|
20
- file << "hi \xAD"
21
- file.close
22
-
23
- expect(TTY::File.binary?(file)).to eq(true)
24
- end
25
- end
26
-
27
- it "identifies image file as binary" do
28
- file = tmp_path('blackhole.png')
29
-
30
- expect(TTY::File.binary?(file)).to eq(true)
31
- end
32
-
33
- it "identifies an image file given as a pathname as binary" do
34
- file = tmp_pathname('blackhole.png')
35
-
36
- expect(TTY::File.binary?(file)).to eq(true)
37
- end
38
-
39
- it 'identifies image file provided by a Pathname instance as binary' do
40
- file = tmp_pathname('blackhole.png')
41
-
42
- expect(TTY::File.binary?(file)).to eq(true)
43
- end
44
-
45
- it "indetifies text file as non-binary" do
46
- file = tmp_path('Gemfile')
47
-
48
- expect(TTY::File.binary?(file)).to eq(false)
49
- end
50
-
51
- it 'identifies text file provided by a Pathname instance as non-binary' do
52
- file = tmp_pathname('Gemfile')
53
-
54
- expect(TTY::File.binary?(file)).to eq(false)
55
- end
56
-
57
- it "indentifies a file as non-binary when greater than 4096 bytes with unicode chars", unless: RSpec::Support::OS.windows? do
58
- file = tmp_pathname("binary", "unicode.txt")
59
-
60
- expect(TTY::File.binary?(file)).to eq(false)
61
- end
62
-
63
- it "indetifies a null-terminated string file as binary" do
64
- Tempfile.open('tty-file-binary-spec') do |file|
65
- file.write("Binary content.\0")
66
- file.close
67
-
68
- expect(TTY::File.binary?(file)).to eq(true)
69
- end
70
- end
71
-
72
- it "indetifies a null-terminated multi-line string file as binary" do
73
- Tempfile.open('tty-file-binary-spec') do |file|
74
- file.write("Binary content.\non manylnes\nreally\0")
75
- file.close
76
-
77
- expect(TTY::File.binary?(file)).to eq(true)
78
- end
79
- end
80
-
81
- context "when the default external encoding is UTF-8" do
82
- before do
83
- @saved_verbosity = $VERBOSE
84
- @saved_encoding = Encoding.default_external
85
- $VERBOSE = nil
86
- Encoding.default_external = Encoding::UTF_8
87
- end
88
-
89
- after do
90
- Encoding.default_external = @saved_encoding
91
- $VERBOSE = @saved_verbosity
92
- end
93
-
94
- it "identifies ASCII as non-binary" do
95
- Tempfile.open('tty-file-binary-spec') do |file|
96
- file << ascii
97
- file.close
98
-
99
- expect(TTY::File.binary?(file)).to eq(false)
100
- end
101
- end
102
-
103
- it "identifies UTF-8 as non-binary" do
104
- Tempfile.open('tty-file-binary-spec') do |file|
105
- file << utf_8
106
- file.close
107
-
108
- expect(TTY::File.binary?(file)).to eq(false)
109
- end
110
- end
111
-
112
- it "indentifies Latin-1 as invalid UTF-8 and hence as binary" do
113
- Tempfile.open('tty-file-binary-spec') do |file|
114
- file << latin_1
115
- file.close
116
-
117
- expect(TTY::File.binary?(file)).to eq(true)
118
- end
119
- end
120
-
121
- it "identifies Shift-JIS as invalid UTF-8 and hence as binary" do
122
- Tempfile.open('tty-file-binary-spec') do |file|
123
- file << shift_jis
124
- file.close
125
-
126
- expect(TTY::File.binary?(file)).to eq(true)
127
- end
128
- end
129
- end
130
-
131
- context "when the default external encoding is Latin-1" do
132
- before do
133
- @saved_verbosity = $VERBOSE
134
- @saved_encoding = Encoding.default_external
135
- $VERBOSE = nil
136
- Encoding.default_external = Encoding::ISO_8859_1
137
- end
138
-
139
- after do
140
- Encoding.default_external = @saved_encoding
141
- $VERBOSE = @saved_verbosity
142
- end
143
-
144
- it "identifies ASCII as non-binary" do
145
- Tempfile.open('tty-file-binary-spec') do |file|
146
- file << ascii
147
- file.close
148
-
149
- expect(TTY::File.binary?(file)).to eq(false)
150
- end
151
- end
152
-
153
- it "identifies UTF-8 as invalid Latin-1 and hence binary" do
154
- Tempfile.open('tty-file-binary-spec') do |file|
155
- file << utf_8
156
- file.close
157
-
158
- expect(TTY::File.binary?(file)).to eq(true)
159
- end
160
- end
161
-
162
- it "indentifies Latin-1 as non-binary" do
163
- Tempfile.open('tty-file-binary-spec') do |file|
164
- file << latin_1
165
- file.close
166
-
167
- expect(TTY::File.binary?(file)).to eq(false)
168
- end
169
- end
170
-
171
- it "identifies Shift-JIS as invalid Latin-1 and hence as binary" do
172
- Tempfile.open('tty-file-binary-spec') do |file|
173
- file << shift_jis
174
- file.close
175
-
176
- expect(TTY::File.binary?(file)).to eq(true)
177
- end
178
- end
179
- end
180
-
181
- context "when the default external encoding is Shift-JIS" do
182
- before do
183
- @saved_verbosity = $VERBOSE
184
- @saved_encoding = Encoding.default_external
185
- $VERBOSE = nil
186
- Encoding.default_external = Encoding::SHIFT_JIS
187
- end
188
-
189
- after do
190
- Encoding.default_external = @saved_encoding
191
- $VERBOSE = @saved_verbosity
192
- end
193
-
194
- it "identifies ASCII as non-binary" do
195
- Tempfile.open('tty-file-binary-spec') do |file|
196
- file << ascii
197
- file.close
198
-
199
- expect(TTY::File.binary?(file)).to eq(false)
200
- end
201
- end
202
-
203
- it "identifies UTF-8 as invalid Shift-JIS and hence as binary" do
204
- Tempfile.open('tty-file-binary-spec') do |file|
205
- file << utf_8
206
- file.close
207
-
208
- expect(TTY::File.binary?(file)).to eq(true)
209
- end
210
- end
211
-
212
- it "indentifies Latin-1 as invalid Shift-JIS and hence as binary" do
213
- Tempfile.open('tty-file-binary-spec') do |file|
214
- file << latin_1
215
- file.close
216
-
217
- expect(TTY::File.binary?(file)).to eq(true)
218
- end
219
- end
220
-
221
- it "identifies Shift-JIS as non-binary" do
222
- Tempfile.open('tty-file-binary-spec') do |file|
223
- file << shift_jis
224
- file.close
225
-
226
- expect(TTY::File.binary?(file)).to eq(false)
227
- end
228
- end
229
- end
230
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::File, '#checksum_file' do
4
- it "generates checksum for a file" do
5
- file = tmp_path('checksum/README.md')
6
-
7
- checksum = TTY::File.checksum_file(file)
8
- expected = '76ba1beb6c611fa32624ed253444138cdf23eb938a3812137f8a399c5b375bfe'
9
-
10
- expect(checksum).to eq(expected)
11
- end
12
-
13
- it "generates checksum for a Pathname instance" do
14
- file = tmp_pathname('checksum/README.md')
15
-
16
- checksum = TTY::File.checksum_file(file)
17
- expected = '76ba1beb6c611fa32624ed253444138cdf23eb938a3812137f8a399c5b375bfe'
18
-
19
- expect(checksum).to eq(expected)
20
- end
21
-
22
- it "generates checksum for IO object" do
23
- io = StringIO.new("Some content\nThe end")
24
-
25
- checksum = TTY::File.checksum_file(io, 'md5')
26
- expected = "ad0962e2374b1899fcfb818896703e50"
27
-
28
- expect(checksum).to eq(expected)
29
- end
30
-
31
- it "generates checksum for String" do
32
- string = "Some content\nThe end"
33
-
34
- checksum = TTY::File.checksum_file(string, 'sha1')
35
- expected = "289388f187404135e6c15b21460442cf867180dd"
36
-
37
- expect(checksum).to eq(expected)
38
- end
39
-
40
- it "doesn't digest when :noop option" do
41
- digester = double(:digester)
42
- allow(TTY::File::DigestFile).to receive(:new).and_return(digester)
43
-
44
- TTY::File.checksum_file('string', noop: true)
45
-
46
- expect(digester).to_not receive(:call)
47
- end
48
- end
@@ -1,92 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::File, "#chmod" do
4
- shared_context "changing file permissions" do
5
- context 'when octal permisssions' do
6
- it "adds permissions to file - user executable",
7
- unless: RSpec::Support::OS.windows? do
8
-
9
- file = path_factory.call('script.sh')
10
- mode = File.lstat(file).mode
11
- expect(File.executable?(file)).to eq(false)
12
-
13
- TTY::File.chmod(file, mode | TTY::File::U_X, verbose: false)
14
-
15
- expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X)
16
- end
17
-
18
- it "logs status when :verbose flag is true",
19
- unless: RSpec::Support::OS.windows? do
20
-
21
- file = path_factory.call('script.sh')
22
- mode = File.lstat(file).mode
23
- expect(File.executable?(file)).to eq(false)
24
-
25
- expect {
26
- TTY::File.chmod(file, mode | TTY::File::U_X)
27
- }.to output(/chmod/).to_stdout_from_any_process
28
-
29
- expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X)
30
- end
31
-
32
- it "doesn't change permission when :noop flag is true" do
33
- file = path_factory.call('script.sh')
34
- mode = File.lstat(file).mode
35
- expect(File.executable?(file)).to eq(false)
36
-
37
- TTY::File.chmod(file, mode | TTY::File::U_X, verbose: false, noop: true)
38
-
39
- expect(File.lstat(file).mode).to eq(mode)
40
- end
41
- end
42
-
43
- context 'when human readable permissions' do
44
- it "adds permisions to file - user executable",
45
- unless: RSpec::Support::OS.windows? do
46
-
47
- file = path_factory.call('script.sh')
48
- mode = File.lstat(file).mode
49
- expect(File.executable?(file)).to eq(false)
50
-
51
- TTY::File.chmod(file, 'u+x', verbose: false)
52
-
53
- expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X)
54
- end
55
-
56
- it "removes permission for user executable" do
57
- file = path_factory.call('script.sh')
58
- mode = File.lstat(file).mode
59
- expect(File.writable?(file)).to eq(true)
60
-
61
- TTY::File.chmod(file, 'u-w', verbose: false)
62
-
63
- expect(File.lstat(file).mode).to eq(mode ^ TTY::File::U_W)
64
- expect(File.writable?(file)).to eq(false)
65
- end
66
-
67
- it "adds multiple permissions separated by comma",
68
- unless: RSpec::Support::OS.windows? do
69
-
70
- file = path_factory.call('script.sh')
71
- mode = File.lstat(file).mode
72
- expect(File.executable?(file)).to eq(false)
73
-
74
- TTY::File.chmod(file, 'u+x,g+x', verbose: false)
75
-
76
- expect(File.lstat(file).mode).to eq(mode | TTY::File::U_X | TTY::File::G_X)
77
- end
78
- end
79
- end
80
-
81
- context "when passed a String instance for the file argument" do
82
- let(:path_factory) { method(:tmp_path) }
83
-
84
- include_context "changing file permissions"
85
- end
86
-
87
- context "when passed a Pathname instance for the file argument" do
88
- let(:path_factory) { method(:tmp_pathname) }
89
-
90
- include_context "changing file permissions"
91
- end
92
- end
@@ -1,120 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe TTY::File, "#copy_directory" do
4
- shared_context "copies directory" do
5
- it "copies directory of files recursively" do
6
- app = path_factory.call('cli_app')
7
- apps = path_factory.call('apps')
8
-
9
- variables = OpenStruct.new
10
- variables[:name] = 'tty'
11
- variables[:foo] = 'Foo'
12
- variables[:bar] = 'Bar'
13
-
14
- TTY::File.copy_directory(app, apps, context: variables, verbose: false)
15
-
16
- expect(Find.find(apps.to_s).to_a).to eq([
17
- tmp_path('apps'),
18
- tmp_path('apps/README'),
19
- tmp_path('apps/command.rb'),
20
- tmp_path('apps/commands'),
21
- tmp_path('apps/commands/subcommand.rb'),
22
- tmp_path('apps/excluded'),
23
- tmp_path('apps/excluded/command.rb'),
24
- tmp_path('apps/excluded/tty_cli.rb'),
25
- tmp_path('apps/tty_cli.rb')
26
- ])
27
-
28
- expect(File.read(tmp_path('apps/command.rb'))).to eq("class FooCommand\nend\n")
29
- expect(File.read(tmp_path('apps/excluded/command.rb'))).to eq("class BarCommand\nend\n")
30
- end
31
-
32
- it "copies top level directory of files and evalutes templates" do
33
- app = path_factory.call('cli_app')
34
- apps = path_factory.call('apps')
35
-
36
- variables = OpenStruct.new
37
- variables[:name] = 'tty'
38
- variables[:foo] = 'Foo'
39
- variables[:bar] = 'Bar'
40
-
41
- TTY::File.copy_directory(app, apps, recursive: false,
42
- context: variables,
43
- verbose: false)
44
-
45
- expect(Find.find(apps.to_s).to_a).to eq([
46
- tmp_path('apps'),
47
- tmp_path('apps/README'),
48
- tmp_path('apps/command.rb'),
49
- tmp_path('apps/tty_cli.rb')
50
- ])
51
- end
52
-
53
- it "handles glob characters in the path" do
54
- src = path_factory.call("foo[1]")
55
- dest = path_factory.call("foo1")
56
- TTY::File.copy_directory(src, dest, verbose: false)
57
-
58
- expect(Find.find(dest.to_s).to_a).to eq([
59
- tmp_path('foo1'),
60
- tmp_path('foo1/README.md')
61
- ])
62
- end
63
-
64
- it "ignores excluded directories" do
65
- src = path_factory.call('cli_app')
66
- dest = path_factory.call('ignored')
67
-
68
- variables = OpenStruct.new
69
- variables[:name] = 'tty'
70
- variables[:foo] = 'Foo'
71
- variables[:bar] = 'Bar'
72
-
73
- TTY::File.copy_directory(src, dest, context: variables,
74
- exclude: %r{excluded/},
75
- verbose: false)
76
-
77
- expect(Find.find(dest.to_s).to_a).to eq([
78
- tmp_path('ignored'),
79
- tmp_path('ignored/README'),
80
- tmp_path('ignored/command.rb'),
81
- tmp_path('ignored/commands'),
82
- tmp_path('ignored/commands/subcommand.rb'),
83
- tmp_path('ignored/tty_cli.rb')
84
- ])
85
- end
86
-
87
- it "raises error when source directory doesn't exist" do
88
- expect {
89
- TTY::File.copy_directory('unknown')
90
- }.to raise_error(ArgumentError, %r{File path "unknown" does not exist.})
91
- end
92
-
93
- it "logs status" do
94
- app = path_factory.call('cli_app')
95
- apps = path_factory.call('apps')
96
-
97
- variables = OpenStruct.new
98
- variables[:name] = 'tty'
99
- variables[:class_name] = 'TTY'
100
-
101
- expect {
102
- TTY::File.copy_directory(app, apps, context: variables, verbose: true)
103
- }.to output(
104
- %r{create(.*)apps/tty_cli.rb\n(.*)create(.*)apps/README\n(.*)create(.*)apps/command.rb\n}m
105
- ).to_stdout_from_any_process
106
- end
107
- end
108
-
109
- context "when passed String instances for the file arguments" do
110
- let(:path_factory) { method(:tmp_path) }
111
-
112
- it_behaves_like "copies directory"
113
- end
114
-
115
- context "when passed Pathname instances for the file arguments" do
116
- let(:path_factory) { method(:tmp_pathname) }
117
-
118
- it_behaves_like "copies directory"
119
- end
120
- end