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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
103 changes: 44 additions & 59 deletions spec/aruba/api/filesystem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@
RSpec.describe Aruba::Api::Filesystem do
include_context "uses aruba API"

describe "#all_paths" do
let(:name) { @file_name }
let(:path) { @file_path }
let(:name) { @file_name }
let(:path) { @file_path }
let(:dir_name) { "test.d" }
let(:dir_path) { @aruba.expand_path(dir_name) }

describe "#append_lines_to_file" do
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
context "when file exists" do
before do
Aruba.platform.write_file(path, "")
Expand All @@ -33,9 +49,6 @@
end

describe "#all_files" do
let(:name) { @file_name }
let(:path) { @file_path }

context "when file exists" do
before do
Aruba.platform.write_file(path, "")
Expand All @@ -61,9 +74,6 @@
end

describe "#all_directories" do
let(:name) { @file_name }
let(:path) { @file_path }

context "when file exists" do
before do
Aruba.platform.write_file(path, "")
Expand All @@ -89,8 +99,6 @@
end

describe "#file_size" do
let(:name) { @file_name }
let(:path) { @file_path }
let(:size) { file_size(name) }

context "when file exists" do
Expand All @@ -109,8 +117,6 @@
end

describe "#touch" do
let(:name) { @file_name }
let(:path) { @file_path }
let(:options) { {} }

before do
Expand Down Expand Up @@ -181,9 +187,6 @@
end

describe "#absolute?" do
let(:name) { @file_name }
let(:path) { File.expand_path(File.join(@aruba.aruba.current_directory, name)) }

context "when is absolute path" do
it { expect(@aruba).to be_absolute(path) }
end
Expand All @@ -194,9 +197,6 @@
end

describe "#relative?" do
let(:name) { @file_name }
let(:path) { File.expand_path(File.join(@aruba.aruba.current_directory, name)) }

context "when given an absolute path" do
it { expect(@aruba).not_to be_relative(path) }
end
Expand All @@ -208,9 +208,6 @@

describe "#exist?" do
context "when given a file" do
let(:name) { @file_name }
let(:path) { @file_path }

context "when it exists" do
before do
Aruba.platform.write_file(path, "")
Expand All @@ -225,28 +222,22 @@
end

context "when given a directory" do
let(:name) { "test.d" }
let(:path) { File.join(@aruba.aruba.current_directory, name) }

context "when it exists" do
before do
Aruba.platform.mkdir(path)
Aruba.platform.mkdir(dir_path)
end

it { expect(@aruba).to be_exist(name) }
it { expect(@aruba).to be_exist(dir_name) }
end

context "when it does not exist" do
it { expect(@aruba).not_to be_exist(name) }
it { expect(@aruba).not_to be_exist(dir_name) }
end
end
end

describe "#file?" do
context "when given a file" do
let(:name) { @file_name }
let(:path) { @file_path }

context "when it exists" do
before do
Aruba.platform.write_file(path, "")
Expand Down Expand Up @@ -280,9 +271,6 @@

describe "#directory?" do
context "when given a file" do
let(:name) { @file_name }
let(:path) { @file_path }

context "when it exists" do
before do
Aruba.platform.write_file(path, "")
Expand All @@ -297,19 +285,16 @@
end

context "when given a directory" do
let(:name) { "test.d" }
let(:path) { File.join(@aruba.aruba.current_directory, name) }

context "when it exists" do
before do
Aruba.platform.mkdir(path)
Aruba.platform.mkdir(dir_path)
end

it { expect(@aruba).to be_directory(name) }
it { expect(@aruba).to be_directory(dir_name) }
end

context "when does not exist" do
it { expect(@aruba).not_to be_directory(name) }
it { expect(@aruba).not_to be_directory(dir_name) }
end
end
end
Expand Down Expand Up @@ -443,44 +428,44 @@

describe "#write_file" do
it "writes file" do
@aruba.write_file(@file_name, "")
@aruba.write_file(name, "")

expect(File.exist?(@file_path)).to eq true
expect(File.exist?(path)).to eq true
end
end

describe "#write_fixed_size_file" do
let(:file_size) { @file_size }

it "writes a fixed sized file" do
@aruba.write_fixed_size_file(@file_name, @file_size)
expect(File.exist?(@file_path)).to eq true
expect(File.size(@file_path)).to eq @file_size
@aruba.write_fixed_size_file(name, file_size)
expect(File.exist?(path)).to eq true
expect(File.size(path)).to eq file_size
end

it "works with ~ in path name" do
file_path = File.join("~", random_string)

@aruba.with_environment "HOME" => File.expand_path(aruba.current_directory) do
@aruba.write_fixed_size_file(file_path, @file_size)
@aruba.write_fixed_size_file(file_path, file_size)

expect(File.exist?(File.expand_path(file_path))).to eq true
expect(File.size(File.expand_path(file_path))).to eq @file_size
expect(File.size(File.expand_path(file_path))).to eq file_size
end
end
end

describe "#chmod" do
def actual_permissions
format("%o", File::Stat.new(file_path).mode)[-4, 4]
format("%o", File::Stat.new(path).mode)[-4, 4]
end

let(:file_name) { @file_name }
let(:file_path) { @file_path }
let(:permissions) { "0644" }

before do
@aruba.set_environment_variable "HOME", File.expand_path(@aruba.aruba.current_directory)
File.open(file_path, "w") { |f| f << "" }
@aruba.chmod(permissions, file_name)
File.open(path, "w") { |f| f << "" }
@aruba.chmod(permissions, name)
end

context "when file exists" do
Expand All @@ -495,9 +480,9 @@ def actual_permissions
end

context "and path has ~ in it" do
let(:path) { random_string }
let(:file_name) { File.join("~", path) }
let(:file_path) { File.join(@aruba.aruba.current_directory, path) }
let(:basename) { random_string }
let(:name) { File.join("~", basename) }
let(:path) { File.join(@aruba.aruba.current_directory, basename) }

it { expect(actual_permissions).to eq("0644") }
end
Expand All @@ -506,11 +491,11 @@ def actual_permissions

describe "#with_file_content" do
before do
@aruba.write_file(@file_name, "foo bar baz")
@aruba.write_file(name, "foo bar baz")
end

it "checks the given file's full content against the expectations in the passed block" do
@aruba.with_file_content @file_name do |full_content|
@aruba.with_file_content name do |full_content|
expect(full_content).to eq "foo bar baz"
end
end
Expand All @@ -530,7 +515,7 @@ def actual_permissions
context "checking the file's content against the expectations in the block" do
it "is successful when the inner expectations match" do
expect do
@aruba.with_file_content @file_name do |full_content|
@aruba.with_file_content name do |full_content|
expect(full_content).to match(/foo/)
expect(full_content).not_to match(/zoo/)
end
Expand All @@ -539,7 +524,7 @@ def actual_permissions

it "raises ExpectationNotMetError when the inner expectations don't match" do
expect do
@aruba.with_file_content @file_name do |full_content|
@aruba.with_file_content name do |full_content|
expect(full_content).to match(/zoo/)
expect(full_content).not_to match(/foo/)
end
Expand Down