tty-file 0.7.0 → 0.7.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/lib/tty/file.rb +35 -21
- data/lib/tty/file/version.rb +1 -1
- data/spec/spec_helper.rb +4 -0
- data/spec/unit/append_to_file_spec.rb +96 -71
- data/spec/unit/binary_spec.rb +19 -1
- data/spec/unit/checksum_file_spec.rb +9 -0
- data/spec/unit/chmod_spec.rb +65 -51
- data/spec/unit/copy_directory_spec.rb +110 -96
- data/spec/unit/copy_file_spec.rb +117 -102
- data/spec/unit/create_directory_spec.rb +83 -69
- data/spec/unit/create_file_spec.rb +96 -82
- data/spec/unit/diff_spec.rb +97 -83
- data/spec/unit/download_file_spec.rb +49 -35
- data/spec/unit/inject_into_file_spec.rb +162 -148
- data/spec/unit/prepend_to_file_spec.rb +109 -83
- data/spec/unit/remove_file_spec.rb +45 -31
- data/spec/unit/replace_in_file_spec.rb +127 -113
- data/spec/unit/tail_file_spec.rb +61 -47
- data/tty-file.gemspec +4 -4
- metadata +12 -13
| @@ -1,54 +1,68 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 2 |  | 
| 3 3 | 
             
            RSpec.describe TTY::File, '#download_file' do
         | 
| 4 | 
            -
               | 
| 5 | 
            -
                 | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 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 | 
            -
             | 
| 10 | 
            +
                  TTY::File.download_file('https://example.com/README.md', path, verbose: false)
         | 
| 10 11 |  | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 12 | 
            +
                  expect(File.read(path)).to eq(body)
         | 
| 13 | 
            +
                end
         | 
| 13 14 |  | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 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 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 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 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 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 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 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 | 
            -
             | 
| 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 | 
            -
             | 
| 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 | 
            -
             | 
| 42 | 
            -
             | 
| 43 | 
            -
                }.to raise_error(TTY::File::DownloadError)
         | 
| 53 | 
            +
                  exists_and_identical?('Gemfile', 'app/Gemfile')
         | 
| 54 | 
            +
                end
         | 
| 44 55 | 
             
              end
         | 
| 45 56 |  | 
| 46 | 
            -
               | 
| 47 | 
            -
                 | 
| 48 | 
            -
             | 
| 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 | 
            -
             | 
| 63 | 
            +
              context 'when passed a Pathname instance for the file argument' do
         | 
| 64 | 
            +
                let(:path_factory) { method(:tmp_pathname) }
         | 
| 51 65 |  | 
| 52 | 
            -
                 | 
| 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 | 
            -
               | 
| 5 | 
            -
                file  | 
| 6 | 
            -
             | 
| 7 | 
            -
                   | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
                   | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
                file  | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
                   | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
                   | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 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 | 
            -
                   | 
| 39 | 
            -
                 | 
| 40 | 
            -
             | 
| 41 | 
            -
                 | 
| 42 | 
            -
                   | 
| 43 | 
            -
             | 
| 44 | 
            -
                   | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
             | 
| 51 | 
            -
             | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
             | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
                   | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
             | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 78 | 
            -
             | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 81 | 
            -
             | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
             | 
| 88 | 
            -
                   | 
| 89 | 
            -
             | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 99 | 
            -
             | 
| 100 | 
            -
             | 
| 101 | 
            -
                   | 
| 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 | 
            -
             | 
| 107 | 
            -
             | 
| 108 | 
            -
             | 
| 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 | 
            -
             | 
| 119 | 
            -
             | 
| 120 | 
            -
             | 
| 121 | 
            -
             | 
| 122 | 
            -
             | 
| 123 | 
            -
             | 
| 124 | 
            -
                 | 
| 125 | 
            -
             | 
| 126 | 
            -
             | 
| 127 | 
            -
                   | 
| 128 | 
            -
                  "gem ' | 
| 129 | 
            -
             | 
| 130 | 
            -
             | 
| 131 | 
            -
             | 
| 132 | 
            -
             | 
| 133 | 
            -
             | 
| 134 | 
            -
             | 
| 135 | 
            -
                   | 
| 136 | 
            -
             | 
| 137 | 
            -
             | 
| 138 | 
            -
             | 
| 139 | 
            -
                   | 
| 140 | 
            -
             | 
| 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 | 
            -
               | 
| 144 | 
            -
                 | 
| 165 | 
            +
              context "when passed a String instance for the file argument" do
         | 
| 166 | 
            +
                let(:path_factory) { method(:tmp_path) }
         | 
| 145 167 |  | 
| 146 | 
            -
                 | 
| 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 | 
            -
               | 
| 152 | 
            -
                 | 
| 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 | 
            -
                 | 
| 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 | 
            -
               | 
| 5 | 
            -
                file  | 
| 6 | 
            -
             | 
| 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 | 
            -
             | 
| 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 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
                   | 
| 35 | 
            -
             | 
| 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 | 
            -
             | 
| 40 | 
            -
             | 
| 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 | 
            -
             | 
| 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 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
                   | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
                 | 
| 69 | 
            -
             | 
| 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 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
             | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 78 | 
            -
             | 
| 79 | 
            -
                   | 
| 80 | 
            -
                  "gem ' | 
| 81 | 
            -
                   | 
| 82 | 
            -
             | 
| 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 | 
            -
               | 
| 86 | 
            -
                 | 
| 87 | 
            -
             | 
| 88 | 
            -
             | 
| 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 | 
            -
               | 
| 93 | 
            -
                 | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 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
         |