11// Copyright 2017 Sourcerer Inc. All Rights Reserved.
22// Author: Liubov Yaronskaya ([email protected] )3+ // Author: Anatoly Kislov ([email protected] )34
4- package test
5+ package app
56
6- import app.hashers.CommitHasher
77import app.api.MockApi
8- import app.config.MockConfigurator
8+ import app.hashers.CommitHasher
99import app.model.*
1010import app.utils.RepoHelper
1111import org.eclipse.jgit.api.Git
1212import org.jetbrains.spek.api.Spek
13- import org.jetbrains.spek.api.dsl.*
13+ import org.jetbrains.spek.api.dsl.given
14+ import org.jetbrains.spek.api.dsl.it
1415import java.io.File
1516import java.util.stream.StreamSupport.stream
1617import kotlin.streams.toList
1718import kotlin.test.assertEquals
1819import kotlin.test.assertNotEquals
1920
20- class CommitUploadProtocolTest : Spek ({
21+ class CommitHasherTest : Spek ({
22+ val userName = " Contributor"
23+ val userEmail =
" [email protected] " 24+
25+ // Creation of test repo.
2126 val repoPath = " ./tmp_repo/.git"
2227 val git = Git .init ().setGitDir(File (repoPath)).call()
28+ val config = git.repository.config
29+ config.setString("user", null, "name", userName)
30+ config.setString("user", null, "email", userEmail)
31+ config.save()
32+
33+ // Common parameters for CommitHasher.
2334 val gitHasher = Git .open(File (repoPath))
24- val localRepo : LocalRepo = LocalRepo (repoPath)
25- val initialCommit = Commit (git.commit().setMessage("Initial commit.").call())
26- val repoRehash = RepoHelper .calculateRepoRehash(initialCommit.rehash, localRepo)
27- var repo = Repo (rehash = repoRehash, initialCommitRehash = initialCommit.rehash)
28- repo.commits = listOf(initialCommit)
35+ val localRepo = LocalRepo (repoPath)
36+ localRepo.author = Author (userName, userEmail)
37+ val initialCommit = Commit (git.commit().setMessage("Initial commit").call())
38+ val repoRehash = RepoHelper .calculateRepoRehash(initialCommit.rehash,
39+ localRepo)
40+ val repo = Repo (rehash = repoRehash,
41+ initialCommitRehash = initialCommit.rehash)
2942
3043 fun getRepoRehash(git: Git , localRepo: LocalRepo ): String {
3144
3245 val initialRevCommit = stream(git.log().call().spliterator(), false)
3346 .toList().first()
3447 val initialCommit = Commit (initialRevCommit)
3548 val repoRehash = RepoHelper .calculateRepoRehash(initialCommit.rehash,
36- localRepo)
49+ localRepo)
3750 return repoRehash
3851 }
3952
@@ -43,12 +56,26 @@ class CommitUploadProtocolTest : Spek({
4356 return lastCommit
4457 }
4558
46- given("empty repo") {
59+ given("repo with initial commit and no history") {
60+ repo.commits = listOf()
61+
62+ val mockApi = MockApi (mockRepo = repo)
63+ CommitHasher (localRepo, repo, mockApi, gitHasher).update()
64+
65+ it("send added commits") {
66+ assertEquals(1, mockApi.receivedAddedCommits.size)
67+ }
68+
69+ it("doesn't send deleted commits") {
70+ assertEquals(0, mockApi.receivedDeletedCommits.size)
71+ }
72+ }
73+
74+ given("repo with initial commit") {
4775 repo.commits = listOf(getLastCommit(git))
4876
4977 val mockApi = MockApi (mockRepo = repo)
50- val mockConfigurator = MockConfigurator (mockRepos = mutableListOf(repo))
51- CommitHasher (localRepo, repo, mockApi, mockConfigurator, gitHasher).update()
78+ CommitHasher (localRepo, repo, mockApi, gitHasher).update()
5279
5380 it("doesn't send added commits") {
5481 assertEquals(0, mockApi.receivedAddedCommits.size)
@@ -61,12 +88,12 @@ class CommitUploadProtocolTest : Spek({
6188
6289 given("happy path: added one commit") {
6390 repo.commits = listOf(getLastCommit(git))
91+
6492 val mockApi = MockApi (mockRepo = repo)
65- val mockConfigurator = MockConfigurator (mockRepos = mutableListOf(repo))
6693
6794 val revCommit = git.commit().setMessage("Second commit.").call()
6895 val addedCommit = Commit (revCommit)
69- CommitHasher (localRepo, repo, mockApi, mockConfigurator, gitHasher).update()
96+ CommitHasher (localRepo, repo, mockApi, gitHasher).update()
7097
7198 it("doesn't send deleted commits") {
7299 assertEquals(0, mockApi.receivedDeletedCommits.size)
@@ -83,21 +110,24 @@ class CommitUploadProtocolTest : Spek({
83110
84111 given("happy path: added a few new commits") {
85112 repo.commits = listOf(getLastCommit(git))
113+
86114 val mockApi = MockApi (mockRepo = repo)
87- val mockConfigurator = MockConfigurator (mockRepos = mutableListOf(repo))
88115
89116 val otherAuthorsNames = listOf("a", "b", "a")
90117 val otherAuthorsEmails = listOf("a@a", "b@b", "a@a")
91118 for (i in 0..2) {
92- git.commit().setMessage("Create $i.").setAuthor(otherAuthorsNames.get(i), otherAuthorsEmails.get(i)).call()
119+ git.commit().setMessage("Create $i.")
120+ .setAuthor(otherAuthorsNames.get(i),
121+ otherAuthorsEmails.get(i))
122+ .call()
93123 }
94124 val authorCommits = mutableListOf<Commit >()
95125 for (i in 0..4) {
96126 val message = " Created $i by author."
97127 val revCommit = git.commit().setMessage(message).call()
98128 authorCommits.add(Commit (revCommit))
99129 }
100- CommitHasher (localRepo, repo, mockApi, mockConfigurator, gitHasher).update()
130+ CommitHasher (localRepo, repo, mockApi, gitHasher).update()
101131
102132 it("posts five commits as added") {
103133 assertEquals(5, mockApi.receivedAddedCommits.size)
@@ -108,12 +138,12 @@ class CommitUploadProtocolTest : Spek({
108138 }
109139
110140 it("processes author's commits") {
111- assertEquals(authorCommits.asReversed(), mockApi.receivedAddedCommits)
141+ assertEquals(authorCommits.asReversed(),
142+ mockApi.receivedAddedCommits)
112143 }
113144 }
114145
115146 given("fork event") {
116-
117147 val forkedRepoPath = " ./forked_repo/"
118148 val originalRepoPath = " ./original_repo/"
119149 val forked = Git .cloneRepository()
@@ -138,13 +168,12 @@ class CommitUploadProtocolTest : Spek({
138168 forked.close()
139169 original.repository.close()
140170 original.close()
141-
142171 }
143172
144173 given("lost server") {
145174 repo.commits = listOf(getLastCommit(git))
146- var mockApi = MockApi (mockRepo = repo)
147- var mockConfigurator = MockConfigurator (mockRepos = mutableListOf( repo) )
175+
176+ val mockApi = MockApi (mockRepo = repo)
148177
149178 // Add some commits.
150179 val addedCommits = mutableListOf<Commit >()
@@ -153,14 +182,12 @@ class CommitUploadProtocolTest : Spek({
153182 val revCommit = git.commit().setMessage(message).call()
154183 addedCommits.add(Commit (revCommit))
155184 }
156- CommitHasher (localRepo, repo, mockApi, mockConfigurator, gitHasher).update()
157185
158186 // Remove one commit from server history.
159187 val removedCommit = addedCommits.removeAt(1)
160188 repo.commits = addedCommits.toList().asReversed()
161- mockConfigurator = MockConfigurator (mockRepos = mutableListOf(repo))
162- mockApi = MockApi (mockRepo = repo)
163- CommitHasher (localRepo, repo, mockApi, mockConfigurator, gitHasher).update()
189+
190+ CommitHasher (localRepo, repo, mockApi, gitHasher).update()
164191
165192 it("adds posts one commit as added and received commit is lost one") {
166193 assertEquals(1, mockApi.receivedAddedCommits.size)
@@ -170,7 +197,6 @@ class CommitUploadProtocolTest : Spek({
170197 it("doesn't posts deleted commits") {
171198 assertEquals(0, mockApi.receivedDeletedCommits.size)
172199 }
173-
174200 }
175201
176202 Runtime .getRuntime().exec("src/test/delete_repo.sh").waitFor()
0 commit comments