Search in sources :

Example 1 with ExternalTestRunnerTestSpec

use of com.facebook.buck.rules.ExternalTestRunnerTestSpec in project buck by facebook.

the class AppleTest method getTestCommand.

public Pair<ImmutableList<Step>, ExternalTestRunnerTestSpec> getTestCommand(ExecutionContext context, TestRunningOptions options, SourcePathResolver pathResolver, TestRule.TestReportingCallback testReportingCallback) {
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    ExternalTestRunnerTestSpec.Builder externalSpec = ExternalTestRunnerTestSpec.builder().setTarget(getBuildTarget()).setLabels(getLabels()).setContacts(getContacts());
    Path resolvedTestBundleDirectory = pathResolver.getAbsolutePath(Preconditions.checkNotNull(testBundle.getSourcePathToOutput()));
    Path pathToTestOutput = getProjectFilesystem().resolve(getPathToTestOutputDirectory());
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), pathToTestOutput));
    Path resolvedTestLogsPath = getProjectFilesystem().resolve(testLogsPath);
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), resolvedTestLogsPath));
    Path resolvedTestOutputPath = getProjectFilesystem().resolve(testOutputPath);
    Optional<Path> testHostAppPath = Optional.empty();
    if (testHostApp.isPresent()) {
        Path resolvedTestHostAppDirectory = pathResolver.getAbsolutePath(Preconditions.checkNotNull(testHostApp.get().getSourcePathToOutput()));
        testHostAppPath = Optional.of(resolvedTestHostAppDirectory.resolve(testHostApp.get().getUnzippedOutputFilePathToBinary()));
    }
    if (!useXctest) {
        if (!xctool.isPresent()) {
            throw new HumanReadableException("Set xctool_path = /path/to/xctool or xctool_zip_target = //path/to:xctool-zip " + "in the [apple] section of .buckconfig to run this test");
        }
        ImmutableSet.Builder<Path> logicTestPathsBuilder = ImmutableSet.builder();
        ImmutableMap.Builder<Path, Path> appTestPathsToHostAppsBuilder = ImmutableMap.builder();
        if (testHostAppPath.isPresent()) {
            appTestPathsToHostAppsBuilder.put(resolvedTestBundleDirectory, testHostAppPath.get());
        } else {
            logicTestPathsBuilder.add(resolvedTestBundleDirectory);
        }
        xctoolStdoutReader = Optional.of(new AppleTestXctoolStdoutReader(testReportingCallback));
        Optional<String> destinationSpecifierArg;
        if (!destinationSpecifier.get().isEmpty()) {
            destinationSpecifierArg = Optional.of(Joiner.on(',').join(Iterables.transform(destinationSpecifier.get().entrySet(), input -> input.getKey() + "=" + input.getValue())));
        } else {
            destinationSpecifierArg = defaultDestinationSpecifier;
        }
        Optional<String> snapshotReferenceImagesPath = Optional.empty();
        if (this.snapshotReferenceImagesPath.isPresent()) {
            if (this.snapshotReferenceImagesPath.get().isLeft()) {
                snapshotReferenceImagesPath = Optional.of(pathResolver.getAbsolutePath(this.snapshotReferenceImagesPath.get().getLeft()).toString());
            } else if (this.snapshotReferenceImagesPath.get().isRight()) {
                snapshotReferenceImagesPath = Optional.of(getProjectFilesystem().getPathForRelativePath(this.snapshotReferenceImagesPath.get().getRight()).toString());
            }
        }
        XctoolRunTestsStep xctoolStep = new XctoolRunTestsStep(getProjectFilesystem(), pathResolver.getAbsolutePath(xctool.get()), options.getEnvironmentOverrides(), xctoolStutterTimeout, platformName, destinationSpecifierArg, logicTestPathsBuilder.build(), appTestPathsToHostAppsBuilder.build(), resolvedTestOutputPath, xctoolStdoutReader, xcodeDeveloperDirSupplier, options.getTestSelectorList(), context.isDebugEnabled(), Optional.of(testLogDirectoryEnvironmentVariable), Optional.of(resolvedTestLogsPath), Optional.of(testLogLevelEnvironmentVariable), Optional.of(testLogLevel), testRuleTimeoutMs, snapshotReferenceImagesPath);
        steps.add(xctoolStep);
        externalSpec.setType("xctool-" + (testHostApp.isPresent() ? "application" : "logic"));
        externalSpec.setCommand(xctoolStep.getCommand());
        externalSpec.setEnv(xctoolStep.getEnv(context));
    } else {
        xctestOutputReader = Optional.of(new AppleTestXctestOutputReader(testReportingCallback));
        HashMap<String, String> environment = new HashMap<>();
        environment.putAll(xctest.getEnvironment(pathResolver));
        environment.putAll(options.getEnvironmentOverrides());
        if (testHostAppPath.isPresent()) {
            environment.put("XCInjectBundleInto", testHostAppPath.get().toString());
        }
        XctestRunTestsStep xctestStep = new XctestRunTestsStep(getProjectFilesystem(), ImmutableMap.copyOf(environment), xctest.getCommandPrefix(pathResolver), resolvedTestBundleDirectory, resolvedTestOutputPath, xctestOutputReader, xcodeDeveloperDirSupplier);
        steps.add(xctestStep);
        externalSpec.setType("xctest");
        externalSpec.setCommand(xctestStep.getCommand());
        externalSpec.setEnv(xctestStep.getEnv(context));
    }
    return new Pair<>(steps.build(), externalSpec.build());
}
Also used : Path(java.nio.file.Path) ForwardingBuildTargetSourcePath(com.facebook.buck.rules.ForwardingBuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) OptionalCompat(com.facebook.buck.util.OptionalCompat) HasRuntimeDeps(com.facebook.buck.rules.HasRuntimeDeps) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) TestCaseSummary(com.facebook.buck.test.TestCaseSummary) TestRunningOptions(com.facebook.buck.test.TestRunningOptions) TestResults(com.facebook.buck.test.TestResults) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Pair(com.facebook.buck.model.Pair) TestRule(com.facebook.buck.rules.TestRule) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) ForwardingBuildTargetSourcePath(com.facebook.buck.rules.ForwardingBuildTargetSourcePath) ImmutableMap(com.google.common.collect.ImmutableMap) BuildableContext(com.facebook.buck.rules.BuildableContext) BuildTarget(com.facebook.buck.model.BuildTarget) StandardCharsets(java.nio.charset.StandardCharsets) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) List(java.util.List) Stream(java.util.stream.Stream) ExternalTestRunnerTestSpec(com.facebook.buck.rules.ExternalTestRunnerTestSpec) Optional(java.util.Optional) Joiner(com.google.common.base.Joiner) ExternalTestRunnerRule(com.facebook.buck.rules.ExternalTestRunnerRule) Iterables(com.google.common.collect.Iterables) Step(com.facebook.buck.step.Step) Supplier(com.google.common.base.Supplier) SourcePath(com.facebook.buck.rules.SourcePath) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Either(com.facebook.buck.model.Either) BuildRule(com.facebook.buck.rules.BuildRule) ExecutionContext(com.facebook.buck.step.ExecutionContext) Label(com.facebook.buck.rules.Label) Tool(com.facebook.buck.rules.Tool) ImmutableList(com.google.common.collect.ImmutableList) MoreCollectors(com.facebook.buck.util.MoreCollectors) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) Files(java.nio.file.Files) IOException(java.io.IOException) HumanReadableException(com.facebook.buck.util.HumanReadableException) InputStreamReader(java.io.InputStreamReader) BuildContext(com.facebook.buck.rules.BuildContext) Preconditions(com.google.common.base.Preconditions) BufferedReader(java.io.BufferedReader) BuildTargets(com.facebook.buck.model.BuildTargets) Collections(java.util.Collections) InputStream(java.io.InputStream) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) Step(com.facebook.buck.step.Step) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableSet(com.google.common.collect.ImmutableSet) HumanReadableException(com.facebook.buck.util.HumanReadableException) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) ExternalTestRunnerTestSpec(com.facebook.buck.rules.ExternalTestRunnerTestSpec) Pair(com.facebook.buck.model.Pair)

Example 2 with ExternalTestRunnerTestSpec

use of com.facebook.buck.rules.ExternalTestRunnerTestSpec in project buck by facebook.

the class TestCommand method runTestsExternal.

private int runTestsExternal(final CommandRunnerParams params, Build build, Iterable<String> command, Iterable<TestRule> testRules, SourcePathResolver pathResolver) throws InterruptedException, IOException {
    TestRunningOptions options = getTestRunningOptions(params);
    // Walk the test rules, collecting all the specs.
    List<ExternalTestRunnerTestSpec> specs = Lists.newArrayList();
    for (TestRule testRule : testRules) {
        if (!(testRule instanceof ExternalTestRunnerRule)) {
            params.getBuckEventBus().post(ConsoleEvent.severe(String.format("Test %s does not support external test running", testRule.getBuildTarget())));
            return 1;
        }
        ExternalTestRunnerRule rule = (ExternalTestRunnerRule) testRule;
        specs.add(rule.getExternalTestRunnerSpec(build.getExecutionContext(), options, pathResolver));
    }
    // Serialize the specs to a file to pass into the test runner.
    Path infoFile = params.getCell().getFilesystem().resolve(params.getCell().getFilesystem().getBuckPaths().getScratchDir()).resolve("external_runner_specs.json");
    Files.createDirectories(infoFile.getParent());
    Files.deleteIfExists(infoFile);
    params.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(infoFile.toFile(), specs);
    // Launch and run the external test runner, forwarding it's stdout/stderr to the console.
    // We wait for it to complete then returns its error code.
    ListeningProcessExecutor processExecutor = new ListeningProcessExecutor();
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addAllCommand(command).addAllCommand(withDashArguments).setEnvironment(params.getEnvironment()).addCommand("--buck-test-info", infoFile.toString()).addCommand("--jobs", String.valueOf(getConcurrencyLimit(params.getBuckConfig()).threadLimit)).setDirectory(params.getCell().getFilesystem().getRootPath()).build();
    ForwardingProcessListener processListener = new ForwardingProcessListener(Channels.newChannel(params.getConsole().getStdOut()), Channels.newChannel(params.getConsole().getStdErr()));
    ListeningProcessExecutor.LaunchedProcess process = processExecutor.launchProcess(processExecutorParams, processListener);
    try {
        return processExecutor.waitForProcess(process);
    } finally {
        processExecutor.destroyProcess(process, /* force */
        false);
        processExecutor.waitForProcess(process);
    }
}
Also used : Path(java.nio.file.Path) TestRule(com.facebook.buck.rules.TestRule) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ListeningProcessExecutor(com.facebook.buck.util.ListeningProcessExecutor) ExternalTestRunnerRule(com.facebook.buck.rules.ExternalTestRunnerRule) ForwardingProcessListener(com.facebook.buck.util.ForwardingProcessListener) TestRunningOptions(com.facebook.buck.test.TestRunningOptions) ExternalTestRunnerTestSpec(com.facebook.buck.rules.ExternalTestRunnerTestSpec)

Aggregations

ExternalTestRunnerRule (com.facebook.buck.rules.ExternalTestRunnerRule)2 ExternalTestRunnerTestSpec (com.facebook.buck.rules.ExternalTestRunnerTestSpec)2 TestRule (com.facebook.buck.rules.TestRule)2 TestRunningOptions (com.facebook.buck.test.TestRunningOptions)2 BuildTarget (com.facebook.buck.model.BuildTarget)1 BuildTargets (com.facebook.buck.model.BuildTargets)1 Either (com.facebook.buck.model.Either)1 Pair (com.facebook.buck.model.Pair)1 AbstractBuildRule (com.facebook.buck.rules.AbstractBuildRule)1 AddToRuleKey (com.facebook.buck.rules.AddToRuleKey)1 BuildContext (com.facebook.buck.rules.BuildContext)1 BuildRule (com.facebook.buck.rules.BuildRule)1 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)1 BuildableContext (com.facebook.buck.rules.BuildableContext)1 ForwardingBuildTargetSourcePath (com.facebook.buck.rules.ForwardingBuildTargetSourcePath)1 HasRuntimeDeps (com.facebook.buck.rules.HasRuntimeDeps)1 Label (com.facebook.buck.rules.Label)1 SourcePath (com.facebook.buck.rules.SourcePath)1 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)1 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)1