File tree Expand file tree Collapse file tree 4 files changed +24
-15
lines changed
main/kotlin/com/gabrielfeo/gradle/enterprise/api
test/kotlin/com/gabrielfeo/gradle/enterprise/api Expand file tree Collapse file tree 4 files changed +24
-15
lines changed Original file line number Diff line number Diff line change @@ -35,22 +35,22 @@ class Options internal constructor(
3535 ) {
3636
3737 /* *
38- * Provides the URL of a Gradle Enterprise API instance (without `/api`) . By default, uses
39- * environment variable `GRADLE_ENTERPRISE_URL `.
38+ * Provides the URL of a Gradle Enterprise API instance REST API . By default, uses
39+ * environment variable `GRADLE_ENTERPRISE_API_URL`. Must end with `/api/ `.
4040 */
4141 var url: () -> String = {
42- env[" GRADLE_ENTERPRISE_URL " ]
43- ? : error(" GE instance URL is required" )
42+ env[" GRADLE_ENTERPRISE_API_URL " ]
43+ ? : error(" GRADLE_ENTERPRISE_API_URL is required" )
4444 }
4545
4646 /* *
4747 * Provides the access token for a Gradle Enterprise API instance. By default, uses keychain entry
48- * `gradle-enterprise-api-token` or environment variable `GRADLE_ENTERPRISE_URL `.
48+ * `gradle-enterprise-api-token` or environment variable `GRADLE_ENTERPRISE_API_TOKEN `.
4949 */
5050 var token: () -> String = {
5151 keychain[" gradle-enterprise-api-token" ]
5252 ? : env[" GRADLE_ENTERPRISE_API_TOKEN" ]
53- ? : error(" GE token is required" )
53+ ? : error(" GRADLE_ENTERPRISE_API_TOKEN is required" )
5454 }
5555 }
5656
Original file line number Diff line number Diff line change @@ -22,9 +22,10 @@ internal fun buildRetrofit(
2222 client : OkHttpClient ,
2323 moshi : Moshi ,
2424) = with (Retrofit .Builder ()) {
25- val url = options.gradleEnterpriseInstance.url()
26- check(" /api" !in url) { " Instance URL must be the plain instance URL, without /api" }
27- baseUrl(url)
25+ val apiUrl = options.gradleEnterpriseInstance.url()
26+ check(" /api/" in apiUrl) { " A valid API URL must end in /api/" }
27+ val instanceUrl = apiUrl.substringBefore(" api/" )
28+ baseUrl(instanceUrl)
2829 addConverterFactory(ScalarsConverterFactory .create())
2930 addConverterFactory(MoshiConverterFactory .create(moshi))
3031 client(client)
Original file line number Diff line number Diff line change @@ -12,13 +12,22 @@ import kotlin.test.assertTrue
1212class OptionsTest {
1313
1414 @Test
15- fun `URL from env is required ` () {
15+ fun `Given no URL set in env, url() fails ` () {
1616 val options = Options (FakeEnv (), FakeKeychain ())
1717 assertFails {
1818 options.gradleEnterpriseInstance.url()
1919 }
2020 }
2121
22+ @Test
23+ fun `Given URL set in env, url() returns env URL` () {
24+ val options = Options (
25+ FakeEnv (" GRADLE_ENTERPRISE_API_URL" to " https://example.com/api/" ),
26+ FakeKeychain (),
27+ )
28+ assertEquals(" https://example.com/api/" , options.gradleEnterpriseInstance.url())
29+ }
30+
2231 @Test
2332 fun `Token from keychain is preferred` () {
2433 val options = Options (
Original file line number Diff line number Diff line change @@ -15,18 +15,19 @@ import kotlin.test.*
1515class RetrofitTest {
1616
1717 @Test
18- fun `Sets URL from options` () {
18+ fun `Sets instance URL from options, stripping api segment ` () {
1919 val retrofit = buildRetrofit(
20- " GRADLE_ENTERPRISE_URL " to " https://example.com/" ,
20+ " GRADLE_ENTERPRISE_API_URL " to " https://example.com/api /" ,
2121 )
22+ // That's what generated classes expect
2223 assertEquals(" https://example.com/" , retrofit.baseUrl().toString())
2324 }
2425
2526 @Test
2627 fun `Rejects invalid URL` () {
2728 assertFails {
2829 buildRetrofit(
29- " GRADLE_ENTERPRISE_URL " to " https://example.com/api /" ,
30+ " GRADLE_ENTERPRISE_API_URL " to " https://example.com/" ,
3031 )
3132 }
3233 }
@@ -37,8 +38,6 @@ class RetrofitTest {
3738 val env = FakeEnv (* envVars)
3839 if (" GRADLE_ENTERPRISE_API_TOKEN" !in env)
3940 env[" GRADLE_ENTERPRISE_API_TOKEN" ] = " example-token"
40- if (" GRADLE_ENTERPRISE_URL" !in env)
41- env[" GRADLE_ENTERPRISE_URL" ] = " example-url"
4241 val options = Options (env, FakeKeychain ())
4342 return buildRetrofit(
4443 options = options,
You can’t perform that action at this time.
0 commit comments