<commit_before>/*
 * Copyright 2013 the original author or authors.
 *
 * 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 org.gradle.api.internal.tasks.execution

import com.google.common.collect.ImmutableSortedMap
import com.google.common.collect.ImmutableSortedSet
import org.gradle.api.execution.TaskActionListener
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.TaskInternal
import org.gradle.api.internal.TaskOutputsInternal
import org.gradle.api.internal.changedetection.TaskExecutionMode
import org.gradle.api.internal.file.FileOperations
import org.gradle.api.internal.file.TestFiles
import org.gradle.api.internal.project.ProjectInternal
import org.gradle.api.internal.tasks.InputChangesAwareTaskAction
import org.gradle.api.internal.tasks.TaskExecutionContext
import org.gradle.api.internal.tasks.TaskExecutionOutcome
import org.gradle.api.internal.tasks.TaskStateInternal
import org.gradle.api.internal.tasks.properties.TaskProperties
import org.gradle.api.tasks.StopActionException
import org.gradle.api.tasks.StopExecutionException
import org.gradle.api.tasks.TaskExecutionException
import org.gradle.caching.internal.controller.BuildCacheController
import org.gradle.groovy.scripts.ScriptSource
import org.gradle.initialization.DefaultBuildCancellationToken
import org.gradle.internal.event.ListenerManager
import org.gradle.internal.exceptions.DefaultMultiCauseException
import org.gradle.internal.exceptions.MultiCauseException
import org.gradle.internal.execution.DefaultOutputSnapshotter
import org.gradle.internal.execution.OutputChangeListener
import org.gradle.internal.execution.history.AfterPreviousExecutionState
import org.gradle.internal.execution.history.ExecutionHistoryStore
import org.gradle.internal.execution.history.OverlappingOutputDetector
import org.gradle.internal.execution.history.changes.DefaultExecutionStateChangeDetector
import org.gradle.internal.execution.impl.DefaultExecutionEngine
import org.gradle.internal.execution.impl.DefaultInputFingerprinter
import org.gradle.internal.execution.impl.DefaultWorkValidationContext
import org.gradle.internal.execution.steps.AssignWorkspaceStep
import org.gradle.internal.execution.steps.BroadcastChangingOutputsStep
import org.gradle.internal.execution.steps.CancelExecutionStep
import org.gradle.internal.execution.steps.CaptureStateAfterExecutionStep
import org.gradle.internal.execution.steps.CaptureStateBeforeExecutionStep
import org.gradle.internal.execution.steps.ExecuteStep
import org.gradle.internal.execution.steps.IdentifyStep
import org.gradle.internal.execution.steps.IdentityCacheStep
import org.gradle.internal.execution.steps.LoadExecutionStateStep
import org.gradle.internal.execution.steps.RemovePreviousOutputsStep
import org.gradle.internal.execution.steps.ResolveCachingStateStep
import org.gradle.internal.execution.steps.ResolveChangesStep
import org.gradle.internal.execution.steps.ResolveInputChangesStep
import org.gradle.internal.execution.steps.SkipEmptyWorkStep
import org.gradle.internal.execution.steps.SkipUpToDateStep
import org.gradle.internal.execution.steps.ValidateStep
import org.gradle.internal.file.ReservedFileSystemLocationRegistry
import org.gradle.internal.fingerprint.DirectorySensitivity
import org.gradle.internal.fingerprint.FileCollectionFingerprinterRegistry
import org.gradle.internal.fingerprint.impl.AbsolutePathFileCollectionFingerprinter
import org.gradle.internal.fingerprint.impl.DefaultFileCollectionSnapshotter
import org.gradle.internal.hash.ClassLoaderHierarchyHasher
import org.gradle.internal.hash.HashCode
import org.gradle.internal.id.UniqueId
import org.gradle.internal.operations.BuildOperationContext
import org.gradle.internal.operations.BuildOperationExecutor
import org.gradle.internal.operations.RunnableBuildOperation
import org.gradle.internal.operations.TestBuildOperationExecutor
import org.gradle.internal.snapshot.impl.DefaultValueSnapshotter
import org.gradle.internal.snapshot.impl.ImplementationSnapshot
import org.gradle.internal.work.AsyncWorkTracker
import org.gradle.logging.StandardOutputCapture
import spock.lang.Specification

import java.util.function.Supplier

import static java.util.Collections.emptyList
import static org.gradle.internal.work.AsyncWorkTracker.ProjectLockRetention.RELEASE_AND_REACQUIRE_PROJECT_LOCKS
import static org.gradle.internal.work.AsyncWorkTracker.ProjectLockRetention.RELEASE_PROJECT_LOCKS

class ExecuteActionsTaskExecuterTest extends Specification {
    def task = Mock(TaskInternal)
    def taskOutputs = Mock(TaskOutputsInternal)
    def action1 = Mock(InputChangesAwareTaskAction) {
        getActionImplementation(_ as ClassLoaderHierarchyHasher) >> ImplementationSnapshot.of("Action1", HashCode.fromInt(1234))
    }
    def action2 = Mock(InputChangesAwareTaskAction) {
        getActionImplementation(_ as ClassLoaderHierarchyHasher) >> ImplementationSnapshot.of("Action2", HashCode.fromInt(1234))
    }
    def state = new TaskStateInternal()
    def taskProperties = Stub(TaskProperties) {
        getInputPropertyValues() >> { { -> ImmutableSortedMap.of() } as Supplier }
        getInputFileProperties() >> ImmutableSortedSet.of()
        getOutputFileProperties() >> ImmutableSortedSet.of()
    }
    def previousState = Stub(AfterPreviousExecutionState) {
        getInputProperties() >> ImmutableSortedMap.of()
        getInputFileProperties() >> ImmutableSortedMap.of()
        getOutputFilesProducedByWork() >> ImmutableSortedMap.of()
    }
    def validationContext = new DefaultWorkValidationContext()
    def executionContext = Stub(TaskExecutionContext)
    def scriptSource = Mock(ScriptSource)
    def standardOutputCapture = Mock(StandardOutputCapture)
    def buildOperationExecutorForTaskExecution = Mock(BuildOperationExecutor)
    def buildOperationExecutor = new TestBuildOperationExecutor()
    def asyncWorkTracker = Mock(AsyncWorkTracker)

    def virtualFileSystem = TestFiles.virtualFileSystem()
    def fileSystemAccess = TestFiles.fileSystemAccess(virtualFileSystem)
    def fileCollectionSnapshotter = new DefaultFileCollectionSnapshotter(fileSystemAccess, TestFiles.genericFileTreeSnapshotter(), TestFiles.fileSystem())
    def outputSnapshotter = new DefaultOutputSnapshotter(fileCollectionSnapshotter)
    def fingerprinter = new AbsolutePathFileCollectionFingerprinter(DirectorySensitivity.DEFAULT, fileCollectionSnapshotter)
    def fingerprinterRegistry = Stub(FileCollectionFingerprinterRegistry) {
        getFingerprinter(_) >> fingerprinter
    }
    def executionHistoryStore = Mock(ExecutionHistoryStore)
    def buildId = UniqueId.generate()

    def actionListener = Stub(TaskActionListener)
    def outputChangeListener = Stub(OutputChangeListener)
    def cancellationToken = new DefaultBuildCancellationToken()
    def changeDetector = new DefaultExecutionStateChangeDetector()
    def taskCacheabilityResolver = Mock(TaskCacheabilityResolver)
    def buildCacheController = Stub(BuildCacheController)
    def listenerManager = Stub(ListenerManager)
    def classloaderHierarchyHasher = new ClassLoaderHierarchyHasher() {
        @Override
        HashCode getClassLoaderHash(ClassLoader classLoader) {
            return HashCode.fromInt(1234)
        }
    }
    def valueSnapshotter = new DefaultValueSnapshotter(classloaderHierarchyHasher, null)
    def inputFingerprinter = new DefaultInputFingerprinter(valueSnapshotter)
    def reservedFileSystemLocationRegistry = Stub(ReservedFileSystemLocationRegistry)
    def emptySourceTaskSkipper = Stub(EmptySourceTaskSkipper)
    def overlappingOutputDetector = Stub(OverlappingOutputDetector)
    def fileCollectionFactory = TestFiles.fileCollectionFactory()
    def fileOperations = Stub(FileOperations)
    def deleter = TestFiles.deleter()
    def validationWarningReporter = Stub(ValidateStep.ValidationWarningRecorder)

    // @formatter:off
    def executionEngine = new DefaultExecutionEngine(
        new IdentifyStep<>(inputFingerprinter,
        new IdentityCacheStep<>(
        new AssignWorkspaceStep<>(
        new LoadExecutionStateStep<>(
        new SkipEmptyWorkStep<>(
        new ValidateStep<>(virtualFileSystem, validationWarningReporter,
        new CaptureStateBeforeExecutionStep(buildOperationExecutor, classloaderHierarchyHasher, inputFingerprinter, outputSnapshotter, overlappingOutputDetector,
        new ResolveCachingStateStep(buildCacheController, false,
        new ResolveChangesStep<>(changeDetector,
        new SkipUpToDateStep<>(
        new BroadcastChangingOutputsStep<>(outputChangeListener,
        new CaptureStateAfterExecutionStep<>(buildOperationExecutor, buildId, outputSnapshotter,
        new CancelExecutionStep<>(cancellationToken,
        new ResolveInputChangesStep<>(
        new RemovePreviousOutputsStep<>(deleter, outputChangeListener,
        new ExecuteStep<>(buildOperationExecutor
    )))))))))))))))))
    // @formatter:on

    def executer = new ExecuteActionsTaskExecuter(
        ExecuteActionsTaskExecuter.BuildCacheState.DISABLED,
        ExecuteActionsTaskExecuter.ScanPluginState.NOT_APPLIED,
        executionHistoryStore,
        buildOperationExecutorForTaskExecution,
        asyncWorkTracker,
        actionListener,
        taskCacheabilityResolver,
        fingerprinterRegistry,
        classloaderHierarchyHasher,
        executionEngine,
        listenerManager,
        reservedFileSystemLocationRegistry,
        emptySourceTaskSkipper,
        fileCollectionFactory,
        fileOperations
    )

    def setup() {
        ProjectInternal project = Mock(ProjectInternal)
        task.getProject() >> project
        task.getState() >> state
        task.getOutputs() >> taskOutputs
        task.getPath() >> "task"
        taskOutputs.setPreviousOutputFiles(_ as FileCollection)
        project.getBuildScriptSource() >> scriptSource
        task.getStandardOutputCapture() >> standardOutputCapture
        executionContext.getTaskExecutionMode() >> TaskExecutionMode.INCREMENTAL
        executionContext.getTaskProperties() >> taskProperties
        executionContext.getValidationContext() >> validationContext
        executionHistoryStore.load("task") >> Optional.of(previousState)
        taskProperties.getOutputFileProperties() >> ImmutableSortedSet.of()
    }

    void noMoreInteractions() {
        interaction {
            0 * action1._
            0 * action2._
            0 * standardOutputCapture._
        }
    }

    def doesNothingWhenTaskHasNoActions() {
        given:<commit_msg>Update test to use a Mock where interactions are verified<commit_after>/*
 * Copyright 2013 the original author or authors.
 *
 * 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 org.gradle.api.internal.tasks.execution

import com.google.common.collect.ImmutableSortedMap
import com.google.common.collect.ImmutableSortedSet
import org.gradle.api.execution.TaskActionListener
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.TaskInternal
import org.gradle.api.internal.TaskOutputsInternal
import org.gradle.api.internal.changedetection.TaskExecutionMode
import org.gradle.api.internal.file.FileOperations
import org.gradle.api.internal.file.TestFiles
import org.gradle.api.internal.project.ProjectInternal
import org.gradle.api.internal.tasks.InputChangesAwareTaskAction
import org.gradle.api.internal.tasks.TaskExecutionContext
import org.gradle.api.internal.tasks.TaskExecutionOutcome
import org.gradle.api.internal.tasks.TaskStateInternal
import org.gradle.api.internal.tasks.properties.TaskProperties
import org.gradle.api.tasks.StopActionException
import org.gradle.api.tasks.StopExecutionException
import org.gradle.api.tasks.TaskExecutionException
import org.gradle.caching.internal.controller.BuildCacheController
import org.gradle.groovy.scripts.ScriptSource
import org.gradle.initialization.DefaultBuildCancellationToken
import org.gradle.internal.event.ListenerManager
import org.gradle.internal.exceptions.DefaultMultiCauseException
import org.gradle.internal.exceptions.MultiCauseException
import org.gradle.internal.execution.DefaultOutputSnapshotter
import org.gradle.internal.execution.OutputChangeListener
import org.gradle.internal.execution.history.AfterPreviousExecutionState
import org.gradle.internal.execution.history.ExecutionHistoryStore
import org.gradle.internal.execution.history.OverlappingOutputDetector
import org.gradle.internal.execution.history.changes.DefaultExecutionStateChangeDetector
import org.gradle.internal.execution.impl.DefaultExecutionEngine
import org.gradle.internal.execution.impl.DefaultInputFingerprinter
import org.gradle.internal.execution.impl.DefaultWorkValidationContext
import org.gradle.internal.execution.steps.AssignWorkspaceStep
import org.gradle.internal.execution.steps.BroadcastChangingOutputsStep
import org.gradle.internal.execution.steps.CancelExecutionStep
import org.gradle.internal.execution.steps.CaptureStateAfterExecutionStep
import org.gradle.internal.execution.steps.CaptureStateBeforeExecutionStep
import org.gradle.internal.execution.steps.ExecuteStep
import org.gradle.internal.execution.steps.IdentifyStep
import org.gradle.internal.execution.steps.IdentityCacheStep
import org.gradle.internal.execution.steps.LoadExecutionStateStep
import org.gradle.internal.execution.steps.RemovePreviousOutputsStep
import org.gradle.internal.execution.steps.ResolveCachingStateStep
import org.gradle.internal.execution.steps.ResolveChangesStep
import org.gradle.internal.execution.steps.ResolveInputChangesStep
import org.gradle.internal.execution.steps.SkipEmptyWorkStep
import org.gradle.internal.execution.steps.SkipUpToDateStep
import org.gradle.internal.execution.steps.ValidateStep
import org.gradle.internal.file.ReservedFileSystemLocationRegistry
import org.gradle.internal.fingerprint.DirectorySensitivity
import org.gradle.internal.fingerprint.FileCollectionFingerprinterRegistry
import org.gradle.internal.fingerprint.impl.AbsolutePathFileCollectionFingerprinter
import org.gradle.internal.fingerprint.impl.DefaultFileCollectionSnapshotter
import org.gradle.internal.hash.ClassLoaderHierarchyHasher
import org.gradle.internal.hash.HashCode
import org.gradle.internal.id.UniqueId
import org.gradle.internal.operations.BuildOperationContext
import org.gradle.internal.operations.BuildOperationExecutor
import org.gradle.internal.operations.RunnableBuildOperation
import org.gradle.internal.operations.TestBuildOperationExecutor
import org.gradle.internal.snapshot.impl.DefaultValueSnapshotter
import org.gradle.internal.snapshot.impl.ImplementationSnapshot
import org.gradle.internal.work.AsyncWorkTracker
import org.gradle.logging.StandardOutputCapture
import spock.lang.Specification

import java.util.function.Consumer
import java.util.function.Supplier

import static java.util.Collections.emptyList
import static org.gradle.internal.work.AsyncWorkTracker.ProjectLockRetention.RELEASE_AND_REACQUIRE_PROJECT_LOCKS
import static org.gradle.internal.work.AsyncWorkTracker.ProjectLockRetention.RELEASE_PROJECT_LOCKS

class ExecuteActionsTaskExecuterTest extends Specification {
    def task = Mock(TaskInternal)
    def taskOutputs = Mock(TaskOutputsInternal)
    def action1 = Mock(InputChangesAwareTaskAction) {
        getActionImplementation(_ as ClassLoaderHierarchyHasher) >> ImplementationSnapshot.of("Action1", HashCode.fromInt(1234))
    }
    def action2 = Mock(InputChangesAwareTaskAction) {
        getActionImplementation(_ as ClassLoaderHierarchyHasher) >> ImplementationSnapshot.of("Action2", HashCode.fromInt(1234))
    }
    def state = new TaskStateInternal()
    def taskProperties = Stub(TaskProperties) {
        getInputPropertyValues() >> { { -> ImmutableSortedMap.of() } as Supplier }
        getInputFileProperties() >> ImmutableSortedSet.of()
        getOutputFileProperties() >> ImmutableSortedSet.of()
    }
    def previousState = Stub(AfterPreviousExecutionState) {
        getInputProperties() >> ImmutableSortedMap.of()
        getInputFileProperties() >> ImmutableSortedMap.of()
        getOutputFilesProducedByWork() >> ImmutableSortedMap.of()
    }
    def validationContext = new DefaultWorkValidationContext()
    def executionContext = Mock(TaskExecutionContext)
    def scriptSource = Mock(ScriptSource)
    def standardOutputCapture = Mock(StandardOutputCapture)
    def buildOperationExecutorForTaskExecution = Mock(BuildOperationExecutor)
    def buildOperationExecutor = new TestBuildOperationExecutor()
    def asyncWorkTracker = Mock(AsyncWorkTracker)

    def virtualFileSystem = TestFiles.virtualFileSystem()
    def fileSystemAccess = TestFiles.fileSystemAccess(virtualFileSystem)
    def fileCollectionSnapshotter = new DefaultFileCollectionSnapshotter(fileSystemAccess, TestFiles.genericFileTreeSnapshotter(), TestFiles.fileSystem())
    def outputSnapshotter = new DefaultOutputSnapshotter(fileCollectionSnapshotter)
    def fingerprinter = new AbsolutePathFileCollectionFingerprinter(DirectorySensitivity.DEFAULT, fileCollectionSnapshotter)
    def fingerprinterRegistry = Stub(FileCollectionFingerprinterRegistry) {
        getFingerprinter(_) >> fingerprinter
    }
    def executionHistoryStore = Mock(ExecutionHistoryStore)
    def buildId = UniqueId.generate()

    def actionListener = Stub(TaskActionListener)
    def outputChangeListener = Stub(OutputChangeListener)
    def cancellationToken = new DefaultBuildCancellationToken()
    def changeDetector = new DefaultExecutionStateChangeDetector()
    def taskCacheabilityResolver = Mock(TaskCacheabilityResolver)
    def buildCacheController = Stub(BuildCacheController)
    def listenerManager = Stub(ListenerManager)
    def classloaderHierarchyHasher = new ClassLoaderHierarchyHasher() {
        @Override
        HashCode getClassLoaderHash(ClassLoader classLoader) {
            return HashCode.fromInt(1234)
        }
    }
    def valueSnapshotter = new DefaultValueSnapshotter(classloaderHierarchyHasher, null)
    def inputFingerprinter = new DefaultInputFingerprinter(valueSnapshotter)
    def reservedFileSystemLocationRegistry = Stub(ReservedFileSystemLocationRegistry)
    def emptySourceTaskSkipper = Stub(EmptySourceTaskSkipper)
    def overlappingOutputDetector = Stub(OverlappingOutputDetector)
    def fileCollectionFactory = TestFiles.fileCollectionFactory()
    def fileOperations = Stub(FileOperations)
    def deleter = TestFiles.deleter()
    def validationWarningReporter = Stub(ValidateStep.ValidationWarningRecorder)

    // @formatter:off
    def executionEngine = new DefaultExecutionEngine(
        new IdentifyStep<>(inputFingerprinter,
        new IdentityCacheStep<>(
        new AssignWorkspaceStep<>(
        new LoadExecutionStateStep<>(
        new SkipEmptyWorkStep<>(
        new ValidateStep<>(virtualFileSystem, validationWarningReporter,
        new CaptureStateBeforeExecutionStep(buildOperationExecutor, classloaderHierarchyHasher, inputFingerprinter, outputSnapshotter, overlappingOutputDetector,
        new ResolveCachingStateStep(buildCacheController, false,
        new ResolveChangesStep<>(changeDetector,
        new SkipUpToDateStep<>(
        new BroadcastChangingOutputsStep<>(outputChangeListener,
        new CaptureStateAfterExecutionStep<>(buildOperationExecutor, buildId, outputSnapshotter,
        new CancelExecutionStep<>(cancellationToken,
        new ResolveInputChangesStep<>(
        new RemovePreviousOutputsStep<>(deleter, outputChangeListener,
        new ExecuteStep<>(buildOperationExecutor
    )))))))))))))))))
    // @formatter:on

    def executer = new ExecuteActionsTaskExecuter(
        ExecuteActionsTaskExecuter.BuildCacheState.DISABLED,
        ExecuteActionsTaskExecuter.ScanPluginState.NOT_APPLIED,
        executionHistoryStore,
        buildOperationExecutorForTaskExecution,
        asyncWorkTracker,
        actionListener,
        taskCacheabilityResolver,
        fingerprinterRegistry,
        classloaderHierarchyHasher,
        executionEngine,
        listenerManager,
        reservedFileSystemLocationRegistry,
        emptySourceTaskSkipper,
        fileCollectionFactory,
        fileOperations
    )

    def setup() {
        ProjectInternal project = Mock(ProjectInternal)
        task.getProject() >> project
        task.getState() >> state
        task.getOutputs() >> taskOutputs
        task.getPath() >> "task"
        taskOutputs.setPreviousOutputFiles(_ as FileCollection)
        project.getBuildScriptSource() >> scriptSource
        task.getStandardOutputCapture() >> standardOutputCapture
        executionContext.getTaskExecutionMode() >> TaskExecutionMode.INCREMENTAL
        executionContext.getTaskProperties() >> taskProperties
        executionContext.getValidationContext() >> validationContext
        executionContext.getValidationAction() >> { { c -> } as Consumer }
        executionHistoryStore.load("task") >> Optional.of(previousState)
        taskProperties.getOutputFileProperties() >> ImmutableSortedSet.of()
    }

    void noMoreInteractions() {
        interaction {
            0 * action1._
            0 * action2._
            0 * executionContext._
            0 * standardOutputCapture._
        }
    }

    def doesNothingWhenTaskHasNoActions() {
        given:
{'fraction_non_alphanumeric': 0.07277301109563811, 'fraction_numerical': 0.002650375095458425, 'mean_word_length': 5.185607113086968, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 116, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4639137', 'n_tokens_mistral': 5764, 'n_tokens_neox': 5564, 'n_words': 1030}