Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add step and API to add whole lines to a file #780

Merged
merged 4 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ AllCops:
NewCops: enable
TargetRubyVersion: 2.4

# Spec blocks can be any size
Metrics/BlockLength:
Exclude:
- '**/*.gemspec'
- 'spec/**/*'

# Use older RuboCop default
Style/PercentLiteralDelimiters:
PreferredDelimiters:
Expand Down
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config --no-offense-counts --no-auto-gen-timestamp`
# using RuboCop version 1.6.1.
# using RuboCop version 1.8.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -40,7 +40,7 @@ Metrics/AbcSize:
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
# IgnoredMethods: refine
Metrics/BlockLength:
Max: 594
Max: 68

# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Expand All @@ -56,7 +56,7 @@ Metrics/MethodLength:

# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 182
Max: 193

# Configuration parameters: IgnoredMethods.
Metrics/PerceivedComplexity:
Expand Down
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 an 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
22 changes: 22 additions & 0 deletions lib/aruba/api/filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,28 @@ 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) do |f|
f.seek(-3, IO::SEEK_END)
f.read
end

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 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pop these as top level? Seem to be used in nearly all of the examples (I scrolled down past L40)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, yes.

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