Skip to content

Commit

Permalink
RNGP - Migrate settings-plugin to AssertJ (#45575)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #45575

We should move over to use AssertJ as per our linter.
I'm adding it here to a first test and will use it as a reference for some OSS contributions from outside.

Changelog:
[Internal] [Changed] - Migrate settings-plugin to Assertj

Reviewed By: cipolleschi

Differential Revision: D60037797

fbshipit-source-id: 579ed7bf5fb219e25577af3ab87934503ee7898e
  • Loading branch information
cortinico authored and facebook-github-bot committed Jul 23, 2024
1 parent fedbe2d commit 91ecd7e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 48 deletions.
2 changes: 2 additions & 0 deletions packages/gradle-plugin/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ guava = "31.0.1-jre"
javapoet = "1.13.0"
junit = "4.13.2"
kotlin = "1.9.24"
assertj = "3.25.1"

[libraries]
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
Expand All @@ -13,6 +14,7 @@ gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
javapoet = { module = "com.squareup:javapoet", version.ref = "javapoet" }
junit = {module = "junit:junit", version.ref = "junit" }
assertj = { module = "org.assertj:assertj-core", version.ref = "assertj" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
implementation(libs.javapoet)

testImplementation(libs.junit)
testImplementation(libs.assertj)
testImplementation(project(":shared-testutil"))
}

Expand Down
1 change: 1 addition & 0 deletions packages/gradle-plugin/settings-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
implementation(libs.javapoet)

testImplementation(libs.junit)
testImplementation(libs.assertj)
testImplementation(project(":shared-testutil"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ package com.facebook.react
import com.facebook.react.ReactSettingsExtension.Companion.checkAndUpdateLockfiles
import com.facebook.react.ReactSettingsExtension.Companion.computeSha256
import com.facebook.react.ReactSettingsExtension.Companion.getLibrariesToAutolink
import groovy.test.GroovyTestCase.assertEquals
import java.io.File
import org.assertj.core.api.Assertions.assertThat
import org.gradle.testfixtures.ProjectBuilder
import org.intellij.lang.annotations.Language
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
Expand All @@ -34,10 +32,8 @@ class ReactSettingsExtensionTest {
}
"""
.trimIndent())

assertEquals(
"838aa9a72a16fdd55b0d49b510a82e264a30f59333b5fdd97c7798a29146f6a8",
computeSha256(validFile))
assertThat(computeSha256(validFile))
.isEqualTo("838aa9a72a16fdd55b0d49b510a82e264a30f59333b5fdd97c7798a29146f6a8")
}

@Test
Expand All @@ -52,7 +48,7 @@ class ReactSettingsExtensionTest {
.trimIndent())

val map = getLibrariesToAutolink(validJsonFile)
assertEquals(0, map.keys.size)
assertThat(map.keys).isEmpty()
}

@Test
Expand Down Expand Up @@ -97,11 +93,9 @@ class ReactSettingsExtensionTest {
.trimIndent())

val map = getLibrariesToAutolink(validJsonFile)
assertEquals(1, map.keys.size)
assertTrue(":react-native_oss-library-example" in map.keys)
assertEquals(
File("./node_modules/@react-native/oss-library-example/android"),
map[":react-native_oss-library-example"])
assertThat(map.keys).containsExactly(":react-native_oss-library-example")
assertThat(map[":react-native_oss-library-example"])
.isEqualTo(File("./node_modules/@react-native/oss-library-example/android"))
}

@Test
Expand Down Expand Up @@ -130,14 +124,14 @@ class ReactSettingsExtensionTest {
.trimIndent())

val map = getLibrariesToAutolink(validJsonFile)
assertEquals(0, map.keys.size)
assertThat(map.keys).isEmpty()
}

@Test
fun checkAndUpdateLockfiles_withNothingToCheck_returnsFalse() {
val project = ProjectBuilder.builder().build()
val noFiles = project.files()
assertFalse(checkAndUpdateLockfiles(noFiles, tempFolder.root))
assertThat(checkAndUpdateLockfiles(noFiles, tempFolder.root)).isFalse()
}

@Test
Expand All @@ -147,11 +141,10 @@ class ReactSettingsExtensionTest {
tempFolder.newFile("yarn.lock").apply { writeText("I'm a lockfile") }
val lockfileCollection = project.files("yarn.lock")

assertTrue(checkAndUpdateLockfiles(lockfileCollection, buildFolder))
assertTrue(File(buildFolder, "yarn.lock.sha").exists())
assertEquals(
"76046b72442ee7eb130627e56c3db7c9907eef4913b17ad130335edc0eb702a8",
File(buildFolder, "yarn.lock.sha").readText())
assertThat(checkAndUpdateLockfiles(lockfileCollection, buildFolder)).isTrue()
assertThat(File(buildFolder, "yarn.lock.sha").exists()).isTrue()
assertThat(File(buildFolder, "yarn.lock.sha").readText())
.isEqualTo("76046b72442ee7eb130627e56c3db7c9907eef4913b17ad130335edc0eb702a8")
}

@Test
Expand All @@ -164,11 +157,10 @@ class ReactSettingsExtensionTest {
tempFolder.newFile("yarn.lock").apply { writeText("I'm a lockfile") }
val lockfileCollection = project.files("yarn.lock")

assertTrue(checkAndUpdateLockfiles(lockfileCollection, buildFolder))
assertTrue(File(buildFolder, "yarn.lock.sha").exists())
assertEquals(
"76046b72442ee7eb130627e56c3db7c9907eef4913b17ad130335edc0eb702a8",
File(buildFolder, "yarn.lock.sha").readText())
assertThat(checkAndUpdateLockfiles(lockfileCollection, buildFolder)).isTrue()
assertThat(File(buildFolder, "yarn.lock.sha").exists()).isTrue()
assertThat(File(buildFolder, "yarn.lock.sha").readText())
.isEqualTo("76046b72442ee7eb130627e56c3db7c9907eef4913b17ad130335edc0eb702a8")
}

@Test
Expand All @@ -182,11 +174,10 @@ class ReactSettingsExtensionTest {
tempFolder.newFile("yarn.lock").apply { writeText("I'm a lockfile") }
val lockfileCollection = project.files("yarn.lock")

assertFalse(checkAndUpdateLockfiles(lockfileCollection, buildFolder))
assertTrue(File(buildFolder, "yarn.lock.sha").exists())
assertEquals(
"76046b72442ee7eb130627e56c3db7c9907eef4913b17ad130335edc0eb702a8",
File(buildFolder, "yarn.lock.sha").readText())
assertThat(checkAndUpdateLockfiles(lockfileCollection, buildFolder)).isFalse()
assertThat(File(buildFolder, "yarn.lock.sha").exists()).isTrue()
assertThat(File(buildFolder, "yarn.lock.sha").readText())
.isEqualTo("76046b72442ee7eb130627e56c3db7c9907eef4913b17ad130335edc0eb702a8")
}

@Test
Expand All @@ -200,15 +191,13 @@ class ReactSettingsExtensionTest {
tempFolder.newFile("package-lock.json").apply { writeText("and I'm another lockfile") }
val lockfileCollection = project.files("yarn.lock", "package-lock.json")

assertTrue(checkAndUpdateLockfiles(lockfileCollection, buildFolder))
assertTrue(File(buildFolder, "yarn.lock.sha").exists())
assertTrue(File(buildFolder, "package-lock.json.sha").exists())
assertEquals(
"76046b72442ee7eb130627e56c3db7c9907eef4913b17ad130335edc0eb702a8",
File(buildFolder, "yarn.lock.sha").readText())
assertEquals(
"9be5bca432b81becf4f54451aea021add68376330581eaa93ab9a0b3e4e29a3b",
File(buildFolder, "package-lock.json.sha").readText())
assertThat(checkAndUpdateLockfiles(lockfileCollection, buildFolder)).isTrue()
assertThat(File(buildFolder, "yarn.lock.sha").exists()).isTrue()
assertThat(File(buildFolder, "package-lock.json.sha").exists()).isTrue()
assertThat(File(buildFolder, "yarn.lock.sha").readText())
.isEqualTo("76046b72442ee7eb130627e56c3db7c9907eef4913b17ad130335edc0eb702a8")
assertThat(File(buildFolder, "package-lock.json.sha").readText())
.isEqualTo("9be5bca432b81becf4f54451aea021add68376330581eaa93ab9a0b3e4e29a3b")
}

@Test
Expand All @@ -225,15 +214,13 @@ class ReactSettingsExtensionTest {
tempFolder.newFile("package-lock.json").apply { writeText("and I'm another lockfile") }
val lockfileCollection = project.files("yarn.lock", "package-lock.json")

assertFalse(checkAndUpdateLockfiles(lockfileCollection, buildFolder))
assertTrue(File(buildFolder, "yarn.lock.sha").exists())
assertTrue(File(buildFolder, "package-lock.json.sha").exists())
assertEquals(
"76046b72442ee7eb130627e56c3db7c9907eef4913b17ad130335edc0eb702a8",
File(buildFolder, "yarn.lock.sha").readText())
assertEquals(
"9be5bca432b81becf4f54451aea021add68376330581eaa93ab9a0b3e4e29a3b",
File(buildFolder, "package-lock.json.sha").readText())
assertThat(checkAndUpdateLockfiles(lockfileCollection, buildFolder)).isFalse()
assertThat(File(buildFolder, "yarn.lock.sha").exists()).isTrue()
assertThat(File(buildFolder, "package-lock.json.sha").exists()).isTrue()
assertThat(File(buildFolder, "yarn.lock.sha").readText())
.isEqualTo("76046b72442ee7eb130627e56c3db7c9907eef4913b17ad130335edc0eb702a8")
assertThat(File(buildFolder, "package-lock.json.sha").readText())
.isEqualTo("9be5bca432b81becf4f54451aea021add68376330581eaa93ab9a0b3e4e29a3b")
}

private fun createJsonFile(@Language("JSON") input: String) =
Expand Down
1 change: 1 addition & 0 deletions packages/gradle-plugin/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
implementation(libs.gson)
implementation(libs.guava)
testImplementation(libs.junit)
testImplementation(libs.assertj)
testImplementation(project(":shared-testutil"))
}

Expand Down

0 comments on commit 91ecd7e

Please sign in to comment.