|
| 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.ensureExists(config.gitCfg.gitAskPass.parent) |
| 53 | + _ <- fileAlg.writeFile( |
| 54 | + config.gitCfg.gitAskPass, |
| 55 | + """ echo bogus-password """ |
| 56 | + ) |
| 57 | + _ <- supplement.git("add", "build.sbt")(repoDir) |
| 58 | + _ <- context.gitAlg.commitAll(repo, "Initial commit") |
| 59 | + // Create another simulated curated update branch with |
| 60 | + _ <- context.gitAlg.createBranch(repo, branch) |
| 61 | + _ <- fileAlg.writeFile( |
| 62 | + repoDir / "build.sbt", |
| 63 | + s"""libraryDependency += "foo" % "bar" % "${update.newerVersions.head}" """ |
| 64 | + ) |
| 65 | + _ <- supplement.git("add", "build.sbt")(repoDir) |
| 66 | + _ <- context.gitAlg.commitAll(repo, "Update bar to 1.2.4") |
| 67 | + _ <- context.gitAlg.checkoutBranch(repo, master) |
| 68 | + } yield () |
| 69 | + } |
| 70 | + |
| 71 | + test("Ensure unique patchesets are pushed") { |
| 72 | + val firstBranch = Branch("update/foo-1.2.4") |
| 73 | + val duplicateBranch = Branch("update/foo-duplicate-1.2.4") |
| 74 | + val update = Update.Single("foo" % "bar" % "1.2.3", Nel.one("1.2.4")) |
| 75 | + val firstChangeset = (firstBranch, update) |
| 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 | + update, |
| 102 | + master, |
| 103 | + sha1, |
| 104 | + Branch("bump") |
| 105 | + ) |
| 106 | + secondData = firstData.copy( |
| 107 | + updateBranch = duplicateBranch, |
| 108 | + update = update |
| 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 branch = Branch("update/foo-1.2.4") |
| 133 | + val update = Update.Single("foo" % "bar" % "1.2.3", Nel.one("1.2.4")) |
| 134 | + val identicalBranch = (branch, update) |
| 135 | + val res = ({ |
| 136 | + val pushCommits: (UpdateData, List[Commit]) => IO[ProcessResult] = |
| 137 | + (_, _) => IO.pure(ProcessResult.Updated) |
| 138 | + |
| 139 | + val createPullRequest: UpdateData => IO[ProcessResult] = _ => IO.pure(ProcessResult.Updated) |
| 140 | + |
| 141 | + val repo = Repo("myorg", "myrepo") |
| 142 | + val fork = Repo("myfork", "myrepo") |
| 143 | + step0.use { case (implicit0(processAlg: ProcessAlg[IO]), context) => |
| 144 | + for { |
| 145 | + _ <- setupRepo(repo, identicalBranch) |
| 146 | + seenBranchesRef <- Ref[IO].of(List(branch)) |
| 147 | + sha1 <- IO.fromEither(Sha1.from("adc83b19e793491b1c6ea0fd8b46cd9f32e592fc")) |
| 148 | + data = UpdateData( |
| 149 | + RepoData( |
| 150 | + repo, |
| 151 | + RepoCache(sha1, List.empty, Option.empty), |
| 152 | + RepoConfig() |
| 153 | + ), |
| 154 | + fork, |
| 155 | + update, |
| 156 | + master, |
| 157 | + sha1, |
| 158 | + Branch("bump") |
| 159 | + ) |
| 160 | + seenBranches <- seenBranchesRef.getAndUpdate(identity _) |
| 161 | + res <- context.applyAlg.applyNewUpdate(data, seenBranches, pushCommits, createPullRequest) |
| 162 | + } yield res |
| 163 | + } |
| 164 | + }).unsafeRunSync() |
| 165 | + |
| 166 | + assertEquals(res, ProcessResult.Ignored) |
| 167 | + } |
| 168 | +} |
0 commit comments