Skip to content

Commit 773ee53

Browse files
author
Devon Stewart
committed
Exploratory commit to rough out what could constitute a useful test
I don't like this strategy, but I'm interested to see what codecov (and others) think about this approach. I've basically just reimplemented the business logic in the test, but short of mocking out the whole git repo in order to extract state, I'm not sure how best to represent what I'm trying to test.
1 parent 2defcef commit 773ee53

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,29 +155,18 @@ final class NurtureAlg[F[_]](config: Config)(implicit
155155
gitAlg.removeBranch(repo, branch)
156156
}
157157

158-
def ensureDistinctBranch(
159-
data: UpdateData,
160-
seenBranches: Ref[F, List[Branch]],
161-
whenDistinct: F[ProcessResult]
162-
): F[ProcessResult] =
163-
seenBranches.get
164-
.flatMap(_.forallM(gitAlg.diff(data.repo, _).map(_.nonEmpty)))
165-
.ifM(
166-
whenDistinct,
167-
logger.warn("Discovered a duplicate branch, not pushing").as(Ignored)
168-
)
169-
170158
def applyNewUpdate(data: UpdateData, seenBranches: Ref[F, List[Branch]]): F[ProcessResult] =
171159
gitAlg.returnToCurrentBranch(data.repo) {
172160
val createBranch = logger.info(s"Create branch ${data.updateBranch.name}") >>
173161
gitAlg.createBranch(data.repo, data.updateBranch)
174162
editAlg.applyUpdate(data.repoData, data.update, createBranch).flatMap { editCommits =>
175163
if (editCommits.isEmpty) logger.warn("No commits created").as(Ignored)
176164
else
177-
ensureDistinctBranch(
178-
data,
165+
NurtureAlg.ensureDistinctBranch(
179166
seenBranches,
180-
pushCommits(data, editCommits) >> createPullRequest(data)
167+
gitAlg.diff(data.repo, _).map(_.nonEmpty),
168+
pushCommits(data, editCommits) >> createPullRequest(data),
169+
logger.warn("Discovered a duplicate branch, not pushing").as[ProcessResult](Ignored)
181170
)
182171
}
183172
}
@@ -267,10 +256,11 @@ final class NurtureAlg[F[_]](config: Config)(implicit
267256
)
268257
maybeMergeCommit <- gitAlg.mergeTheirs(data.repo, data.baseBranch)
269258
editCommits <- editAlg.applyUpdate(data.repoData, data.update)
270-
result <- ensureDistinctBranch(
271-
data,
259+
result <- NurtureAlg.ensureDistinctBranch(
272260
seenBranches,
273-
pushCommits(data, maybeMergeCommit.toList ++ editCommits)
261+
gitAlg.diff(data.repo, _).map(_.nonEmpty),
262+
pushCommits(data, maybeMergeCommit.toList ++ editCommits),
263+
logger.warn("Discovered a duplicate branch, not pushing").as[ProcessResult](Ignored)
274264
)
275265
} yield result
276266
}
@@ -295,4 +285,14 @@ object NurtureAlg {
295285
.compile
296286
.drain
297287
}
288+
289+
def ensureDistinctBranch[F[_]: Sync: cats.FlatMap](
290+
seenBranches: Ref[F, List[Branch]],
291+
isDistinct: Branch => F[Boolean],
292+
whenDistinct: F[ProcessResult],
293+
whenDuplicate: F[ProcessResult]
294+
): F[ProcessResult] =
295+
seenBranches.get
296+
.flatMap(_.forallM(isDistinct))
297+
.ifM(whenDistinct, whenDuplicate)
298298
}

modules/core/src/test/scala/org/scalasteward/core/TestInstances.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.scalacheck.{Arbitrary, Cogen, Gen}
99
import org.scalasteward.core.TestSyntax._
1010
import org.scalasteward.core.data.Update.Single
1111
import org.scalasteward.core.data._
12-
import org.scalasteward.core.git.Sha1
12+
import org.scalasteward.core.git.{Branch, Sha1}
1313
import org.scalasteward.core.git.Sha1.HexString
1414
import org.scalasteward.core.repocache.RepoCache
1515
import org.scalasteward.core.repoconfig.PullRequestFrequency.{Asap, Timespan}
@@ -59,6 +59,13 @@ object TestInstances {
5959
} yield Single(groupId % artifactId % currentVersion, Nel.one(newerVersion))
6060
)
6161

62+
implicit val branchArbitrary: Arbitrary[Branch] =
63+
Arbitrary(
64+
for {
65+
name <- Gen.alphaStr
66+
} yield Branch(name)
67+
)
68+
6269
private val hashGen: Gen[String] =
6370
for {
6471
sep <- Gen.oneOf('-', '+')

modules/core/src/test/scala/org/scalasteward/core/nurture/NurtureAlgTest.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package org.scalasteward.core.nurture
22

3+
import cats.Applicative
34
import cats.data.StateT
45
import cats.effect.IO
6+
import cats.effect.concurrent.Ref
7+
import cats.syntax.all._
58
import eu.timepit.refined.types.numeric.NonNegInt
69
import munit.ScalaCheckSuite
710
import org.scalacheck.Prop._
811
import org.scalasteward.core.TestInstances._
912
import org.scalasteward.core.data.ProcessResult.{Ignored, Updated}
1013
import org.scalasteward.core.data.{ProcessResult, Update}
14+
import org.scalasteward.core.git.Branch
1115

1216
class NurtureAlgTest extends ScalaCheckSuite {
1317
test("processUpdates with No Limiting") {
@@ -42,4 +46,29 @@ class NurtureAlgTest extends ScalaCheckSuite {
4246
assertEquals(obtained, updates.size)
4347
}
4448
}
49+
50+
test(
51+
"Ensure distinct should not push branches that are not different to other visited branches"
52+
) {
53+
forAll { branches: Set[Branch] =>
54+
val (duplicateBranches, distinctBranches) = branches.toList.splitAt(branches.size / 2)
55+
type F[A] = StateT[IO, Int, A]
56+
val F = Applicative[F]
57+
val f: StateT[IO, Int, ProcessResult] =
58+
StateT[IO, Int, ProcessResult](actionAcc => IO.pure(actionAcc + 1 -> Updated))
59+
val obtained = (for {
60+
seenBranches <- Ref[F].of(duplicateBranches)
61+
res <- (duplicateBranches ++ distinctBranches).traverse { branch =>
62+
NurtureAlg
63+
.ensureDistinctBranch(
64+
seenBranches,
65+
_ => F.pure(distinctBranches.contains(branch)),
66+
f,
67+
F.pure[ProcessResult](Ignored)
68+
)
69+
}
70+
} yield res).runS(0).unsafeRunSync()
71+
assertEquals(obtained, distinctBranches.length)
72+
}
73+
}
4574
}

0 commit comments

Comments
 (0)