use of org.gradle.language.swift.SwiftComponent in project gradle by gradle.
the class XCTestConventionPlugin method configureTestSuiteWithTestedComponentWhenAvailable.
private void configureTestSuiteWithTestedComponentWhenAvailable(final Project project, final DefaultSwiftXCTestSuite testSuite, final DefaultSwiftXCTestBinary testExecutable) {
SwiftComponent target = testSuite.getTestedComponent().getOrNull();
if (!(target instanceof ProductionSwiftComponent)) {
return;
}
final ProductionSwiftComponent testedComponent = (ProductionSwiftComponent) target;
final TaskContainer tasks = project.getTasks();
testedComponent.getBinaries().whenElementFinalized(testedBinary -> {
if (testedBinary != testedComponent.getDevelopmentBinary().get()) {
return;
}
// Setup the dependency on the main binary
// This should all be replaced by a single dependency that points at some "testable" variants of the main binary
// Inherit implementation dependencies
testExecutable.getImplementationDependencies().extendsFrom(((DefaultSwiftBinary) testedBinary).getImplementationDependencies());
// Configure test binary to compile against binary under test
Dependency compileDependency = project.getDependencies().create(project.files(testedBinary.getModuleFile()));
testExecutable.getImportPathConfiguration().getDependencies().add(compileDependency);
// Configure test binary to link against tested component compiled objects
ConfigurableFileCollection testableObjects = project.files();
if (testedComponent instanceof SwiftApplication) {
TaskProvider<UnexportMainSymbol> unexportMainSymbol = tasks.register("relocateMainForTest", UnexportMainSymbol.class, task -> {
String dirName = ((DefaultSwiftBinary) testedBinary).getNames().getDirName();
task.getOutputDirectory().set(project.getLayout().getBuildDirectory().dir("obj/for-test/" + dirName));
task.getObjects().from(testedBinary.getObjects());
});
testableObjects.from(unexportMainSymbol.map(task -> task.getRelocatedObjects()));
} else {
testableObjects.from(testedBinary.getObjects());
}
Dependency linkDependency = project.getDependencies().create(testableObjects);
testExecutable.getLinkConfiguration().getDependencies().add(linkDependency);
});
}
use of org.gradle.language.swift.SwiftComponent in project gradle by gradle.
the class XCTestConventionPlugin method apply.
@Override
public void apply(Project project) {
project.getPluginManager().apply(SwiftBasePlugin.class);
project.getPluginManager().apply(NativeTestingBasePlugin.class);
final ProviderFactory providers = project.getProviders();
// Create test suite component
// TODO - Reuse logic from Swift*Plugin
// TODO - component name and extension name aren't the same
// TODO - should use `src/xctest/swift` as the convention?
// Add the test suite and extension
DefaultSwiftXCTestSuite testSuite = componentFactory.newInstance(SwiftXCTestSuite.class, DefaultSwiftXCTestSuite.class, "test");
project.getExtensions().add(SwiftXCTestSuite.class, "xctest", testSuite);
project.getComponents().add(testSuite);
// Setup component
testSuite.getModule().set(GUtil.toCamelCase(project.getName() + "Test"));
final DefaultSwiftXCTestSuite testComponent = testSuite;
testComponent.getTargetMachines().convention(useHostAsDefaultTargetMachine(targetMachineFactory));
testComponent.getSourceCompatibility().convention(testComponent.getTestedComponent().flatMap(it -> it.getSourceCompatibility()));
final String mainComponentName = "main";
project.getComponents().withType(ProductionSwiftComponent.class, component -> {
if (mainComponentName.equals(component.getName())) {
testComponent.getTargetMachines().convention(component.getTargetMachines());
testComponent.getTestedComponent().convention(component);
}
});
testComponent.getTestBinary().convention(project.provider(() -> {
return testComponent.getBinaries().get().stream().filter(SwiftXCTestBinary.class::isInstance).map(SwiftXCTestBinary.class::cast).findFirst().orElse(null);
}));
testComponent.getBinaries().whenElementKnown(DefaultSwiftXCTestBinary.class, binary -> {
// Create test suite test task
TaskProvider<XCTest> testingTask = project.getTasks().register("xcTest", XCTest.class, task -> {
task.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
task.setDescription("Executes XCTest suites");
task.getTestInstallDirectory().set(binary.getInstallDirectory());
task.getRunScriptFile().set(binary.getRunScriptFile());
task.getWorkingDirectory().set(binary.getInstallDirectory());
});
binary.getRunTask().set(testingTask);
configureTestSuiteBuildingTasks(project, binary);
configureTestSuiteWithTestedComponentWhenAvailable(project, testComponent, binary);
});
project.afterEvaluate(p -> {
final SwiftComponent mainComponent = testComponent.getTestedComponent().getOrNull();
final SetProperty<TargetMachine> mainTargetMachines = mainComponent != null ? mainComponent.getTargetMachines() : null;
Dimensions.unitTestVariants(testComponent.getModule(), testComponent.getTargetMachines(), mainTargetMachines, objectFactory, attributesFactory, providers.provider(() -> project.getGroup().toString()), providers.provider(() -> project.getVersion().toString()), variantIdentity -> {
if (tryToBuildOnHost(variantIdentity)) {
testComponent.getSourceCompatibility().finalizeValue();
ToolChainSelector.Result<SwiftPlatform> result = toolChainSelector.select(SwiftPlatform.class, new DefaultSwiftPlatform(variantIdentity.getTargetMachine(), testComponent.getSourceCompatibility().getOrNull()));
// Create test suite executable
if (result.getTargetPlatform().getTargetMachine().getOperatingSystemFamily().isMacOs()) {
testComponent.addBundle(variantIdentity, result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
} else {
testComponent.addExecutable(variantIdentity, result.getTargetPlatform(), result.getToolChain(), result.getPlatformToolProvider());
}
}
});
testComponent.getBinaries().realizeNow();
});
}
Aggregations