55package test.tests.hashers
66
77import app.api.MockApi
8+ import app.extractors.Extractor
89import app.hashers.CommitHasher
910import app.hashers.CommitCrawler
1011import app.model.*
@@ -13,6 +14,7 @@ import org.eclipse.jgit.api.Git
1314import org.jetbrains.spek.api.Spek
1415import org.jetbrains.spek.api.dsl.given
1516import org.jetbrains.spek.api.dsl.it
17+ import test.utils.TestRepo
1618import java.io.File
1719import java.util.stream.StreamSupport.stream
1820import kotlin.streams.toList
@@ -236,5 +238,64 @@ class CommitHasherTest : Spek({
236238 }
237239 }*/
238240
241+ given("commits with syntax stats") {
242+
243+ val lines = listOf("x = [i**2 for i range(9999)]", "def fn()", "x = 1",
244+ "x = map(lambda x: x**2, range(9999))",
245+ "x = map(lambda x: x**2, map(lambda x: x**3, range(10))",
246+ "x = map(lambda x: x**2, range(10))," +
247+ "map(lambda x: x**3, range(10)))")
248+
249+ val authorEmail =
" [email protected] " 250+ val author = Author ("Test ", authorEmail)
251+
252+ val testRepoPath = " ../testrepo-commit-hasher-"
253+ val testRepo = TestRepo (testRepoPath + "python-facts")
254+
255+ val emails = hashSetOf(authorEmail)
256+ val mockApi = MockApi (mockRepo = repo)
257+ val observable = CommitCrawler .getObservable(testRepo.git, repo)
258+
259+ it("sends stats") {
260+ for (i in 0..lines.size - 1) {
261+ val line = lines[i]
262+ val fileName = " file$i .py"
263+ testRepo.createFile(fileName, listOf(line))
264+ testRepo.commit(message = "$line in $fileName", author = author)
265+ }
266+
267+ val errors = mutableListOf<Throwable >()
268+
269+ val rehashes = (0..lines.size - 1).map { " r$it " }
270+
271+ CommitHasher (repo, mockApi, rehashes, emails)
272+ .updateFromObservable(observable, { e -> errors.add(e) })
273+ if (errors.size > 0) {
274+ println(errors[0].message)
275+ }
276+ assertEquals(0, errors.size)
277+
278+ val syntaxStats = mockApi.receivedAddedCommits
279+ .fold(mutableListOf<CommitStats >()) { allStats, commit ->
280+ allStats.addAll(commit.stats)
281+ allStats
282+ }.filter { it.type == Extractor .TYPE_SYNTAX }
283+
284+ val mapStats = syntaxStats.filter { it.tech == " python>map" }
285+ val listStats = syntaxStats.filter { it.tech == " python>list" }
286+ assertEquals(3, mapStats.size)
287+ assertEquals(1, listStats.size)
288+ assertEquals(5, mapStats.map { it.numLinesAdded }.sum())
289+ assertEquals(0, mapStats.map { it.numLinesDeleted }.sum())
290+
291+ assertEquals(1, listStats.map { it.numLinesAdded }.sum())
292+ assertEquals(0, listStats.map { it.numLinesDeleted }.sum())
293+ }
294+
295+ afterGroup {
296+ testRepo.destroy()
297+ }
298+ }
299+
239300 cleanRepos()
240301})
0 commit comments