|
| 1 | +// Copyright 2018 Sourcerer Inc. All Rights Reserved. |
| 2 | +// Author: Alexander Surkov ([email protected]) |
| 3 | + |
| 4 | +package test.tests.hashers |
| 5 | + |
| 6 | +import app.api.MockApi |
| 7 | +import app.extractors.Extractor |
| 8 | +import app.hashers.CommitHasher |
| 9 | +import app.hashers.CommitCrawler |
| 10 | +import app.model.* |
| 11 | +import app.utils.RepoHelper |
| 12 | +import org.eclipse.jgit.api.Git |
| 13 | +import org.jetbrains.spek.api.Spek |
| 14 | +import org.jetbrains.spek.api.dsl.given |
| 15 | +import org.jetbrains.spek.api.dsl.it |
| 16 | +import test.utils.TestRepo |
| 17 | +import java.io.File |
| 18 | +import java.util.stream.StreamSupport.stream |
| 19 | +import kotlin.streams.toList |
| 20 | +import kotlin.test.assertEquals |
| 21 | + |
| 22 | +class IgnorePathsTest : Spek({ |
| 23 | + fun cleanRepos() { |
| 24 | + Runtime.getRuntime().exec("src/test/delete_repo.sh").waitFor() |
| 25 | + } |
| 26 | + |
| 27 | + val userName = "Contributor" |
| 28 | + val userEmail = "[email protected]" |
| 29 | + |
| 30 | + // Creation of test repo. |
| 31 | + cleanRepos() |
| 32 | + |
| 33 | + given("commits with syntax stats") { |
| 34 | + val lines = listOf("x = [i**2 for i range(9999)]", "def fn()", "x = 1", |
| 35 | + "x = map(lambda x: x**2, range(9999))", |
| 36 | + "x = map(lambda x: x**2, map(lambda x: x**3, range(10))", |
| 37 | + "x = map(lambda x: x**2, range(10))," + |
| 38 | + "map(lambda x: x**3, range(10)))") |
| 39 | + |
| 40 | + val author = Author(userName, userEmail) |
| 41 | + val emails = hashSetOf(userEmail) |
| 42 | + |
| 43 | + val testRepoPath = "../IgnorePaths_t1" |
| 44 | + val testRepo = TestRepo(testRepoPath) |
| 45 | + |
| 46 | + val testRehash = "rehash_IgnorePaths_t1" |
| 47 | + val serverRepo = Repo(rehash = testRehash) |
| 48 | + |
| 49 | + val mockApi = MockApi(mockRepo = serverRepo) |
| 50 | + val observable = CommitCrawler.getObservable(testRepo.git, serverRepo) |
| 51 | + |
| 52 | + it("t1") { |
| 53 | + testRepo.createFile("test.py", lines) |
| 54 | + testRepo.commit(message = "commit1", author = author) |
| 55 | + |
| 56 | + testRepo.createFile("ignore.py", lines) |
| 57 | + testRepo.commit(message = "commit2", author = author) |
| 58 | + |
| 59 | + // Add config, ignore.py from previous commit should be |
| 60 | + // ignored for stats. |
| 61 | + testRepo.createFile(".sourcerer-conf", |
| 62 | + listOf("[ignore]", "ignore.py", "#test.py")) |
| 63 | + testRepo.commit(message = "commit3", author = author) |
| 64 | + |
| 65 | + // Uncomment test.py file in config and delete it. The change |
| 66 | + // should be ignored for statistics. |
| 67 | + testRepo.deleteLines(".sourcerer-conf", 1, 1) |
| 68 | + testRepo.insertLines(".sourcerer-conf", 1, listOf("test.py")) |
| 69 | + testRepo.commit(message = "commit4", author = author) |
| 70 | + |
| 71 | + testRepo.deleteFile("test.py") |
| 72 | + testRepo.commit(message = "commit5", author = author) |
| 73 | + |
| 74 | + val errors = mutableListOf<Throwable>() |
| 75 | + CommitHasher(serverRepo, mockApi, listOf("rehashes"), emails) |
| 76 | + .updateFromObservable(observable, { e -> errors.add(e) }) |
| 77 | + if (errors.size > 0) { |
| 78 | + println(errors[0].message) |
| 79 | + } |
| 80 | + assertEquals(0, errors.size) |
| 81 | + |
| 82 | + val syntaxStats = mockApi.receivedAddedCommits |
| 83 | + .fold(mutableListOf<CommitStats>()) { allStats, commit -> |
| 84 | + allStats.addAll(commit.stats) |
| 85 | + allStats |
| 86 | + }.filter { it.type == Extractor.TYPE_SYNTAX } |
| 87 | + |
| 88 | + val mapStats = syntaxStats.filter { it.tech == "python>map" } |
| 89 | + assertEquals(1, mapStats.size) |
| 90 | + assertEquals(5, mapStats.map { it.numLinesAdded }.sum()) |
| 91 | + assertEquals(0, mapStats.map { it.numLinesDeleted }.sum()) |
| 92 | + } |
| 93 | + |
| 94 | + afterGroup { |
| 95 | + testRepo.destroy() |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + cleanRepos() |
| 100 | +}) |
0 commit comments