/
Http4sGitHubApiAlgTest.scala
115 lines (100 loc) · 3.39 KB
/
Http4sGitHubApiAlgTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package org.scalasteward.core.github.http4s
import cats.effect.IO
import io.circe.literal._
import org.http4s.circe._
import org.http4s.client.Client
import org.http4s.dsl.io._
import org.http4s.implicits._
import org.http4s.{Http4sLiteralSyntax, HttpRoutes}
import org.scalasteward.core.git.Sha1.HexString
import org.scalasteward.core.git.{Branch, Sha1}
import org.scalasteward.core.mock.MockContext.config
import org.scalasteward.core.util.HttpJsonClient
import org.scalasteward.core.vcs.data._
import org.scalatest.Matchers
import org.scalatest.funsuite.AnyFunSuite
class Http4sGitHubApiAlgTest extends AnyFunSuite with Matchers {
val routes: HttpRoutes[IO] =
HttpRoutes.of[IO] {
case GET -> Root / "repos" / "fthomas" / "base.g8" =>
Ok(
json""" {
"name": "base.g8",
"owner": { "login": "fthomas" },
"clone_url": "https://github.com/fthomas/base.g8.git",
"default_branch": "master"
} """
)
case GET -> Root / "repos" / "fthomas" / "base.g8" / "branches" / "master" =>
Ok(
json""" {
"name": "master",
"commit": { "sha": "07eb2a203e297c8340273950e98b2cab68b560c1" }
} """
)
case POST -> Root / "repos" / "fthomas" / "base.g8" / "forks" =>
Ok(
json""" {
"name": "base.g8-1",
"owner": { "login": "scala-steward" },
"parent": {
"name": "base.g8",
"owner": { "login": "fthomas" },
"clone_url": "https://github.com/fthomas/base.g8.git",
"default_branch": "master"
},
"clone_url": "https://github.com/scala-steward/base.g8-1.git",
"default_branch": "master"
} """
)
case req =>
println(req.toString())
NotFound()
}
implicit val client: Client[IO] = Client.fromHttpApp(routes.orNotFound)
implicit val httpJsonClient: HttpJsonClient[IO] = new HttpJsonClient[IO]
val gitHubApiAlg = new Http4sGitHubApiAlg[IO](config.vcsApiHost, _ => IO.pure)
val repo = Repo("fthomas", "base.g8")
val parent = RepoOut(
"base.g8",
UserOut("fthomas"),
None,
uri"https://github.com/fthomas/base.g8.git",
Branch("master")
)
val fork = RepoOut(
"base.g8-1",
UserOut("scala-steward"),
Some(parent),
uri"https://github.com/scala-steward/base.g8-1.git",
Branch("master")
)
val defaultBranch = BranchOut(
Branch("master"),
CommitOut(Sha1(HexString("07eb2a203e297c8340273950e98b2cab68b560c1")))
)
test("createForkOrGetRepo") {
val repoOut =
gitHubApiAlg.createForkOrGetRepo(config, repo).unsafeRunSync()
repoOut shouldBe fork
}
test("createForkOrGetRepo without forking") {
val repoOut =
gitHubApiAlg.createForkOrGetRepo(config.copy(doNotFork = true), repo).unsafeRunSync()
repoOut shouldBe parent
}
test("createForkOrGetRepoWithDefaultBranch") {
val (repoOut, branchOut) =
gitHubApiAlg.createForkOrGetRepoWithDefaultBranch(config, repo).unsafeRunSync()
repoOut shouldBe fork
branchOut shouldBe defaultBranch
}
test("createForkOrGetRepoWithDefaultBranch without forking") {
val (repoOut, branchOut) =
gitHubApiAlg
.createForkOrGetRepoWithDefaultBranch(config.copy(doNotFork = true), repo)
.unsafeRunSync()
repoOut shouldBe parent
branchOut shouldBe defaultBranch
}
}