Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;

public class SubtractingFileCollection extends AbstractOpaqueFileCollection {
Expand Down Expand Up @@ -58,8 +59,31 @@ protected Set<File> getIntrinsicFiles() {
return files;
}

@Override
public Optional<FileCollectionExecutionTimeValue> calculateExecutionTimeValue() {
return left.calculateExecutionTimeValue()
.flatMap(leftEtv -> ((FileCollectionInternal) right).calculateExecutionTimeValue()
.map(rightEtv -> new SubtractingExecutionTimeValue(leftEtv, rightEtv)));
}

@Override
public boolean contains(File file) {
return left.contains(file) && !right.contains(file);
}

private static class SubtractingExecutionTimeValue implements FileCollectionExecutionTimeValue {
private final FileCollectionExecutionTimeValue left;
private final FileCollectionExecutionTimeValue right;

private SubtractingExecutionTimeValue(FileCollectionExecutionTimeValue left, FileCollectionExecutionTimeValue right) {
this.left = left;
this.right = right;
}

@Override
public FileCollectionInternal toFileCollection(FileCollectionFactory fileCollectionFactory) {
return (FileCollectionInternal) left.toFileCollection(fileCollectionFactory)
.minus(right.toFileCollection(fileCollectionFactory));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2025 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.file


import spock.lang.Specification

import static org.gradle.api.internal.file.TestFiles.fileCollectionFactory

class SubtractingFileCollectionExecutionTimeValueTest extends Specification {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💅 The test class seems to be more about the SubtractingFileCollection. Testing execution time value behavior is only one aspect. I'd rename the test class to accommodate for future unit tests for the collection itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a bunch of tests similarly named like this (by me). They are targeting only ETV intentionally, since further ETV implementations will not so simple, and perhaps will require more extensive coverage.

def "SubtractingFileCollection supports execution time value"() {
given:
def left = fileCollectionFactory().fixed(new File("foo.txt"), new File("bar.txt"))
def right = fileCollectionFactory().fixed(new File("bar.txt"))
def collection = new SubtractingFileCollection(left, right)

when:
def executionTimeValue = collection.calculateExecutionTimeValue()

then:
executionTimeValue.present

and:
def newCollection = executionTimeValue.get().toFileCollection(fileCollectionFactory())

then:
newCollection.files == [new File("foo.txt")] as Set
}

def "SubtractingFileCollection supports absence of execution time value"() {
given:
def collection = new SubtractingFileCollection(left, right)

when:
def executionTimeValue = collection.calculateExecutionTimeValue()

then:
executionTimeValue.present == isPresent

where:
left | right | isPresent
new EmptyExecutionTimeValueFileCollection() | fileCollectionFactory().fixed(new File("foo.txt")) | false
fileCollectionFactory().fixed(new File("foo.txt")) | new EmptyExecutionTimeValueFileCollection() | false
new EmptyExecutionTimeValueFileCollection() | new EmptyExecutionTimeValueFileCollection() | false
fileCollectionFactory().fixed(new File("foo.txt")) | fileCollectionFactory().fixed(new File("bar.txt")) | true
}
}

Loading