Skip to content

Commit

Permalink
Remove injection of newline to FileSystem
Browse files Browse the repository at this point in the history
Un-doing: e1b56bb
  • Loading branch information
cllns committed Jan 21, 2022
1 parent 619954e commit 2710d51
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 54 deletions.
10 changes: 7 additions & 3 deletions lib/dry/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class Files
#
# @since 0.1.0
# @api public
def initialize(memory: false, adapter: Adapter.call(memory: memory,
newline: $INPUT_RECORD_SEPARATOR))
def initialize(memory: false, adapter: Adapter.call(memory: memory), newline: $INPUT_RECORD_SEPARATOR)
@adapter = adapter
@newline = newline
end

# Read file content
Expand Down Expand Up @@ -74,7 +74,7 @@ def touch(path)
# @since 0.1.0
# @api public
def write(path, lines)
joined_lines = Array(lines).flatten.map { |line| line.chomp.concat($/) }.join
joined_lines = Array(lines).flatten.map { |line| line.chomp.concat(newline) }.join
adapter.write(path, joined_lines)
end

Expand Down Expand Up @@ -777,6 +777,10 @@ def remove_block(path, target)
# @api private
attr_reader :adapter

# @since x.x.x
# @api private
attr_reader :newline

# @since 0.1.0
# @api private
def match?(content, target)
Expand Down
4 changes: 2 additions & 2 deletions lib/dry/files/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class Files
class Adapter
# @since 0.1.0
# @api private
def self.call(newline:, memory:)
def self.call(memory:)
if memory
require_relative "./memory_file_system"
MemoryFileSystem.new
else
require_relative "./file_system"
FileSystem.new(newline: newline)
FileSystem.new
end
end
end
Expand Down
7 changes: 1 addition & 6 deletions lib/dry/files/file_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ class FileSystem
# @api private
attr_reader :file_utils

# @since x.x.x
# @api private
attr_reader :newline

# Creates a new instance
#
# @param file [Class]
Expand All @@ -29,10 +25,9 @@ class FileSystem
# @return [Dry::Files::FileSystem]
#
# @since 0.1.0
def initialize(newline:, file: File, file_utils: FileUtils)
def initialize(file: File, file_utils: FileUtils)
@file = file
@file_utils = file_utils
@newline = newline
end

# Opens (or creates) a new file for both read/write operations.
Expand Down
50 changes: 9 additions & 41 deletions spec/unit/dry/files/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,21 @@

RSpec.describe Dry::Files::Adapter do
describe ".call" do
let(:subject) { described_class.call(memory: memory, newline: newline) }
let(:subject) { described_class.call(memory: memory) }

describe "default newline character" do
let(:newline) { $INPUT_RECORD_SEPARATOR }
context "memory: true" do
let(:memory) { true }

context "memory: true" do
let(:memory) { true }

it "returns memory file system adapter" do
expect(subject).to be_kind_of(Dry::Files::MemoryFileSystem)
end
end

context "memory: false" do
let(:memory) { false }

it "returns real file system adapter" do
expect(subject).to be_kind_of(Dry::Files::FileSystem)
end

it "returns newline" do
expect(subject.newline).to eq("\n")
end
it "returns memory file system adapter" do
expect(subject).to be_kind_of(Dry::Files::MemoryFileSystem)
end
end

describe "override newline character" do
let(:newline) { "\r\n" }

context "memory: true" do
let(:memory) { true }

it "returns memory file system adapter" do
expect(subject).to be_kind_of(Dry::Files::MemoryFileSystem)
end
end

context "memory: false" do
let(:memory) { false }

it "returns real file system adapter" do
expect(subject).to be_kind_of(Dry::Files::FileSystem)
end
context "memory: false" do
let(:memory) { false }

it "returns newline" do
expect(subject.newline).to eq("\r\n")
end
it "returns real file system adapter" do
expect(subject).to be_kind_of(Dry::Files::FileSystem)
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions spec/unit/dry/files/file_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
let(:root) { Pathname.new(Dir.pwd).join("tmp", SecureRandom.uuid).tap(&:mkpath) }
let(:newline) { $INPUT_RECORD_SEPARATOR }

subject { described_class.new(newline: newline) }

after do
FileUtils.remove_entry_secure(root)
end
Expand Down

0 comments on commit 2710d51

Please sign in to comment.