Skip to content

Commit

Permalink
Add step and API to add whole lines to a file
Browse files Browse the repository at this point in the history
Due to how multi-line string literals work in Gherkin, it is easy to
accidentally have fewer newlines than intended when building up a file
using append_to_file.

To mitigate this while still allowing building up files without inserted
newlines, this change introduces a new API method #append_lines_to_file and
a corresponding step definition, that will ensure a newline is present
between the original content and the added content.
  • Loading branch information
mvz committed Jan 24, 2021
1 parent bf78279 commit 607f376
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Feature: Append content to file
Background:
Given I use a fixture named "cli-app"

Scenario: Append to a existing file
Given a file named "features/non-existence.feature" with:
Scenario: Append to an existing file
Given a file named "features/appending.feature" with:
"""
Feature: Existence
Scenario: Existence
Expand All @@ -26,6 +26,28 @@ Feature: Append content to file
When I run `cucumber`
Then the features should all pass

Scenario: Append whole lines to a existing file
Given a file named "features/appending.feature" with:
"""
Feature: Existence
Scenario: Existence
Given a file named "foo/bar/example.txt" with:
\"\"\"
hello world
\"\"\"
When I append the following lines to "foo/bar/example.txt":
\"\"\"
this was appended
\"\"\"
Then the file named "foo/bar/example.txt" should contain:
\"\"\"
hello world
this was appended
\"\"\"
"""
When I run `cucumber`
Then the features should all pass

Scenario: Append to a non-existing file
Given a file named "features/non-existence.feature" with:
"""
Expand Down
18 changes: 18 additions & 0 deletions lib/aruba/api/filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,24 @@ def append_to_file(file_name, file_content)
File.open(file_name, 'a') { |f| f << file_content }
end

# Append lines to a (text) file. This will make sure a newline is present
# between the old content and the new.
#
# @param [String] file_name
# The name of the file to be used
#
# @param [String] file_content
# The lines which should be appended to file
def append_lines_to_file(file_name, file_content)
file_name = expand_path(file_name)

last = File.open(file_name) { |f| f.seek(-3, IO::SEEK_END); f.read }
File.open(file_name, 'a') do |f|
f << "\n" unless last.end_with? "\n"
f << file_content
end
end

# Create a directory in current directory
#
# @param [String] directory_name
Expand Down
4 changes: 4 additions & 0 deletions lib/aruba/cucumber/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
append_to_file(file_name, file_content)
end

When(/^I append the following lines to "([^"]*)":$/) do |file_name, file_content|
append_lines_to_file(file_name, file_content)
end

When(/^I append to "([^"]*)" with "([^"]*)"$/) do |file_name, file_content|
append_to_file(file_name, file_content)
end
Expand Down
17 changes: 17 additions & 0 deletions spec/aruba/api/filesystem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
RSpec.describe Aruba::Api::Filesystem do
include_context 'uses aruba API'

describe '#append_lines_to_file' do
let(:path) { @file_path }
let(:name) { @file_name }

it "inserts a newline if existing file does not end in one" do
Aruba.platform.write_file(path, "foo\nbar")
append_lines_to_file(name, "baz")
expect(File.read(path)).to eq "foo\nbar\nbaz"
end

it "does not insert a newline if the existing file ends in one" do
Aruba.platform.write_file(path, "foo\nbar\n")
append_lines_to_file(name, "baz")
expect(File.read(path)).to eq "foo\nbar\nbaz"
end
end

describe '#all_paths' do
let(:name) { @file_name }
let(:path) { @file_path }
Expand Down

0 comments on commit 607f376

Please sign in to comment.