Skip to content

Commit

Permalink
Pull up common let statements in spec
Browse files Browse the repository at this point in the history
  • Loading branch information
mvz committed Jan 31, 2021
1 parent ac69b3e commit adef003
Showing 1 changed file with 24 additions and 56 deletions.
80 changes: 24 additions & 56 deletions spec/aruba/api/filesystem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
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 }
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")
Expand All @@ -22,9 +24,6 @@
end

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

context "when file exists" do
before do
Aruba.platform.write_file(path, "")
Expand All @@ -50,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 @@ -78,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 @@ -106,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 @@ -126,8 +117,6 @@
end

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

before do
Expand Down Expand Up @@ -198,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 @@ -211,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 @@ -225,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 @@ -242,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 @@ -297,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 @@ -314,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 @@ -460,27 +428,29 @@

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
Expand All @@ -490,8 +460,6 @@ def actual_permissions
format("%o", File::Stat.new(file_path).mode)[-4, 4]
end

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

before do
Expand Down Expand Up @@ -523,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 @@ -547,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 @@ -556,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

0 comments on commit adef003

Please sign in to comment.