tty-file 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +18 -2
  3. data/README.md +58 -16
  4. data/Rakefile +1 -1
  5. data/lib/tty-file.rb +0 -2
  6. data/lib/tty/file.rb +35 -9
  7. data/lib/tty/file/create_file.rb +3 -1
  8. data/lib/tty/file/differ.rb +3 -2
  9. data/lib/tty/file/digest_file.rb +1 -1
  10. data/lib/tty/file/download_file.rb +3 -3
  11. data/lib/tty/file/read_backward_file.rb +0 -1
  12. data/lib/tty/file/version.rb +2 -2
  13. data/spec/fixtures/cli_app/%name%_cli.rb +2 -0
  14. data/spec/fixtures/cli_app/commands/subcommand.rb +2 -0
  15. data/spec/fixtures/cli_app/excluded/%name%_cli.rb +2 -0
  16. data/spec/fixtures/templates/%file_name%.rb +1 -0
  17. data/spec/fixtures/templates/unit_test.rb +1 -0
  18. data/spec/spec_helper.rb +90 -0
  19. data/spec/unit/append_to_file_spec.rb +85 -0
  20. data/spec/unit/binary_spec.rb +206 -0
  21. data/spec/unit/checksum_file_spec.rb +39 -0
  22. data/spec/unit/chmod_spec.rb +78 -0
  23. data/spec/unit/copy_directory_spec.rb +106 -0
  24. data/spec/unit/copy_file_spec.rb +157 -0
  25. data/spec/unit/create_directory_spec.rb +79 -0
  26. data/spec/unit/create_file_spec.rb +116 -0
  27. data/spec/unit/diff_spec.rb +93 -0
  28. data/spec/unit/differ/call_spec.rb +101 -0
  29. data/spec/unit/download_file_spec.rb +54 -0
  30. data/spec/unit/escape_glob_path_spec.rb +14 -0
  31. data/spec/unit/inject_into_file_spec.rb +162 -0
  32. data/spec/unit/prepend_to_file_spec.rb +98 -0
  33. data/spec/unit/remove_file_spec.rb +53 -0
  34. data/spec/unit/replace_in_file_spec.rb +126 -0
  35. data/spec/unit/tail_file_spec.rb +63 -0
  36. data/tasks/console.rake +1 -1
  37. data/tasks/coverage.rake +1 -1
  38. data/tasks/spec.rake +1 -1
  39. data/tty-file.gemspec +5 -4
  40. metadata +30 -13
  41. data/.gitignore +0 -10
  42. data/.rspec +0 -3
  43. data/.travis.yml +0 -26
  44. data/CODE_OF_CONDUCT.md +0 -49
  45. data/Gemfile +0 -9
  46. data/appveyor.yml +0 -26
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe TTY::File, '#remove_file' do
4
+ it "removes a given file", unless: RSpec::Support::OS.windows? do
5
+ src_path = tmp_path('Gemfile')
6
+
7
+ TTY::File.remove_file(src_path, verbose: false)
8
+
9
+ expect(::File.exist?(src_path)).to be(false)
10
+ end
11
+
12
+ it "removes a directory" do
13
+ src_path = tmp_path('templates')
14
+
15
+ TTY::File.remove_file(src_path, verbose: false)
16
+
17
+ expect(::File.exist?(src_path)).to be(false)
18
+ end
19
+
20
+ it "pretends removing file" do
21
+ src_path = tmp_path('Gemfile')
22
+
23
+ TTY::File.remove_file(src_path, noop: true, verbose: false)
24
+
25
+ expect(::File.exist?(src_path)).to be(true)
26
+ end
27
+
28
+ it "removes files in secure mode" do
29
+ src_path = tmp_path('Gemfile')
30
+ allow(::FileUtils).to receive(:rm_r)
31
+
32
+ TTY::File.remove_file(src_path, verbose: false, secure: false)
33
+
34
+ expect(::FileUtils).to have_received(:rm_r).
35
+ with(src_path.to_s, force: nil, secure: false)
36
+ end
37
+
38
+ it "logs status" do
39
+ src_path = tmp_path('Gemfile')
40
+
41
+ expect {
42
+ TTY::File.remove_file(src_path, noop: true)
43
+ }.to output(/\e\[31mremove\e\[0m(.*)Gemfile/).to_stdout_from_any_process
44
+ end
45
+
46
+ it "logs status without color" do
47
+ src_path = tmp_path('Gemfile')
48
+
49
+ expect {
50
+ TTY::File.remove_file(src_path, noop: true, color: false)
51
+ }.to output(/\s+remove(.*)Gemfile/).to_stdout_from_any_process
52
+ end
53
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe TTY::File, '#replace_in_file' do
4
+ it "replaces file content with a matching string" do
5
+ file = tmp_path('Gemfile')
6
+ status = nil
7
+ expect {
8
+ status = TTY::File.replace_in_file(file, /gem 'rails'/, "gem 'hanami'")
9
+ }.to output(/replace/).to_stdout_from_any_process
10
+
11
+ expect(status).to eq(true)
12
+ expect(File.read(file)).to eq([
13
+ "gem 'nokogiri'\n",
14
+ "gem 'hanami', '5.0.0'\n",
15
+ "gem 'rack', '>=1.0'\n"
16
+ ].join)
17
+ end
18
+
19
+ it "replaces file content with a matching block value" do
20
+ file = tmp_path('Gemfile')
21
+ status = nil
22
+ expect {
23
+ status =TTY::File.replace_in_file(file, /gem 'rails'/, verbose: false) do |match|
24
+ match = "gem 'hanami'"
25
+ end
26
+ }.to_not output(/replace/).to_stdout_from_any_process
27
+
28
+ expect(status).to eq(true)
29
+ expect(File.read(file)).to eq([
30
+ "gem 'nokogiri'\n",
31
+ "gem 'hanami', '5.0.0'\n",
32
+ "gem 'rack', '>=1.0'\n"
33
+ ].join)
34
+ end
35
+
36
+ it "doesn't match file content" do
37
+ file = tmp_path('Gemfile')
38
+ content = ::File.read(file)
39
+ status = TTY::File.replace_in_file(file, /unknown/, 'Hello', verbose: false)
40
+
41
+ expect(status).to eq(false)
42
+ expect(::File.read(file)).to eq(content)
43
+ end
44
+
45
+ it "silences verbose output" do
46
+ content = "gem 'hanami'"
47
+ file = tmp_path('Gemfile')
48
+ expect {
49
+ TTY::File.replace_in_file(file, /gem 'rails'/, content, verbose: false)
50
+ }.to_not output(/replace/).to_stdout_from_any_process
51
+ end
52
+
53
+ it "fails to replace content when missing correct file path" do
54
+ expect {
55
+ TTY::File.replace_in_file('/non-existent-path',
56
+ /gem 'rails'/, "gem 'hanami'", verbose: false)
57
+ }.to raise_error(ArgumentError, /File path (.)* does not exist/)
58
+ end
59
+
60
+ it "logs action" do
61
+ content = "gem 'hanami'"
62
+ file = tmp_path('Gemfile')
63
+
64
+ expect {
65
+ TTY::File.replace_in_file(file, /gem 'rails'/, content, noop: true)
66
+ }.to output(/\e\[32mreplace\e\[0m(.*)Gemfile/).to_stdout_from_any_process
67
+ end
68
+
69
+ it "logs action without color" do
70
+ content = "gem 'hanami'"
71
+ file = tmp_path('Gemfile')
72
+
73
+ expect {
74
+ TTY::File.replace_in_file(file, /gem 'rails'/, content,
75
+ noop: true, color: false)
76
+ }.to output(/\s+replace(.*)Gemfile/).to_stdout_from_any_process
77
+ end
78
+
79
+ it "allows for noop run" do
80
+ content = "gem 'hanami'"
81
+ file = tmp_path('Gemfile')
82
+
83
+ TTY::File.replace_in_file(file, /gem 'rails'/, content,
84
+ noop: true, verbose: false)
85
+
86
+ expect(File.read(file)).to eq([
87
+ "gem 'nokogiri'\n",
88
+ "gem 'rails', '5.0.0'\n",
89
+ "gem 'rack', '>=1.0'\n"
90
+ ].join)
91
+ end
92
+
93
+ it "doesn't replace content when no match found" do
94
+ content = "gem 'hanami'"
95
+ file = tmp_path('Gemfile')
96
+
97
+ status = TTY::File.gsub_file(file, /gem 'rails'/, content, verbose: false)
98
+ expect(status).to eq(true)
99
+ expect(File.read(file)).to eq([
100
+ "gem 'nokogiri'\n",
101
+ "gem 'hanami', '5.0.0'\n",
102
+ "gem 'rack', '>=1.0'\n"
103
+ ].join)
104
+
105
+ status = TTY::File.gsub_file(file, /gem 'rails'/, content, verbose: false)
106
+ expect(status).to eq(false)
107
+
108
+ expect(File.read(file)).to eq([
109
+ "gem 'nokogiri'\n",
110
+ "gem 'hanami', '5.0.0'\n",
111
+ "gem 'rack', '>=1.0'\n"
112
+ ].join)
113
+ end
114
+
115
+ it "replaces with multibyte content" do
116
+ content = "gem 'ようこそ'"
117
+ file = tmp_path('Gemfile')
118
+
119
+ TTY::File.gsub_file(file, /gem 'rails'/, content, verbose: false)
120
+ expect(File.open(file, 'r:UTF-8', &:read)).to eq([
121
+ "gem 'nokogiri'\n",
122
+ "#{content}, '5.0.0'\n",
123
+ "gem 'rack', '>=1.0'\n"
124
+ ].join)
125
+ end
126
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe TTY::File, '#tail_file' do
4
+ it "tails file for lines with chunks smaller than file size" do
5
+ file = tmp_path('tail/lines')
6
+
7
+ lines = TTY::File.tail_file(file, 5, chunk_size: 2**3)
8
+
9
+ expect(lines).to eq([
10
+ "line12",
11
+ "line13",
12
+ "line14",
13
+ "line15",
14
+ "line16"
15
+ ])
16
+ end
17
+
18
+ it "tails file for lines with chunks equal file size" do
19
+ file = tmp_path('tail/lines')
20
+
21
+ lines = TTY::File.tail_file(file, 5, chunk_size: file.size)
22
+
23
+ expect(lines).to eq([
24
+ "line12",
25
+ "line13",
26
+ "line14",
27
+ "line15",
28
+ "line16"
29
+ ])
30
+
31
+ end
32
+
33
+ it "tails file for lines with chunks larger than file size" do
34
+ file = tmp_path('tail/lines')
35
+
36
+ lines = TTY::File.tail_file(file, 5, chunk_size: 2**9)
37
+
38
+ expect(lines).to eq([
39
+ "line12",
40
+ "line13",
41
+ "line14",
42
+ "line15",
43
+ "line16"
44
+ ])
45
+ end
46
+
47
+ it "tails file and yields lines" do
48
+ file = tmp_path('tail/lines')
49
+ lines = []
50
+
51
+ TTY::File.tail_file(file, 5, chunk_size: 8) do |line|
52
+ lines << line
53
+ end
54
+
55
+ expect(lines).to eq([
56
+ "line12",
57
+ "line13",
58
+ "line14",
59
+ "line15",
60
+ "line16"
61
+ ])
62
+ end
63
+ end
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  desc 'Load gem inside irb console'
4
4
  task :console do
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  desc 'Measure code coverage'
4
4
  task :coverage do
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  begin
4
4
  require 'rspec/core/rake_task'
@@ -1,4 +1,3 @@
1
- # coding: utf-8
2
1
  lib = File.expand_path('../lib', __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'tty/file/version'
@@ -14,7 +13,9 @@ Gem::Specification.new do |spec|
14
13
  spec.homepage = "https://piotrmurach.github.io/tty"
15
14
  spec.license = "MIT"
16
15
 
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.files = Dir['{lib,spec}/**/*.rb']
17
+ spec.files += Dir['{bin,tasks}/*', 'tty-file.gemspec']
18
+ spec.files += Dir['README.md', 'CHANGELOG.md', 'LICENSE.txt', 'Rakefile']
18
19
  spec.bindir = "exe"
19
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
21
  spec.require_paths = ["lib"]
@@ -22,8 +23,8 @@ Gem::Specification.new do |spec|
22
23
  spec.required_ruby_version = '>= 2.0.0'
23
24
 
24
25
  spec.add_dependency 'pastel', '~> 0.7.2'
25
- spec.add_dependency 'tty-prompt', '~> 0.16.1'
26
- spec.add_dependency 'diff-lcs', '~> 1.3.0'
26
+ spec.add_dependency 'tty-prompt', '~> 0.18'
27
+ spec.add_dependency 'diff-lcs', '~> 1.3'
27
28
 
28
29
  spec.add_development_dependency 'bundler', '~> 1.5'
29
30
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tty-file
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-21 00:00:00.000000000 Z
11
+ date: 2018-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel
@@ -30,28 +30,28 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.16.1
33
+ version: '0.18'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.16.1
40
+ version: '0.18'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: diff-lcs
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 1.3.0
47
+ version: '1.3'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 1.3.0
54
+ version: '1.3'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -115,16 +115,10 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
- - ".gitignore"
119
- - ".rspec"
120
- - ".travis.yml"
121
118
  - CHANGELOG.md
122
- - CODE_OF_CONDUCT.md
123
- - Gemfile
124
119
  - LICENSE.txt
125
120
  - README.md
126
121
  - Rakefile
127
- - appveyor.yml
128
122
  - bin/console
129
123
  - bin/setup
130
124
  - lib/tty-file.rb
@@ -135,6 +129,29 @@ files:
135
129
  - lib/tty/file/download_file.rb
136
130
  - lib/tty/file/read_backward_file.rb
137
131
  - lib/tty/file/version.rb
132
+ - spec/fixtures/cli_app/%name%_cli.rb
133
+ - spec/fixtures/cli_app/commands/subcommand.rb
134
+ - spec/fixtures/cli_app/excluded/%name%_cli.rb
135
+ - spec/fixtures/templates/%file_name%.rb
136
+ - spec/fixtures/templates/unit_test.rb
137
+ - spec/spec_helper.rb
138
+ - spec/unit/append_to_file_spec.rb
139
+ - spec/unit/binary_spec.rb
140
+ - spec/unit/checksum_file_spec.rb
141
+ - spec/unit/chmod_spec.rb
142
+ - spec/unit/copy_directory_spec.rb
143
+ - spec/unit/copy_file_spec.rb
144
+ - spec/unit/create_directory_spec.rb
145
+ - spec/unit/create_file_spec.rb
146
+ - spec/unit/diff_spec.rb
147
+ - spec/unit/differ/call_spec.rb
148
+ - spec/unit/download_file_spec.rb
149
+ - spec/unit/escape_glob_path_spec.rb
150
+ - spec/unit/inject_into_file_spec.rb
151
+ - spec/unit/prepend_to_file_spec.rb
152
+ - spec/unit/remove_file_spec.rb
153
+ - spec/unit/replace_in_file_spec.rb
154
+ - spec/unit/tail_file_spec.rb
138
155
  - tasks/console.rake
139
156
  - tasks/coverage.rake
140
157
  - tasks/spec.rake
@@ -159,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
176
  version: '0'
160
177
  requirements: []
161
178
  rubyforge_project:
162
- rubygems_version: 2.5.1
179
+ rubygems_version: 2.7.3
163
180
  signing_key:
164
181
  specification_version: 4
165
182
  summary: File manipulation utility methods.
data/.gitignore DELETED
@@ -1,10 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
- /vendor/
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --require spec_helper
2
- --warnings
3
- --color
@@ -1,26 +0,0 @@
1
- ---
2
- language: ruby
3
- sudo: false
4
- cache: bundler
5
- script: "bundle update && bundle exec rake ci"
6
- rvm:
7
- - 2.0.0
8
- - 2.1.10
9
- - 2.2.9
10
- - 2.3.6
11
- - 2.4.4
12
- - 2.5.1
13
- - ruby-head
14
- - jruby-9.1.1.0
15
- env:
16
- global:
17
- - JRUBY_OPTS=''
18
- matrix:
19
- allow_failures:
20
- - rvm: ruby-head
21
- - rvm: jruby-9.1.1.0
22
- fast_finish: true
23
- branches:
24
- only: master
25
- notifications:
26
- email: false
@@ -1,49 +0,0 @@
1
- # Contributor Code of Conduct
2
-
3
- As contributors and maintainers of this project, and in the interest of
4
- fostering an open and welcoming community, we pledge to respect all people who
5
- contribute through reporting issues, posting feature requests, updating
6
- documentation, submitting pull requests or patches, and other activities.
7
-
8
- We are committed to making participation in this project a harassment-free
9
- experience for everyone, regardless of level of experience, gender, gender
10
- identity and expression, sexual orientation, disability, personal appearance,
11
- body size, race, ethnicity, age, religion, or nationality.
12
-
13
- Examples of unacceptable behavior by participants include:
14
-
15
- * The use of sexualized language or imagery
16
- * Personal attacks
17
- * Trolling or insulting/derogatory comments
18
- * Public or private harassment
19
- * Publishing other's private information, such as physical or electronic
20
- addresses, without explicit permission
21
- * Other unethical or unprofessional conduct
22
-
23
- Project maintainers have the right and responsibility to remove, edit, or
24
- reject comments, commits, code, wiki edits, issues, and other contributions
25
- that are not aligned to this Code of Conduct, or to ban temporarily or
26
- permanently any contributor for other behaviors that they deem inappropriate,
27
- threatening, offensive, or harmful.
28
-
29
- By adopting this Code of Conduct, project maintainers commit themselves to
30
- fairly and consistently applying these principles to every aspect of managing
31
- this project. Project maintainers who do not follow or enforce the Code of
32
- Conduct may be permanently removed from the project team.
33
-
34
- This code of conduct applies both within project spaces and in public spaces
35
- when an individual is representing the project or its community.
36
-
37
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
- reported by contacting a project maintainer at [email]. All
39
- complaints will be reviewed and investigated and will result in a response that
40
- is deemed necessary and appropriate to the circumstances. Maintainers are
41
- obligated to maintain confidentiality with regard to the reporter of an
42
- incident.
43
-
44
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
- version 1.3.0, available at
46
- [http://contributor-covenant.org/version/1/3/0/][version]
47
-
48
- [homepage]: http://contributor-covenant.org
49
- [version]: http://contributor-covenant.org/version/1/3/0/