Search in sources :

Example 26 with VisibleForTesting

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.annotations.VisibleForTesting in project buck by facebook.

the class GenerateCodeCoverageReportStep method saveParametersToPropertyStream.

@VisibleForTesting
void saveParametersToPropertyStream(ProjectFilesystem filesystem, Set<Path> extractedClassesDirectories, OutputStream outputStream) throws IOException {
    final Properties properties = new Properties();
    properties.setProperty("jacoco.output.dir", filesystem.resolve(outputDirectory).toString());
    properties.setProperty("jacoco.exec.data.file", JACOCO_EXEC_COVERAGE_FILE);
    properties.setProperty("jacoco.format", format.toString().toLowerCase());
    properties.setProperty("jacoco.title", title);
    properties.setProperty("classes.jars", formatPathSet(jarFiles));
    properties.setProperty("classes.dir", formatPathSet(extractedClassesDirectories));
    properties.setProperty("src.dir", Joiner.on(":").join(sourceDirectories));
    if (coverageIncludes.isPresent()) {
        properties.setProperty("jacoco.includes", coverageIncludes.get());
    }
    if (coverageExcludes.isPresent()) {
        properties.setProperty("jacoco.excludes", coverageExcludes.get());
    }
    try (Writer writer = new OutputStreamWriter(outputStream, "utf8")) {
        properties.store(writer, "Parameters for Jacoco report generator.");
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) Properties(java.util.Properties) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 27 with VisibleForTesting

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.annotations.VisibleForTesting in project buck by facebook.

the class CopyResourcesStep method buildSteps.

@VisibleForTesting
ImmutableList<Step> buildSteps() {
    ImmutableList.Builder<Step> allSteps = ImmutableList.builder();
    if (resources.isEmpty()) {
        return allSteps.build();
    }
    String targetPackageDir = javaPackageFinder.findJavaPackage(target);
    for (SourcePath rawResource : resources) {
        // If the path to the file defining this rule were:
        // "first-party/orca/lib-http/tests/com/facebook/orca/BUCK"
        //
        // And the value of resource were:
        // "first-party/orca/lib-http/tests/com/facebook/orca/protocol/base/batch_exception1.txt"
        //
        // Assuming that `src_roots = tests` were in the [java] section of the .buckconfig file,
        // then javaPackageAsPath would be:
        // "com/facebook/orca/protocol/base/"
        //
        // And the path that we would want to copy to the classes directory would be:
        // "com/facebook/orca/protocol/base/batch_exception1.txt"
        //
        // Therefore, some path-wrangling is required to produce the correct string.
        Optional<BuildRule> underlyingRule = ruleFinder.getRule(rawResource);
        Path relativePathToResource = resolver.getRelativePath(rawResource);
        String resource;
        if (underlyingRule.isPresent()) {
            BuildTarget underlyingTarget = underlyingRule.get().getBuildTarget();
            if (underlyingRule.get() instanceof HasOutputName) {
                resource = MorePaths.pathWithUnixSeparators(underlyingTarget.getBasePath().resolve(((HasOutputName) underlyingRule.get()).getOutputName()));
            } else {
                Path genOutputParent = BuildTargets.getGenPath(filesystem, underlyingTarget, "%s").getParent();
                Path scratchOutputParent = BuildTargets.getScratchPath(filesystem, underlyingTarget, "%s").getParent();
                Optional<Path> outputPath = MorePaths.stripPrefix(relativePathToResource, genOutputParent).map(Optional::of).orElse(MorePaths.stripPrefix(relativePathToResource, scratchOutputParent));
                Preconditions.checkState(outputPath.isPresent(), "%s is used as a resource but does not output to a default output directory", underlyingTarget.getFullyQualifiedName());
                resource = MorePaths.pathWithUnixSeparators(underlyingTarget.getBasePath().resolve(outputPath.get()));
            }
        } else {
            resource = MorePaths.pathWithUnixSeparators(relativePathToResource);
        }
        Path javaPackageAsPath = javaPackageFinder.findJavaPackageFolder(outputDirectory.getFileSystem().getPath(resource));
        Path relativeSymlinkPath;
        if ("".equals(javaPackageAsPath.toString())) {
            // In this case, the project root is acting as the default package, so the resource path
            // works fine.
            relativeSymlinkPath = relativePathToResource.getFileName();
        } else {
            int lastIndex = resource.lastIndexOf(MorePaths.pathWithUnixSeparatorsAndTrailingSlash(javaPackageAsPath));
            if (lastIndex < 0) {
                Preconditions.checkState(rawResource instanceof BuildTargetSourcePath, "If resource path %s does not contain %s, then it must be a BuildTargetSourcePath.", relativePathToResource, javaPackageAsPath);
                // Handle the case where we depend on the output of another BuildRule. In that case, just
                // grab the output and put in the same package as this target would be in.
                relativeSymlinkPath = outputDirectory.getFileSystem().getPath(String.format("%s%s%s", targetPackageDir, targetPackageDir.isEmpty() ? "" : "/", resolver.getRelativePath(rawResource).getFileName()));
            } else {
                relativeSymlinkPath = outputDirectory.getFileSystem().getPath(resource.substring(lastIndex));
            }
        }
        Path target = outputDirectory.resolve(relativeSymlinkPath);
        MkdirAndSymlinkFileStep link = new MkdirAndSymlinkFileStep(filesystem, resolver.getAbsolutePath(rawResource), target);
        allSteps.add(link);
    }
    return allSteps.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Path(java.nio.file.Path) HasOutputName(com.facebook.buck.model.HasOutputName) ImmutableList(com.google.common.collect.ImmutableList) Step(com.facebook.buck.step.Step) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) MkdirAndSymlinkFileStep(com.facebook.buck.step.fs.MkdirAndSymlinkFileStep) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 28 with VisibleForTesting

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.annotations.VisibleForTesting in project buck by facebook.

the class BaseCompileToJarStepFactory method addPostprocessClassesCommands.

/**
   * Adds a BashStep for each postprocessClasses command that runs the command followed by the
   * outputDirectory of javac outputs.
   *
   * The expectation is that the command will inspect and update the directory by
   * modifying, adding, and deleting the .class files in the directory.
   *
   * The outputDirectory should be a valid java root.  I.e., if outputDirectory
   * is buck-out/bin/java/abc/lib__abc__classes/, then a contained class abc.AbcModule
   * should be at buck-out/bin/java/abc/lib__abc__classes/abc/AbcModule.class
   *
   * @param filesystem the project filesystem.
   * @param postprocessClassesCommands the list of commands to post-process .class files.
   * @param outputDirectory the directory that will contain all the javac output.
   * @param declaredClasspathEntries the list of classpath entries.
   * @param bootClasspath the compilation boot classpath.
   */
@VisibleForTesting
static ImmutableList<Step> addPostprocessClassesCommands(ProjectFilesystem filesystem, List<String> postprocessClassesCommands, Path outputDirectory, ImmutableSortedSet<Path> declaredClasspathEntries, Optional<String> bootClasspath) {
    if (postprocessClassesCommands.isEmpty()) {
        return ImmutableList.of();
    }
    ImmutableList.Builder<Step> commands = new ImmutableList.Builder<Step>();
    ImmutableMap.Builder<String, String> envVarBuilder = ImmutableMap.builder();
    envVarBuilder.put("COMPILATION_CLASSPATH", Joiner.on(':').join(Iterables.transform(declaredClasspathEntries, filesystem::resolve)));
    if (bootClasspath.isPresent()) {
        envVarBuilder.put("COMPILATION_BOOTCLASSPATH", bootClasspath.get());
    }
    ImmutableMap<String, String> envVars = envVarBuilder.build();
    for (final String postprocessClassesCommand : postprocessClassesCommands) {
        BashStep bashStep = new BashStep(filesystem.getRootPath(), postprocessClassesCommand + " " + outputDirectory) {

            @Override
            public ImmutableMap<String, String> getEnvironmentVariables(ExecutionContext context) {
                return envVars;
            }
        };
        commands.add(bashStep);
    }
    return commands.build();
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) ImmutableList(com.google.common.collect.ImmutableList) BashStep(com.facebook.buck.shell.BashStep) Step(com.facebook.buck.step.Step) BashStep(com.facebook.buck.shell.BashStep) ImmutableMap(com.google.common.collect.ImmutableMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 29 with VisibleForTesting

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.annotations.VisibleForTesting in project buck by facebook.

the class MoreFiles method diffFileContents.

/**
   * Log a simplistic diff between lines and the contents of file.
   */
@VisibleForTesting
static List<String> diffFileContents(Iterable<String> lines, File file) throws IOException {
    List<String> diffLines = Lists.newArrayList();
    Iterator<String> iter = lines.iterator();
    try (BufferedReader reader = Files.newBufferedReader(file.toPath(), Charsets.UTF_8)) {
        while (iter.hasNext()) {
            String lineA = reader.readLine();
            String lineB = iter.next();
            if (!Objects.equal(lineA, lineB)) {
                diffLines.add(String.format("| %s | %s |", lineA == null ? "" : lineA, lineB));
            }
        }
        String lineA;
        while ((lineA = reader.readLine()) != null) {
            diffLines.add(String.format("| %s |  |", lineA));
        }
    }
    return diffLines;
}
Also used : BufferedReader(java.io.BufferedReader) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 30 with VisibleForTesting

use of org.apache.beam.vendor.calcite.v1_28_0.com.google.common.annotations.VisibleForTesting in project buck by facebook.

the class Watchman method build.

@VisibleForTesting
@SuppressWarnings("PMD.PrematureDeclaration")
static Watchman build(ListeningProcessExecutor executor, Function<Path, Optional<WatchmanClient>> watchmanConnector, ImmutableSet<Path> projectWatchList, ImmutableMap<String, String> env, ExecutableFinder exeFinder, Console console, Clock clock, Optional<Long> commandTimeoutMillis) throws InterruptedException {
    LOG.info("Creating for: " + projectWatchList);
    Optional<WatchmanClient> watchmanClient = Optional.empty();
    try {
        Path watchmanPath = exeFinder.getExecutable(WATCHMAN, env).toAbsolutePath();
        Optional<? extends Map<String, ?>> result;
        long timeoutMillis = commandTimeoutMillis.orElse(DEFAULT_COMMAND_TIMEOUT_MILLIS);
        long endTimeNanos = clock.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis);
        result = execute(executor, console, clock, timeoutMillis, TimeUnit.MILLISECONDS.toNanos(timeoutMillis), watchmanPath, "get-sockname");
        if (!result.isPresent()) {
            return NULL_WATCHMAN;
        }
        String rawSockname = (String) result.get().get("sockname");
        if (rawSockname == null) {
            return NULL_WATCHMAN;
        }
        Path socketPath = Paths.get(rawSockname);
        LOG.info("Connecting to Watchman version %s at %s", result.get().get("version"), socketPath);
        watchmanClient = watchmanConnector.apply(socketPath);
        if (!watchmanClient.isPresent()) {
            LOG.warn("Could not connect to Watchman, disabling.");
            return NULL_WATCHMAN;
        }
        LOG.debug("Connected to Watchman");
        long versionQueryStartTimeNanos = clock.nanoTime();
        result = watchmanClient.get().queryWithTimeout(endTimeNanos - versionQueryStartTimeNanos, "version", ImmutableMap.of("required", REQUIRED_CAPABILITIES, "optional", ALL_CAPABILITIES.keySet()));
        LOG.info("Took %d ms to query capabilities %s", TimeUnit.NANOSECONDS.toMillis(clock.nanoTime() - versionQueryStartTimeNanos), ALL_CAPABILITIES);
        if (!result.isPresent()) {
            LOG.warn("Could not get version response from Watchman, disabling Watchman");
            watchmanClient.get().close();
            return NULL_WATCHMAN;
        }
        ImmutableSet.Builder<Capability> capabilitiesBuilder = ImmutableSet.builder();
        if (!extractCapabilities(result.get(), capabilitiesBuilder)) {
            LOG.warn("Could not extract capabilities, disabling Watchman");
            watchmanClient.get().close();
            return NULL_WATCHMAN;
        }
        ImmutableSet<Capability> capabilities = capabilitiesBuilder.build();
        LOG.debug("Got Watchman capabilities: %s", capabilities);
        ImmutableMap.Builder<Path, ProjectWatch> projectWatchesBuilder = ImmutableMap.builder();
        ImmutableMap.Builder<Path, String> clockIdsBuilder = ImmutableMap.builder();
        for (Path rootPath : projectWatchList) {
            Optional<ProjectWatch> projectWatch = queryWatchProject(watchmanClient.get(), rootPath, clock, endTimeNanos - clock.nanoTime());
            if (!projectWatch.isPresent()) {
                watchmanClient.get().close();
                return NULL_WATCHMAN;
            }
            projectWatchesBuilder.put(rootPath, projectWatch.get());
            Optional<String> clockId = queryClock(watchmanClient.get(), projectWatch.get().getWatchRoot(), capabilities, clock, endTimeNanos - clock.nanoTime());
            if (clockId.isPresent()) {
                clockIdsBuilder.put(rootPath, clockId.get());
            }
        }
        return new Watchman(projectWatchesBuilder.build(), capabilities, clockIdsBuilder.build(), Optional.of(socketPath), watchmanClient);
    } catch (ClassCastException | HumanReadableException | IOException e) {
        LOG.warn(e, "Unable to determine the version of watchman. Going without.");
        if (watchmanClient.isPresent()) {
            try {
                watchmanClient.get().close();
            } catch (IOException ioe) {
                LOG.warn(ioe, "Could not close watchman query client");
            }
        }
        return NULL_WATCHMAN;
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableSet(com.google.common.collect.ImmutableSet) HumanReadableException(com.facebook.buck.util.HumanReadableException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1954 IOException (java.io.IOException)284 ArrayList (java.util.ArrayList)214 Map (java.util.Map)156 HashMap (java.util.HashMap)147 List (java.util.List)113 File (java.io.File)94 ImmutableMap (com.google.common.collect.ImmutableMap)72 HashSet (java.util.HashSet)67 Path (org.apache.hadoop.fs.Path)63 ImmutableList (com.google.common.collect.ImmutableList)60 Path (java.nio.file.Path)60 Set (java.util.Set)52 Matcher (java.util.regex.Matcher)46 Collectors (java.util.stream.Collectors)46 Collection (java.util.Collection)39 Optional (java.util.Optional)38 NotNull (org.jetbrains.annotations.NotNull)37 ImmutableSet (com.google.common.collect.ImmutableSet)34 TreeMap (java.util.TreeMap)34