Skip to content

Commit ef94d73

Browse files
author
Devon Stewart
committed
Wrote integration-style test for ApplyAlg
1 parent 98a386c commit ef94d73

File tree

1 file changed

+167
-0
lines changed

1 file changed

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