-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a part of RetrieveChangedFilesTest test cases
Yet to add cases for retrieving and checking test targets
- Loading branch information
Showing
2 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
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
139 changes: 139 additions & 0 deletions
139
scripts/src/javatests/org/oppia/android/scripts/ci/RetrieveChangedFilesTest.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,139 @@ | ||
package org.oppia.android.scripts.ci | ||
|
||
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.scripts.common.CommandExecutor | ||
import org.oppia.android.scripts.common.CommandExecutorImpl | ||
import org.oppia.android.scripts.common.ProtoStringEncoder.Companion.toCompressedBase64 | ||
import org.oppia.android.scripts.common.ScriptBackgroundCoroutineDispatcher | ||
import org.oppia.android.scripts.proto.ChangedFilesBucket | ||
import org.oppia.android.scripts.testing.TestBazelWorkspace | ||
import org.oppia.android.testing.assertThrows | ||
import java.io.ByteArrayOutputStream | ||
import java.io.File | ||
import java.io.OutputStream | ||
import java.io.PrintStream | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** Tests for the retrieve_changed_files utility. */ | ||
// FunctionName: test names are conventionally named with underscores. | ||
@Suppress("FunctionName") | ||
class RetrieveChangedFilesTest { | ||
@field:[Rule JvmField] val tempFolder = TemporaryFolder() | ||
|
||
private val scriptBgDispatcher by lazy { ScriptBackgroundCoroutineDispatcher() } | ||
private lateinit var commandExecutor: CommandExecutor | ||
private lateinit var testBazelWorkspace: TestBazelWorkspace | ||
private lateinit var pendingOutputStream: ByteArrayOutputStream | ||
private lateinit var originalStandardOutputStream: OutputStream | ||
|
||
@Before | ||
fun setUp() { | ||
commandExecutor = initializeCommandExecutorWithLongProcessWaitTime() | ||
testBazelWorkspace = TestBazelWorkspace(tempFolder) | ||
|
||
// Redirect script output for testing purposes. | ||
pendingOutputStream = ByteArrayOutputStream() | ||
originalStandardOutputStream = System.out | ||
System.setOut(PrintStream(pendingOutputStream)) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
// Reinstate test output redirection. | ||
System.setOut(PrintStream(pendingOutputStream)) | ||
|
||
// Print the status of the git repository to help with debugging in the cases of test failures | ||
// and to help manually verify the expect git state at the end of each test. | ||
println("git status (at end of test):") | ||
|
||
scriptBgDispatcher.close() | ||
} | ||
|
||
@Test | ||
fun testUtility_noArguments_printsUsageStringAndExits() { | ||
val exception = assertThrows<SecurityException>() { runScript() } | ||
|
||
// Bazel catches the System.exit() call and throws a SecurityException. This is a bit hacky way | ||
// to verify that System.exit() is called, but it's helpful. | ||
assertThat(exception).hasMessageThat().contains("System.exit()") | ||
assertThat(pendingOutputStream.toString()).contains("Usage:") | ||
} | ||
|
||
@Test | ||
fun testUtility_invalidArguments_printsUsageStringAndExits() { | ||
for (argCount in 0..4) { | ||
val args = Array(argCount) { "arg${it + 1}" } | ||
val exception = assertThrows<SecurityException> { runScript(*args) } | ||
|
||
// Bazel catches the System.exit() call and throws a SecurityException. | ||
assertThat(exception).hasMessageThat().contains("System.exit()") | ||
assertThat(pendingOutputStream.toString()).contains("Usage:") | ||
} | ||
} | ||
|
||
@Test | ||
fun testUtility_invalidBase64_throwsException() { | ||
assertThrows<IllegalArgumentException>() { runScript("${tempFolder.root}", "badbase64", "file1", "file2", "file3") } | ||
} | ||
|
||
@Test | ||
fun testUtility_validBase64_oneTest_writesCacheNameFile() { | ||
val cacheNameFilePath = tempFolder.getNewTempFilePath("cache_name") | ||
val changedFilePath = tempFolder.getNewTempFilePath("changed_file_list") | ||
val testTargetFilePath = tempFolder.getNewTempFilePath("test_target_list") | ||
val base64String = computeBase64String( | ||
ChangedFilesBucket.newBuilder().apply { | ||
cacheBucketName = "example" | ||
addChangedFiles("//example/to/a/file/Demonstration.kt") | ||
}.build() | ||
) | ||
|
||
runScript(tempFolder.root.absolutePath, base64String, cacheNameFilePath, changedFilePath, testTargetFilePath) | ||
|
||
assertThat(File(cacheNameFilePath).readText().trim()).isEqualTo("example") | ||
} | ||
|
||
@Test | ||
fun testUtility_validBase64_oneTest_writesChangedFilePathWithCorrectFile() { | ||
val cacheNameFilePath = tempFolder.getNewTempFilePath("cache_name") | ||
val changedFilePath = tempFolder.getNewTempFilePath("changed_file_list") | ||
val testTargetFilePath = tempFolder.getNewTempFilePath("test_target_list") | ||
val base64String = computeBase64String( | ||
ChangedFilesBucket.newBuilder().apply { | ||
cacheBucketName = "example" | ||
addChangedFiles("//example/to/a/file/Demonstration.kt") | ||
}.build() | ||
) | ||
|
||
runScript(tempFolder.root.absolutePath, base64String, cacheNameFilePath, changedFilePath, testTargetFilePath) | ||
|
||
assertThat(File(changedFilePath).readText().trim()).isEqualTo( | ||
"//example/to/a/file/Demonstration.kt" | ||
) | ||
} | ||
|
||
private fun runScript(vararg args: String) { | ||
main(args.toList().toTypedArray()) | ||
} | ||
|
||
private fun computeBase64String(changedFilesBucket: ChangedFilesBucket): String = | ||
changedFilesBucket.toCompressedBase64() | ||
|
||
/** | ||
* Returns the absolute file path of a new file that can be written under this [TemporaryFolder] | ||
* (but does not create the file). | ||
*/ | ||
private fun TemporaryFolder.getNewTempFilePath(name: String) = | ||
File(tempFolder.root, name).absolutePath | ||
|
||
private fun initializeCommandExecutorWithLongProcessWaitTime(): CommandExecutorImpl { | ||
return CommandExecutorImpl( | ||
scriptBgDispatcher, processTimeout = 5, processTimeoutUnit = TimeUnit.MINUTES | ||
) | ||
} | ||
} |