-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #3498: Add CI check to ensure license text is never checked into …
…Git (#3632) * Call out dependency names when license details incomplete exception is thrown * Modify message to include rerunning the script after updating maven_dependencies.textproto * Correct typo * Add suggested changes * Add full stop * Correct comment sentence in versions.bzl * Add some more test cases and add suggested changes * Separate all methods in a new file * Correct imports * Update to reflect changes of coordinate name * Add CI check * Correct static_checks.yml * Remove empty line * Correct visibility of bazel targets and delete one argument from the script * Correct path in exemptions file * Add import for LicenseFetcher in test file * Add suggested changes * Add suggested change * correct kdoc * Use associateWith instead of associateTo * Resolve conflicts * Use associateWith instead of associateTo * Add suggested changes * Add tests for MavenDependenciesListCheck * Remove unwanted lines * Add test file for MavenDependenciesListGenerator * Add test cases for MavenDependenciesListGenerator.kt * Correct dep name * Fix lint * Add KDocs and more test cases * Revert changes in maven_dependencies.textproto * Correct name of binary in static_checks.yml * Correct name of Checks * Run GenerateMavenDependenciesList.kt to update maven_dependencies.textproto * Modify message to include wiki link and to run the GenerateMavenDependenciesList.kt script * Add more test cases * Add more test cases * Add CI check * Add suggested changes * Fix newly added test case * Refactor check into a new package * Refactor check to new package * Correct script library * Fix deps and main_class attribute in root BUILD * Add @BenHenning as the CODEOWNER for third_party_dependencies.xml * Add suggested changes * Remove extra blank line * Add suggested changes * Correct library target in maven_dependencies_check_lib * Correct paths in test exemptions * Add suggested changes * Add suggested changes * Correct name of the test file * Add script to run in the CI to also verify license details. * Correct path * Remove redundant test case * Correct import in GenerateMavenDependenciesListTest.kt * Reorder CI checks * Reorder CI checks * Remove unused imports * Correct package name from data to model * Correct import in MavenDependenciesRetrieverTest.kt * Correct import in MavenDependenciesRetrieverTest.kt * Correct format in static_checks.yml * Correct name of script in XML comment * Add suggested changes * Add check and test cases for catching incomplete license details and remove extra CI check. * Remove unused code * Remove omit function from test file * Correct warning comment * Add suggested changes * Add suggested changes
- Loading branch information
Showing
8 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
scripts/src/java/org/oppia/android/scripts/license/LicenseTextsCheck.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.oppia.android.scripts.license | ||
|
||
import java.io.File | ||
|
||
private const val WARNING_COMMENT = | ||
"<!-- Do not edit this file. It is generated by running RetrieveLicenseTexts.kt. -->" | ||
|
||
/** | ||
* Script to verify that the actual license texts are never checked into our VCS. It mainly | ||
* verifies that the script-generated version of third_party_dependencies.xml file is never | ||
* checked in. | ||
* | ||
* Usage: | ||
* bazel run //scripts:maven_dependencies_list_check -- <path_to_third_party_deps_xml> | ||
* | ||
* Arguments: | ||
* - path_to_third_party_deps_xml: path to the third_party_dependencies.xml | ||
* | ||
* Example: | ||
* bazel run //scripts:maven_dependencies_list_check -- $(pwd)/app/src/main/res/values/third_party_dependencies.xml | ||
*/ | ||
fun main(args: Array<String>) { | ||
if (args.size < 1) { | ||
throw Exception("Too few arguments passed") | ||
} | ||
val pathToThirdPartyDepsXml = args[0] | ||
val thirdPartyDepsXml = File(pathToThirdPartyDepsXml) | ||
check(thirdPartyDepsXml.exists()) { "File does not exist: $thirdPartyDepsXml" } | ||
|
||
val xmlContent = thirdPartyDepsXml.readText() | ||
|
||
checkIfCommentIsPresent(xmlContent = xmlContent, comment = WARNING_COMMENT) | ||
|
||
println("License texts Check Passed") | ||
} | ||
|
||
private fun checkIfCommentIsPresent(xmlContent: String, comment: String) { | ||
if (comment !in xmlContent) { | ||
println("Please revert the changes in third_party_dependencies.xml") | ||
throw Exception("License texts potentially checked into VCS") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
scripts/src/javatests/org/oppia/android/scripts/license/LicenseTextsCheckTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package org.oppia.android.scripts.license | ||
|
||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.TemporaryFolder | ||
import org.oppia.android.testing.assertThrows | ||
import java.io.ByteArrayOutputStream | ||
import java.io.PrintStream | ||
|
||
/** Tests for [LicenseTextsCheck]. */ | ||
class LicenseTextsCheckTest { | ||
private val WARNING_COMMENT = | ||
"<!-- Do not edit this file. It is generated by running RetrieveLicenseTexts.kt. -->" | ||
private val SCRIPT_PASSED_MESSAGE = "License texts Check Passed" | ||
private val LICENSE_TEXTS_CHECKED_IN_FAILURE = "License texts potentially checked into VCS" | ||
private val TOO_FEW_ARGS_PASSED_FAILURE = "Too few arguments passed" | ||
|
||
private val outContent: ByteArrayOutputStream = ByteArrayOutputStream() | ||
private val originalOut: PrintStream = System.out | ||
|
||
@Rule | ||
@JvmField | ||
var tempFolder = TemporaryFolder() | ||
|
||
@Before | ||
fun setUp() { | ||
tempFolder.newFolder("values") | ||
System.setOut(PrintStream(outContent)) | ||
} | ||
|
||
@After | ||
fun restoreStreams() { | ||
System.setOut(originalOut) | ||
} | ||
|
||
@Test | ||
fun testLicenseTexsCheck_noArgsPassed_failWithException() { | ||
val thirdPartyDepsXmlFile = tempFolder.newFile("values/third_party_dependencies.xml") | ||
thirdPartyDepsXmlFile.writeText(WARNING_COMMENT) | ||
|
||
val exception = assertThrows(Exception::class) { | ||
main(arrayOf()) | ||
} | ||
|
||
assertThat(exception).hasMessageThat().contains(TOO_FEW_ARGS_PASSED_FAILURE) | ||
} | ||
|
||
@Test | ||
fun testLicenseTexsCheck_emptyXmlFile_checkFailsWithException() { | ||
val thirdPartyDepsXmlFile = tempFolder.newFile("values/third_party_dependencies.xml") | ||
thirdPartyDepsXmlFile.writeText("") | ||
|
||
val exception = assertThrows(Exception::class) { | ||
main(arrayOf("${tempFolder.root}/values/third_party_dependencies.xml")) | ||
} | ||
|
||
assertThat(exception).hasMessageThat().contains(LICENSE_TEXTS_CHECKED_IN_FAILURE) | ||
} | ||
|
||
@Test | ||
fun testLicenseTexsCheck_warningCommentNotPresent_checkFailsWithException() { | ||
val thirdPartyDepsXmlFile = tempFolder.newFile("values/third_party_dependencies.xml") | ||
thirdPartyDepsXmlFile.writeText( | ||
""" | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string name="third_party_dependency_name_0">Glide</string> | ||
</resources> | ||
""".trimIndent() | ||
) | ||
|
||
val exception = assertThrows(Exception::class) { | ||
main(arrayOf("${tempFolder.root}/values/third_party_dependencies.xml")) | ||
} | ||
|
||
assertThat(exception).hasMessageThat().contains(LICENSE_TEXTS_CHECKED_IN_FAILURE) | ||
} | ||
|
||
@Test | ||
fun testLicenseTexsCheck_onlyWarningCommentPresent_checkPasses() { | ||
val thirdPartyDepsXmlFile = tempFolder.newFile("values/third_party_dependencies.xml") | ||
thirdPartyDepsXmlFile.writeText(WARNING_COMMENT) | ||
|
||
main(arrayOf("${tempFolder.root}/values/third_party_dependencies.xml")) | ||
|
||
assertThat(outContent.toString()).contains(SCRIPT_PASSED_MESSAGE) | ||
} | ||
|
||
@Test | ||
fun testLicenseTexsCheck_warningCommentPresentWithSomeXmlCode_checkPasses() { | ||
val thirdPartyDepsXmlFile = tempFolder.newFile("values/third_party_dependencies.xml") | ||
thirdPartyDepsXmlFile.writeText( | ||
""" | ||
<?xml version="1.0" encoding="utf-8"?> | ||
$WARNING_COMMENT | ||
<resources> | ||
<string name="third_party_dependency_name_0">Glide</string> | ||
</resources> | ||
""".trimIndent() | ||
) | ||
|
||
main(arrayOf("${tempFolder.root}/values/third_party_dependencies.xml")) | ||
|
||
assertThat(outContent.toString()).contains(SCRIPT_PASSED_MESSAGE) | ||
} | ||
|
||
@Test | ||
fun testLicenseTexsCheck_xmlFileNotPresent_checkFailsWithFileNotFoundException() { | ||
val pathToThirdPartyDepsXml = "${tempFolder.root}/values/third_party_dependencies.xml" | ||
val exception = assertThrows(Exception::class) { | ||
main(arrayOf("${tempFolder.root}/values/third_party_dependencies.xml")) | ||
} | ||
|
||
assertThat(exception).hasMessageThat().contains( | ||
"File does not exist: $pathToThirdPartyDepsXml" | ||
) | ||
} | ||
} |