Skip to content

Commit d67b544

Browse files
author
Devon Stewart
committed
Wrote integration-style test for ApplyAlg
1 parent 3bf3f64 commit d67b544

File tree

1 file changed

+167
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)