Skip to content

Commit

Permalink
Handle mutliple files when copying and deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
lptr committed Sep 3, 2024
1 parent fcc1ecc commit 43c4016
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 17 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ A scenario can define changes that should be applied to the source before each b
- `clear-jars-cache-before`: Deletes the contents of the instrumented jars cache before the scenario is executed (`SCENARIO`), before cleanup (`CLEANUP`) or before the build is executed (`BUILD`).
- `clear-project-cache-before`: Deletes the contents of the `.gradle` and `buildSrc/.gradle` project cache directories before the scenario is executed (`SCENARIO`), before cleanup (`CLEANUP`) or before the build is executed (`BUILD`).
- `clear-transform-cache-before`: Deletes the contents of the transform cache before the scenario is executed (`SCENARIO`), before cleanup (`CLEANUP`) or before the build is executed (`BUILD`).
- `copy-file`: Copies a file or a directory from one location to another. Has to specify a `source` and a `target` path; relative paths are resolved against the project directory.
- `delete-file`: Deletes a file or a directory. Has to specify a `target` path; when relative it is resolved against the project directory.
- `copy-file`: Copies a file or a directory from one location to another. Has to specify a `source` and a `target` path; relative paths are resolved against the project directory. Can take an array of operations.
- `delete-file`: Deletes a file or a directory. Has to specify a `target` path; when relative it is resolved against the project directory. Can take an array of operations.
- `git-checkout`: Checks out a specific commit for the build step, and a different one for the cleanup step.
- `git-revert`: Reverts a given set of commits before the build and resets it afterward.
- `iterations`: Number of builds to actually measure
Expand Down Expand Up @@ -346,10 +346,15 @@ They can be added to a scenario file like this:
clear-build-cache-before = SCENARIO
clear-transform-cache-before = BUILD
show-build-cache-size = true
copy-file {
copy-file = {
source = "../develocity.xml"
target = ".mvn/develocity.xml"
}
delete-file = [{
target = ".mvn/develocity.xml"
}, {
target = ".gradle"
}]
git-checkout = {
cleanup = "efb43a1"
build = "master"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class BuildToolCommandLineInvoker<T extends BuildToolCommandLine
protected void doRun(T scenario, InvocationSettings settings, Consumer<BuildInvocationResult> resultConsumer, List<String> commandLine) {
ScenarioContext scenarioContext = ScenarioContext.from(settings, scenario);

BuildMutator mutator = new CompositeBuildMutator(scenario.getBuildMutators());
BuildMutator mutator = CompositeBuildMutator.from(scenario.getBuildMutators());
mutator.beforeScenario(scenarioContext);
try {
for (int iteration = 1; iteration <= scenario.getWarmUpCount(); iteration++) {
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/org/gradle/profiler/CompositeBuildMutator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.gradle.profiler;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import java.util.List;
Expand All @@ -8,11 +9,22 @@
public class CompositeBuildMutator implements BuildMutator {
private final List<BuildMutator> mutators;

public CompositeBuildMutator(List<BuildMutator> mutators) {
this.mutators = mutators;
private CompositeBuildMutator(List<BuildMutator> mutators) {
this.mutators = ImmutableList.copyOf(mutators);
}

@Override
public static BuildMutator from(List<BuildMutator> mutators) {
switch (mutators.size()) {
case 0:
return BuildMutator.NOOP;
case 1:
return mutators.get(0);
default:
return new CompositeBuildMutator(mutators);
}
}

@Override
public void beforeScenario(ScenarioContext context) {
for (BuildMutator mutator : mutators) {
mutator.beforeScenario(context);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/gradle/profiler/ConfigUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gradle.profiler;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigException.WrongType;
import com.typesafe.config.ConfigValue;

import java.util.Collections;
Expand All @@ -23,6 +24,18 @@ public static Map<String, String> map(Config config, String key, Map<String, Str
}
}

public static List<? extends Config> configs(Config config, String key) {
if (config.hasPath(key)) {
try {
return config.getConfigList(key);
} catch (WrongType e) {
return Collections.singletonList(config.getConfig(key));
}
} else {
return Collections.emptyList();
}
}

public static Integer optionalInteger(Config config, String key) {
if (config.hasPath(key)) {
return Integer.valueOf(config.getString(key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void run(GradleScenarioDefinition scenario, InvocationSettings settings,

daemonControl.stop(buildConfiguration);

BuildMutator mutator = new CompositeBuildMutator(scenario.getBuildMutators());
BuildMutator mutator = CompositeBuildMutator.from(scenario.getBuildMutators());
ScenarioContext scenarioContext = ScenarioContext.from(settings, scenario);
GradleClient gradleClient = scenario.getInvoker().getClient().create(buildConfiguration, settings);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public BuildMutator configure(String key, BuildMutatorConfiguratorSpec spec) {
})
.collect(Collectors.toList());

return new CompositeBuildMutator(mutatorsForKey);
return CompositeBuildMutator.from(mutatorsForKey);
}

private ProjectCombinations getProjectCombinations(BuildMutatorConfiguratorSpec spec, int numberOfProjects, int appliedProjectDependencies) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/gradle/profiler/mutations/CopyFileMutator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import com.google.common.base.Strings;
import com.google.common.io.Files;
import com.typesafe.config.Config;
import org.gradle.profiler.BuildMutator;
import org.gradle.profiler.CompositeBuildMutator;
import org.gradle.profiler.ConfigUtil;
import org.gradle.profiler.ScenarioContext;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.stream.Collectors;

public class CopyFileMutator extends AbstractFileSystemMutator {

Expand Down Expand Up @@ -42,8 +45,13 @@ public String toString() {
public static class Configurator implements BuildMutatorConfigurator {

@Override
public CopyFileMutator configure(String key, BuildMutatorConfiguratorSpec spec) {
Config config = spec.getScenario().getConfig(key);
public BuildMutator configure(String key, BuildMutatorConfiguratorSpec spec) {
return CompositeBuildMutator.from(ConfigUtil.configs(spec.getScenario(), key)
.stream().map(config -> createMutator(spec, config))
.collect(Collectors.toList()));
}

private static CopyFileMutator createMutator(BuildMutatorConfiguratorSpec spec, Config config) {
String source = ConfigUtil.string(config, "source", null);
String target = ConfigUtil.string(config, "target", null);

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/gradle/profiler/mutations/DeleteFileMutator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.typesafe.config.Config;
import org.apache.commons.io.FileUtils;
import org.gradle.profiler.BuildMutator;
import org.gradle.profiler.CompositeBuildMutator;
import org.gradle.profiler.ConfigUtil;
import org.gradle.profiler.ScenarioContext;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.stream.Collectors;

public class DeleteFileMutator extends AbstractFileSystemMutator {

Expand All @@ -32,8 +35,13 @@ public void beforeScenario(ScenarioContext context) {
public static class Configurator implements BuildMutatorConfigurator {

@Override
public DeleteFileMutator configure(String key, BuildMutatorConfiguratorSpec spec) {
Config config = spec.getScenario().getConfig(key);
public BuildMutator configure(String key, BuildMutatorConfiguratorSpec spec) {
return CompositeBuildMutator.from(ConfigUtil.configs(spec.getScenario(), key)
.stream().map(config -> createMutator(spec, config))
.collect(Collectors.toList()));
}

private static DeleteFileMutator createMutator(BuildMutatorConfiguratorSpec spec, Config config) {
String target = ConfigUtil.string(config, "target");
File projectDir = spec.getProjectDir();
return new DeleteFileMutator(resolveProjectFile(projectDir, target));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public BuildMutator configure(String key, BuildMutatorConfiguratorSpec spec) {
mutatorsForKey.add(getBuildMutatorForFile(sourceFileToChange));
}

return new CompositeBuildMutator(mutatorsForKey);
return CompositeBuildMutator.from(mutatorsForKey);
}

private BuildMutator getBuildMutatorForFile(File sourceFileToChange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CopyFileMutatorTest extends AbstractMutatorTest {
def target = new File(testDir, "nested/target.txt")

def spec = mockConfigSpec("""{
copy-file {
copy-file = {
source = "source.txt"
target = "nested/target.txt"
}
Expand All @@ -26,4 +26,35 @@ class CopyFileMutatorTest extends AbstractMutatorTest {
target.exists()
target.text == expectedContents
}

def "copies multiple sets of source and target"() {
def testDir = tmpDir.newFolder()

def expectedContents = "Copy file from source to target"
def source = new File(testDir, "source.txt")
source.text = expectedContents
def target1 = new File(testDir, "nested/target1.txt")
def target2 = new File(testDir, "nested/target2.txt")

def spec = mockConfigSpec("""{
copy-file = [{
source = "source.txt"
target = "nested/target1.txt"
}, {
source = "source.txt"
target = "nested/target2.txt"
}]
}""")
_ * spec.projectDir >> testDir

when:
def mutator = new CopyFileMutator.Configurator().configure("copy-file", spec)
mutator.beforeScenario(buildContext)

then:
target1.exists()
target1.text == expectedContents
target2.exists()
target2.text == expectedContents
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DeleteFileMutatorTest extends AbstractMutatorTest {
new File(testDir, "file.txt").text = expectedContents

def spec = mockConfigSpec("""{
delete-file {
delete-file = {
target = "target-dir"
}
}""")
Expand All @@ -51,7 +51,7 @@ class DeleteFileMutatorTest extends AbstractMutatorTest {
def testFile = new File(testDir, "missing-file.txt")

def spec = mockConfigSpec("""{
delete-file {
delete-file = {
target = "missing-file.txt"
}
}""")
Expand All @@ -64,4 +64,31 @@ class DeleteFileMutatorTest extends AbstractMutatorTest {
then: "File should not exist"
!testFile.exists()
}

def "deletes multiple files"() {
def testDir = tmpDir.newFolder()
def expectedContents = "Copy file from source to target"
def file1 = new File(testDir, "file1.txt")
file1.text = expectedContents
def file2 = new File(testDir, "file2.txt")
file2.text = expectedContents

def spec = mockConfigSpec("""{
delete-file = [{
target = "file1.txt"
}, {
target = "file2.txt"
}]
}""")
_ * spec.projectDir >> testDir

when:
def mutator = new DeleteFileMutator.Configurator().configure("delete-file", spec)
mutator.beforeScenario(buildContext)

then: "Files should not exist"
!file1.exists()
!file2.exists()
}

}

0 comments on commit 43c4016

Please sign in to comment.