Skip to content

Commit

Permalink
Improve the error reporting
Browse files Browse the repository at this point in the history
versionCheck checks that the declared version number (e.g. 1.2.3) conforms to the declared compatibility guarantees (e.g. Compatibility.BinaryAndSourceCompatible).

Previously, when versionCheck failed, it only reported that the declared version was invalid. Now, it shows both the declared version and the compatibility intention. It also mentions the possibility to restrict the compatibility guarantees to fix the failure.
  • Loading branch information
julienrf committed Jul 18, 2023
1 parent f0ae4dc commit 8b81e8a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,14 @@ object Compatibility {
* Validates that the given new `version` matches the claimed `compatibility` level.
* @return Some validation error, or None if the version is valid.
*/
def isValidVersion(compatibility: Compatibility, version: String): Boolean = {
val versionNumber = VersionNumber(version)
def isValidVersion(compatibility: Compatibility, version: String): Boolean =
isValidVersion(compatibility, VersionNumber(version))

/**
* Validates that the given new `version` matches the claimed `compatibility` level.
* @return Some validation error, or None if the version is valid.
*/
private[sbtversionpolicy] def isValidVersion(compatibility: Compatibility, versionNumber: VersionNumber): Boolean = {
val major = versionNumber._1
val minor = versionNumber._2
val patch = versionNumber._3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,24 @@ object SbtVersionPolicySettings {
versionPolicyIntention.?.value
.getOrElse(throw new MessageOnlyException("Please set the key versionPolicyIntention to declare the compatibility guarantees of this release"))
val versionValue = version.value
val versionNumber = VersionNumber(versionValue)
val s = streams.value
val moduleName = projectID.value.name

if (Compatibility.isValidVersion(intention, versionValue)) {
if (Compatibility.isValidVersion(intention, versionNumber)) {
s.log.info(s"Module ${moduleName} has a valid version number: $versionValue (versionPolicyIntention is set to 'Compatibility.${intention}')")
} else {
val detail =
if (intention == Compatibility.None) "You must increment the major version number (or the minor version number, if major version is 0) to publish a binary incompatible release."
else "You must increment the minor version number to publish a source incompatible release."
throw new MessageOnlyException(s"Module ${moduleName} has an invalid version number: $versionValue. $detail")
if (intention == Compatibility.None) {
val matchingIntention =
if (versionNumber._3.contains(0)) Compatibility.BinaryCompatible else Compatibility.BinaryAndSourceCompatible
"If you want to publish a binary incompatible release, you must increment the major version number (or the minor version number, if the major version is 0). " +
s"Otherwise, to release version ${versionValue}, you need to restrict the versionPolicyIntention to ${matchingIntention} and run versionPolicyCheck again."
} else {
"If you want to publish a source incompatible release, you must increment the minor version number. " +
s"Otherwise, to release version ${versionValue}, you need to restrict the versionPolicyIntention to ${Compatibility.BinaryAndSourceCompatible} and run versionPolicyCheck again."
}
throw new MessageOnlyException(s"Module ${moduleName} has a declared version number ${versionValue} that does not conform to its declared versionPolicyIntention of ${intention}. $detail")
}
}).value,
versionPolicyCheck := Def.ifS((versionPolicyCheck / skip).toTask)(Def.task {
Expand Down

0 comments on commit 8b81e8a

Please sign in to comment.