Skip to content

Commit

Permalink
Merge pull request #30 from planningcenter/km/fix/yarn-3-deploying
Browse files Browse the repository at this point in the history
fix: improve reliability of version comparisons
  • Loading branch information
kylemellander authored Oct 1, 2024
2 parents 78cbdd5 + 1a5ad3b commit 9a784b1
Show file tree
Hide file tree
Showing 6 changed files with 992 additions and 15 deletions.
3 changes: 3 additions & 0 deletions deploy/lib/deployer/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ class FailedToCreatePRError < BaseError

class AutoMergeFailure < BaseError
end

class VersionCompareFailure < BaseError
end
end
44 changes: 29 additions & 15 deletions deploy/lib/deployer/repo/version_compare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ class VersionCompare
def initialize(
package_name:,
version:,
current_version: default_current_version
current_version: nil,
yarn_lock_file_path: "yarn.lock"
)
@version = Gem::Version.new(version)
@yarn_lock_file_path = yarn_lock_file_path
@current_version = current_version
@package_name = package_name
end
Expand All @@ -19,20 +21,32 @@ def major_upgrade?

private

attr_reader :package_name, :version, :current_version

def default_current_version
@default_current_version ||=
begin
yarn_lock_file = File.read("yarn.lock")
current_version_string =
yarn_lock_file.match(
/"#{package_name}@[^"]+":\n\s\sversion "([^"]+)"/m
)[
1
]
Gem::Version.new(current_version_string)
end
attr_reader :package_name, :version, :yarn_lock_file_path

def current_version
@current_version ||= find_current_version
end

def find_current_version
match =
yarn_lock_file.match(
/"#{Regexp.escape(package_name)}@[^"]+":?\n\s+version:?\s"?([^"\n]+)/m
)
package_not_found if match.nil?

current_version_string = match[1]
Gem::Version.new(current_version_string)
end

def yarn_lock_file
@yarn_lock_file ||= File.read(yarn_lock_file_path)
rescue Errno::ENOENT
raise VersionCompareFailure, "No yarn.lock file found"
end

def package_not_found
raise VersionCompareFailure,
"Could not find #{package_name} in yarn.lock"
end
end
end
Expand Down
55 changes: 55 additions & 0 deletions deploy/spec/deployer/repo/version_compare_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
describe Deployer::Repo::VersionCompare do
describe "#major_upgrade?" do
it "works when detecting a yarn 3 file" do
version_compare =
described_class.new(
package_name: "@planningcenter/tapestry-react",
version: "4.6.0",
yarn_lock_file_path: "spec/fixtures/yarn3.lock"
)

expect(version_compare.major_upgrade?).to eq(false)
end

it "works when detecting a yarn file" do
version_compare =
described_class.new(
package_name: "@planningcenter/tapestry-react",
version: "4.6.0",
yarn_lock_file_path: "spec/fixtures/yarn.lock"
)

expect(version_compare.major_upgrade?).to eq(false)
end

it "raises error when lock file does not contain the dependency" do
version_compare =
described_class.new(
package_name: "@planningcenter/tapestry-react",
version: "4.6.0",
yarn_lock_file_path: "spec/fixtures/empty_lock.lock"
)

expect { version_compare.major_upgrade? }.to raise_error(
Deployer::VersionCompareFailure,
"[Deployer::VersionCompareFailure]: Could not find @planningcenter/tapestry-react in yarn.lock"
)
end

context "when there is no lock file" do
it "raises error" do
version_compare =
described_class.new(
package_name: "@planningcenter/tapestry-react",
version: "4.6.0",
yarn_lock_file_path: "spec/fixtures/missing.lock"
)

expect { version_compare.major_upgrade? }.to raise_error(
Deployer::VersionCompareFailure,
"[Deployer::VersionCompareFailure]: No yarn.lock file found"
)
end
end
end
end
Empty file.
Loading

0 comments on commit 9a784b1

Please sign in to comment.