diff --git a/src/main/groovy/nebula/plugin/dependencylock/utils/CoreLockingHelper.groovy b/src/main/groovy/nebula/plugin/dependencylock/utils/CoreLockingHelper.groovy index af1428a4..e03e3bf1 100644 --- a/src/main/groovy/nebula/plugin/dependencylock/utils/CoreLockingHelper.groovy +++ b/src/main/groovy/nebula/plugin/dependencylock/utils/CoreLockingHelper.groovy @@ -42,6 +42,7 @@ class CoreLockingHelper { boolean migrationTaskWasRequested = project.gradle.startParameter.taskNames.contains(DependencyLockTaskConfigurer.MIGRATE_TO_CORE_LOCKS_TASK_NAME) boolean hasWriteLocksFlag = project.gradle.startParameter.isWriteDependencyLocks() + boolean projectIsOffline = project.gradle.startParameter.isOffline() boolean hasDependenciesToUpdate = !project.gradle.startParameter.getLockedDependenciesToUpdate().isEmpty() boolean isUpdatingDependencies = hasWriteLocksFlag || hasDependenciesToUpdate @@ -186,4 +187,10 @@ class CoreLockingHelper { }) } } + + void notifyWhenUsingOfflineMode() { + if (projectIsOffline) { + LOGGER.lifecycle("${project.name}: offline mode enabled. Using cached dependencies") + } + } } diff --git a/src/main/kotlin/nebula/plugin/dependencylock/DependencyLockPlugin.kt b/src/main/kotlin/nebula/plugin/dependencylock/DependencyLockPlugin.kt index 2b64de5a..80d60cd2 100644 --- a/src/main/kotlin/nebula/plugin/dependencylock/DependencyLockPlugin.kt +++ b/src/main/kotlin/nebula/plugin/dependencylock/DependencyLockPlugin.kt @@ -85,6 +85,7 @@ class DependencyLockPlugin : Plugin { DependencyResolutionVerifier.verifySuccessfulResolution(project) } coreLockingHelper.disableCachingWhenUpdatingDependencies() + coreLockingHelper.notifyWhenUsingOfflineMode() val lockFile = File(project.projectDir, extension.lockFile) diff --git a/src/test/groovy/nebula/plugin/dependencylock/caching/OfflineModeSpec.groovy b/src/test/groovy/nebula/plugin/dependencylock/caching/OfflineModeSpec.groovy new file mode 100644 index 00000000..21d1e80c --- /dev/null +++ b/src/test/groovy/nebula/plugin/dependencylock/caching/OfflineModeSpec.groovy @@ -0,0 +1,67 @@ +/** + * + * Copyright 2019 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package nebula.plugin.dependencylock.caching + +import com.github.tomakehurst.wiremock.client.WireMock +import com.github.tomakehurst.wiremock.stubbing.ServeEvent + +class OfflineModeSpec extends AbstractCachingAndCoreLockingSpec { + private static final String OFFLINE_MODE_NOTIFICATION = 'offline mode enabled. Using cached dependencies' + + def setup() { + buildFile << """ + dependencies { + implementation 'test.offlineMode:z-$uniqueId:latest.release' + } + """.stripIndent() + } + + def 'offline mode with core locking uses cached dependencies'() { + given: + setupBaseDependencyAndMockedResponses(uniqueId, "offlineMode") + + when: + def result = runTasks('dependencies', '--write-locks', '--configuration', 'compileClasspath') + + then: + result.output.contains("test.offlineMode:z-$uniqueId:latest.release -> 1.0.0") + result.output.contains("\\--- test.nebula:a-$uniqueId:1.0.0") + !result.output.contains(OFFLINE_MODE_NOTIFICATION) + + def lockFile = new File(projectDir, 'gradle/dependency-locks/compileClasspath.lockfile') + lockFile.exists() + + List allServeEvents = WireMock.getAllServeEvents() + WireMock.verify(WireMock.exactly(1), WireMock.getRequestedFor(WireMock.urlEqualTo("/${parseGroup('test.offlineMode')}/z-${uniqueId}/maven-metadata.xml"))) + WireMock.verify(WireMock.exactly(1), WireMock.getRequestedFor(WireMock.urlEqualTo('/' + filePathFor('test.offlineMode', "z-$uniqueId", '1.0.0', 'pom')))) + WireMock.verify(WireMock.exactly(1), WireMock.getRequestedFor(WireMock.urlEqualTo('/' + filePathFor('test.nebula', "a-$uniqueId", '1.0.0', 'pom')))) + assert allServeEvents.size() == 3 + + when: + def offlineResults = runTasks('dependencies', '--configuration', 'compileClasspath', '--offline') + + then: + offlineResults.output.contains("test.offlineMode:z-$uniqueId:latest.release -> 1.0.0") + offlineResults.output.contains("\\--- test.nebula:a-$uniqueId:1.0.0") + offlineResults.output.contains(OFFLINE_MODE_NOTIFICATION) + + List allServeEventsUpdated = WireMock.getAllServeEvents() + assert allServeEventsUpdated.size() == 3 // there are no new events because offline mode used the cache + } +}