Exemplo n.º 1
0
  private ProcessResult runBuckCommandWithEnvironmentAndContext(
      Optional<NGContext> context, String... args) throws IOException {
    assertTrue("setUp() must be run before this method is invoked", isSetUp);
    CapturingPrintStream stdout = new CapturingPrintStream();
    CapturingPrintStream stderr = new CapturingPrintStream();

    final ImmutableList.Builder<BuckEvent> capturedEventsListBuilder =
        new ImmutableList.Builder<>();
    BuckEventListener capturingEventListener =
        new BuckEventListener() {
          @Subscribe
          public void captureEvent(BuckEvent event) {
            capturedEventsListBuilder.add(event);
          }

          @Override
          public void outputTrace(BuildId buildId) throws InterruptedException {
            // empty
          }
        };

    Main main = new Main(stdout, stderr, Optional.of(capturingEventListener));
    int exitCode = 0;
    try {
      exitCode = main.runMainWithExitCode(new BuildId(), destDir, context, args);
    } catch (InterruptedException e) {
      e.printStackTrace(stderr);
      exitCode = Main.FAIL_EXIT_CODE;
      Thread.currentThread().interrupt();
    }

    return new ProcessResult(
        exitCode,
        stdout.getContentsAsString(Charsets.UTF_8),
        stderr.getContentsAsString(Charsets.UTF_8),
        capturedEventsListBuilder.build());
  }
Exemplo n.º 2
0
  public ProcessResult runBuckCommandWithEnvironmentOverridesAndContext(
      Path repoRoot,
      Optional<NGContext> context,
      ImmutableMap<String, String> environmentOverrides,
      CapturingPrintStream stderr,
      String... args)
      throws IOException {
    assertTrue("setUp() must be run before this method is invoked", isSetUp);
    CapturingPrintStream stdout = new CapturingPrintStream();
    InputStream stdin = new ByteArrayInputStream("".getBytes());

    // Construct a limited view of the parent environment for the child.
    // TODO(#5754812): we should eventually get tests working without requiring these be set.
    ImmutableList<String> inheritedEnvVars =
        ImmutableList.of(
            "ANDROID_HOME",
            "ANDROID_NDK",
            "ANDROID_NDK_REPOSITORY",
            "ANDROID_SDK",
            // TODO(grumpyjames) Write an equivalent of the groovyc and startGroovy
            // scripts provided by the groovy distribution in order to remove these two.
            "GROOVY_HOME",
            "JAVA_HOME",
            "NDK_HOME",
            "PATH",
            "PATHEXT",

            // Needed by ndk-build on Windows
            "OS",
            "ProgramW6432",
            "ProgramFiles(x86)",

            // The haskell integration tests call into GHC, which needs HOME to be set.
            "HOME",

            // TODO(#6586154): set TMP variable for ShellSteps
            "TMP");
    Map<String, String> envBuilder = new HashMap<>();
    for (String variable : inheritedEnvVars) {
      String value = System.getenv(variable);
      if (value != null) {
        envBuilder.put(variable, value);
      }
    }
    envBuilder.putAll(environmentOverrides);
    ImmutableMap<String, String> sanizitedEnv = ImmutableMap.copyOf(envBuilder);

    Main main = new Main(stdout, stderr, stdin);
    int exitCode;
    try {
      exitCode =
          main.runMainWithExitCode(
              new BuildId(),
              repoRoot,
              context,
              sanizitedEnv,
              CommandMode.TEST,
              WatchmanWatcher.FreshInstanceAction.NONE,
              args);
    } catch (InterruptedException e) {
      e.printStackTrace(stderr);
      exitCode = Main.FAIL_EXIT_CODE;
      Thread.currentThread().interrupt();
    }

    return new ProcessResult(
        exitCode,
        stdout.getContentsAsString(Charsets.UTF_8),
        stderr.getContentsAsString(Charsets.UTF_8));
  }
Exemplo n.º 3
0
  public ProcessResult runBuckCommandWithEnvironmentAndContext(
      Path repoRoot,
      Optional<NGContext> context,
      Optional<BuckEventListener> eventListener,
      Optional<ImmutableMap<String, String>> env,
      String... args)
      throws IOException {
    assertTrue("setUp() must be run before this method is invoked", isSetUp);
    CapturingPrintStream stdout = new CapturingPrintStream();
    final CapturingPrintStream stderr = new CapturingPrintStream();

    final ImmutableList.Builder<BuckEvent> capturedEventsListBuilder =
        new ImmutableList.Builder<>();
    BuckEventListener capturingEventListener =
        new BuckEventListener() {
          @Subscribe
          public void captureEvent(BuckEvent event) {
            if (event instanceof ConsoleEvent) {
              try {
                stderr.write(((ConsoleEvent) event).getMessage().getBytes(Charsets.UTF_8));
              } catch (IOException e) {
                throw new RuntimeException(e);
              }
            }
            capturedEventsListBuilder.add(event);
          }

          @Override
          public void outputTrace(BuildId buildId) throws InterruptedException {
            // empty
          }
        };
    ImmutableList.Builder<BuckEventListener> eventListeners = ImmutableList.builder();
    eventListeners.add(capturingEventListener);
    if (eventListener.isPresent()) {
      eventListeners.add(eventListener.get());
    }

    // Construct a limited view of the parent environment for the child.
    //
    // TODO(#5754812): we should eventually get tests working without requiring these be set.
    ImmutableList<String> inheritedEnvVars =
        ImmutableList.of(
            "ANDROID_HOME",
            "ANDROID_NDK",
            "ANDROID_NDK_REPOSITORY",
            "ANDROID_SDK",
            "NDK_HOME",
            "PATH",
            "PATHEXT",

            // Needed by ndk-build on Windows
            "OS",
            "ProgramW6432",
            "ProgramFiles(x86)",

            // TODO(#6586154): set TMP variable for ShellSteps
            "TMP");
    ImmutableMap.Builder<String, String> envBuilder = ImmutableMap.builder();
    for (String variable : inheritedEnvVars) {
      String value = System.getenv(variable);
      if (value != null) {
        envBuilder.put(variable, value);
      }
    }
    ImmutableMap<String, String> sanizitedEnv = envBuilder.build();

    Main main = new Main(stdout, stderr, eventListeners.build());
    int exitCode = 0;
    try {
      exitCode =
          main.runMainWithExitCode(new BuildId(), repoRoot, context, env.or(sanizitedEnv), args);
    } catch (InterruptedException e) {
      e.printStackTrace(stderr);
      exitCode = Main.FAIL_EXIT_CODE;
      Thread.currentThread().interrupt();
    }

    return new ProcessResult(
        exitCode,
        stdout.getContentsAsString(Charsets.UTF_8),
        stderr.getContentsAsString(Charsets.UTF_8),
        capturedEventsListBuilder.build());
  }