From f2d8a3bc4cc8198cf2671b517f68fd6a490f0328 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sun, 25 Dec 2022 15:16:30 +0100 Subject: [PATCH 1/6] Remove superfluous method definitions from specs --- spec/aruba/matchers/command/have_output_size_spec.rb | 4 ---- spec/aruba/matchers/command_spec.rb | 8 -------- spec/aruba/matchers/path_spec.rb | 4 ---- 3 files changed, 16 deletions(-) diff --git a/spec/aruba/matchers/command/have_output_size_spec.rb b/spec/aruba/matchers/command/have_output_size_spec.rb index 76e5bd47a..acceecc00 100644 --- a/spec/aruba/matchers/command/have_output_size_spec.rb +++ b/spec/aruba/matchers/command/have_output_size_spec.rb @@ -3,10 +3,6 @@ RSpec.describe "Output Matchers" do include_context "uses aruba API" - def expand_path(*args) - @aruba.expand_path(*args) - end - describe "#to_have_output_size" do let(:obj) { "string" } diff --git a/spec/aruba/matchers/command_spec.rb b/spec/aruba/matchers/command_spec.rb index dd1fb937f..4083d1fab 100644 --- a/spec/aruba/matchers/command_spec.rb +++ b/spec/aruba/matchers/command_spec.rb @@ -3,14 +3,6 @@ RSpec.describe "Command Matchers" do include_context "uses aruba API" - def expand_path(*args) - @aruba.expand_path(*args) - end - - def announcer(*args) - @aruba.send(:announcer, *args) - end - describe "#to_have_exit_status" do let(:cmd) { "true" } diff --git a/spec/aruba/matchers/path_spec.rb b/spec/aruba/matchers/path_spec.rb index 560974132..d463395d0 100644 --- a/spec/aruba/matchers/path_spec.rb +++ b/spec/aruba/matchers/path_spec.rb @@ -6,10 +6,6 @@ RSpec.describe "Path Matchers" do include_context "uses aruba API" - def expand_path(*args) - @aruba.expand_path(*args) - end - describe "to_be_an_absolute_path" do let(:name) { @file_name } let(:path) { @aruba.expand_path(name) } From 9dbad6b7eaa86d4c3ffcc547d51423cda5a4cecf Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sun, 28 Nov 2021 13:47:48 +0100 Subject: [PATCH 2/6] Use RSpec annotations instead of included class to trigger RSpec hooks Since we already use :type to trigger inclusion of Aruba::Api, we can safely use it to activate the around hooks as well. This also increases safety for custom integrations, since having those work correctly depended on "aruba/rspec" never being loaded. --- lib/aruba/rspec.rb | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/lib/aruba/rspec.rb b/lib/aruba/rspec.rb index 7ab2200a2..4071a8f81 100644 --- a/lib/aruba/rspec.rb +++ b/lib/aruba/rspec.rb @@ -8,47 +8,35 @@ config.include Aruba::Api, type: :aruba # Setup environment for aruba - config.around :each do |example| - if self.class.include? Aruba::Api - setup_aruba + config.around :each, type: :aruba do |example| + setup_aruba - # Modify PATH to include project/bin - prepend_environment_variable( - "PATH", - aruba.config.command_search_paths.join(File::PATH_SEPARATOR) + File::PATH_SEPARATOR - ) + # Modify PATH to include project/bin + prepend_environment_variable( + "PATH", + aruba.config.command_search_paths.join(File::PATH_SEPARATOR) + File::PATH_SEPARATOR + ) - # Use configured home directory as HOME - set_environment_variable "HOME", aruba.config.home_directory - end + # Use configured home directory as HOME + set_environment_variable "HOME", aruba.config.home_directory example.run - next unless self.class.include? Aruba::Api - stop_all_commands end - config.around :each do |example| - if self.class.include? Aruba::Api - with_environment do - example.run - end - else + config.around :each, type: :aruba do |example| + with_environment do example.run end end - config.before :each do |example| - next unless self.class.include? Aruba::Api - + config.before :each, type: :aruba do |example| example.metadata.each { |k, v| aruba.config.set_if_option(k, v) } end # Activate announcers based on rspec metadata - config.before :each do |example| - next unless self.class.include?(Aruba::Api) - + config.before :each, type: :aruba do |example| if example.metadata[:announce_full_environment] aruba.announcer.activate(:full_environment) end From d983fb6361566c8266edc82d94e165c1dd128e19 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sun, 25 Dec 2022 15:14:31 +0100 Subject: [PATCH 3/6] Set up Aruba environment properly in 'uses aruba API' shared context --- spec/support/shared_contexts/aruba.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spec/support/shared_contexts/aruba.rb b/spec/support/shared_contexts/aruba.rb index d7889547b..3018dbf11 100644 --- a/spec/support/shared_contexts/aruba.rb +++ b/spec/support/shared_contexts/aruba.rb @@ -17,7 +17,7 @@ def create_test_files(files, data = "a") end end - before do + around do |example| klass = Class.new do include Aruba::Api @@ -36,5 +36,12 @@ def set_tag(tag_name, value) if @aruba.aruba.current_directory[0] == "/" raise "We must work with relative paths, everything else is dangerous" end + + # Use configured home directory as HOME + @aruba.set_environment_variable "HOME", aruba.config.home_directory + + @aruba.with_environment do + example.run + end end end From 40a2fad36754b0701a67024945f5aeea58491cd4 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sun, 25 Dec 2022 15:34:29 +0100 Subject: [PATCH 4/6] WIP --- spec/support/shared_contexts/aruba.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/support/shared_contexts/aruba.rb b/spec/support/shared_contexts/aruba.rb index 3018dbf11..029fcee0a 100644 --- a/spec/support/shared_contexts/aruba.rb +++ b/spec/support/shared_contexts/aruba.rb @@ -8,6 +8,7 @@ def random_string(options = {}) def create_test_files(files, data = "a") Array(files).each do |s| + # TODO: Only allow absolute paths here next if s.to_s[0] == "%" local_path = @aruba.expand_path(s) From 7b5c8ee595cdd619e613c3e54d234dfa13fec91a Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Mon, 1 Apr 2024 16:55:33 +0200 Subject: [PATCH 5/6] In shared context, set up the working directory before using #expand_path --- spec/support/shared_contexts/aruba.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/support/shared_contexts/aruba.rb b/spec/support/shared_contexts/aruba.rb index 029fcee0a..21f1c6655 100644 --- a/spec/support/shared_contexts/aruba.rb +++ b/spec/support/shared_contexts/aruba.rb @@ -1,6 +1,7 @@ require "fileutils" require "securerandom" +# Provide Aruba API via an instance variable, instead of including it into the RSpec class. RSpec.shared_context "uses aruba API" do def random_string(options = {}) options[:prefix].to_s + SecureRandom.hex + options[:suffix].to_s @@ -28,11 +29,11 @@ def set_tag(tag_name, value) end @aruba = klass.new + @aruba.setup_aruba @file_name = "test.txt" @file_size = 256 @file_path = @aruba.expand_path(@file_name) - @aruba.setup_aruba if @aruba.aruba.current_directory[0] == "/" raise "We must work with relative paths, everything else is dangerous" From c8a91c694944bbac2f9df44186d0858409570d96 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Mon, 1 Apr 2024 17:23:21 +0200 Subject: [PATCH 6/6] Add some notes. The setup in our specs is a bit weird In particular, if we use the shared context 'uses aruba API', we have an object @aruba that includes Aruba::Api, and all the methods available on that are also available on each spec example. The results can be different however, since each has its own config instance. --- lib/aruba/api/core.rb | 1 + lib/aruba/platforms/unix_platform.rb | 1 + spec/support/configs/rspec.rb | 2 ++ 3 files changed, 4 insertions(+) diff --git a/lib/aruba/api/core.rb b/lib/aruba/api/core.rb index 43f22d3b4..46cfb6686 100644 --- a/lib/aruba/api/core.rb +++ b/lib/aruba/api/core.rb @@ -175,6 +175,7 @@ def expand_path(file_name, dir_string = nil) path elsif prefix == "~" + # FIXME: Can we force use of config.home_directory path = with_environment do File.expand_path(file_name) end diff --git a/lib/aruba/platforms/unix_platform.rb b/lib/aruba/platforms/unix_platform.rb index 11881c802..d7efe3c8e 100644 --- a/lib/aruba/platforms/unix_platform.rb +++ b/lib/aruba/platforms/unix_platform.rb @@ -99,6 +99,7 @@ def current_ruby ::File.join(RbConfig::CONFIG["bindir"], RbConfig::CONFIG["ruby_install_name"]) end + # FIXME: Why is this part of platform!? Just use Ruby or just list all files to require. def require_matching_files(pattern, base) ::Dir.glob(::File.expand_path(pattern, base)).each { |f| require_relative f } end diff --git a/spec/support/configs/rspec.rb b/spec/support/configs/rspec.rb index 8014aa1c4..ff7e210f8 100644 --- a/spec/support/configs/rspec.rb +++ b/spec/support/configs/rspec.rb @@ -13,6 +13,8 @@ c.syntax = :expect end + # FIXME: Right, so we always include Aruba::Api, and all the special matchers + # only work when that's done (and also are not loaded unless that's done?) config.include Aruba::Api config.before { setup_aruba } end