Skip to content

Commit

Permalink
Wrote integration-style test for ApplyAlg
Browse files Browse the repository at this point in the history
  • Loading branch information
Devon Stewart committed Mar 23, 2021
1 parent d127cae commit 199fe0a
Showing 1 changed file with 169 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package org.scalasteward.core.nurture

import munit.ScalaCheckSuite
import org.scalasteward.core.TestInstances._
import org.scalasteward.core.data.{ProcessResult, RepoData, Update, UpdateData}

import better.files.File
import cats.Applicative
import cats.effect.Blocker
import cats.effect.Resource
import cats.effect.{ConcurrentEffect, IO}
import cats.effect.concurrent.Ref
import org.http4s.HttpApp
import org.http4s.client.Client
import org.scalasteward.core.TestSyntax._
import org.scalasteward.core.application.Config
import org.scalasteward.core.application.Context
import org.scalasteward.core.git.FileGitAlg
import org.scalasteward.core.git.FileGitAlgTest.{master, Supplement}
import org.scalasteward.core.git.GenGitAlg
import org.scalasteward.core.git.{Branch, Commit, Sha1}
import org.scalasteward.core.io.{FileAlg, ProcessAlg, WorkspaceAlg}
import org.scalasteward.core.mock.MockContext
import org.scalasteward.core.mock.MockContext.{config, mockRoot}
import org.scalasteward.core.repocache._
import org.scalasteward.core.repoconfig.RepoConfig
import org.scalasteward.core.util.Nel
import org.scalasteward.core.vcs.data.Repo

class ApplyAlgTest extends ScalaCheckSuite {
implicit private val fileAlg: FileAlg[IO] = FileAlg.create[IO]
implicit private val workspaceAlg: WorkspaceAlg[IO] = WorkspaceAlg.create[IO](config)

def step0(implicit CE: ConcurrentEffect[IO]): Resource[IO, (ProcessAlg[IO], Context[IO])] = for {
blocker <- Blocker[IO]
config = Config.from(MockContext.args)
implicit0(client: Client[IO]) = Client.fromHttpApp[IO](HttpApp.notFound)
implicit0(fileAlg: FileAlg[IO]) = FileAlg.create[IO]
implicit0(processAlg: ProcessAlg[IO]) = ProcessAlg.create[IO](blocker, config.processCfg)
implicit0(workspaceAlg: WorkspaceAlg[IO]) = WorkspaceAlg.create[IO](config)
context <- Resource.liftF(Context.step1[IO](config))
} yield (processAlg, context)

def setupRepo(repo: Repo, identicalBranch: (Branch, Update.Single)): IO[Unit] =
step0.use { case (implicit0(processAlg: ProcessAlg[IO]), context) =>
import context.gitAlg
implicit val ioGitAlg: GenGitAlg[IO, File] =
new FileGitAlg[IO](config.gitCfg).contramapRepoF(Applicative[IO].pure)
val supplement = new Supplement[IO]
val repoDir = mockRoot / "workspace" / "repos" / repo.owner / repo.repo
for {
_ <- supplement.createRepo(repoDir)
_ <- fileAlg.writeFile(
repoDir / "build.sbt",
"""libraryDependency += "foo" % "bar" % "1.2.3" """
)
_ <- supplement.git("add", "build.sbt")(repoDir)
_ <- gitAlg.commitAll(repo, "Initial commit")
// Create another simulated curated update branch with
_ <- gitAlg.createBranch(repo, identicalBranch._1)
_ <- fileAlg.writeFile(
repoDir / "build.sbt",
s"""libraryDependency += "foo" % "bar" % "${identicalBranch._2.newerVersions.head}" """
)
_ <- supplement.git("add", "build.sbt")(repoDir)
_ <- gitAlg.commitAll(repo, "Update bar to 1.2.4")
_ <- gitAlg.checkoutBranch(repo, master)
} yield ()
}

test("Ensure unique patchesets are pushed") {
val firstChangeset =
(Branch("update/foo-1.2.4"), Update.Single("foo" % "bar" % "1.2.3", Nel.one("1.2.4")))
val duplicateChangeset = (
Branch("update/foo-duplicate-1.2.4"),
Update.Single("foo" % "bar" % "1.2.3", Nel.one("1.2.4"))
)
val res = ({
def pushCommits(
seenBranchesRef: Ref[IO, List[Branch]]
): (UpdateData, List[Commit]) => IO[ProcessResult] = { (data, _) =>
for {
_ <- seenBranchesRef.update(data.updateBranch :: _)
} yield ProcessResult.Updated
}

val createPullRequest: UpdateData => IO[ProcessResult] = _ => IO.pure(ProcessResult.Updated)

val repo = Repo("myorg", "myrepo")
val fork = Repo("myfork", "myrepo")
step0.use { case (implicit0(processAlg: ProcessAlg[IO]), context) =>
for {
_ <- setupRepo(repo, firstChangeset)
seenBranchesRef <- Ref[IO].of(List.empty[Branch])
sha1 <- IO.fromEither(Sha1.from("adc83b19e793491b1c6ea0fd8b46cd9f32e592fc"))
firstData = UpdateData(
RepoData(
repo,
RepoCache(sha1, List.empty, Option.empty),
RepoConfig()
),
fork,
firstChangeset._2,
master,
sha1,
Branch("bump")
)
secondData = firstData.copy(
updateBranch = duplicateChangeset._1,
update = duplicateChangeset._2
)
seenBranches <- seenBranchesRef.getAndUpdate(identity _)
res1 <- context.applyAlg.applyNewUpdate(
firstData,
seenBranches,
pushCommits(seenBranchesRef),
createPullRequest
)
seenBranches <- seenBranchesRef.getAndUpdate(identity _)
res2 <- context.applyAlg.applyNewUpdate(
secondData,
seenBranches,
pushCommits(seenBranchesRef),
createPullRequest
)
} yield (res1, res2)
}
}).unsafeRunSync()

assertEquals(res, (ProcessResult.Updated, ProcessResult.Ignored))
}

test("Ensure non-unique patchesets are not pushed") {
val identicalBranch =
(Branch("update/foo-1.2.4"), Update.Single("foo" % "bar" % "1.2.3", Nel.one("1.2.4")))
val res = ({
val pushCommits: (UpdateData, List[Commit]) => IO[ProcessResult] =
(_, _) => IO.pure(ProcessResult.Updated)

val createPullRequest: UpdateData => IO[ProcessResult] = _ => IO.pure(ProcessResult.Updated)

val repo = Repo("myorg", "myrepo")
val fork = Repo("myfork", "myrepo")
step0.use { case (implicit0(processAlg: ProcessAlg[IO]), context) =>
for {
_ <- setupRepo(repo, identicalBranch)
seenBranchesRef <- Ref[IO].of(List(identicalBranch._1))
sha1 <- IO.fromEither(Sha1.from("adc83b19e793491b1c6ea0fd8b46cd9f32e592fc"))
data = UpdateData(
RepoData(
repo,
RepoCache(sha1, List.empty, Option.empty),
RepoConfig()
),
fork,
identicalBranch._2,
master,
sha1,
Branch("bump")
)
seenBranches <- seenBranchesRef.getAndUpdate(identity _)
res <- context.applyAlg.applyNewUpdate(data, seenBranches, pushCommits, createPullRequest)
} yield res
}
}).unsafeRunSync()

assertEquals(res, ProcessResult.Ignored)
}
}

0 comments on commit 199fe0a

Please sign in to comment.