From 607f37691564f34278046d9e5815171d663a4923 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sun, 24 Jan 2021 11:57:39 +0100 Subject: [PATCH] Add step and API to add whole lines to a file 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. --- .../steps/filesystem/append_to_file.feature | 26 +++++++++++++++++-- lib/aruba/api/filesystem.rb | 18 +++++++++++++ lib/aruba/cucumber/file.rb | 4 +++ spec/aruba/api/filesystem_spec.rb | 17 ++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/features/03_testing_frameworks/cucumber/steps/filesystem/append_to_file.feature b/features/03_testing_frameworks/cucumber/steps/filesystem/append_to_file.feature index 65d9b8600..1fb70d0eb 100644 --- a/features/03_testing_frameworks/cucumber/steps/filesystem/append_to_file.feature +++ b/features/03_testing_frameworks/cucumber/steps/filesystem/append_to_file.feature @@ -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 @@ -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: """ diff --git a/lib/aruba/api/filesystem.rb b/lib/aruba/api/filesystem.rb index 8e8915d7e..c3b8052ef 100644 --- a/lib/aruba/api/filesystem.rb +++ b/lib/aruba/api/filesystem.rb @@ -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 diff --git a/lib/aruba/cucumber/file.rb b/lib/aruba/cucumber/file.rb index cabbb702a..f30d0f899 100644 --- a/lib/aruba/cucumber/file.rb +++ b/lib/aruba/cucumber/file.rb @@ -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 diff --git a/spec/aruba/api/filesystem_spec.rb b/spec/aruba/api/filesystem_spec.rb index 5a9e0e4b9..3bf3dafe7 100644 --- a/spec/aruba/api/filesystem_spec.rb +++ b/spec/aruba/api/filesystem_spec.rb @@ -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 }