Search in sources :

Example 61 with TestConsole

use of com.facebook.buck.testutil.TestConsole in project buck by facebook.

the class ProcessExecutorTest method testProcessTimeoutHandlerIsInvoked.

@Test
public void testProcessTimeoutHandlerIsInvoked() throws IOException, InterruptedException {
    @SuppressWarnings("PMD.PrematureDeclaration") ProcessExecutor executor = new DefaultProcessExecutor(new TestConsole(Verbosity.ALL));
    final AtomicBoolean called = new AtomicBoolean(false);
    String cmd = (Platform.detect() == Platform.WINDOWS) ? "ping -n 50 0.0.0.0" : "sleep 50";
    ProcessExecutorParams params = ProcessExecutorParams.ofCommand(makeCommandArray(cmd));
    ProcessExecutor.Result result = executor.launchAndExecute(params, /* options */
    ImmutableSet.<ProcessExecutor.Option>builder().build(), /* stdin */
    Optional.empty(), /* timeOutMs */
    Optional.of((long) 100), /* timeOutHandler */
    Optional.of(ignored -> called.set(true)));
    assertTrue("process was reported as timed out", result.isTimedOut());
    assertTrue("timeOutHandler was called when a timeout was hit", called.get());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) TestConsole(com.facebook.buck.testutil.TestConsole) Assert.assertFalse(org.junit.Assert.assertFalse) StringTokenizer(java.util.StringTokenizer) Platform(com.facebook.buck.util.environment.Platform) Assert.assertTrue(org.junit.Assert.assertTrue) Optional(java.util.Optional) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) IOException(java.io.IOException) Assert.assertEquals(org.junit.Assert.assertEquals) EnumSet(java.util.EnumSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 62 with TestConsole

use of com.facebook.buck.testutil.TestConsole in project buck by facebook.

the class AndroidBinaryIntegrationTest method testNativeLibraryMerging.

@Test
public void testNativeLibraryMerging() throws IOException, InterruptedException {
    NdkCxxPlatform platform = AndroidNdkHelper.getNdkCxxPlatform(workspace, filesystem);
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    Path tmpDir = tmpFolder.newFolder("merging_tmp");
    SymbolGetter syms = new SymbolGetter(new DefaultProcessExecutor(new TestConsole()), tmpDir, platform.getObjdump(), pathResolver);
    SymbolsAndDtNeeded info;
    workspace.replaceFileContents(".buckconfig", "#cpu_abis", "cpu_abis = x86");
    Map<String, Path> paths = workspace.buildMultipleAndReturnOutputs("//apps/sample:app_with_merged_libs", "//apps/sample:app_with_alternate_merge_glue", "//apps/sample:app_with_merged_libs_modular");
    Path apkPath = paths.get("//apps/sample:app_with_merged_libs");
    ZipInspector zipInspector = new ZipInspector(apkPath);
    zipInspector.assertFileDoesNotExist("lib/x86/lib1a.so");
    zipInspector.assertFileDoesNotExist("lib/x86/lib1b.so");
    zipInspector.assertFileDoesNotExist("lib/x86/lib2e.so");
    zipInspector.assertFileDoesNotExist("lib/x86/lib2f.so");
    info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/lib1.so");
    assertThat(info.symbols.global, Matchers.hasItem("A"));
    assertThat(info.symbols.global, Matchers.hasItem("B"));
    assertThat(info.symbols.global, Matchers.hasItem("glue_1"));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
    assertThat(info.dtNeeded, Matchers.hasItem("libnative_merge_C.so"));
    assertThat(info.dtNeeded, Matchers.hasItem("libnative_merge_D.so"));
    assertThat(info.dtNeeded, not(Matchers.hasItem("libnative_merge_B.so")));
    info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/libnative_merge_C.so");
    assertThat(info.symbols.global, Matchers.hasItem("C"));
    assertThat(info.symbols.global, Matchers.hasItem("static_func_C"));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_1")));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
    assertThat(info.dtNeeded, Matchers.hasItem("libnative_merge_D.so"));
    assertThat(info.dtNeeded, Matchers.hasItem("libprebuilt_for_C.so"));
    info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/libnative_merge_D.so");
    assertThat(info.symbols.global, Matchers.hasItem("D"));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_1")));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
    assertThat(info.dtNeeded, Matchers.hasItem("lib2.so"));
    assertThat(info.dtNeeded, not(Matchers.hasItem("libnative_merge_E.so")));
    assertThat(info.dtNeeded, not(Matchers.hasItem("libnative_merge_F.so")));
    info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/lib2.so");
    assertThat(info.symbols.global, Matchers.hasItem("E"));
    assertThat(info.symbols.global, Matchers.hasItem("F"));
    assertThat(info.symbols.global, Matchers.hasItem("static_func_F"));
    assertThat(info.symbols.global, Matchers.hasItem("glue_1"));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
    assertThat(info.dtNeeded, Matchers.hasItem("libprebuilt_for_F.so"));
    Path otherPath = paths.get("//apps/sample:app_with_alternate_merge_glue");
    info = syms.getSymbolsAndDtNeeded(otherPath, "lib/x86/lib2.so");
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_1")));
    assertThat(info.symbols.global, Matchers.hasItem("glue_2"));
    assertThat(info.dtNeeded, Matchers.hasItem("libprebuilt_for_F.so"));
    Path modularPath = paths.get("//apps/sample:app_with_merged_libs_modular");
    ZipInspector modularZipInspector = new ZipInspector(modularPath);
    modularZipInspector.assertFileDoesNotExist("lib/x86/lib1a.so");
    modularZipInspector.assertFileDoesNotExist("lib/x86/lib1b.so");
    modularZipInspector.assertFileDoesNotExist("lib/x86/lib2e.so");
    modularZipInspector.assertFileDoesNotExist("lib/x86/lib2f.so");
    modularZipInspector.assertFileExists("assets/native.merge.A/libs.txt");
    modularZipInspector.assertFileExists("assets/native.merge.A/libs.xzs");
    modularZipInspector.assertFileDoesNotExist("lib/x86/lib1.so");
    modularZipInspector.assertFileDoesNotExist("lib/x86/lib2.so");
    Path disassembly = workspace.buildAndReturnOutput("//apps/sample:disassemble_app_with_merged_libs_gencode");
    List<String> disassembledLines = filesystem.readLines(disassembly);
    Pattern fieldPattern = Pattern.compile("^\\.field public static final ([^:]+):Ljava/lang/String; = \"([^\"]+)\"$");
    ImmutableMap.Builder<String, String> mapBuilder = ImmutableMap.builder();
    for (String line : disassembledLines) {
        Matcher m = fieldPattern.matcher(line);
        if (!m.matches()) {
            continue;
        }
        mapBuilder.put(m.group(1), m.group(2));
    }
    assertThat(mapBuilder.build(), Matchers.equalTo(ImmutableMap.of("lib1a_so", "lib1_so", "lib1b_so", "lib1_so", "lib2e_so", "lib2_so", "lib2f_so", "lib2_so")));
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) SymbolsAndDtNeeded(com.facebook.buck.android.AndroidNdkHelper.SymbolsAndDtNeeded) Matcher(java.util.regex.Matcher) ZipInspector(com.facebook.buck.testutil.integration.ZipInspector) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ImmutableMap(com.google.common.collect.ImmutableMap) SymbolGetter(com.facebook.buck.android.AndroidNdkHelper.SymbolGetter) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 63 with TestConsole

use of com.facebook.buck.testutil.TestConsole in project buck by facebook.

the class AdbHelperTest method createAdbHelper.

private AdbHelper createAdbHelper(ExecutionContext executionContext, AdbOptions adbOptions, TargetDeviceOptions targetDeviceOptions) throws CmdLineException {
    Console console = new TestConsole();
    BuckEventBus eventBus = BuckEventBusFactory.newInstance();
    return new AdbHelper(adbOptions, targetDeviceOptions, executionContext, console, eventBus, true) {

        @Override
        protected boolean isDeviceTempWritable(IDevice device, String name) {
            return true;
        }
    };
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) Console(com.facebook.buck.util.Console) TestConsole(com.facebook.buck.testutil.TestConsole) IDevice(com.android.ddmlib.IDevice) TestConsole(com.facebook.buck.testutil.TestConsole)

Example 64 with TestConsole

use of com.facebook.buck.testutil.TestConsole in project buck by facebook.

the class AdbHelperTest method testQuietDeviceInstall.

@Test
public void testQuietDeviceInstall() throws InterruptedException {
    final File apk = new File("/some/file.apk");
    final AtomicReference<String> apkPath = new AtomicReference<>();
    TestDevice device = new TestDevice() {

        @Override
        public void installPackage(String s, boolean b, String... strings) throws InstallException {
            apkPath.set(s);
        }
    };
    device.setSerialNumber("serial#1");
    device.setName("testDevice");
    final List<IDevice> deviceList = Lists.newArrayList((IDevice) device);
    TestConsole console = new TestConsole();
    BuckEventBus eventBus = BuckEventBusFactory.newInstance();
    AdbHelper adbHelper = new AdbHelper(new AdbOptions(), new TargetDeviceOptions(), TestExecutionContext.newInstance(), console, eventBus, true) {

        @Override
        protected boolean isDeviceTempWritable(IDevice device, String name) {
            return true;
        }

        @Override
        public List<IDevice> getDevices(boolean quiet) {
            return deviceList;
        }
    };
    boolean success = adbHelper.adbCall(new AdbHelper.AdbCallable() {

        @Override
        public boolean call(IDevice device) throws Exception {
            return basicAdbHelper.installApkOnDevice(device, apk, false, true);
        }

        @Override
        public String toString() {
            return "install apk";
        }
    }, true);
    assertTrue(success);
    assertEquals(apk.getAbsolutePath(), apkPath.get());
    assertEquals("", console.getTextWrittenToStdOut());
    assertEquals("", console.getTextWrittenToStdErr());
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) IDevice(com.android.ddmlib.IDevice) AtomicReference(java.util.concurrent.atomic.AtomicReference) AdbOptions(com.facebook.buck.step.AdbOptions) CmdLineException(org.kohsuke.args4j.CmdLineException) InstallException(com.android.ddmlib.InstallException) TargetDeviceOptions(com.facebook.buck.step.TargetDeviceOptions) TestConsole(com.facebook.buck.testutil.TestConsole) File(java.io.File) Test(org.junit.Test)

Example 65 with TestConsole

use of com.facebook.buck.testutil.TestConsole in project buck by facebook.

the class AndroidBinaryIntegrationTest method testNativeLibraryCrossCellMerging.

@Test
public void testNativeLibraryCrossCellMerging() throws IOException, InterruptedException {
    // Set up a cross-cell workspace
    ProjectWorkspace secondary = TestDataHelper.createProjectWorkspaceForScenario(new AndroidBinaryIntegrationTest(), "android_project/secondary", secondaryFolder);
    secondary.setUp();
    NdkCxxPlatform platform = AndroidNdkHelper.getNdkCxxPlatform(workspace, filesystem);
    SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
    Path tmpDir = tmpFolder.newFolder("merging_tmp");
    SymbolGetter syms = new SymbolGetter(new DefaultProcessExecutor(new TestConsole()), tmpDir, platform.getObjdump(), pathResolver);
    SymbolsAndDtNeeded info;
    TestDataHelper.overrideBuckconfig(workspace, ImmutableMap.of("ndk", ImmutableMap.of("cpu_abis", "x86"), "repositories", ImmutableMap.of("secondary", secondary.getPath(".").normalize().toString())));
    workspace.replaceFileContents(workspace.getPath("apps/sample/BUCK").normalize().toString(), "#'secondary//merge:G'", "'secondary//merge:G'");
    Path apkPath = workspace.buildAndReturnOutput("//apps/sample:app_with_merged_cross_cell_libs");
    ZipInspector zipInspector = new ZipInspector(apkPath);
    zipInspector.assertFileDoesNotExist("lib/x86/lib1a.so");
    zipInspector.assertFileDoesNotExist("lib/x86/lib1b.so");
    zipInspector.assertFileDoesNotExist("lib/x86/lib1g.so");
    zipInspector.assertFileDoesNotExist("lib/x86/lib1h.so");
    info = syms.getSymbolsAndDtNeeded(apkPath, "lib/x86/lib1.so");
    assertThat(info.symbols.global, Matchers.hasItem("A"));
    assertThat(info.symbols.global, Matchers.hasItem("B"));
    assertThat(info.symbols.global, Matchers.hasItem("G"));
    assertThat(info.symbols.global, Matchers.hasItem("H"));
    assertThat(info.symbols.global, Matchers.hasItem("glue_1"));
    assertThat(info.symbols.global, not(Matchers.hasItem("glue_2")));
    assertThat(info.dtNeeded, not(Matchers.hasItem("libnative_merge_B.so")));
    assertThat(info.dtNeeded, not(Matchers.hasItem("libmerge_G.so")));
    assertThat(info.dtNeeded, not(Matchers.hasItem("libmerge_H.so")));
}
Also used : Path(java.nio.file.Path) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) SymbolsAndDtNeeded(com.facebook.buck.android.AndroidNdkHelper.SymbolsAndDtNeeded) ZipInspector(com.facebook.buck.testutil.integration.ZipInspector) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) SymbolGetter(com.facebook.buck.android.AndroidNdkHelper.SymbolGetter) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Aggregations

TestConsole (com.facebook.buck.testutil.TestConsole)100 Test (org.junit.Test)74 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)27 DefaultProcessExecutor (com.facebook.buck.util.DefaultProcessExecutor)27 ExecutionContext (com.facebook.buck.step.ExecutionContext)25 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)25 Path (java.nio.file.Path)21 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)16 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)16 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)16 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)16 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)16 BuckEventBus (com.facebook.buck.event.BuckEventBus)15 FakeProcess (com.facebook.buck.util.FakeProcess)14 ProcessExecutor (com.facebook.buck.util.ProcessExecutor)13 ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)12 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)11 Clock (com.facebook.buck.timing.Clock)10 FakeProcessExecutor (com.facebook.buck.util.FakeProcessExecutor)9 BuildTarget (com.facebook.buck.model.BuildTarget)8