Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Jan 14, 2021
1 parent 754811a commit 2977397
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 159 deletions.
156 changes: 46 additions & 110 deletions dynver/src/test/scala/sbtdynver/DynVerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,126 +5,62 @@ import org.scalacheck._, Prop._
import RepoStates._

object VersionSpec extends Properties("VersionSpec") {
property("not a git repo") = notAGitRepo().version() ?= "HEAD+20160917-0000"
property("no commits") = noCommits().version() ?= "HEAD+20160917-0000"
property("on commit, w/o local changes") = onCommit().version() ?= "0.0.0+3-1234abcd"
property("on commit with local changes") = onCommitDirty().version() ?= "0.0.0+3-1234abcd+20160917-0000"
property("on tag v1.0.0, w/o local changes") = onTag().version() ?= "1.0.0"
property("on tag v1.0.0 with local changes") = onTagDirty().version() ?= "1.0.0+0-1234abcd+20160917-0000"
property("on tag v1.0.0 and 1 commit, w/o local changes") = onTagAndCommit().version() ?= "1.0.0+1-1234abcd"
property("on tag v1.0.0 and 1 commit with local changes") = onTagAndCommitDirty().version() ?= "1.0.0+1-1234abcd+20160917-0000"
property("on tag v2") = onTag("v2").version() ?= "2" // #7, didn't match
property("not a git repo") = notAGitRepo .version ?= "HEAD+20160917-0000"
property("no commits") = noCommits .version ?= "HEAD+20160917-0000"
property("on commit, w/o local changes") = onCommit .version ?= "0.0.0+3-1234abcd"
property("on commit with local changes") = onCommit.dirty .version ?= "0.0.0+3-1234abcd+20160917-0000"
property("on tag v1.0.0, w/o local changes") = onTag .version ?= "1.0.0"
property("on tag v1.0.0 with local changes") = onTag.dirty .version ?= "1.0.0+0-1234abcd+20160917-0000"
property("on tag v1.0.0 and 1 commit, w/o local changes") = onTag.commit .version ?= "1.0.0+1-1234abcd"
property("on tag v1.0.0 and 1 commit with local changes") = onTag.commit.dirty .version ?= "1.0.0+1-1234abcd+20160917-0000"
property("on tag v2") = onTag.commit.tag("2").version ?= "2" // #7, didn't match
}

object PreviousVersionSpec extends Properties("PreviousVersionSpec") {
property("not a git repo") = notAGitRepo().previousVersion() ?= None
property("no commits") = noCommits().previousVersion() ?= None
property("on commit, w/o local changes") = onCommit().previousVersion() ?= None
property("on commit with local changes") = onCommitDirty().previousVersion() ?= None
property("on tag v1.0.0, w/o local changes") = onTag().previousVersion() ?= None
property("on tag v1.0.0 with local changes") = onTagDirty().previousVersion() ?= None
property("on tag v1.0.0 and 1 commit, w/o local changes") = onTagAndCommit().previousVersion() ?= Some("1.0.0")
property("on tag v1.0.0 and 1 commit with local changes") = onTagAndCommitDirty().previousVersion() ?= Some("1.0.0")
property("on tag v1.0.0 and 2 commits, w/o local changes") = onTagAndSecondCommit().previousVersion() ?= Some("1.0.0")
property("on tag v2.0.0, w/o local changes") = onSecondTag().previousVersion() ?= Some("1.0.0")
property("with merge commits") = {
val state = onTag()
.branch("newbranch")
.commit()
.checkout("master")
.merge("newbranch")
.tag("v2.0.0")

state.previousVersion() ?= Some("1.0.0")
}

property("with merge commits and with untagged HEAD") = {
val state = onTag()
.branch("newbranch")
.commit()
.checkout("master")
.merge("newbranch")

state.previousVersion() ?= Some("1.0.0")
}

property("multiple branches, each with their own tags") = {
val state = onTag()

state
.branch("v2")
.commit()
.tag("v2.0.0")

state
.commit()
.tag("v2.1.0")

state
.checkout("master") // checkout the v1.x branch
.commit()
.tag("v1.1.0")

state.checkout("v2")

state.previousVersion() ?= Some("2.0.0")
}

property("merge commit where both branches have tags - should use the first parent (branch that was merged into)") = {
val state = onTag()

state
.branch("v2")
.commit()
.tag("v2.0.0")

state
.commit()
.tag("v2.1.0")

state
.checkout("master") // checkout the v1.x branch
.commit()
.tag("v1.1.0")

state.merge("v2")

state.previousVersion() ?= Some("1.1.0")
}

property("contains unrelated tags in-between version tags") = {
val state = onTag().commit().tag("unrelated").commit().tag("v2.0.0")
state.previousVersion() ?= Some("1.0.0")
}
property("not a git repo") = notAGitRepo .previousVersion ?= None
property("no commits") = noCommits .previousVersion ?= None
property("on commit, w/o local changes") = onCommit .previousVersion ?= None
property("on commit with local changes") = onCommit.dirty .previousVersion ?= None
property("on tag v1.0.0, w/o local changes") = onTag .previousVersion ?= None
property("on tag v1.0.0 with local changes") = onTag.dirty .previousVersion ?= None
property("on tag v1.0.0 and 1 commit, w/o local changes") = onTag.commit .previousVersion ?= Some("1.0.0")
property("on tag v1.0.0 and 1 commit with local changes") = onTag.commit.dirty .previousVersion ?= Some("1.0.0")
property("on tag v2.0.0, w/o local changes") = onTag.commit.tag("2.0.0").previousVersion ?= Some("1.0.0")

property("w/ merge commits") = onBranch2x.checkout("master").merge("2.x") .previousVersion ?= Some("1.0.0")
property("w/ merge commits + tag") = onBranch2Tag .previousVersion ?= Some("1.0.0")
property("on multiple branches") = onMultiBranch.checkout("2.x") .previousVersion ?= Some("2.0.0")
property("on merging branch with tags") = onMultiBranch.merge("2.x") .previousVersion ?= Some("1.1.0")
property("with non-version prev tag") = onTag.commit.tag("hm").commit.tag("v2.0.0").previousVersion ?= Some("1.0.0")
}

object IsSnapshotSpec extends Properties("IsSnapshotSpec") {
property("not a git repo") = notAGitRepo().isSnapshot() ?= true
property("no commits") = noCommits().isSnapshot() ?= true
property("on commit, w/o local changes") = onCommit().isSnapshot() ?= true
property("on commit with local changes") = onCommitDirty().isSnapshot() ?= true
property("on tag v1.0.0, w/o local changes") = onTag().isSnapshot() ?= false
property("on tag v1.0.0 with local changes") = onTagDirty().isSnapshot() ?= true
property("on tag v1.0.0 and 1 commit, w/o local changes") = onTagAndCommit().isSnapshot() ?= true
property("on tag v1.0.0 and 1 commit with local changes") = onTagAndCommitDirty().isSnapshot() ?= true
property("on tag v2") = onTag("v2").isSnapshot() ?= false
property("not a git repo") = notAGitRepo .isSnapshot ?= true
property("no commits") = noCommits .isSnapshot ?= true
property("on commit, w/o local changes") = onCommit .isSnapshot ?= true
property("on commit with local changes") = onCommit.dirty .isSnapshot ?= true
property("on tag v1.0.0, w/o local changes") = onTag .isSnapshot ?= false
property("on tag v1.0.0 with local changes") = onTag.dirty .isSnapshot ?= true
property("on tag v1.0.0 and 1 commit, w/o local changes") = onTag.commit .isSnapshot ?= true
property("on tag v1.0.0 and 1 commit with local changes") = onTag.commit.dirty .isSnapshot ?= true
property("on tag v2") = onTag.commit.tag("2").isSnapshot ?= false
}

object SonatypeSnapshotSpec extends Properties("SonatypeSnapshotSpec") {
property("on tag v1.0.0 with local changes") = onTagDirty().sonatypeVersion() ?= "1.0.0+0-1234abcd+20160917-0000-SNAPSHOT"
property("on tag v1.0.0 and 1 commit, w/o local changes S") = onTagAndCommit().sonatypeVersion() ?= "1.0.0+1-1234abcd-SNAPSHOT"
property("on tag v1.0.0, w/o local changes") = onTag().sonatypeVersion() ?= "1.0.0"
property("on tag v2") = onTag("v2").sonatypeVersion() ?= "2"
property("on tag v1.0.0 with local changes") = onTag.dirty .sonatypeVersion ?= "1.0.0+0-1234abcd+20160917-0000-SNAPSHOT"
property("on tag v1.0.0 and 1 commit, w/o local changes S") = onTag.commit .sonatypeVersion ?= "1.0.0+1-1234abcd-SNAPSHOT"
property("on tag v1.0.0, w/o local changes") = onTag .sonatypeVersion ?= "1.0.0"
property("on tag v2") = onTag.commit.tag("2").sonatypeVersion ?= "2"
}

object isVersionStableSpec extends Properties("IsVersionStableSpec") {
property("not a git repo") = notAGitRepo().isVersionStable() ?= false
property("no commits") = noCommits().isVersionStable() ?= false
property("on commit, w/o local changes") = onCommit().isVersionStable() ?= true
property("on commit with local changes") = onCommitDirty().isVersionStable() ?= false
property("on tag v1.0.0, w/o local changes") = onTag().isVersionStable() ?= true
property("on tag v1.0.0 with local changes") = onTagDirty().isVersionStable() ?= false
property("on tag v1.0.0 and 1 commit, w/o local changes") = onTagAndCommit().isVersionStable() ?= true
property("on tag v1.0.0 and 1 commit with local changes") = onTagAndCommitDirty().isVersionStable() ?= false
property("on tag v2") = onTag("v2").isVersionStable() ?= true
property("not a git repo") = notAGitRepo .isVersionStable ?= false
property("no commits") = noCommits .isVersionStable ?= false
property("on commit, w/o local changes") = onCommit .isVersionStable ?= true
property("on commit with local changes") = onCommit.dirty .isVersionStable ?= false
property("on tag v1.0.0, w/o local changes") = onTag .isVersionStable ?= true
property("on tag v1.0.0 with local changes") = onTag.dirty .isVersionStable ?= false
property("on tag v1.0.0 and 1 commit, w/o local changes") = onTag.commit .isVersionStable ?= true
property("on tag v1.0.0 and 1 commit with local changes") = onTag.commit.dirty .isVersionStable ?= false
property("on tag v2") = onTag.commit.tag("2").isVersionStable ?= true
}
74 changes: 34 additions & 40 deletions dynver/src/test/scala/sbtdynver/RepoStates.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,79 +13,73 @@ import org.eclipse.jgit.merge.MergeStrategy
object RepoStates extends RepoStates(tagPrefix = "v")

sealed class RepoStates(tagPrefix: String) {
def notAGitRepo() = new State()
def noCommits() = notAGitRepo().init()
def onCommit() = noCommits().commit().commit().commit()
def onCommitDirty() = onCommit().dirty()
def onTag(n: String = "1.0.0") = onCommit().tag(optPrefix(n))
def onTagDirty() = onTag().dirty()
def onTagAndCommit() = onTag().commit()
def onTagAndCommitDirty() = onTagAndCommit().dirty()
def onTagAndSecondCommit() = onTagAndCommitDirty().commit()
def onSecondTag(n: String = "2.0.0") = onTagAndSecondCommit().tag(optPrefix(n))
def notAGitRepo = new State
def noCommits = notAGitRepo.init
def onCommit = noCommits.commit.commit.commit
def onTag = onCommit.tag("1.0.0")
def onBranch2x = onTag.branch("2.x").commit
def onBranch2Tag = onBranch2x.tag("2.0.0")
def onMultiBranch = onBranch2Tag.commit.tag("2.1.0").checkout("master").commit.tag("1.1.0")

private def optPrefix(s: String) = if (s.startsWith(tagPrefix)) s else s"$tagPrefix$s"

locally {
noCommits() // & seed JGit's FS.FileStoreAttributes.attributeCache with my tmp directory's BsdFileStore
}
// seed JGit's FS.FileStoreAttributes.attributeCache with my tmp directory's BsdFileStore
locally(noCommits)

final class State() {
val dir = doto(Files.createTempDirectory(s"dynver-test-").toFile)(_.deleteOnExit())
val date = new GregorianCalendar(2016, 8, 17).getTime
val dynver = DynVer(Some(dir), DynVer.separator, tagPrefix)
val dir = doto(Files.createTempDirectory(s"dynver-test-").toFile)(_.deleteOnExit)
val sep = DynVer.separator
val date = new GregorianCalendar(2016, 8, 17).getTime
val dynver = DynVer(Some(dir), sep, tagPrefix)

var git: Git = _
var sha: String = "undefined"
var sha = "undefined"

def init() = andThis(git = Git.init().setDirectory(dir).call())
def dirty() = {
def init = andThis(git = Git.init.setDirectory(dir).call())
def dirty = {
// We randomize the content added otherwise we will get the same git hash for two separate commits
// because our commits are made at almost the same time
andThis(Files.write(dir.toPath.resolve("f.txt"), Seq(scala.util.Random.nextString(10)).asJava, CREATE, APPEND))
}
def tag(n: String) = andThis(git.tag().setName(n).call())
def tag(n: String) = andThis(git.tag.setName(optPrefix(n)).call())

def commit() = andThis {
dirty()
git.add().addFilepattern(".").call()
sha = git.commit().setMessage("1").setSign(false).call().abbreviate(8).name()
def commit = andThis {
dirty
git.add.addFilepattern(".").call()
sha = git.commit.setMessage("1").setSign(false).call().abbreviate(8).name
}

def branch(branchName: String) = andThis {
git.branchCreate().setName(branchName).call()
git.checkout().setName(branchName).call()
git.branchCreate.setName(branchName).call()
git.checkout.setName(branchName).call()
}

def checkout(branchName: String) = andThis {
git.checkout().setName(branchName).call()
sha = getCurrentHeadSHA
git.checkout.setName(branchName).call()
sha = getHeadSHA
}

def merge(branchName: String) = andThis {
git.merge()
git.merge
.include(git.getRepository.findRef(branchName))
.setCommit(false)
.setFastForward(FastForwardMode.NO_FF)
.setStrategy(MergeStrategy.OURS)
.call()
commit()
sha = getCurrentHeadSHA
commit
sha = getHeadSHA
}

def version() = dynver.version(date).replaceAllLiterally(sha, "1234abcd")
def sonatypeVersion() = dynver.sonatypeVersion(date).replaceAllLiterally(sha, "1234abcd")
def previousVersion() = dynver.previousVersion
def isSnapshot() = dynver.isSnapshot()
def isVersionStable() = dynver.isVersionStable()
def version = dynver. version(date).replaceAllLiterally(sha, "1234abcd")
def sonatypeVersion = dynver.sonatypeVersion(date).replaceAllLiterally(sha, "1234abcd")
def previousVersion = dynver.previousVersion
def isSnapshot = dynver.isSnapshot
def isVersionStable = dynver.isVersionStable

private def doalso[A, U](x: A)(xs: U*) = x
private def doto[A, U](x: A)(f: A => U) = doalso(x)(f(x))
private def andThis[U](x: U) = this

private def getCurrentHeadSHA = {
git.getRepository.findRef("HEAD").getObjectId.abbreviate(8).name()
}
private def getHeadSHA = git.getRepository.findRef("HEAD").getObjectId.abbreviate(8).name
}

}
17 changes: 8 additions & 9 deletions dynver/src/test/scala/sbtdynver/TagPatternSpec.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package sbtdynver

import org.scalacheck._, Prop._
import testkit._

object TagPatternVersionSpec extends Properties("TagPatternVersionSpec") {
val repoStates = new RepoStates(tagPrefix = "")
import repoStates._

property("not a git repo") = notAGitRepo().version() ?= "HEAD+20160917-0000"
property("no commits") = noCommits().version() ?= "HEAD+20160917-0000"
property("on commit, w/o local changes") = onCommit().version() ??= "0.0.0+3-1234abcd"
property("on commit with local changes") = onCommitDirty().version() ??= "0.0.0+3-1234abcd+20160917-0000"
property("on tag 1.0.0, w/o local changes") = onTag().version() ?= "1.0.0"
property("on tag 1.0.0 with local changes") = onTagDirty().version() ?= "1.0.0+0-1234abcd+20160917-0000"
property("on tag 1.0.0 and 1 commit, w/o local changes") = onTagAndCommit().version() ?= "1.0.0+1-1234abcd"
property("on tag 1.0.0 and 1 commit with local changes") = onTagAndCommitDirty().version() ?= "1.0.0+1-1234abcd+20160917-0000"
property("not a git repo") = notAGitRepo .version ?= "HEAD+20160917-0000"
property("no commits") = noCommits .version ?= "HEAD+20160917-0000"
property("on commit, w/o local changes") = onCommit .version ?= "0.0.0+3-1234abcd"
property("on commit with local changes") = onCommit.dirty .version ?= "0.0.0+3-1234abcd+20160917-0000"
property("on tag 1.0.0, w/o local changes") = onTag .version ?= "1.0.0"
property("on tag 1.0.0 with local changes") = onTag.dirty .version ?= "1.0.0+0-1234abcd+20160917-0000"
property("on tag 1.0.0 and 1 commit, w/o local changes") = onTag.commit .version ?= "1.0.0+1-1234abcd"
property("on tag 1.0.0 and 1 commit with local changes") = onTag.commit.dirty.version ?= "1.0.0+1-1234abcd+20160917-0000"

override def overrideParameters(p: Test.Parameters) =
p.withMinSuccessfulTests(3) // no need to run 100 times!
Expand Down

0 comments on commit 2977397

Please sign in to comment.