Search in sources :

Example 11 with MacroException

use of com.facebook.buck.model.MacroException in project buck by facebook.

the class MavenCoordinatesMacroExpanderTest method testHasMavenCoordinatesBuildRule.

@Test
public void testHasMavenCoordinatesBuildRule() throws Exception {
    String mavenCoords = "org.foo:bar:1.0";
    BuildRule rule = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//test:java")).setMavenCoords(mavenCoords).build(resolver);
    try {
        String actualCoords = expander.getMavenCoordinates(rule);
        assertEquals("Return maven coordinates do not match provides ones", mavenCoords, actualCoords);
    } catch (MacroException e) {
        fail(String.format("Unexpected MacroException: %s", e.getMessage()));
    }
}
Also used : FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) MacroException(com.facebook.buck.model.MacroException) Test(org.junit.Test)

Example 12 with MacroException

use of com.facebook.buck.model.MacroException in project buck by facebook.

the class MavenCoordinatesMacroExpanderTest method testExpansionOfMavenCoordinates.

@Test
public void testExpansionOfMavenCoordinates() throws NoSuchBuildTargetException {
    String mavenCoords = "org.foo:bar:1.0";
    BuildTarget target = BuildTargetFactory.newInstance("//:java");
    JavaLibraryBuilder.createBuilder(target).setMavenCoords(mavenCoords).build(resolver);
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    MacroHandler macroHandler = new MacroHandler(ImmutableMap.of("maven_coords", expander));
    try {
        String expansion = macroHandler.expand(target, createCellRoots(filesystem), resolver, "$(maven_coords //:java)");
        assertEquals("Return maven coordinates do not match provides ones", mavenCoords, expansion);
    } catch (MacroException e) {
        fail(String.format("Unexpected MacroException: %s", e.getMessage()));
    }
}
Also used : BuildTarget(com.facebook.buck.model.BuildTarget) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) MacroException(com.facebook.buck.model.MacroException) Test(org.junit.Test)

Example 13 with MacroException

use of com.facebook.buck.model.MacroException in project buck by facebook.

the class WorkerMacroArgTest method testWorkerMacroArgWithBadReference.

@Test
public void testWorkerMacroArgWithBadReference() throws MacroException, NoSuchBuildTargetException {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    BuildRule nonWorkerBuildRule = new FakeBuildRule(BuildTargetFactory.newInstance("//:not_worker_rule"), new SourcePathResolver(new SourcePathRuleFinder(resolver)));
    resolver.addToIndex(nonWorkerBuildRule);
    MacroHandler macroHandler = new MacroHandler(ImmutableMap.of("worker", new WorkerMacroExpander()));
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    try {
        new WorkerMacroArg(macroHandler, BuildTargetFactory.newInstance("//:rule"), TestCellBuilder.createCellRoots(filesystem), resolver, "$(worker //:not_worker_rule)");
    } catch (MacroException e) {
        assertThat(e.getMessage(), Matchers.containsString("does not correspond to a worker_tool"));
    }
}
Also used : MacroHandler(com.facebook.buck.rules.macros.MacroHandler) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) WorkerMacroExpander(com.facebook.buck.rules.macros.WorkerMacroExpander) MacroException(com.facebook.buck.model.MacroException) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 14 with MacroException

use of com.facebook.buck.model.MacroException in project buck by facebook.

the class WorkerMacroArgTest method testWorkerMacroArgWithMacroInWrongLocation.

@Test
public void testWorkerMacroArgWithMacroInWrongLocation() {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    MacroHandler macroHandler = new MacroHandler(ImmutableMap.of("worker", new WorkerMacroExpander()));
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    try {
        new WorkerMacroArg(macroHandler, BuildTargetFactory.newInstance("//:rule"), TestCellBuilder.createCellRoots(filesystem), resolver, "mkdir && $(worker :worker)");
    } catch (MacroException e) {
        assertThat(e.getMessage(), Matchers.containsString("must be at the beginning"));
    }
}
Also used : MacroHandler(com.facebook.buck.rules.macros.MacroHandler) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) WorkerMacroExpander(com.facebook.buck.rules.macros.WorkerMacroExpander) MacroException(com.facebook.buck.model.MacroException) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Example 15 with MacroException

use of com.facebook.buck.model.MacroException in project buck by facebook.

the class CxxTestDescription method createBuildRule.

@SuppressWarnings("PMD.PrematureDeclaration")
@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams inputParams, final BuildRuleResolver resolver, final A args) throws NoSuchBuildTargetException {
    Optional<StripStyle> flavoredStripStyle = StripStyle.FLAVOR_DOMAIN.getValue(inputParams.getBuildTarget());
    Optional<LinkerMapMode> flavoredLinkerMapMode = LinkerMapMode.FLAVOR_DOMAIN.getValue(inputParams.getBuildTarget());
    inputParams = CxxStrip.removeStripStyleFlavorInParams(inputParams, flavoredStripStyle);
    inputParams = LinkerMapMode.removeLinkerMapModeFlavorInParams(inputParams, flavoredLinkerMapMode);
    final BuildRuleParams params = inputParams;
    Optional<CxxPlatform> platform = cxxPlatforms.getValue(params.getBuildTarget());
    CxxPlatform cxxPlatform = platform.orElse(defaultCxxPlatform);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    if (params.getBuildTarget().getFlavors().contains(CxxCompilationDatabase.COMPILATION_DATABASE)) {
        BuildRuleParams paramsWithoutFlavor = params.withoutFlavor(CxxCompilationDatabase.COMPILATION_DATABASE);
        CxxLinkAndCompileRules cxxLinkAndCompileRules = CxxDescriptionEnhancer.createBuildRulesForCxxBinaryDescriptionArg(targetGraph, paramsWithoutFlavor, resolver, cxxBuckConfig, cxxPlatform, args, flavoredStripStyle, flavoredLinkerMapMode);
        return CxxCompilationDatabase.createCompilationDatabase(params, cxxLinkAndCompileRules.compileRules);
    }
    if (params.getBuildTarget().getFlavors().contains(CxxCompilationDatabase.UBER_COMPILATION_DATABASE)) {
        return CxxDescriptionEnhancer.createUberCompilationDatabase(platform.isPresent() ? params : params.withAppendedFlavor(cxxPlatform.getFlavor()), resolver);
    }
    if (params.getBuildTarget().getFlavors().contains(CxxDescriptionEnhancer.SANDBOX_TREE_FLAVOR)) {
        return CxxDescriptionEnhancer.createSandboxTreeBuildRule(resolver, args, cxxPlatform, params);
    }
    // Generate the link rule that builds the test binary.
    final CxxLinkAndCompileRules cxxLinkAndCompileRules = CxxDescriptionEnhancer.createBuildRulesForCxxBinaryDescriptionArg(targetGraph, params, resolver, cxxBuckConfig, cxxPlatform, args, flavoredStripStyle, flavoredLinkerMapMode);
    // Construct the actual build params we'll use, notably with an added dependency on the
    // CxxLink rule above which builds the test binary.
    BuildRuleParams testParams = params.copyReplacingDeclaredAndExtraDeps(() -> cxxLinkAndCompileRules.deps, params.getExtraDeps()).copyAppendingExtraDeps(cxxLinkAndCompileRules.executable.getDeps(ruleFinder));
    testParams = CxxStrip.restoreStripStyleFlavorInParams(testParams, flavoredStripStyle);
    testParams = LinkerMapMode.restoreLinkerMapModeFlavorInParams(testParams, flavoredLinkerMapMode);
    // Supplier which expands macros in the passed in test environment.
    ImmutableMap<String, String> testEnv = ImmutableMap.copyOf(Maps.transformValues(args.env, CxxDescriptionEnhancer.MACRO_HANDLER.getExpander(params.getBuildTarget(), params.getCellRoots(), resolver)));
    // Supplier which expands macros in the passed in test arguments.
    Supplier<ImmutableList<String>> testArgs = () -> args.args.stream().map(CxxDescriptionEnhancer.MACRO_HANDLER.getExpander(params.getBuildTarget(), params.getCellRoots(), resolver)::apply).collect(MoreCollectors.toImmutableList());
    Supplier<ImmutableSortedSet<BuildRule>> additionalDeps = () -> {
        ImmutableSortedSet.Builder<BuildRule> deps = ImmutableSortedSet.naturalOrder();
        // It's not uncommon for users to add dependencies onto other binaries that they run
        // during the test, so make sure to add them as runtime deps.
        deps.addAll(Sets.difference(params.getDeps(), cxxLinkAndCompileRules.getBinaryRule().getDeps()));
        // Add any build-time from any macros embedded in the `env` or `args` parameter.
        for (String part : Iterables.concat(args.args, args.env.values())) {
            try {
                deps.addAll(CxxDescriptionEnhancer.MACRO_HANDLER.extractBuildTimeDeps(params.getBuildTarget(), params.getCellRoots(), resolver, part));
            } catch (MacroException e) {
                throw new HumanReadableException(e, "%s: %s", params.getBuildTarget(), e.getMessage());
            }
        }
        return deps.build();
    };
    CxxTest test;
    CxxTestType type = args.framework.orElse(getDefaultTestType());
    switch(type) {
        case GTEST:
            {
                test = new CxxGtestTest(testParams, ruleFinder, cxxLinkAndCompileRules.getBinaryRule(), cxxLinkAndCompileRules.executable, testEnv, testArgs, FluentIterable.from(args.resources).transform(p -> new PathSourcePath(params.getProjectFilesystem(), p)).toSortedSet(Ordering.natural()), additionalDeps, args.labels, args.contacts, args.runTestSeparately.orElse(false), args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs), cxxBuckConfig.getMaximumTestOutputSize());
                break;
            }
        case BOOST:
            {
                test = new CxxBoostTest(testParams, ruleFinder, cxxLinkAndCompileRules.getBinaryRule(), cxxLinkAndCompileRules.executable, testEnv, testArgs, FluentIterable.from(args.resources).transform(p -> new PathSourcePath(params.getProjectFilesystem(), p)).toSortedSet(Ordering.natural()), additionalDeps, args.labels, args.contacts, args.runTestSeparately.orElse(false), args.testRuleTimeoutMs.map(Optional::of).orElse(defaultTestRuleTimeoutMs));
                break;
            }
        default:
            {
                Preconditions.checkState(false, "Unhandled C++ test type: %s", type);
                throw new RuntimeException();
            }
    }
    return test;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Optional(java.util.Optional) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) HumanReadableException(com.facebook.buck.util.HumanReadableException) MacroException(com.facebook.buck.model.MacroException)

Aggregations

MacroException (com.facebook.buck.model.MacroException)20 BuildRule (com.facebook.buck.rules.BuildRule)10 Test (org.junit.Test)9 MacroHandler (com.facebook.buck.rules.macros.MacroHandler)8 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)7 HumanReadableException (com.facebook.buck.util.HumanReadableException)7 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)6 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)6 BuildTarget (com.facebook.buck.model.BuildTarget)5 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)4 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)4 ImmutableList (com.google.common.collect.ImmutableList)4 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)3 WorkerMacroExpander (com.facebook.buck.rules.macros.WorkerMacroExpander)3 NoSuchBuildTargetException (com.facebook.buck.parser.NoSuchBuildTargetException)2 BuildRuleParams (com.facebook.buck.rules.BuildRuleParams)2 PathSourcePath (com.facebook.buck.rules.PathSourcePath)2 SourcePath (com.facebook.buck.rules.SourcePath)2 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)2 ExecutableMacroExpander (com.facebook.buck.rules.macros.ExecutableMacroExpander)2