Skip to content

Commit

Permalink
[git] move file history tests to GitHistoryTest
Browse files Browse the repository at this point in the history
  • Loading branch information
juliabeliaeva committed May 4, 2018
1 parent 6a893f7 commit 08b40d4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 56 deletions.
66 changes: 63 additions & 3 deletions plugins/git4idea/tests/git4idea/history/GitHistoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ package git4idea.history
import com.intellij.dvcs.DvcsUtil
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.vcs.Executor
import com.intellij.openapi.vcs.Executor.ourCurrentDir
import com.intellij.openapi.vcs.Executor.touch
import com.intellij.openapi.vcs.VcsException
import com.intellij.openapi.vcs.history.VcsFileRevision
import com.intellij.util.CollectConsumer
import com.intellij.util.Consumer
import com.intellij.util.ExceptionUtil
import com.intellij.vcsUtil.VcsUtil
import git4idea.GitFileRevision
import git4idea.test.*
Expand Down Expand Up @@ -68,21 +73,76 @@ class GitHistoryTest : GitSingleRepoTest() {
val vFile = VcsUtil.getVirtualFileWithRefresh(commits.first().file)
TestCase.assertNotNull(vFile)
val history = GitFileHistory.collectHistory(myProject, VcsUtil.getFilePath(vFile!!))
TestCase.assertEquals("History size doesn't match. Actual history: \n" + toReadable(history), commits.size, history.size)
TestCase.assertEquals("History is different.", toReadable(commits), toReadable(history))
assertSameHistory(commits, history)
}

@Throws(Exception::class)
fun `test history`() {
val commits = ArrayList<TestCommit>()

commits.add(add("a.txt", ourCurrentDir()))
commits.add(modify(commits.last().file))

commits.add(rename(commits.last().file, File(Executor.mkdir("dir"), "b.txt")))

for (i in 0..3) {
commits.add(modify(commits.last().file))
}

commits.reverse()

val history = GitFileHistory.collectHistory(myProject, VcsUtil.getFilePath(commits.first().file))
assertSameHistory(commits, history)
}

@Throws(Exception::class)
fun `test appendable history`() {
val commits = ArrayList<TestCommit>()

commits.add(add("a.txt", ourCurrentDir()))
commits.add(modify(commits.last().file))

commits.add(rename(commits.last().file, File(Executor.mkdir("dir"), "b.txt")))

for (i in 0..3) {
commits.add(modify(commits.last().file))
}

commits.reverse()

val history = ArrayList<GitFileRevision>()
GitFileHistory.loadHistory(myProject, VcsUtil.getFilePath(commits.first().file), repo.root, null, CollectConsumer(history),
Consumer { exception: VcsException ->
TestCase.fail("No exception expected " + ExceptionUtil.getThrowableText(exception))
})
assertSameHistory(commits, history)
}

private fun assertSameHistory(expected: List<TestCommit>, actual: List<VcsFileRevision>) {
TestCase.assertEquals("History size doesn't match. Actual history: \n" + toReadable(actual), expected.size, actual.size)
TestCase.assertEquals("History is different.", toReadable(expected), toReadable(actual))
}

private class TestCommit(val hash: String, val commitMessage: String, val file: File)

private fun move(file: File, dir: File): TestCommit {
repo.mv(file.path, dir.path)
repo.mv(file, dir)

val message = "Moved ${file.path} to ${dir.name}"
repo.addCommit(message)

return TestCommit(last(), message, File(dir, file.name))
}

private fun rename(file: File, newFile: File): TestCommit {
repo.mv(file, newFile)

val message = "Renamed ${file.path} to ${newFile.name}"
repo.addCommit(message)

return TestCommit(last(), message, newFile)
}

@Throws(IOException::class)
private fun modify(file: File): TestCommit {
FileUtil.appendToFile(file, "Modified")
Expand Down
53 changes: 0 additions & 53 deletions plugins/git4idea/tests/git4idea/history/GitHistoryUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@
package git4idea.history

import com.intellij.openapi.vcs.Executor.*
import com.intellij.openapi.vcs.VcsException
import com.intellij.openapi.vcs.history.VcsFileRevision
import com.intellij.util.CollectConsumer
import com.intellij.util.Consumer
import com.intellij.util.ExceptionUtil
import com.intellij.vcsUtil.VcsUtil.getFilePath
import git4idea.GitFileRevision
import git4idea.GitRevisionNumber
import git4idea.test.*
import junit.framework.TestCase
Expand Down Expand Up @@ -174,24 +168,6 @@ class GitHistoryUtilsTest : GitSingleRepoTest() {
TestCase.assertEquals(revisionNumber.timestamp, timeStampToDate(hashAndDate[1]))
}

@Throws(Exception::class)
fun testHistory() {
val revisions = GitFileHistory.collectHistory(myProject, getFilePath(bfile!!))
assertHistory(revisions)
}

@Throws(Exception::class)
fun testAppendableHistory() {
val revisions = ArrayList<GitFileRevision>(3)

GitFileHistory.loadHistory(myProject, getFilePath(bfile!!), repo.root, null, CollectConsumer(revisions),
Consumer { exception: VcsException ->
TestCase.fail("No exception expected " + ExceptionUtil.getThrowableText(exception))
})

assertHistory(revisions)
}

@Throws(Exception::class)
fun testOnlyHashesHistory() {
val history = GitHistoryUtils.onlyHashesHistory(myProject, getFilePath(bfile!!), projectRoot)
Expand All @@ -204,35 +180,6 @@ class GitHistoryUtilsTest : GitSingleRepoTest() {
}
}

@Throws(VcsException::class)
private fun assertHistory(actualRevisions: List<VcsFileRevision>) {
TestCase.assertEquals("Incorrect number of commits in history", revisions!!.size, actualRevisions.size)
for (i in actualRevisions.indices) {
assertEqualRevisions(actualRevisions[i] as GitFileRevision, revisions!![i])
}
}

@Throws(VcsException::class)
private fun assertEqualRevisions(actual: GitFileRevision, expected: GitTestRevision) {
val actualRev = (actual.revisionNumber as GitRevisionNumber).rev
TestCase.assertEquals(expected.hash, actualRev)
TestCase.assertEquals(expected.date, (actual.revisionNumber as GitRevisionNumber).timestamp)
// TODO: whitespaces problem is known, remove convertWhitespaces... when it's fixed
TestCase.assertEquals(convertWhitespacesToSpacesAndRemoveDoubles(expected.commitMessage),
convertWhitespacesToSpacesAndRemoveDoubles(actual.commitMessage!!))
TestCase.assertEquals(expected.authorName, actual.author)
TestCase.assertEquals(expected.authorEmail, actual.authorEmail)
TestCase.assertEquals(expected.committerName, actual.committerName)
TestCase.assertEquals(expected.committerEmail, actual.committerEmail)
TestCase.assertEquals(expected.branchName, actual.branchName)
TestCase.assertNotNull("No content in revision $actualRev", actual.content)
TestCase.assertEquals(String(expected.content), String(actual.content!!))
}

private fun convertWhitespacesToSpacesAndRemoveDoubles(s: String): String {
return s.replace("[\\s^ ]".toRegex(), " ").replace(" +".toRegex(), " ")
}

private fun timeStampToDate(timestamp: String): Date {
return Date(java.lang.Long.parseLong(timestamp) * 1000)
}
Expand Down

0 comments on commit 08b40d4

Please sign in to comment.