diff --git a/lib/spm_version_updates/git.rb b/lib/spm_version_updates/git.rb index b4b8255..16962d9 100644 --- a/lib/spm_version_updates/git.rb +++ b/lib/spm_version_updates/git.rb @@ -13,7 +13,6 @@ def self.trim_repo_url(repo_url) # @return [String] def self.repo_name(repo_url) match = repo_url.match(%r{([\w-]+/[\w-]+)(.git)?$}) - if match match[1] || match[0] else @@ -36,8 +35,7 @@ def self.version_tags(repo_url) nil end } - versions.sort! - versions.reverse! + versions.sort!.reverse! versions end diff --git a/lib/spm_version_updates/plugin.rb b/lib/spm_version_updates/plugin.rb index bbdac80..6df6ff9 100644 --- a/lib/spm_version_updates/plugin.rb +++ b/lib/spm_version_updates/plugin.rb @@ -55,9 +55,7 @@ def check_for_updates(xcodeproj_path) end if kind == "branch" - branch = requirement["branch"] - last_commit = Git.branch_last_commit(repository_url, branch) - warn("Newer commit available for #{name} (#{branch}): #{last_commit}") unless last_commit == resolved_version + warn_for_branch(requirement["branch"], name, repository_url, resolved_version) next end @@ -80,6 +78,16 @@ def check_for_updates(xcodeproj_path) private + # Warns if the branch has a newer commit than the resolved version. + # @param branch [String] the branch name + # @param name [String] the dependency name + # @param repository_url [String] the Git repository URL + # @param resolved_version [String] the currently resolved version of the branch + def warn_for_branch(branch, name, repository_url, resolved_version) + last_commit = Git.branch_last_commit(repository_url, branch) + warn("Newer commit available for #{name} (#{branch}): #{last_commit}") unless last_commit == resolved_version + end + def warn_for_new_versions_exact(available_versions, name, resolved_version) newest_version = available_versions.find { |version| report_pre_releases ? true : version.pre.nil? @@ -92,7 +100,12 @@ def warn_for_new_versions_exact(available_versions, name, resolved_version) end def warn_for_new_versions_range(available_versions, name, requirement, resolved_version) - max_version = Semantic::Version.new(requirement["maximumVersion"]) + begin + max_version = Semantic::Version.new(requirement["maximumVersion"]) + rescue ArgumentError => e + $stderr.puts("Unable to extract semver from #{requirement} for #{name} (#{e})") + return + end if available_versions.first < max_version warn("Newer version of #{name}: #{available_versions.first}") else @@ -109,7 +122,12 @@ def warn_for_new_versions_range(available_versions, name, requirement, resolved_ end def warn_for_new_versions(major_or_minor, available_versions, name, resolved_version_string) - resolved_version = Semantic::Version.new(resolved_version_string) + begin + resolved_version = Semantic::Version.new(resolved_version_string) + rescue ArgumentError => e + $stderr.puts("Unable to extract semver from #{resolved_version_string} for #{name} (#{e})") + return + end newest_meeting_reqs = available_versions.find { |version| (version.send(major_or_minor) == resolved_version.send(major_or_minor)) && (report_pre_releases ? true : version.pre.nil?) } diff --git a/spec/spm_version_updates_spec.rb b/spec/spm_version_updates_spec.rb index f42298b..b071d21 100644 --- a/spec/spm_version_updates_spec.rb +++ b/spec/spm_version_updates_spec.rb @@ -182,13 +182,26 @@ module Danger expect(@dangerfile.status_report[:warnings]).to eq([]) end + it "Prints to stderr when resolved version is unexpectedly null" do + expect { + @my_plugin.check_for_updates("#{File.dirname(__FILE__)}/support/fixtures/PackageV1Commit.xcodeproj") + }.to output( + %r{Unable to extract semver from 12f19662426d0434d6c330c6974d53e2eb10ecd9 for AliSoftware/OHHTTPStubs.*} + ).to_stderr + end + + it "Does not fail when resolved version is unexpectedly null" do + @my_plugin.check_for_updates("#{File.dirname(__FILE__)}/support/fixtures/PackageV1Commit.xcodeproj") + expect(@dangerfile.status_report[:warnings]).to eq([]) + end + it "Does not crash or warn when resolved version is missing from xcodeproj" do @my_plugin.check_for_updates("#{File.dirname(__FILE__)}/support/fixtures/NoResolvedVersion.xcodeproj") expect(@dangerfile.status_report[:warnings]).to eq([]) end - it "Does print to stderr when resolved version is missing from xcodeproj" do + it "Prints to stderr when resolved version is missing from xcodeproj" do expect { @my_plugin.check_for_updates("#{File.dirname(__FILE__)}/support/fixtures/NoResolvedVersion.xcodeproj") }.to output( @@ -290,6 +303,14 @@ module Danger ) end + it "Extracts repo name from URL" do + expect(Git.repo_name("https://github.com/hbmartin/danger-spm_version_updates")).to eq("hbmartin/danger-spm_version_updates") + end + + it "Returns repo name from param when not URL" do + expect(Git.repo_name("hbmartin/danger-spm_version_updates")).to eq("hbmartin/danger-spm_version_updates") + end + it "Reports new versions for version=1 Package.resolved" do allow(Git).to receive(:version_tags) .and_return [ diff --git a/spec/support/fixtures/PackageV1Commit.xcodeproj/project.pbxproj b/spec/support/fixtures/PackageV1Commit.xcodeproj/project.pbxproj new file mode 100644 index 0000000..334deb5 --- /dev/null +++ b/spec/support/fixtures/PackageV1Commit.xcodeproj/project.pbxproj @@ -0,0 +1,80 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 54; + objects = { +/* Begin PBXGroup section */ + F1465EF423AA94BF0055F7C3 = { + isa = PBXGroup; + children = ( + F1465EFF23AA94BF0055F7C3 /* Demo */, + ); + sourceTree = ""; + }; + F1465EFF23AA94BF0055F7C3 /* Demo */ = { + isa = PBXGroup; + children = (); + path = Demo; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXProject section */ + F1465EF523AA94BF0055F7C3 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = YES; + LastSwiftUpdateCheck = 1420; + LastUpgradeCheck = 1420; + TargetAttributes = { + F1465EFC23AA94BF0055F7C3 = { + CreatedOnToolsVersion = 11.2.1; + }; + }; + }; + buildConfigurationList = F1465EF823AA94BF0055F7C3 /* Build configuration list for PBXProject "Demo" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + es, + "zh-Hans", + fr, + ); + mainGroup = F1465EF423AA94BF0055F7C3; + packageReferences = ( + 11BBD37629C1571400F7A968 /* XCRemoteSwiftPackageReference "OHHTTPStubs" */, + ); + productRefGroup = F1465EFE23AA94BF0055F7C3 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = (); + }; +/* End PBXProject section */ + +/* Begin XCConfigurationList section */ + F1465EF823AA94BF0055F7C3 /* Build configuration list for PBXNativeTarget "Demo" */ = { + isa = XCConfigurationList; + buildConfigurations = (); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + +/* Begin XCRemoteSwiftPackageReference section */ + 11BBD37629C1571400F7A968 /* XCRemoteSwiftPackageReference "OHHTTPStubs" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/AliSoftware/OHHTTPStubs"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 9.1.0; + }; + }; +/* End XCRemoteSwiftPackageReference section */ + }; + rootObject = F1465EF523AA94BF0055F7C3 /* Project object */; +} diff --git a/spec/support/fixtures/PackageV1Commit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/spec/support/fixtures/PackageV1Commit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved new file mode 100644 index 0000000..3f765de --- /dev/null +++ b/spec/support/fixtures/PackageV1Commit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -0,0 +1,16 @@ +{ + "object": { + "pins": [ + { + "package": "OHHTTPStubs", + "repositoryURL": "https://github.com/AliSoftware/OHHTTPStubs", + "state": { + "branch": "9.1.0", + "revision": "12f19662426d0434d6c330c6974d53e2eb10ecd9", + "version": null + } + } + ] + }, + "version": 1 +}