Skip to content

Commit 199fe0a

Browse files
author
Devon Stewart
committed
Wrote integration-style test for ApplyAlg
1 parent d127cae commit 199fe0a

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package org.scalasteward.core.nurture
2+
3+
import munit.ScalaCheckSuite
4+
import org.scalasteward.core.TestInstances._
5+
import org.scalasteward.core.data.{ProcessResult, RepoData, Update, UpdateData}
6+
7+
import better.files.File
8+
import cats.Applicative
9+
import cats.effect.Blocker
10+
import cats.effect.Resource
11+
import cats.effect.{ConcurrentEffect, IO}
12+
import cats.effect.concurrent.Ref
13+
import org.http4s.HttpApp
14+
import org.http4s.client.Client
15+
import org.scalasteward.core.TestSyntax._
16+
import org.scalasteward.core.application.Config
17+
import org.scalasteward.core.application.Context
18+
import org.scalasteward.core.git.FileGitAlg
19+
import org.scalasteward.core.git.FileGitAlgTest.{master, Supplement}
20+
import org.scalasteward.core.git.GenGitAlg
21+
import org.scalasteward.core.git.{Branch, Commit, Sha1}
22+
import org.scalasteward.core.io.{FileAlg, ProcessAlg, WorkspaceAlg}
23+
import org.scalasteward.core.mock.MockContext
24+
import org.scalasteward.core.mock.MockContext.{config, mockRoot}
25+
import org.scalasteward.core.repocache._
26+
import org.scalasteward.core.repoconfig.RepoConfig
27+
import org.scalasteward.core.util.Nel
28+
import org.scalasteward.core.vcs.data.Repo
29+
30+
class ApplyAlgTest extends ScalaCheckSuite {
31+
implicit private val fileAlg: FileAlg[IO] = FileAlg.create[IO]
32+
implicit private val workspaceAlg: WorkspaceAlg[IO] = WorkspaceAlg.create[IO](config)
33+
34+
def step0(implicit CE: ConcurrentEffect[IO]): Resource[IO, (ProcessAlg[IO], Context[IO])] = for {
35+
blocker <- Blocker[IO]
36+
config = Config.from(MockContext.args)
37+
implicit0(client: Client[IO]) = Client.fromHttpApp[IO](HttpApp.notFound)
38+
implicit0(fileAlg: FileAlg[IO]) = FileAlg.create[IO]
39+
implicit0(processAlg: ProcessAlg[IO]) = ProcessAlg.create[IO](blocker, config.processCfg)
40+
implicit0(workspaceAlg: WorkspaceAlg[IO]) = WorkspaceAlg.create[IO](config)
41+
context <- Resource.liftF(Context.step1[IO](config))
42+
} yield (processAlg, context)
43+
44+
def setupRepo(repo: Repo, identicalBranch: (Branch, Update.Single)): IO[Unit] =
45+
step0.use { case (implicit0(processAlg: ProcessAlg[IO]), context) =>
46+
import context.gitAlg
47+
implicit val ioGitAlg: GenGitAlg[IO, File] =
48+
new FileGitAlg[IO](config.gitCfg).contramapRepoF(Applicative[IO].pure)
49+
val supplement = new Supplement[IO]
50+
val repoDir = mockRoot / "workspace" / "repos" / repo.owner / repo.repo
51+
for {
52+
_ <- supplement.createRepo(repoDir)
53+
_ <- fileAlg.writeFile(
54+
repoDir / "build.sbt",
55+
"""libraryDependency += "foo" % "bar" % "1.2.3" """
56+
)
57+
_ <- supplement.git("add", "build.sbt")(repoDir)
58+
_ <- gitAlg.commitAll(repo, "Initial commit")
59+
// Create another simulated curated update branch with
60+
_ <- gitAlg.createBranch(repo, identicalBranch._1)
61+
_ <- fileAlg.writeFile(
62+
repoDir / "build.sbt",
63+
s"""libraryDependency += "foo" % "bar" % "${identicalBranch._2.newerVersions.head}" """
64+
)
65+
_ <- supplement.git("add", "build.sbt")(repoDir)
66+
_ <- gitAlg.commitAll(repo, "Update bar to 1.2.4")
67+
_ <- gitAlg.checkoutBranch(repo, master)
68+
} yield ()
69+
}
70+
71+
test("Ensure unique patchesets are pushed") {
72+
val firstChangeset =
73+
(Branch("update/foo-1.2.4"), Update.Single("foo" % "bar" % "1.2.3", Nel.one("1.2.4")))
74+
val duplicateChangeset = (
75+
Branch("update/foo-duplicate-1.2.4"),
76+
Update.Single("foo" % "bar" % "1.2.3", Nel.one("1.2.4"))
77+
)
78+
val res = ({
79+
def pushCommits(
80+
seenBranchesRef: Ref[IO, List[Branch]]
81+
): (UpdateData, List[Commit]) => IO[ProcessResult] = { (data, _) =>
82+
for {
83+
_ <- seenBranchesRef.update(data.updateBranch :: _)
84+
} yield ProcessResult.Updated
85+
}
86+
87+
val createPullRequest: UpdateData => IO[ProcessResult] = _ => IO.pure(ProcessResult.Updated)
88+
89+
val repo = Repo("myorg", "myrepo")
90+
val fork = Repo("myfork", "myrepo")
91+
step0.use { case (implicit0(processAlg: ProcessAlg[IO]), context) =>
92+
for {
93+
_ <- setupRepo(repo, firstChangeset)
94+
seenBranchesRef <- Ref[IO].of(List.empty[Branch])
95+
sha1 <- IO.fromEither(Sha1.from("adc83b19e793491b1c6ea0fd8b46cd9f32e592fc"))
96+
firstData = UpdateData(
97+
RepoData(
98+
repo,
99+
RepoCache(sha1, List.empty, Option.empty),
100+
RepoConfig()
101+
),
102+
fork,
103+
firstChangeset._2,
104+
master,
105+
sha1,
106+
Branch("bump")
107+
)
108+
secondData = firstData.copy(
109+
updateBranch = duplicateChangeset._1,
110+
update = duplicateChangeset._2
111+
)
112+
seenBranches <- seenBranchesRef.getAndUpdate(identity _)
113+
res1 <- context.applyAlg.applyNewUpdate(
114+
firstData,
115+
seenBranches,
116+
pushCommits(seenBranchesRef),
117+
createPullRequest
118+
)
119+
seenBranches <- seenBranchesRef.getAndUpdate(identity _)
120+
res2 <- context.applyAlg.applyNewUpdate(
121+
secondData,
122+
seenBranches,
123+
pushCommits(seenBranchesRef),
124+
createPullRequest
125+
)
126+
} yield (res1, res2)
127+
}
128+
}).unsafeRunSync()
129+
130+
assertEquals(res, (ProcessResult.Updated, ProcessResult.Ignored))
131+
}
132+
133+
test("Ensure non-unique patchesets are not pushed") {
134+
val identicalBranch =
135+
(Branch("update/foo-1.2.4"), Update.Single("foo" % "bar" % "1.2.3", Nel.one("1.2.4")))
136+
val res = ({
137+
val pushCommits: (UpdateData, List[Commit]) => IO[ProcessResult] =
138+
(_, _) => IO.pure(ProcessResult.Updated)
139+
140+
val createPullRequest: UpdateData => IO[ProcessResult] = _ => IO.pure(ProcessResult.Updated)
141+
142+
val repo = Repo("myorg", "myrepo")
143+
val fork = Repo("myfork", "myrepo")
144+
step0.use { case (implicit0(processAlg: ProcessAlg[IO]), context) =>
145+
for {
146+
_ <- setupRepo(repo, identicalBranch)
147+
seenBranchesRef <- Ref[IO].of(List(identicalBranch._1))
148+
sha1 <- IO.fromEither(Sha1.from("adc83b19e793491b1c6ea0fd8b46cd9f32e592fc"))
149+
data = UpdateData(
150+
RepoData(
151+
repo,
152+
RepoCache(sha1, List.empty, Option.empty),
153+
RepoConfig()
154+
),
155+
fork,
156+
identicalBranch._2,
157+
master,
158+
sha1,
159+
Branch("bump")
160+
)
161+
seenBranches <- seenBranchesRef.getAndUpdate(identity _)
162+
res <- context.applyAlg.applyNewUpdate(data, seenBranches, pushCommits, createPullRequest)
163+
} yield res
164+
}
165+
}).unsafeRunSync()
166+
167+
assertEquals(res, ProcessResult.Ignored)
168+
}
169+
}

0 commit comments

Comments
 (0)