|
| 1 | +package com.lordcodes.turtle |
| 2 | + |
| 3 | +import org.assertj.core.api.Assertions.assertThat |
| 4 | +import org.junit.jupiter.api.Test |
| 5 | +import org.junit.jupiter.api.assertThrows |
| 6 | +import org.junit.jupiter.api.io.TempDir |
| 7 | +import java.io.File |
| 8 | + |
| 9 | +internal class GitCommandsTest { |
| 10 | + // push - create remote that is a local repo and push to it, check changes in other repo |
| 11 | + // pull - pull changes from the local remote |
| 12 | + // clone - clone from local remote |
| 13 | + // pushTag - push to local remote and check it is there in the remote repo |
| 14 | + |
| 15 | + @TempDir |
| 16 | + lateinit var temporaryFolder: File |
| 17 | + |
| 18 | + private val shell by lazy { ShellScript(temporaryFolder) } |
| 19 | + private val git by lazy { GitCommands(shell) } |
| 20 | + |
| 21 | + @Test |
| 22 | + fun gitInit() { |
| 23 | + git.gitInit() |
| 24 | + |
| 25 | + assertThat(File(temporaryFolder, ".git")).isDirectory() |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + fun status() { |
| 30 | + initUsableRepository() |
| 31 | + val newFile = File(temporaryFolder, "testFile.txt") |
| 32 | + newFile.createNewFile() |
| 33 | + |
| 34 | + val output = git.status() |
| 35 | + |
| 36 | + assertThat(output).isEqualTo("?? ${newFile.name}") |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun addAll() { |
| 41 | + initUsableRepository() |
| 42 | + val modifiedFile = File(temporaryFolder, "testFile.txt") |
| 43 | + modifiedFile.createNewFile() |
| 44 | + shell.command("git", listOf("add", "testFile.txt")) |
| 45 | + shell.command("git", listOf("commit", "-a", "-m", "Add testFile", "--quiet")) |
| 46 | + modifiedFile.writeText("changes") |
| 47 | + val newFile = File(temporaryFolder, "anotherFile.txt") |
| 48 | + newFile.createNewFile() |
| 49 | + |
| 50 | + git.addAll() |
| 51 | + |
| 52 | + val status = git.status() |
| 53 | + assertThat(status).isEqualTo( |
| 54 | + """ |
| 55 | + A ${newFile.name} |
| 56 | + M ${modifiedFile.name} |
| 57 | + """.trimIndent() |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun commit() { |
| 63 | + initUsableRepository() |
| 64 | + val newFile = File(temporaryFolder, "testFile.txt") |
| 65 | + newFile.createNewFile() |
| 66 | + git.addAll() |
| 67 | + |
| 68 | + git.commit("Add testFile") |
| 69 | + |
| 70 | + val message = shell.command("git", listOf("log", "-1", "--pretty=%B")) |
| 71 | + assertThat(message).isEqualTo("Add testFile") |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun commit_includesChanges_doesNotIncludeNewFiles() { |
| 76 | + initUsableRepository() |
| 77 | + val modifiedFile = File(temporaryFolder, "testFile.txt") |
| 78 | + modifiedFile.createNewFile() |
| 79 | + git.addAll() |
| 80 | + git.commit("Add testFile") |
| 81 | + modifiedFile.writeText("changes") |
| 82 | + val newFile = File(temporaryFolder, "anotherFile.txt") |
| 83 | + newFile.createNewFile() |
| 84 | + |
| 85 | + git.commit("Change testFile") |
| 86 | + |
| 87 | + val lastCommit = shell.command("git", listOf("show")) |
| 88 | + assertThat(lastCommit).contains("Change testFile") |
| 89 | + assertThat(lastCommit).contains("testFile.txt") |
| 90 | + assertThat(lastCommit).doesNotContain("anotherFile.txt") |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun commitAllChanges_includesChangesAndNewFiles() { |
| 95 | + initUsableRepository() |
| 96 | + val modifiedFile = File(temporaryFolder, "testFile.txt") |
| 97 | + modifiedFile.createNewFile() |
| 98 | + git.addAll() |
| 99 | + git.commit("Add testFile") |
| 100 | + modifiedFile.writeText("changes") |
| 101 | + val newFile = File(temporaryFolder, "anotherFile.txt") |
| 102 | + newFile.createNewFile() |
| 103 | + |
| 104 | + git.commitAllChanges("Change testFile") |
| 105 | + |
| 106 | + val lastCommit = shell.command("git", listOf("show")) |
| 107 | + assertThat(lastCommit).contains("Change testFile") |
| 108 | + assertThat(lastCommit).contains("testFile.txt") |
| 109 | + assertThat(lastCommit).contains("anotherFile.txt") |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + fun checkout_createIfNecessary() { |
| 114 | + initUsableRepository() |
| 115 | + |
| 116 | + git.checkout("newBranch") |
| 117 | + |
| 118 | + assertThat(git.currentBranch()).isEqualTo("newBranch") |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun checkout_doNotCreateIfNecessary_givenBranchDoesNotExist() { |
| 123 | + initUsableRepository() |
| 124 | + |
| 125 | + val exception = assertThrows<ShellRunException> { |
| 126 | + git.checkout("newBranch", createIfNecessary = false) |
| 127 | + } |
| 128 | + |
| 129 | + assertThat(exception.message).contains("pathspec 'newBranch' did not match") |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + fun checkout_doNotCreateIfNecessary_givenBranchExists() { |
| 134 | + initUsableRepository() |
| 135 | + shell.command("git", listOf("branch", "newBranch")) |
| 136 | + |
| 137 | + git.checkout("newBranch", createIfNecessary = false) |
| 138 | + |
| 139 | + assertThat(git.currentBranch()).isEqualTo("newBranch") |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + fun addTag() { |
| 144 | + initUsableRepository() |
| 145 | + |
| 146 | + git.addTag(tagName = "v1.1.0", message = "Release v1.1.0") |
| 147 | + |
| 148 | + val lastTag = shell.command("git", listOf("describe", "--tags", "--abbrev=0")) |
| 149 | + assertThat(lastTag).isEqualTo("v1.1.0") |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + fun currentBranch() { |
| 154 | + initUsableRepository() |
| 155 | + git.checkout("newBranch") |
| 156 | + |
| 157 | + val currentBranch = git.currentBranch() |
| 158 | + |
| 159 | + assertThat(currentBranch).isEqualTo("newBranch") |
| 160 | + assertThat(currentBranch).isEqualTo(shell.command("git", listOf("rev-parse", "--abbrev-ref", "HEAD"))) |
| 161 | + } |
| 162 | + |
| 163 | + private fun initUsableRepository() { |
| 164 | + git.gitInit() |
| 165 | + shell.command("git", listOf("commit", "--allow-empty", "-n", "-m", "Initial commit", "--quiet")) |
| 166 | + } |
| 167 | +} |
0 commit comments