diff --git a/modules/core/shared/src/test/scala/scaladex/core/test/Values.scala b/modules/core/shared/src/test/scala/scaladex/core/test/Values.scala index 08163b2cd..d4e4cce8b 100644 --- a/modules/core/shared/src/test/scala/scaladex/core/test/Values.scala +++ b/modules/core/shared/src/test/scala/scaladex/core/test/Values.scala @@ -253,6 +253,11 @@ object Values { val creationDate: Instant = Instant.ofEpochSecond(1688667180L) } + object Scala3 { + val organization: Project.Organization = Project.Organization("scala") + val reference: Project.Reference = Project.Reference.from("scala/scala3") + } + private def contributor(login: String): GithubContributor = GithubContributor(login, "", Url(""), 1) diff --git a/modules/infra/src/it/scala/scaladex/infra/GithubClientImplTests.scala b/modules/infra/src/it/scala/scaladex/infra/GithubClientImplTests.scala index 34ae555db..3bc3a50ff 100644 --- a/modules/infra/src/it/scala/scaladex/infra/GithubClientImplTests.scala +++ b/modules/infra/src/it/scala/scaladex/infra/GithubClientImplTests.scala @@ -1,21 +1,34 @@ package scaladex.infra +import scala.concurrent.Await +import scala.concurrent.Future +import scala.concurrent.duration._ + import org.apache.pekko.actor.ActorSystem +import org.scalactic.source.Position +import org.scalatest.compatible.Assertion import org.scalatest.funspec.AsyncFunSpec import org.scalatest.matchers.should.Matchers -import scaladex.core.model.Project -import scaladex.infra.config.GithubConfig import scaladex.core.model.GithubResponse +import scaladex.core.model.GithubResponse._ +import scaladex.core.model.Project import scaladex.core.test.Values._ +import scaladex.infra.config.GithubConfig class GithubClientImplTests extends AsyncFunSpec with Matchers { implicit val system: ActorSystem = ActorSystem("github-client-tests") val config: GithubConfig = GithubConfig.load() - val isCI = System.getenv("CI") != null - - // you need to configure locally a token - val client = new GithubClientImpl(config.token.get) + val token = config.token.getOrElse(throw new Exception(s"Missing GITHUB_TOKEN")) + val client = new GithubClientImpl(token) + val userState = + if (isCI) None + else + Await.result(client.getUserState(), 30.seconds) match { + case Failed(code, errorMessage) => throw new Exception(s"$code $errorMessage") + case MovedPermanently(userState) => Some(userState) + case Ok(userState) => Some(userState) + } it("getProjectInfo") { for (response <- client.getProjectInfo(Scalafix.reference)) @@ -95,7 +108,7 @@ class GithubClientImplTests extends AsyncFunSpec with Matchers { } it("getCommitActivity") { - for (commitActivities <- client.getCommitActivity(Scalafix.reference)) + for (commitActivities <- client.getCommitActivity(Scala3.reference)) yield commitActivities should not be empty } @@ -105,22 +118,25 @@ class GithubClientImplTests extends AsyncFunSpec with Matchers { yield commitActivities shouldBe empty } - if (!isCI) { - it("getUserRepositories") { - for (repos <- client.getUserRepositories("atry", Nil)) - yield repos should not be empty - } - it("getUserOrganizations when empty") { - for (orgs <- client.getUserOrganizations("central-ossrh")) - yield orgs shouldBe empty - } - it("getUserOrganizations") { - for (orgs <- client.getUserOrganizations("atry")) - yield orgs should not be empty - } - it("getOrganizationRepositories") { - for (repos <- client.getOrganizationRepositories("atry", Project.Organization("scala"), Nil)) - yield repos should not be empty - } + testOrIgnore("getUserRepositories") { + for (repos <- client.getUserRepositories("atry", Nil)) + yield repos should not be empty + } + testOrIgnore("getUserOrganizations when empty") { + for (orgs <- client.getUserOrganizations("central-ossrh")) + yield orgs shouldBe empty } + testOrIgnore("getUserOrganizations") { + for (orgs <- client.getUserOrganizations("atry")) + yield orgs should not be empty + } + testOrIgnore("getOrganizationRepositories", ignored = !userState.exists(_.orgs.contains(Scala3.organization))) { + for (repos <- client.getOrganizationRepositories("atry", Scala3.organization, Nil)) + yield repos should contain(Scala3.reference) + } + + private def testOrIgnore(name: String, ignored: Boolean = userState.isEmpty)(testFun: => Future[Assertion])( + implicit pos: Position + ): Unit = + if (ignored) ignore(name)(testFun)(pos) else it(name)(testFun)(pos) }