|
| 1 | +package com.tinder.gitquery.core |
| 2 | + |
| 3 | +import com.tinder.gitquery.core.config.GitQueryConfig |
| 4 | +import com.tinder.gitquery.core.config.GitQueryConfigSchema |
| 5 | +import com.tinder.gitquery.core.config.GitQueryInitConfig |
| 6 | +import org.junit.After |
| 7 | +import org.junit.Before |
| 8 | +import org.junit.Rule |
| 9 | +import org.junit.Test |
| 10 | +import org.junit.rules.TemporaryFolder |
| 11 | +import java.io.File |
| 12 | + |
| 13 | +class GitQueryInitTest { |
| 14 | + @get:Rule |
| 15 | + val testProjectDir = TemporaryFolder() |
| 16 | + |
| 17 | + @Before |
| 18 | + fun setup() { |
| 19 | + testProjectDir.create() |
| 20 | + testProjectDir.apply { |
| 21 | + newFolder("gitquery-output") |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + @After |
| 26 | + fun tearDown() { |
| 27 | + testProjectDir.delete() |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun `initConfig - given missing remote, should fail`() { |
| 32 | + // When |
| 33 | + val actualError = kotlin.runCatching { |
| 34 | + GitQueryInit.initConfig( |
| 35 | + configFile = "${testProjectDir.root}/gitquery.yml", |
| 36 | + config = GitQueryConfig( |
| 37 | + schema = GitQueryConfigSchema(version = "1"), |
| 38 | + branch = "master", |
| 39 | + files = mapOf( |
| 40 | + "definitions" to mapOf("user.proto" to "42933446d0321958e8c12216d04b9f0c382ebf1b") |
| 41 | + ), |
| 42 | + repoDir = "${testProjectDir.root}/tmp/remote", |
| 43 | + outputDir = "${testProjectDir.root}/gitquery-output" |
| 44 | + ), |
| 45 | + buildDir = testProjectDir.root.path |
| 46 | + ) |
| 47 | + }.exceptionOrNull() |
| 48 | + |
| 49 | + // Then |
| 50 | + require(actualError is IllegalArgumentException) |
| 51 | + assert(actualError.message == "Parameter remote may not be a blank string ()") |
| 52 | + |
| 53 | + assert(!File("${testProjectDir.root}/gitquery-output/definitions/user.proto").exists()) |
| 54 | + assert(!File("${testProjectDir.root}/gitquery-output/README.md").exists()) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun `initConfig - given missing branch, should fail`() { |
| 59 | + // When |
| 60 | + val actualError = kotlin.runCatching { |
| 61 | + GitQueryInit.initConfig( |
| 62 | + configFile = "${testProjectDir.root}/gitquery.yml", |
| 63 | + config = GitQueryConfig( |
| 64 | + schema = GitQueryConfigSchema(version = "1"), |
| 65 | + remote = "https://github.com/aminghadersohi/ProtoExample.git", |
| 66 | + branch = "", |
| 67 | + repoDir = "${testProjectDir.root}/tmp/remote", |
| 68 | + outputDir = "${testProjectDir.root}/gitquery-output" |
| 69 | + ), |
| 70 | + buildDir = testProjectDir.root.path |
| 71 | + ) |
| 72 | + }.exceptionOrNull() |
| 73 | + |
| 74 | + // Then |
| 75 | + require(actualError is IllegalArgumentException) |
| 76 | + assert(actualError.message == "Parameter branch may not be a blank string ()") |
| 77 | + |
| 78 | + assert(!File("${testProjectDir.root}/gitquery-output/definitions/user.proto").exists()) |
| 79 | + assert(!File("${testProjectDir.root}/gitquery-output/README.md").exists()) |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun `initConfig - given empty includeGlobs, should fail`() { |
| 84 | + // When |
| 85 | + val actualError = kotlin.runCatching { |
| 86 | + GitQueryInit.initConfig( |
| 87 | + configFile = "${testProjectDir.root}/gitquery.yml", |
| 88 | + config = GitQueryConfig( |
| 89 | + schema = GitQueryConfigSchema(version = "1"), |
| 90 | + remote = "https://github.com/aminghadersohi/ProtoExample.git", |
| 91 | + branch = "master", |
| 92 | + repoDir = "${testProjectDir.root}/tmp/remote", |
| 93 | + outputDir = "${testProjectDir.root}/gitquery-output" |
| 94 | + ), |
| 95 | + buildDir = testProjectDir.root.path |
| 96 | + ) |
| 97 | + }.exceptionOrNull() |
| 98 | + |
| 99 | + // Then |
| 100 | + require(actualError is IllegalArgumentException) |
| 101 | + assert(actualError.message == "Failed to init config: includeGlobs is empty") |
| 102 | + |
| 103 | + assert(!File("${testProjectDir.root}/gitquery-output/definitions/user.proto").exists()) |
| 104 | + assert(!File("${testProjectDir.root}/gitquery-output/README.md").exists()) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun `initConfig - given non-existent file, should create file`() { |
| 109 | + // When |
| 110 | + GitQueryInit.initConfig( |
| 111 | + configFile = "${testProjectDir.root}/gitquery.yml", |
| 112 | + config = GitQueryConfig( |
| 113 | + schema = GitQueryConfigSchema(version = "1"), |
| 114 | + remote = "https://github.com/aminghadersohi/ProtoExample.git", |
| 115 | + branch = "master", |
| 116 | + repoDir = "${testProjectDir.root}/tmp/remote", |
| 117 | + outputDir = "${testProjectDir.root}/gitquery-output", |
| 118 | + initConfig = GitQueryInitConfig( |
| 119 | + includeGlobs = listOf("**/*.proto") |
| 120 | + ) |
| 121 | + ), |
| 122 | + buildDir = testProjectDir.root.path |
| 123 | + ) |
| 124 | + |
| 125 | + assert(File("${testProjectDir.root}/gitquery.yml").exists()) |
| 126 | + } |
| 127 | +} |
0 commit comments