From 0939af9a76a806f9b5b3e7d6ccd09c4fed6aa89b Mon Sep 17 00:00:00 2001 From: sophia Date: Thu, 19 Jan 2023 16:05:02 -0800 Subject: [PATCH] Process rsync exclude regex less This change will prepend "^" and append "/" if a start anchor is detected in the regex string. This allows users to specify relative paths to exclude. It also removes replacing occurences of "*" with "[^/]*". "*" already expresses itself accurately. --- plugins/synced_folders/rsync/helper.rb | 9 +++------ .../plugins/synced_folders/rsync/helper_test.rb | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/plugins/synced_folders/rsync/helper.rb b/plugins/synced_folders/rsync/helper.rb index c99a9dff2..6ee1dea92 100644 --- a/plugins/synced_folders/rsync/helper.rb +++ b/plugins/synced_folders/rsync/helper.rb @@ -27,18 +27,15 @@ module VagrantPlugins if exclude.start_with?("/") start_anchor = true - exclude = exclude[1..-1] end - exclude = "#{exclude}/" if !exclude.end_with?("/") - exclude = "^#{exclude}" - exclude += ".*" if !start_anchor + exclude = "#{exclude}/" if !exclude.end_with?("/") if start_anchor + exclude = "^#{exclude}" if start_anchor + exclude += ".*" if (!start_anchor && !exclude.end_with?("*")) # This is not an ideal solution, but it's a start. We can improve and # keep unit tests passing in the future. exclude = exclude.gsub("**", "|||GLOBAL|||") - exclude = exclude.gsub("*", "|||PATH|||") - exclude = exclude.gsub("|||PATH|||", "[^/]*") exclude = exclude.gsub("|||GLOBAL|||", ".*") Regexp.new(exclude) diff --git a/test/unit/plugins/synced_folders/rsync/helper_test.rb b/test/unit/plugins/synced_folders/rsync/helper_test.rb index 67472f384..44c3d30bd 100644 --- a/test/unit/plugins/synced_folders/rsync/helper_test.rb +++ b/test/unit/plugins/synced_folders/rsync/helper_test.rb @@ -32,23 +32,31 @@ describe VagrantPlugins::SyncedFolderRSync::RsyncHelper do let(:path) { "/foo/bar" } it "converts a directory match" do + expected_regex = /foo\/.*/ expect(described_class.exclude_to_regexp("foo/")). - to eq(/^foo\/.[^\/]*/) + to eq(/foo\/.*/) + expect(path).to match(expected_regex) end it "converts the start anchor" do + expected_regex = /^\/foo\// expect(described_class.exclude_to_regexp("/foo")). - to eq(/^foo\//) + to eq(expected_regex) + expect(path).to match(expected_regex) end it "converts the **" do + expected_regex = /fo.*o.*/ expect(described_class.exclude_to_regexp("fo**o")). - to eq(/^fo.*o\/.[^\/]*/) + to eq(expected_regex) + expect(path).to match(expected_regex) end it "converts the *" do + expected_regex = /fo*o.*/ expect(described_class.exclude_to_regexp("fo*o")). - to eq(/^fo[^\/]*o\/.[^\/]*/) + to eq(expected_regex) + expect(path).to match(expected_regex) end end