text:
// Copyright 2017 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.exec.local;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;
import static org.junit.Assume.assumeTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.matches;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.google.devtools.build.lib.actions.ActionContext;
import com.google.devtools.build.lib.actions.ActionInput;
import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander;
import com.google.devtools.build.lib.actions.CommandLines.ParamFileActionInput;
import com.google.devtools.build.lib.actions.ExecutionRequirements;
import com.google.devtools.build.lib.actions.LocalHostCapacity;
import com.google.devtools.build.lib.actions.MetadataProvider;
import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType;
import com.google.devtools.build.lib.actions.ResourceManager;
import com.google.devtools.build.lib.actions.ResourceSet;
import com.google.devtools.build.lib.actions.Spawn;
import com.google.devtools.build.lib.actions.SpawnResult;
import com.google.devtools.build.lib.actions.cache.MetadataInjector;
import com.google.devtools.build.lib.exec.BinTools;
import com.google.devtools.build.lib.exec.RunfilesTreeUpdater;
import com.google.devtools.build.lib.exec.SpawnRunner.ProgressStatus;
import com.google.devtools.build.lib.exec.SpawnRunner.SpawnExecutionContext;
import com.google.devtools.build.lib.exec.util.SpawnBuilder;
import com.google.devtools.build.lib.runtime.ProcessWrapper;
import com.google.devtools.build.lib.shell.JavaSubprocessFactory;
import com.google.devtools.build.lib.shell.Subprocess;
import com.google.devtools.build.lib.shell.SubprocessBuilder;
import com.google.devtools.build.lib.shell.SubprocessBuilder.StreamAction;
import com.google.devtools.build.lib.shell.SubprocessFactory;
import com.google.devtools.build.lib.testutil.BlazeTestUtils;
import com.google.devtools.build.lib.testutil.TestConstants;
import com.google.devtools.build.lib.testutil.TestUtils;
import com.google.devtools.build.lib.unix.UnixFileSystem;
import com.google.devtools.build.lib.util.NetUtil;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.util.io.FileOutErr;
import com.google.devtools.build.lib.vfs.DigestHashFunction;
import com.google.devtools.build.lib.vfs.FileSystem;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.JavaIoFileSystem;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
import com.google.devtools.common.options.Converters.RegexPatternConverter;
import com.google.devtools.common.options.Options;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Filter;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

/** Unit tests for {@link LocalSpawnRunner}. */
@RunWith(JUnit4.class)
public class LocalSpawnRunnerTest {

  private static class TestedLocalSpawnRunner extends LocalSpawnRunner {
    public TestedLocalSpawnRunner(
        Path execRoot,
        LocalExecutionOptions localExecutionOptions,
        ResourceManager resourceManager,
        ProcessWrapper processWrapper,
        LocalEnvProvider localEnvProvider) {
      super(
          execRoot,
          localExecutionOptions,
          resourceManager,
          localEnvProvider,
          /*binTools=*/ null,
          processWrapper,
          Mockito.mock(RunfilesTreeUpdater.class));
    }

    // Rigged to act on supplied filesystem (e.g. InMemoryFileSystem) for testing purposes
    // TODO(b/70572634): Update FileSystem abstraction to support createTempDirectory() from
    // the java.nio.file.Files package.
    @Override
    protected Path createActionTemp(Path execRoot) throws IOException {
      Path tempDirPath;
      do {
        String idStr =
            Long.toHexString(Thread.currentThread().getId())
                + "_"
                + Long.toHexString(ThreadLocalRandom.current().nextLong());
        tempDirPath = execRoot.getRelative("tmp" + idStr);
      } while (tempDirPath.exists());
      if (!tempDirPath.createDirectory()) {
        throw new IOException(String.format("Could not create temp directory '%s'", tempDirPath));
      }
      return tempDirPath;
    }
  }

  private static class FinishedSubprocess implements Subprocess {
    private final int exitCode;

    public FinishedSubprocess(int exitCode) {
      this.exitCode = exitCode;
    }

    @Override
    public boolean destroy() {
      return false;
    }

    @Override
    public int exitValue() {
      return exitCode;
    }

    @Override
    public boolean finished() {
      return true;
    }

    @Override
    public boolean isAlive() {
      return false;
    }

    @Override
    public boolean timedout() {
      return false;
    }

    @Override
    public void waitFor() throws InterruptedException {
      // Do nothing.
    }

    @Override
    public OutputStream getOutputStream() {
      return ByteStreams.nullOutputStream();
    }

    @Override
    public InputStream getInputStream() {
      return new ByteArrayInputStream(new byte[0]);
    }

    @Override
    public InputStream getErrorStream() {
      return new ByteArrayInputStream(new byte[0]);
    }

    @Override
    public void close() {
      // Do nothing.
    }
  }

  private static final Spawn SIMPLE_SPAWN =
      new SpawnBuilder("/bin/echo", "Hi!").withEnvironment("VARIABLE", "value").build();

  private static final class SubprocessInterceptor implements SubprocessFactory {
    @Override
    public Subprocess create(SubprocessBuilder params) throws IOException {
      throw new UnsupportedOperationException();
    }
  }

  private final class SpawnExecutionContextForTesting implements SpawnExecutionContext {
    private final List<ProgressStatus> reportedStatus = new ArrayList<>();
    private final TreeMap<PathFragment, ActionInput> inputMapping = new TreeMap<>();

    private long timeoutMillis;
    private boolean prefetchCalled;
    private boolean lockOutputFilesCalled;
    private FileOutErr fileOutErr;

    public SpawnExecutionContextForTesting(FileOutErr fileOutErr) {
      this.fileOutErr = fileOutErr;
    }

    @Override
    public int getId() {
      return 0;
    }

    @Override
    public void prefetchInputs() throws IOException {
      prefetchCalled = true;
    }

    @Override
    public void lockOutputFiles() throws InterruptedException {
      lockOutputFilesCalled = true;
    }

    @Override
    public boolean speculating() {
      return false;
    }

    @Override
    public MetadataProvider getMetadataProvider() {
      return mockFileCache;
    }

    @Override
    public ArtifactExpander getArtifactExpander() {
      throw new UnsupportedOperationException();
    }

    @Override
    public Duration getTimeout() {
      return Duration.ofMillis(timeoutMillis);
    }

    @Override
    public FileOutErr getFileOutErr() {
      return fileOutErr;
    }

    @Override
    public SortedMap<PathFragment, ActionInput> getInputMapping() {
      return inputMapping;
    }

    @Override
    public void report(ProgressStatus state, String name) {
      reportedStatus.add(state);
    }

    @Override
    public MetadataInjector getMetadataInjector() {
      throw new UnsupportedOperationException();
    }

    @Override
    public boolean isRewindingEnabled() {
      return false;
    }

    @Override
    public void checkForLostInputs() {}

    @Override
    public <T extends ActionContext> T getContext(Class<T> identifyingType) {
      throw new UnsupportedOperationException();
    }
  }

  private final MetadataProvider mockFileCache = mock(MetadataProvider.class);
  private final ResourceManager resourceManager = ResourceManager.instanceForTestingOnly();

  private Logger logger;

  private static ImmutableMap<String, String> keepLocalEnvUnchanged(
      Map<String, String> env, BinTools binTools, String fallbackTmpDir) {
    return ImmutableMap.copyOf(env);
  }

  @Before
  public final void suppressLogging() {
    logger = Logger.getLogger(TestedLocalSpawnRunner.class.getName());
    logger.setFilter(new Filter() {
      @Override
      public boolean isLoggable(LogRecord record) {
        return false;
      }
    });
  }

  private FileSystem setupEnvironmentForFakeExecution() {
    // Prevent any subprocess execution at all.
    SubprocessBuilder.setDefaultSubprocessFactory(new SubprocessInterceptor());
    resourceManager.setAvailableResources(
        ResourceSet.create(/*memoryMb=*/1, /*cpuUsage=*/1, /*localTestCount=*/1));
    return new InMemoryFileSystem(DigestHashFunction.SHA256);
  }

  private static ProcessWrapper makeProcessWrapper(FileSystem fs, LocalExecutionOptions options) {
    return new ProcessWrapper(
        fs.getPath("/process-wrapper"),
        options.getLocalSigkillGraceSeconds(),
        /*gracefulSigterm=*/ false);
  }

  /**
   * Enables real execution by default.
   *
   * <p>Tests should call setupEnvironmentForFakeExecution() if they do not want real execution.
   */
  @Before
  public final void setupEnvironmentForRealExecution() {
    SubprocessBuilder.setDefaultSubprocessFactory(JavaSubprocessFactory.INSTANCE);
    resourceManager.setAvailableResources(LocalHostCapacity.getLocalHostCapacity());
  }

  @Test
  public void vanillaZeroExit() throws Exception {
    // TODO(#3536): Make this test work on Windows.
    // The Command API implicitly absolutizes the path, and we get weird paths on Windows:
    // T:\execroot\execroot\_bin\process-wrapper
    assumeTrue(OS.getCurrent() != OS.WINDOWS);

    FileSystem fs = setupEnvironmentForFakeExecution();

    SubprocessFactory factory = mock(SubprocessFactory.class);
    ArgumentCaptor<SubprocessBuilder> captor = ArgumentCaptor.forClass(SubprocessBuilder.class);
    when(factory.create(captor.capture())).thenReturn(new FinishedSubprocess(0));
    SubprocessBuilder.setDefaultSubprocessFactory(factory);

    LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
    options.localSigkillGraceSeconds = 456;
    LocalSpawnRunner runner =
        new TestedLocalSpawnRunner(
            fs.getPath("/execroot"),
            options,
            resourceManager,
            makeProcessWrapper(fs, options),
            LocalSpawnRunnerTest::keepLocalEnvUnchanged);

    FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
    policy.timeoutMillis = 123 * 1000L;
    assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
    SpawnResult result = runner.execAsync(SIMPLE_SPAWN, policy).get();
    verify(factory).create(any(SubprocessBuilder.class));
    assertThat(result.status()).isEqualTo(SpawnResult.Status.SUCCESS);
    assertThat(result.exitCode()).isEqualTo(0);
    assertThat(result.setupSuccess()).isTrue();
    assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());

    assertThat(captor.getValue().getArgv())
        .containsExactlyElementsIn(
            ImmutableList.of(
                "/process-wrapper", "--timeout=123", "--kill_delay=456", "/bin/echo", "Hi!"));
    assertThat(captor.getValue().getEnv()).containsExactly("VARIABLE", "value");
    assertThat(captor.getValue().getTimeoutMillis()).isEqualTo(0);
    assertThat(captor.getValue().getStdout()).isEqualTo(StreamAction.REDIRECT);
    assertThat(captor.getValue().getStdoutFile()).isEqualTo(new File("/out/stdout"));
    assertThat(captor.getValue().getStderr()).isEqualTo(StreamAction.REDIRECT);
    assertThat(captor.getValue().getStderrFile()).isEqualTo(new File("/out/stderr"));

    assertThat(policy.lockOutputFilesCalled).isTrue();
    assertThat(policy.reportedStatus)
        .containsExactly(ProgressStatus.SCHEDULING, ProgressStatus.EXECUTING).inOrder();
  }

  @Test
  public void testParamFiles() throws Exception {
    // TODO(#3536): Make this test work on Windows.
    // The Command API implicitly absolutizes the path, and we get weird paths on Windows:
    // T:\execroot\execroot\_bin\process-wrapper
    assumeTrue(OS.getCurrent() != OS.WINDOWS);

    FileSystem fs = setupEnvironmentForFakeExecution();

    SubprocessFactory factory = mock(SubprocessFactory.class);
    when(factory.create(any())).thenReturn(new FinishedSubprocess(0));
    SubprocessBuilder.setDefaultSubprocessFactory(factory);

    LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
    options.localSigkillGraceSeconds = 456;
    Path execRoot = fs.getPath("/execroot");
    LocalSpawnRunner runner =
        new TestedLocalSpawnRunner(
            execRoot,
            options,
            resourceManager,
            makeProcessWrapper(fs, options),
            LocalSpawnRunnerTest::keepLocalEnvUnchanged);
    ParamFileActionInput paramFileActionInput =
        new ParamFileActionInput(
            PathFragment.create("some/dir/params"),
            ImmutableList.of("--foo", "--bar"),
            ParameterFileType.UNQUOTED,
            UTF_8);
    Spawn spawn =
        new SpawnBuilder("/bin/echo", "Hi!")
            .withInput(paramFileActionInput)
            .withEnvironment("VARIABLE", "value")
            .build();
    FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
    policy.timeoutMillis = 123 * 1000L;
    assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
    SpawnResult result = runner.execAsync(spawn, policy).get();
    assertThat(result.status()).isEqualTo(SpawnResult.Status.SUCCESS);
    assertThat(result.exitCode()).isEqualTo(0);
    assertThat(result.setupSuccess()).isTrue();
    assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
    Path paramFile = execRoot.getRelative("some/dir/params");
    assertThat(paramFile.exists()).isTrue();
    try (InputStream inputStream = paramFile.getInputStream()) {
      assertThat(new String(ByteStreams.toByteArray(inputStream), UTF_8).split("\n"))
          .asList()
          .containsExactly("--foo", "--bar");
    }
  }

  @Test
  public void noProcessWrapper() throws Exception {
    // TODO(#3536): Make this test work on Windows.
    // The Command API implicitly absolutizes the path, and we get weird paths on Windows:
    // T:\execroot\bin\echo
    assumeTrue(OS.getCurrent() != OS.WINDOWS);

    FileSystem fs = setupEnvironmentForFakeExecution();

    SubprocessFactory factory = mock(SubprocessFactory.class);
    ArgumentCaptor<SubprocessBuilder> captor = ArgumentCaptor.forClass(SubprocessBuilder.class);
    when(factory.create(captor.capture())).thenReturn(new FinishedSubprocess(0));
    SubprocessBuilder.setDefaultSubprocessFactory(factory);

    LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
    options.localSigkillGraceSeconds = 456;
    LocalSpawnRunner runner =
        new TestedLocalSpawnRunner(
            fs.getPath("/execroot"),
            options,
            resourceManager,
            /*processWrapper=*/ null,
            LocalSpawnRunnerTest::keepLocalEnvUnchanged);

    FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
    policy.timeoutMillis = 123 * 1000L;
    assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
    SpawnResult result = runner.execAsync(SIMPLE_SPAWN, policy).get();
    verify(factory).create(any());
    assertThat(result.status()).isEqualTo(SpawnResult.Status.SUCCESS);
    assertThat(result.exitCode()).isEqualTo(0);
    assertThat(result.setupSuccess()).isTrue();
    assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());

    assertThat(captor.getValue().getArgv())
        .containsExactlyElementsIn(ImmutableList.of("/bin/echo", "Hi!"));
    assertThat(captor.getValue().getEnv()).containsExactly("VARIABLE", "value");
    // Without the process wrapper, we use the Command API to enforce the timeout.
    assertThat(captor.getValue().getTimeoutMillis()).isEqualTo(policy.timeoutMillis);

    assertThat(policy.lockOutputFilesCalled).isTrue();
  }

  @Test
  public void nonZeroExit() throws Exception {
    // TODO(#3536): Make this test work on Windows.
    // The Command API implicitly absolutizes the path, and we get weird paths on Windows:
    // T:\execroot\execroot\_bin\process-wrapper
    assumeTrue(OS.getCurrent() != OS.WINDOWS);

    FileSystem fs = setupEnvironmentForFakeExecution();

    SubprocessFactory factory = mock(SubprocessFactory.class);
    ArgumentCaptor<SubprocessBuilder> captor = ArgumentCaptor.forClass(SubprocessBuilder.class);
    when(factory.create(captor.capture())).thenReturn(new FinishedSubprocess(3));
    SubprocessBuilder.setDefaultSubprocessFactory(factory);

    LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
    LocalSpawnRunner runner =
        new TestedLocalSpawnRunner(
            fs.getPath("/execroot"),
            options,
            resourceManager,
            makeProcessWrapper(fs, options),
            LocalSpawnRunnerTest::keepLocalEnvUnchanged);

    assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
    FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
    SpawnResult result = runner.execAsync(SIMPLE_SPAWN, policy).get();
    verify(factory).create(any(SubprocessBuilder.class));
    assertThat(result.status()).isEqualTo(SpawnResult.Status.NON_ZERO_EXIT);
    assertThat(result.exitCode()).isEqualTo(3);
    assertThat(result.setupSuccess()).isTrue();
    assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());

    assertThat(captor.getValue().getArgv())
        .containsExactlyElementsIn(
            ImmutableList.of(
                "/process-wrapper", "--timeout=0", "--kill_delay=15", "/bin/echo", "Hi!"));
    assertThat(captor.getValue().getEnv()).containsExactly("VARIABLE", "value");
    assertThat(captor.getValue().getStdout()).isEqualTo(StreamAction.REDIRECT);
    assertThat(captor.getValue().getStdoutFile()).isEqualTo(new File("/out/stdout"));
    assertThat(captor.getValue().getStderr()).isEqualTo(StreamAction.REDIRECT);
    assertThat(captor.getValue().getStderrFile()).isEqualTo(new File("/out/stderr"));

    assertThat(policy.lockOutputFilesCalled).isTrue();
  }

  @Test
  public void processStartupThrows() throws Exception {
    FileSystem fs = setupEnvironmentForFakeExecution();

    SubprocessFactory factory = mock(SubprocessFactory.class);
    ArgumentCaptor<SubprocessBuilder> captor = ArgumentCaptor.forClass(SubprocessBuilder.class);
    when(factory.create(captor.capture())).thenThrow(new IOException("I'm sorry, Dave"));
    SubprocessBuilder.setDefaultSubprocessFactory(factory);

    LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
    LocalSpawnRunner runner =
        new TestedLocalSpawnRunner(
            fs.getPath("/execroot"),
            options,
            resourceManager,
            makeProcessWrapper(fs, options),
            LocalSpawnRunnerTest::keepLocalEnvUnchanged);

    assertThat(fs.getPath("/out").createDirectory()).isTrue();
    assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
    FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
    SpawnResult result = runner.execAsync(SIMPLE_SPAWN, policy).get();
    verify(factory).create(any(SubprocessBuilder.class));
    assertThat(result.status()).isEqualTo(SpawnResult.Status.EXECUTION_FAILED);
    assertThat(result.exitCode()).isEqualTo(-1);
    assertThat(result.setupSuccess()).isFalse();
    assertThat(result.getWallTime()).isEmpty();
    assertThat(result.getUserTime()).isEmpty();
    assertThat(result.getSystemTime()).isEmpty();
    assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());

    assertThat(FileSystemUtils.readContent(fs.getPath("/out/stderr"), UTF_8))
        .isEqualTo("Action failed to execute: java.io.IOException: I'm sorry, Dave\n");

    assertThat(policy.lockOutputFilesCalled).isTrue();
  }

  @Test
  public void disallowLocalExecution() throws Exception {
    FileSystem fs = setupEnvironmentForFakeExecution();

    LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
    options.allowedLocalAction = new RegexPatternConverter().convert("none");
    LocalSpawnRunner runner =
        new TestedLocalSpawnRunner(
            fs.getPath("/execroot"),
            options,
            resourceManager,
            makeProcessWrapper(fs, options),
            LocalSpawnRunnerTest::keepLocalEnvUnchanged);

    assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
    FileOutErr fileOutErr = new FileOutErr();
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
    SpawnResult reply = runner.execAsync(SIMPLE_SPAWN, policy).get();
    assertThat(reply.status()).isEqualTo(SpawnResult.Status.EXECUTION_DENIED);
    assertThat(reply.exitCode()).isEqualTo(-1);
    assertThat(reply.setupSuccess()).isFalse();
    assertThat(reply.getWallTime()).isEmpty();
    assertThat(reply.getUserTime()).isEmpty();
    assertThat(reply.getSystemTime()).isEmpty();
    assertThat(reply.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());

    // TODO(ulfjack): Maybe we should only lock after checking?
    assertThat(policy.lockOutputFilesCalled).isTrue();
  }

  @Test
  public void interruptedException() throws Exception {
    FileSystem fs = setupEnvironmentForFakeExecution();

    SubprocessFactory factory = mock(SubprocessFactory.class);
    ArgumentCaptor<SubprocessBuilder> captor = ArgumentCaptor.forClass(SubprocessBuilder.class);
    when(factory.create(captor.capture())).thenReturn(new FinishedSubprocess(3) {
      private boolean destroyed;

      @Override
      public boolean destroy() {
        destroyed = true;
        return true;
      }

      @Override
      public void waitFor() throws InterruptedException {
        if (!destroyed) {
          throw new InterruptedException();
        }
      }
    });
    SubprocessBuilder.setDefaultSubprocessFactory(factory);

    LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
    LocalSpawnRunner runner =
        new TestedLocalSpawnRunner(
            fs.getPath("/execroot"),
            options,
            resourceManager,
            makeProcessWrapper(fs, options),
            LocalSpawnRunnerTest::keepLocalEnvUnchanged);

    FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
    assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
    assertThrows(InterruptedException.class, () -> runner.execAsync(SIMPLE_SPAWN, policy).get());
    Thread.interrupted();
    assertThat(policy.lockOutputFilesCalled).isTrue();
  }

  @Test
  public void interruptWaitsForProcessExit() throws Exception {
    assumeTrue(OS.getCurrent() != OS.WINDOWS);

    Path tempDir = TestUtils.createUniqueTmpDir(new JavaIoFileSystem(DigestHashFunction.SHA256));

    LocalSpawnRunner runner =
        new LocalSpawnRunner(
            tempDir,
            Options.getDefaults(LocalExecutionOptions.class),
            resourceManager,
            LocalEnvProvider.forCurrentOs(ImmutableMap.of()),
            /*binTools=*/ null,
            /*processWrapper=*/ null,
            Mockito.mock(RunfilesTreeUpdater.class));
    FileOutErr fileOutErr =
        new FileOutErr(tempDir.getRelative("stdout"), tempDir.getRelative("stderr"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);

    // This test to exercise a race condition by attempting an operation multiple times. We can get
    // false positives (the test passing without us catching a problem), so try a few times. When
    // implementing this fix on 2019-09-11, this specific configuration was sufficient to catch the
    // previously-existent bug.
    int tries = 10;
    int delaySeconds = 1;

    Path content = tempDir.getChild("content");
    Path started = tempDir.getChild("started");
    // Start a subprocess that blocks until it is killed, and when it is, writes some output to
    // a temporary file after some delay.
    String script =
        "trap 'sleep "
            + delaySeconds
            + "; echo foo >"
            + content.getPathString()
            + "; exit 1' TERM; "
            + "touch "
            + started.getPathString()
            + "; "
            + "while :; do "
            + "  echo 'waiting to be killed'; "
            + "  sleep 1; "
            + "done";
    Spawn spawn = new SpawnBuilder("/bin/sh", "-c", script).build();

    ExecutorService executor = Executors.newSingleThreadExecutor();
    try {
      for (int i = 0; i < tries; i++) {
        content.delete();
        started.delete();
        Semaphore interruptCaught = new Semaphore(0);
        Future<?> future =
            executor.submit(
                () -> {
                  try {
                    runner.exec(spawn, policy);
                  } catch (InterruptedException e) {
                    interruptCaught.release();
                  } catch (Throwable t) {
                    throw new IllegalStateException(t);
                  }
                });
        // Wait until we know the subprocess has started so that delivering a termination signal
        // to it triggers the delayed write to the file.
        while (!started.exists()) {
          Thread.sleep(1);
        }
        future.cancel(true);
        interruptCaught.acquireUninterruptibly();
        // At this point, the subprocess must have fully stopped so write some content to the file
        // and expect that these contents remain unmodified.
        FileSystemUtils.writeContent(content, StandardCharsets.UTF_8, "bar");
        // Wait for longer than the spawn takes to exit before we check the file contents to ensure
        // that we properly awaited for termination of the subprocess.
        Thread.sleep(delaySeconds * 2 * 1000);
        assertThat(FileSystemUtils.readContent(content, StandardCharsets.UTF_8)).isEqualTo("bar");
      }
    } finally {
      executor.shutdown();
    }
  }

  @Test
  public void checkPrefetchCalled() throws Exception {
    FileSystem fs = setupEnvironmentForFakeExecution();

    SubprocessFactory factory = mock(SubprocessFactory.class);
    when(factory.create(any())).thenReturn(new FinishedSubprocess(0));
    SubprocessBuilder.setDefaultSubprocessFactory(factory);

    LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
    LocalSpawnRunner runner =
        new TestedLocalSpawnRunner(
            fs.getPath("/execroot"),
            options,
            resourceManager,
            makeProcessWrapper(fs, options),
            LocalSpawnRunnerTest::keepLocalEnvUnchanged);

    FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
    policy.timeoutMillis = 123 * 1000L;
    assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
    runner.execAsync(SIMPLE_SPAWN, policy).get();
    assertThat(policy.prefetchCalled).isTrue();
  }

  @Test
  public void checkNoPrefetchCalled() throws Exception {
    FileSystem fs = setupEnvironmentForFakeExecution();

    SubprocessFactory factory = mock(SubprocessFactory.class);
    when(factory.create(any())).thenReturn(new FinishedSubprocess(0));
    SubprocessBuilder.setDefaultSubprocessFactory(factory);

    LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
    LocalSpawnRunner runner =
        new TestedLocalSpawnRunner(
            fs.getPath("/execroot"),
            options,
            resourceManager,
            makeProcessWrapper(fs, options),
            LocalSpawnRunnerTest::keepLocalEnvUnchanged);

    FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
    policy.timeoutMillis = 123 * 1000L;

    Spawn spawn = new SpawnBuilder("/bin/echo", "Hi!")
        .withExecutionInfo(ExecutionRequirements.DISABLE_LOCAL_PREFETCH, "").build();
    assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
    runner.execAsync(spawn, policy).get();
    assertThat(policy.prefetchCalled).isFalse();
  }

  @Test
  public void checkLocalEnvProviderCalled() throws Exception {
    FileSystem fs = setupEnvironmentForFakeExecution();

    SubprocessFactory factory = mock(SubprocessFactory.class);
    when(factory.create(any())).thenReturn(new FinishedSubprocess(0));
    SubprocessBuilder.setDefaultSubprocessFactory(factory);
    LocalEnvProvider localEnvProvider = mock(LocalEnvProvider.class);

    LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
    LocalSpawnRunner runner =
        new TestedLocalSpawnRunner(
            fs.getPath("/execroot"),
            options,
            resourceManager,
            makeProcessWrapper(fs, options),
            localEnvProvider);

    FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
    policy.timeoutMillis = 123 * 1000L;
    assertThat(fs.getPath("/execroot").createDirectory()).isTrue();

    runner.execAsync(SIMPLE_SPAWN, policy).get();
    verify(localEnvProvider)
        .rewriteLocalEnv(
            any(),
            any(),
            matches("^/execroot/tmp[0-9a-fA-F]+_[0-9a-fA-F]+/work$"));
  }

  /**
   * Copies the {@code process-wrapper} tool into the path under the temporary execRoot where the
   * {@link LocalSpawnRunner} expects to find it.
   */
  private void copyProcessWrapperIntoExecRoot(Path wrapperPath) throws IOException {
    File realProcessWrapperFile =
        new File(
            PathFragment.create(BlazeTestUtils.runfilesDir())
                .getRelative(TestConstants.PROCESS_WRAPPER_PATH)
                .getPathString());
    assertThat(realProcessWrapperFile.exists()).isTrue();

    wrapperPath.createDirectoryAndParents();
    File wrapperFile = wrapperPath.getPathFile();

    wrapperPath.delete();
    Files.copy(realProcessWrapperFile, wrapperFile);
    assertThat(wrapperPath.exists()).isTrue();

    wrapperPath.setExecutable(true);
  }

  /**
   * Copies the {@code spend_cpu_time} test util into the temporary execRoot so that the {@link
   * LocalSpawnRunner} can execute it.
   */
  private Path copyCpuTimeSpenderIntoExecRoot(Path execRoot) throws IOException {
    File realCpuTimeSpenderFile =
        new File(
            PathFragment.create(BlazeTestUtils.runfilesDir())
                .getRelative(TestConstants.CPU_TIME_SPENDER_PATH)
                .getPathString());
    assertThat(realCpuTimeSpenderFile.exists()).isTrue();

    Path execRootCpuTimeSpenderPath = execRoot.getRelative("spend-cpu-time");
    File execRootCpuTimeSpenderFile = execRootCpuTimeSpenderPath.getPathFile();

    assertThat(execRootCpuTimeSpenderPath.exists()).isFalse();
    Files.copy(realCpuTimeSpenderFile, execRootCpuTimeSpenderFile);
    assertThat(execRootCpuTimeSpenderPath.exists()).isTrue();

    execRootCpuTimeSpenderPath.setExecutable(true);

    return execRootCpuTimeSpenderPath;
  }

  private Path getTemporaryRoot(FileSystem fs, String name) throws IOException {
    Path tempDirPath = TestUtils.createUniqueTmpDir(fs);
    assertThat(tempDirPath.exists()).isTrue();

    Path root = tempDirPath.getRelative(name);
    assertThat(root.createDirectory()).isTrue();
    assertThat(root.exists()).isTrue();

    return root;
  }

  /**
   * Returns an execRoot {@link Path} inside a new temporary directory.
   *
   * <p>The temporary directory will be automatically deleted on exit.
   */
  private Path getTemporaryExecRoot(FileSystem fs) throws IOException {
    return getTemporaryRoot(fs, "execRoot");
  }

  private Path getTemporaryEmbeddedBin(FileSystem fs) throws  IOException {
    return getTemporaryRoot(fs, "embedded_bin");
  }

  @Test
  public void hasExecutionStatistics_whenOptionIsEnabled() throws Exception {
    // TODO(b/62588075) Currently no process-wrapper or execution statistics support in Windows.
    assumeTrue(OS.getCurrent() != OS.WINDOWS);

    FileSystem fs = new UnixFileSystem(DigestHashFunction.SHA256, /*hashAttributeName=*/ "");

    LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
    options.collectLocalExecutionStatistics = true;

    Duration minimumWallTimeToSpend = Duration.ofSeconds(10);

    Duration minimumUserTimeToSpend = minimumWallTimeToSpend;
    // Under normal loads we should be able to use a much lower bound for maxUserTime, but be
    // generous here in case of hardware issues.
    Duration maximumUserTimeToSpend = minimumUserTimeToSpend.plus(Duration.ofSeconds(20));

    Duration minimumSystemTimeToSpend = Duration.ZERO;
    // Under normal loads we should be able to use a much lower bound for maxSysTime, but be
    // generous here in case of hardware issues.
    Duration maximumSystemTimeToSpend = minimumSystemTimeToSpend.plus(Duration.ofSeconds(20));

    Path execRoot = getTemporaryExecRoot(fs);
    Path embeddedBinaries = getTemporaryEmbeddedBin(fs);
    BinTools binTools = BinTools.forEmbeddedBin(embeddedBinaries,
        ImmutableList.of("process-wrapper"));
    Path processWrapperPath = binTools.getEmbeddedPath("process-wrapper");
    copyProcessWrapperIntoExecRoot(processWrapperPath);
    Path cpuTimeSpenderPath = copyCpuTimeSpenderIntoExecRoot(execRoot);

    LocalSpawnRunner runner =
        new LocalSpawnRunner(
            execRoot,
            options,
            resourceManager,
            LocalSpawnRunnerTest::keepLocalEnvUnchanged,
            binTools,
            new ProcessWrapper(
                processWrapperPath, /*killDelay=*/ Duration.ZERO, /*gracefulSigterm=*/ false),
            Mockito.mock(RunfilesTreeUpdater.class));

    Spawn spawn =
        new SpawnBuilder(
                cpuTimeSpenderPath.getPathString(),
                String.valueOf(minimumUserTimeToSpend.getSeconds()),
                String.valueOf(minimumSystemTimeToSpend.getSeconds()))
            .build();

    FileOutErr fileOutErr = new FileOutErr(fs.getPath("/dev/null"), fs.getPath("/dev/null"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);

    SpawnResult spawnResult = runner.execAsync(spawn, policy).get();

    assertThat(spawnResult.status()).isEqualTo(SpawnResult.Status.SUCCESS);
    assertThat(spawnResult.exitCode()).isEqualTo(0);
    assertThat(spawnResult.setupSuccess()).isTrue();
    assertThat(spawnResult.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());

    assertThat(spawnResult.getWallTime()).isPresent();
    assertThat(spawnResult.getWallTime().get()).isAtLeast(minimumWallTimeToSpend);
    // Under heavy starvation, max wall time could be anything, so don't check it here.
    assertThat(spawnResult.getUserTime()).isPresent();
    assertThat(spawnResult.getUserTime().get()).isAtLeast(minimumUserTimeToSpend);
    assertThat(spawnResult.getUserTime().get()).isAtMost(maximumUserTimeToSpend);
    assertThat(spawnResult.getSystemTime()).isPresent();
    assertThat(spawnResult.getSystemTime().get()).isAtLeast(minimumSystemTimeToSpend);
    assertThat(spawnResult.getSystemTime().get()).isAtMost(maximumSystemTimeToSpend);
    assertThat(spawnResult.getNumBlockOutputOperations().get()).isAtLeast(0L);
    assertThat(spawnResult.getNumBlockInputOperations().get()).isAtLeast(0L);
    assertThat(spawnResult.getNumInvoluntaryContextSwitches().get()).isAtLeast(0L);
  }

  @Test
  public void hasNoExecutionStatistics_whenOptionIsDisabled() throws Exception {
    // TODO(b/62588075) Currently no process-wrapper or execution statistics support in Windows.
    assumeTrue(OS.getCurrent() != OS.WINDOWS);

    FileSystem fs = new UnixFileSystem(DigestHashFunction.SHA256, /*hashAttributeName=*/ "");

    LocalExecutionOptions options = Options.getDefaults(LocalExecutionOptions.class);
    options.collectLocalExecutionStatistics = false;

    Duration minimumWallTimeToSpend = Duration.ofSeconds(1);

    Duration minimumUserTimeToSpend = minimumWallTimeToSpend;
    Duration minimumSystemTimeToSpend = Duration.ZERO;

    Path execRoot = getTemporaryExecRoot(fs);
    Path embeddedBinaries = getTemporaryEmbeddedBin(fs);
    BinTools binTools = BinTools.forEmbeddedBin(embeddedBinaries,
        ImmutableList.of("process-wrapper"));
    Path processWrapperPath = binTools.getEmbeddedPath("process-wrapper");
    copyProcessWrapperIntoExecRoot(processWrapperPath);
    Path cpuTimeSpenderPath = copyCpuTimeSpenderIntoExecRoot(execRoot);

    LocalSpawnRunner runner =
        new LocalSpawnRunner(
            execRoot,
            options,
            resourceManager,
            LocalSpawnRunnerTest::keepLocalEnvUnchanged,
            binTools,
            new ProcessWrapper(
                processWrapperPath, /*killDelay=*/ Duration.ZERO, /*gracefulSigterm=*/ false),
            Mockito.mock(RunfilesTreeUpdater.class));

    Spawn spawn =
        new SpawnBuilder(
                cpuTimeSpenderPath.getPathString(),
                String.valueOf(minimumUserTimeToSpend.getSeconds()),
                String.valueOf(minimumSystemTimeToSpend.getSeconds()))
            .build();

    FileOutErr fileOutErr = new FileOutErr(fs.getPath("/dev/null"), fs.getPath("/dev/null"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);

    SpawnResult spawnResult = runner.execAsync(spawn, policy).get();

    assertThat(spawnResult.status()).isEqualTo(SpawnResult.Status.SUCCESS);
    assertThat(spawnResult.exitCode()).isEqualTo(0);
    assertThat(spawnResult.setupSuccess()).isTrue();
    assertThat(spawnResult.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());

    assertThat(spawnResult.getWallTime()).isPresent();
    assertThat(spawnResult.getWallTime().get()).isAtLeast(minimumWallTimeToSpend);
    // Under heavy starvation, max wall time could be anything, so don't check it here.
    assertThat(spawnResult.getUserTime()).isEmpty();
    assertThat(spawnResult.getSystemTime()).isEmpty();
    assertThat(spawnResult.getNumBlockOutputOperations()).isEmpty();
    assertThat(spawnResult.getNumBlockInputOperations()).isEmpty();
    assertThat(spawnResult.getNumInvoluntaryContextSwitches()).isEmpty();
  }

  // Check that relative paths in the Spawn are absolutized relative to the execroot passed to the
  // LocalSpawnRunner.
  @Test
  public void relativePath() throws Exception {
    // TODO(#3536): Make this test work on Windows.
    // The Command API implicitly absolutizes the path, and we get weird paths on Windows:
    // T:\execroot\execroot\_bin\process-wrapper
    assumeTrue(OS.getCurrent() != OS.WINDOWS);

    FileSystem fs = setupEnvironmentForFakeExecution();

    SubprocessFactory factory = mock(SubprocessFactory.class);
    ArgumentCaptor<SubprocessBuilder> captor = ArgumentCaptor.forClass(SubprocessBuilder.class);
    when(factory.create(captor.capture())).thenReturn(new FinishedSubprocess(0));
    SubprocessBuilder.setDefaultSubprocessFactory(factory);

    LocalSpawnRunner runner =
        new TestedLocalSpawnRunner(
            fs.getPath("/execroot"),
            Options.getDefaults(LocalExecutionOptions.class),
            resourceManager,
            /*processWrapper=*/ null,
            LocalSpawnRunnerTest::keepLocalEnvUnchanged);

    FileOutErr fileOutErr = new FileOutErr(fs.getPath("/out/stdout"), fs.getPath("/out/stderr"));
    SpawnExecutionContextForTesting policy = new SpawnExecutionContextForTesting(fileOutErr);
    assertThat(fs.getPath("/execroot").createDirectory()).isTrue();
    runner.execAsync(new SpawnBuilder("foo/bar", "Hi!").build(), policy).get();
    verify(factory).create(any(SubprocessBuilder.class));

    assertThat(captor.getValue().getArgv()).containsExactly("/execroot/foo/bar", "Hi!");
  }
}


text:
export default Ember.Route.extend({
  beforeModel: function() {
    if (this.get('session.hasElectronicConsent')) {
      this.replaceWith('login.terms-of-service-consent');
    }
  },
  actions: {
    electronicConsent: function() {
      // TODO: Server
      this.set('session.hasElectronicConsent', true)
      this.replaceWith('login.terms-of-service-consent');
    },
    reject: function() {
      this.replaceWith('logout');
    }
  }
});

redpajama_set_name: RedPajamaGithub


text:
//---------------------------------------------------------------------
// <copyright file="ODataQueryTestCase.cs" company="Microsoft">
//      Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------

namespace Microsoft.Test.Taupo.Astoria
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.Linq;

#if SILVERLIGHT && !WIN8
    using System.Net.Browser;
#endif
    using Microsoft.Test.Taupo.Astoria.Client;
    using Microsoft.Test.Taupo.Astoria.Contracts;
    using Microsoft.Test.Taupo.Astoria.Contracts.Client;
    using Microsoft.Test.Taupo.Astoria.Contracts.EntityModel;
    using Microsoft.Test.Taupo.Astoria.Contracts.LinqToAstoria;
    using Microsoft.Test.Taupo.Astoria.Contracts.OData;
    using Microsoft.Test.Taupo.Astoria.Contracts.ServiceReferences;
#if SILVERLIGHT
#if WIN8
    using Microsoft.Test.Taupo.Astoria.Contracts.WebServices.DataOracleService.Win8;
#else
    using Microsoft.Test.Taupo.Astoria.Contracts.WebServices.DataOracleService.Silverlight;
#endif
#else
    using Microsoft.Test.Taupo.Astoria.Contracts.WebServices.DataOracleService.DotNet;
#endif
    using Microsoft.Test.Taupo.Astoria.LinqToAstoria;
    using Microsoft.Test.Taupo.Common;
    using Microsoft.Test.Taupo.Contracts;
    using Microsoft.Test.Taupo.Contracts.EntityModel;
    using Microsoft.Test.Taupo.EntityModel;
    using Microsoft.Test.Taupo.Query.Common;
    using Microsoft.Test.Taupo.Query.Contracts;
    using Microsoft.Test.Taupo.Query.Contracts.Linq;
    using Microsoft.Test.Taupo.Query.Contracts.Linq.Expressions;

    /// <summary>
    /// Base class for all astoria tests.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "The testcase base class has to be coupled with many classes")]
    public abstract class ODataQueryTestCase : QueryTestCase
    {
        /// <summary>
        /// Gets or sets the query resolver to resolve untyped expressions to runtime-type bound expression
        /// </summary>
        [InjectDependency(IsRequired = true)]
        public IAstoriaQueryVerifierCapabilityInspector QueryVerifierCapabilityInspector { get; set; }

        /// <summary>
        /// Gets or sets the query resolver to resolve untyped expressions to runtime-type bound expression
        /// </summary>
        [InjectDependency(IsRequired = true)]
        public ILinqToAstoriaQueryResolver QueryResolver { get; set; }

        /// <summary>
        /// Gets or sets the services to use for data generation
        /// </summary>
        [InjectDependency(IsRequired = true)]
        public IEntityModelConceptualDataServices StructuralDataServices { get; set; }

        /// <summary>
        /// Gets or sets the component to convert QueryExpressions to Uris
        /// </summary>
        [InjectDependency(IsRequired = true)]
        public IQueryToUriStringConverter QueryExpressionToUriConverter { get; set; }

        /// <summary>
        /// Gets the collection of tested primitive types
        /// </summary>
        public Collection<Type> PrimitiveTypesTested { get; private set; }

        /// <summary>
        /// Gets a queryExpression that represents a Query to an existing entity
        /// </summary>
        /// <param name="rootQuery"> The root query expression of the set</param>
        /// <param name="existingEntity">The structural value representing an existing entity</param>
        /// <returns>A QueryExpression representing a query to an existing entity</returns>
        /// <remarks>
        /// 1. We don't use the .Key syntax because :
        ///     Key forces the Query option to take on the Key FOrmat
        ///     /Customers('ALFKI')
        ///     We change the query to be  Customers?$filter=Id eq 'ALFKI' and true so that we don't use the key syntax as it doesn't work with Live servers.
        /// 2. We append the 'and true' part because :
        ///     If you use the client library and completely cover the key, the client's linq translator will use the key syntax.
        ///     To avoid this, we completely cover the key and also add the 'true" so that the client doesn't produce a key expression string.
        /// </remarks>
        protected internal static QueryExpression GetExistingEntityQuery(QueryExpression rootQuery, QueryStructuralValue existingEntity)
        {
            // We cannot create new LinqParameterExpressions as we track the parameters by name, we need to reuse the parameter from the root query
            Func<LinqParameterExpression, QueryExpression> generateFilterExpression = (entityParameter) =>
            {
                QueryExpression keyFilterExpression = GetExistingEntityKeyComparisonExpression(entityParameter, existingEntity);
                return keyFilterExpression;
            };

            var query = rootQuery.Where(eParam => generateFilterExpression(eParam).And(CommonQueryBuilder.Constant(true, existingEntity.Type.EvaluationStrategy.BooleanType)));
            return query;
        }

        /// <summary>
        /// Resets the collection of tested primitive types
        /// </summary>
        protected internal void ResetPrimitiveTypesTested()
        {
            this.PrimitiveTypesTested = new Collection<Type>();
        }

        /// <summary>
        /// Generates new property values for the given type using this test's structural data services
        /// </summary>
        /// <param name="set">The type's entity set</param>
        /// <param name="type">The type to generate properties for</param>
        /// <returns>A set of new property values for the given type</returns>
        protected internal IEnumerable<NamedValue> GeneratePropertyValues(EntitySet set, EntityType type)
        {
            ExceptionUtilities.CheckArgumentNotNull(set, "set");
            ExceptionUtilities.CheckArgumentNotNull(type, "type");

            EntityContainer container = set.Container;
            ExceptionUtilities.CheckObjectNotNull(container, "Entity set did not have its entity container set");

            var generator = this.StructuralDataServices.GetStructuralGenerator(type.FullName, container.Name + '.' + set.Name);
            return generator.GenerateData();
        }

        /// <summary>
        /// Generates a set of values for an entity type's key properties. Will throw if any key property is a dependent/foreign-key. For composite keys, only one of the values is guaranteed to be unique.
        /// </summary>
        /// <param name="type">The entity type to generate a key for</param>
        /// <returns>The key property values in metadata order</returns>
        protected internal IEnumerable<NamedValue> GeneratePropertyValuesWithUniqueKey(QueryEntityType type)
        {
            return this.GeneratePropertyValuesWithUniqueKey(type.EntitySet, type.EntityType);
        }

        /// <summary>
        /// Gets the existing values of the given primitive properties using the in-memory query evaluator
        /// </summary>
        /// <param name="set">The entity set</param>
        /// <param name="propertyNames">The properties to get values for</param>
        /// <returns>The values for the given properties as returned by the query evaluator</returns>
        protected internal Dictionary<string, List<object>> GetExistingPrimitivePropertyValues(EntitySet set, IEnumerable<string> propertyNames)
        {
            // build a projection query to find the values for the key properties
            var query = this.BuildRootQueryForSet(set).Select(o => LinqBuilder.New(propertyNames, propertyNames.Select(p => o.Property(p))));

            // evaluate the projection
            var collection = this.Evaluator.Evaluate(query) as QueryCollectionValue;
            ExceptionUtilities.CheckObjectNotNull(collection, "Query did not return a collection: {0}", query);
            ExceptionUtilities.Assert(collection.EvaluationError == null, "Query evaluation error: " + collection.EvaluationError);

            var structuralValues = collection.Elements.Cast<QueryStructuralValue>();
            var existingValues = propertyNames
                .ToDictionary(p => p, p => structuralValues.Select(v => v.GetScalarValue(p).Value).ToList());

            return existingValues;
        }

        /// <summary>
        /// Generates a set of values for an entity type's key properties. Will throw if any key property is a dependent/foreign-key. For composite keys, only one of the values is guaranteed to be unique.
        /// </summary>
        /// <param name="set">The entity set the type belongs to</param>
        /// <param name="type">The entity type to generate a key for</param>
        /// <returns>The key property values in metadata order</returns>
        protected internal IEnumerable<NamedValue> GeneratePropertyValuesWithUniqueKey(EntitySet set, EntityType type)
        {
            ExceptionUtilities.CheckArgumentNotNull(set, "set");
            ExceptionUtilities.CheckArgumentNotNull(type, "type");

            AssertPrimaryKeyHasNoReferentialConstraints(type);

            // get the key property names
            var keyPropertyNames = type.AllKeyProperties.Select(p => p.Name).ToList();

            // get the existing values for the key properties
            var existingValues = this.GetExistingPrimitivePropertyValues(set, keyPropertyNames);

            // generate an initial set of values
            var generatedValues = this.GeneratePropertyValues(set, type);

            // allocate storage for the key values we find. Note that we will generate them in random order, then reorder them later.
            // Doing it this way so that the 1st property is not the one that must be unique for composite keys
            var uniqueValues = new Dictionary<string, object>();

            // loop through the key property names in random order
            // if a unique value is found, then there is no need to check for the rest
            bool keyIsUnique = false;
            while (keyPropertyNames.Any())
            {
                // pick a random property name
                var propertyName = this.Random.ChooseFrom(keyPropertyNames);
                keyPropertyNames.Remove(propertyName);

                // only retry one more time than there are values, since by then we really should have found something
                var tries = existingValues[propertyName].Count + 1;

                // regenerate property values until a unique value is found for this property
                var value = generatedValues.Single(v => v.Name == propertyName).Value;
                while (!keyIsUnique && tries-- > 0)
                {
                    keyIsUnique = !existingValues[propertyName].Any(v => ValueComparer.Instance.Equals(v, value));
                    if (!keyIsUnique)
                    {
                        generatedValues = this.GeneratePropertyValues(set, type);
                        value = generatedValues.Single(v => v.Name == propertyName).Value;
                    }
                }

                // even if we did not find a unique value for this property, we should populate it, because another property
                // may result in the key being unique later on
                uniqueValues[propertyName] = value;
            }

            // if we could not generate a unique key, throw an exception
            ExceptionUtilities.Assert(keyIsUnique, "Could not generate a unique key for type '{0}'", type.FullName);

            if (!type.Annotations.OfType<HasStreamAnnotation>().Any())
            {
                // add in the non-key values
                foreach (var value in generatedValues)
                {
                    if (!uniqueValues.ContainsKey(value.Name))
                    {
                        uniqueValues[value.Name] = value.Value;
                    }
                }
            }

            // return them in metadata order
            return type.AllProperties.SelectMany(p => uniqueValues.Where(pair => pair.Key.Split('.').First().Equals(p.Name)).Select(pair => new NamedValue(pair.Key, pair.Value)));
        }

        /// <summary>
        /// Generates new property values for the given type using this test's structural data services
        /// </summary>
        /// <param name="type">The type to generate properties for</param>
        /// <returns>A set of new property values for the given type</returns>
        protected internal IEnumerable<NamedValue> GeneratePropertyValues(QueryEntityType type)
        {
            ExceptionUtilities.CheckArgumentNotNull(type, "type");

            return this.GeneratePropertyValues(type.EntitySet, type.EntityType);
        }

        /// <summary>
        /// Generates new property values for the given type using this test's structural data services
        /// </summary>
        /// <param name="type">The type to generate properties for</param>
        /// <returns>A set of new property values for the given type</returns>
        protected internal IEnumerable<NamedValue> GeneratePropertyValues(ComplexType type)
        {
            ExceptionUtilities.CheckArgumentNotNull(type, "type");

            var generator = this.StructuralDataServices.GetStructuralGenerator(type.FullName);
            return generator.GenerateData();
        }

        /// <summary>
        /// Gets a queryExpression that represents a Query to an existing entity
        /// </summary>
        /// <param name="entityParameter">The LinqParameterExpression which represents the root parameter</param>
        /// <param name="existingEntity">The structural value representing an existing entity</param>
        /// <returns>A QueryExpression representing a query to an existing entity</returns>
        protected static QueryExpression GetExistingEntityKeyComparisonExpression(QueryExpression entityParameter, QueryStructuralValue existingEntity)
        {
            QueryExpression keyFilterExpression = null;
            QueryEntityType entityType = existingEntity.Type as QueryEntityType;
            foreach (var keyProperty in entityType.EntityType.AllKeyProperties)
            {
                var queryProperty = entityType.Properties.SingleOrDefault(p => p.Name == keyProperty.Name);
                ExceptionUtilities.CheckObjectNotNull(queryProperty, "Could not find property with name '{0}' on type '{1}'", keyProperty.Name, entityType);

                var value = existingEntity.GetScalarValue(keyProperty.Name);
                QueryExpression keyComparision = entityParameter.Property(keyProperty.Name).EqualTo(CommonQueryBuilder.Constant(value));

                if (keyFilterExpression == null)
                {
                    keyFilterExpression = keyComparision;
                }
                else
                {
                    keyFilterExpression = CommonQueryBuilder.And(keyFilterExpression, keyComparision);
                }
            }

            return keyFilterExpression;
        }

        /// <summary>
        /// Get properties in the structural type that support a specific Binary Operation.
        /// </summary>
        /// <param name="type">Structural Type.</param>
        /// <param name="op">An enum that represents a Query Binary Operation.</param>
        /// <returns>A collection of properties.</returns>
        protected IEnumerable<QueryProperty<QueryScalarType>> GetPropertiesWithBinaryOpSupport(QueryStructuralType type, QueryBinaryOperation op)
        {
            List<QueryProperty<QueryScalarType>> queryProperties = type.Properties.Primitive().Where(m => m.PropertyType.Supports(op)).ToList();

            DataServiceExecuteVerifier verifier = this.Verifier as DataServiceExecuteVerifier;
            if (verifier != null && verifier.IsUri == false)
            {
                if (op == QueryBinaryOperation.GreaterThan || op == QueryBinaryOperation.GreaterThanOrEqualTo || op == QueryBinaryOperation.LessThan || op == QueryBinaryOperation.LessThanOrEqualTo)
                {
                    // Exclude string, DateTime and boolean properties as these don't support greater than or less than operators in the Client Linq Code
                    queryProperties = queryProperties.Where(qp => (qp.PropertyType as IQueryClrType).ClrType != typeof(string)).ToList();
                    queryProperties = queryProperties.Where(qp => (qp.PropertyType as IQueryClrType).ClrType != typeof(bool)).ToList();
                    queryProperties = queryProperties.Where(qp => (qp.PropertyType as IQueryClrType).ClrType != typeof(DateTime)).ToList();
#if !WINDOWS_PHONE
                    queryProperties = queryProperties.Where(qp => (qp.PropertyType as IQueryClrType).ClrType != typeof(Guid)).ToList();
#endif
                }
            }

            return queryProperties;
        }

        private static void AssertPrimaryKeyHasNoReferentialConstraints(EntityType type)
        {
            // dependent properties are not supported
            var keyMembersWithReferentialContraints = type.PrimaryKeyPropertiesWithReferentialConstraints();
            if (keyMembersWithReferentialContraints.Any())
            {
                var message = string.Format(
                    CultureInfo.InvariantCulture,
                    "Cannot generate key value for property '{0}' on type '{1}' because it is a foreign key",
                    string.Join(",", keyMembersWithReferentialContraints.Select(p => p.Name).ToArray()),
                    type.FullName);
                throw new TaupoInvalidOperationException(message);
            }
        }
    }
}


text:
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'rubygems'
require 'json'
require 'sinatra'
require 'rufus/decision'

#class App
#  def initialize
#    @rfapp = Rack::File.new(File.dirname(__FILE__) + '/public')
#  end
#  def call (env)
#    return decide(env) if env['PATH_INFO'] == '/decision'
#    env['PATH_INFO'] = '/index.html' if env['PATH_INFO'] == '/'
#    @rfapp.call(env)
#  end
#  protected
#  def in_to_h (keys, values)
#    keys.inject({}) { |h, k| h[k] = values.shift; h }
#  end
#  def decide (env)
#    json = env['rack.input'].read
#    json = JSON.parse(json)
#    dt = Rufus::Decision::Table.new(json.last)
#    input = Rufus::Decision.transpose(json.first)
#      # from array of arrays to array of hashes
#    output = input.inject([]) { |a, hash| a << dt.transform(hash); a }
#    output = Rufus::Decision.transpose(output)
#      # from array of hashes to array of arrays
#    [ 200, {}, output.to_json ]
#  end
#end

use Rack::CommonLogger
use Rack::ShowExceptions

set :public, File.expand_path(File.join(File.dirname(__FILE__), 'public'))
#set :views, File.expand_path(File.join(File.dirname(__FILE__), 'views'))

get '/' do

  redirect '/decision.html'
end

post '/decide' do

  json = env['rack.input'].read
  json = JSON.parse(json)

  dt = Rufus::Decision::Table.new(json.last)
  input = Rufus::Decision.transpose(json.first)
    # from array of arrays to array of hashes

  output = input.inject([]) { |a, hash| a << dt.transform(hash); a }
  output = Rufus::Decision.transpose(output)
    # from array of hashes to array of arrays

  response.headers['Content-Type'] = 'application/json'

  output.to_json
end


text:
import expect from 'expect.js';
import { ByteUtils, Consts, HmacBlockTransform, KdbxError } from '../../lib';

describe('HmacBlockTransform', () => {
    const key = ByteUtils.arrayToBuffer(
        ByteUtils.hexToBytes('1f5c3ef76d43e72ee2c5216c36187c799b153cab3d0cb63a6f3ecccc2627f535')
    );

    it('decrypts and encrypts data', () => {
        const src = new Uint8Array([1, 2, 3, 4, 5]);
        return HmacBlockTransform.encrypt(src.buffer, key).then((enc) => {
            return HmacBlockTransform.decrypt(enc, key).then((dec) => {
                dec = new Uint8Array(dec);
                expect(dec).to.be.eql(src);
            });
        });
    });

    it('decrypts several blocks', () => {
        const src = new Uint8Array(1024 * 1024 * 2 + 2);
        for (let i = 0; i < src.length; i++) {
            src[i] = i % 256;
        }
        return HmacBlockTransform.encrypt(src.buffer, key).then((enc) => {
            return HmacBlockTransform.decrypt(enc, key).then((dec) => {
                expect(ByteUtils.bytesToBase64(dec)).to.be(ByteUtils.bytesToBase64(src));
            });
        });
    });

    it('throws error for invalid hash block', () => {
        const src = new Uint8Array([1, 2, 3, 4, 5]);
        return HmacBlockTransform.encrypt(src.buffer, key).then((enc) => {
            new Uint8Array(enc)[4] = 0;
            return HmacBlockTransform.decrypt(enc, key)
                .then(() => {
                    throw 'We should not get here';
                })
                .catch((e) => {
                    expect(e).to.be.a(KdbxError);
                    expect(e.code).to.be(Consts.ErrorCodes.FileCorrupt);
                });
        });
    });
});


text:
/*
 * NOTE: This class is taken from the AOSP frameworks/base/core/java/android/net/ParseException.java.
 * The package name is modified and the imports were modified as needed.
 */
package org.chromium.content.browser.third_party;

/**
 *
 *
 * When WebAddress Parser Fails, this exception is thrown
 */
public class ParseException extends RuntimeException {
    public String response;

    ParseException(String response) {
        this.response = response;
    }
}


text:
"""
WWW: http://4suite.org/         e-mail: support@4suite.org

Copyright (c) 2000-2001 Fourthought Inc, USA.   All Rights Reserved.
See  http://4suite.org/COPYRIGHT  for license and copyright information
"""

def Not(original,other):
    return filter(lambda x,other=other:x not in other,original)

def Union(left,right):
    if len(left) < len(right):
        loop = left
        compare = right
    else:
        loop = right
        compare = left
    return compare + filter(lambda x,compare = compare:x not in compare,loop)

def Intersection(left,right):
    if len(left) < len(right):
        loop = left
        compare = right
    else:
        loop = right
        compare = left
    return filter(lambda x,compare = compare:x in compare,loop)

def Unique(left):
    return reduce(lambda rt,x:x in rt and rt or rt + [x],left,[])


text:
<?php

namespace App\PictureBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use App\PictureBundle\Entity\Collection;
use App\PictureBundle\Form\CollectionType;

/**
 * Collection controller.
 *
 * @Route("/collection")
 */
class CollectionController extends Controller
{

    /**
     * Lists all Collection entities.
     *
     * @Route("/", name="collection")
     * @Method("GET")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $user = $this->getUser();
        $entities = $em->getRepository('AppPictureBundle:Collection')->findByUser($user);

        return array(
            'entities' => $entities,
        );
    }
    /**
     * Creates a new Collection entity.
     *
     * @Route("/create", name="collection_create")
     * @Method("POST")
     * @Template("AppPictureBundle:Collection:new.html.twig")
     */
    public function createAction(Request $request)
    {
        $entity = new Collection();
        $entity->setUser($this->getUser());
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('collection_show', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
    * Creates a form to create a Collection entity.
    *
    * @param Collection $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createCreateForm(Collection $entity)
    {
        $form = $this->createForm(new CollectionType(), $entity, array(
            'action' => $this->generateUrl('collection_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));
        $form->remove('createdAt')
            ->remove('user');

        return $form;
    }

    /**
     * Displays a form to create a new Collection entity.
     *
     * @Route("/new", name="collection_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
        $entity = new Collection();
        $form   = $this->createCreateForm($entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * Finds and displays a Collection entity.
     *
     * @Route("/{id}", name="collection_show")
     * @Method("GET")
     * @Template()
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('AppPictureBundle:Collection')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Collection entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
     * Displays a form to edit an existing Collection entity.
     *
     * @Route("/{id}/edit", name="collection_edit")
     * @Method("GET")
     * @Template()
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('AppPictureBundle:Collection')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Collection entity.');
        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
    * Creates a form to edit a Collection entity.
    *
    * @param Collection $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(Collection $entity)
    {
        $form = $this->createForm(new CollectionType(), $entity, array(
            'action' => $this->generateUrl('collection_update', array('id' => $entity->getId())),
            'method' => 'PUT',
        ));

        $form->add('submit', 'submit', array('label' => 'Update'));

        return $form;
    }
    /**
     * Edits an existing Collection entity.
     *
     * @Route("/{id}", name="collection_update")
     * @Method("PUT")
     * @Template("AppPictureBundle:Collection:edit.html.twig")
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('AppPictureBundle:Collection')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Collection entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createEditForm($entity);
        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            $em->flush();

            return $this->redirect($this->generateUrl('collection_edit', array('id' => $id)));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }
    /**
     * Deletes a Collection entity.
     *
     * @Route("/delete/{id}", name="collection_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('AppPictureBundle:Collection')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Collection entity.');
            }
var_dump($em);
            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('collection'));
    }

    /**
     * Creates a form to delete a Collection entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('collection_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }
}


text:
package com.github.rvesse.airline.help.sections;

import com.github.rvesse.airline.annotations.Cli;
import com.github.rvesse.airline.annotations.help.Discussion;
import com.github.rvesse.airline.annotations.help.SeeAlso;
import com.github.rvesse.airline.args.Args1;
import com.github.rvesse.airline.command.CommandRemove;
import com.github.rvesse.airline.help.Help;

@Cli(defaultCommand = Help.class, commands = { Help.class, Args1.class, CommandRemove.class }, name = "test")
@Discussion(paragraphs = { "Foo", "Bar" })
@SeeAlso(internalCommands = { "test help" }, externalCommands = { "man", "grep" })
public class CliWithSections {

}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

	<title> Disable drop target checking</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" type="text/css" href="../../../codebase/dhtmlxtree.css"/>

Disable drop target checking

After disabling drop check, item can be dropped everywhere, even on itself. ( to prevent error user must add own code to control drag and drop)

If drop check enabled (default behavior) item can't be dropped on itself, or on any from its children.

				Enable drop target checking

				Disable drop target checking


text:
<?php

use SkinBundle\SkinBundle;
use AppBundle\AppBundle;
use FightingBundle\FightingBundle;
use HouseBundle\HouseBundle;
use GuildBundle\GuildBundle;
use NavigationBundle\NavigationBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;

class AppKernel extends Kernel {

    public function registerBundles() {
        $bundles = [
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new SkinBundle(),
            new FightingBundle(),
            new HouseBundle(),
            new NavigationBundle(),
            new GuildBundle(),
            new AppBundle()
        ];

        if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

    public function getRootDir() {
        return __DIR__;
    }

    public function getCacheDir() {
        return dirname(__DIR__) . '/var/cache/' . $this->getEnvironment();
    }

    public function getLogDir() {
        return dirname(__DIR__) . '/var/logs';
    }

    public function registerContainerConfiguration(LoaderInterface $loader) {
        $loader->load($this->getRootDir() . '/config/config_' . $this->getEnvironment() . '.yml');
    }

}


text:
import React, { Component } from 'react';

import PropTypes from 'prop-types';

class Beber extends Component {
  static propTypes = {
    idade: PropTypes.number.isRequired
  }

  render() {
    const { idade } = this.props;
    if(idade >= 21) {
      return
#BoraProBar #Itaipava #EhNois{idade}

    } else {
      return
Só {idade} anos? Vai estudar, muleque!

    }
  }
}

export default Beber;


text:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kobdig.market;

import java.io.PrintStream;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

/**
 * A market simulation using evolving traders.
 *
 * @author Andrea G. B. Tettamanzi
 */
public class Simulation
{
    /** A population of evolutionary traders. */
    protected List<EvoTrader> pop;

    /** Random Number Generator. */
    protected static Random rnd = new Random();

    /**
     * Creates a new simulation with the given number of traders.
     */
    public Simulation(int size)
    {
        pop = new Vector<EvoTrader>(size);
        for(int i = 0; i<size; i++)
            pop.add(new EvoTrader(Trader.uniqueId(), 100000, 1000, new Genotype()));
    }

    /**
     * Let the agents in the simulation perform their actions.
     */
    public void act()
    {
        Iterator<EvoTrader> i = pop.iterator();
        while(i.hasNext())
            i.next().trade();
    }

    /**
     * Select an individual proportionately to fitness.
     *
     * @param pool
     * @param cumulativeFitness
     * @return
     */
    protected EvoTrader select(List<EvoTrader> pool)
    {
        Iterator<EvoTrader> i = pop.iterator();
        long cumulativeNAV = 0;
        while(i.hasNext())
            cumulativeNAV += i.next().netAssetValue();

        long n = rnd.nextLong() % cumulativeNAV;
        long sum = 0;
        i = pool.iterator();
        while(i.hasNext())
        {
            EvoTrader t = i.next();
            sum += t.netAssetValue();
            if(sum>=n)
                return t;
        }
        // This statement should never be reached:
        return null;
    }

    /**
     * Performs a generation of the evolutionary algorithm.
     */
    public void generation()
    {
        long cumulativeNAV = 0;
        // Cash and asset for redistribution:
        long cash = 0;
        long asset = 0;

        Collections.sort(pop);

        // The worst 30% of the population becomes extinct:
        int worst30pct = 3*pop.size()/10;
        for(int i = 0; i<worst30pct; i++)
        {
            EvoTrader t = pop.remove(0);
            t.cancelAll();
            asset += t.asset();
            cash += t.cash();
        }

        Iterator<EvoTrader> i = pop.iterator();
        while(i.hasNext())
            cumulativeNAV += i.next().netAssetValue();

        i = pop.iterator();
        int assignedCash = 0;
        int assignedAsset = 0;
        while(i.hasNext())
        {
            EvoTrader t = i.next();

            int c = (int) (cash*t.netAssetValue()/cumulativeNAV);
            t.cash(c);
            assignedCash += c;

            int a = (int) (asset*t.netAssetValue()/cumulativeNAV);
            t.asset(a);
            assignedAsset += a;
        }
        // Assign the rest to the best individual:
        pop.get(pop.size() - 1).cash(cash - assignedCash);
        pop.get(pop.size() - 1).asset(asset - assignedAsset);

        List<EvoTrader> offspring = new LinkedList<EvoTrader>();
        for(int count = 0; count<worst30pct; count++)
        {
            EvoTrader mom, dad, baby;

            mom = select(pop);
            do dad = select(pop);
            while(dad==mom);
            baby = new EvoTrader(mom, dad);
            offspring.add(baby);
        }

        pop.addAll(offspring);
    }

    public void histogram(PrintStream out)
    {
        Collections.sort(pop);

        Iterator<EvoTrader> i = pop.iterator();
        while(i.hasNext())
            out.println(i.next().netAssetValue());
    }

    @Override
    public String toString()
    {
        String s = "Population {\n";
        Iterator<EvoTrader> i = pop.iterator();
        while(i.hasNext())
            s += "\t" + i.next() + "\n";
        return s + "}";
    }
}


text:
\documentclass[11pt]{article}

\begin{document}

\begin{theorem}
\InputIfFileExists{parsing1.tex}
\end{theorem}

\begin{proof}
$just a small formula$
\InputIfFileExists{parsing2.tex}
$an even smaller formula (although now it became longer)$
\end{proof}

\begin{lemma}
\InputIfFileExists{parsing3.tex}
\end{lemma}

None of these should actually include the file!

\end{document}


text:
package com.myhoard.app.gps;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

/**
 * Klasa pobierająca na bierzące dane GPS i przekazująca do aktywności przy
 * pomocy przywiązanej usługi
 * <p/>
 * Created by Sebastian Peryt on 07.03.14.
 */
public class GPSProvider extends Service implements LocationListener {

	public static final String BROADCAST_ACTION = "com.myhoard.app.displayevent";
    private static final long MIN_TIME_REQUEST = 1000;
    private static final float MIN_DISTANCE_REQUEST = 5;
	private static final String TAG = GPSProvider.class
			.getSimpleName();
	private static final boolean D = true;// debug

    public static final String POS_LON = "lon";
    public static final String POS_LAT = "lat";

	// Binder dla aktywnosci przywiazanych
	private final IBinder mBinder = new LocalGPSBinder();
	// Zmienne location
	private LocationManager locationManager = null;
	private Location lastLocation = null;
	private String locationProvider;
	// KONIEC - Zmienne location
	private double lon;
	private double lat;
	private boolean enabledGPS = false; // dostępny jest provider GPS

	public GPSProvider() {
		if (D)
			Log.d(TAG, "Konstruktor");
	}

	@Override
	public void onCreate() {
		super.onCreate();

		locationManager = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);

		// Pobranie danych nt. tego, czy dostepne są wybrane LocationManagery
		enabledGPS = locationManager
				.isProviderEnabled(LocationManager.GPS_PROVIDER);

        Bundle b = new Bundle();
        // Przygotowanie broadcasta
        Intent out = new Intent(BROADCAST_ACTION);
        b.putBoolean("gps",enabledGPS);
        out.putExtras(b);
        // Wyslanie broadcasta
        sendBroadcast(out);

		// Uruchamianie providera
		locationProvider = LocationManager.GPS_PROVIDER;

		// Pobranie i ew. ustawienie ostatniej wartości
		lastLocation = locationManager.getLastKnownLocation(locationProvider);
		if (lastLocation != null) {
            onLocationChanged(lastLocation);
			if (D)
				Log.d(TAG, "Jest lastLocation");
			if (D)
				Log.d(TAG, "lastLocation: " + lastLocation.getLatitude()
                        + " / " + lastLocation.getLongitude());
		} else {
			if (D)
				Log.d(TAG, "Brak lastLocation");
		}
	}

	@Override
	public void onDestroy() {
		if (D)
			Log.d(TAG, "KILL");
		locationManager.removeUpdates(this);
	}

	/**
	 * Funkcja zwraca IBinder, który ma info nt. pozycji i statusu GPS
	 */
	@Override
	public IBinder onBind(Intent arg0) {
		if (D)
			Log.d(TAG, "bind");
        locationManager
				.requestLocationUpdates(locationProvider,MIN_TIME_REQUEST,MIN_DISTANCE_REQUEST,this);

		return mBinder;
	}

	/**
	 * Klasa bindera klineta
	 */
	public class LocalGPSBinder extends Binder {
		public GPSProvider getService() {
			// Zwrocenie instancje GPSProvide aby klienci mogli
			// wywolywac metody publiczne
			return GPSProvider.this;
		}
	}

    @Override
    public void onLocationChanged(Location location) {
        if (D)
            Log.d(TAG, "Uruchomiono onLocationChaged");
        if (location == null)
            return;

        if (lastLocation != null && lastLocation.equals(location))
            return;

        lon = location.getLongitude();
        lat = location.getLatitude();

        Bundle b = new Bundle();
        // Przygotowanie broadcasta
        Intent out = new Intent(BROADCAST_ACTION);
        b.putDouble(GPSProvider.POS_LON, lon);
        b.putDouble(GPSProvider.POS_LAT, lat);
        b.putBoolean("gps",enabledGPS);
        out.putExtras(b);
        // Wyslanie broadcasta
        sendBroadcast(out);

        lastLocation = location;
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {
        Log.e("TAG","enabled: " + s);
    }

    @Override
    public void onProviderDisabled(String s) {
        Log.e("TAG","disabled: " + s);
    }
}


text:
<?php

namespace Business\Tests;

use Business\DateTimePeriod;

class DateTimePeriodTest extends \PHPUnit_Framework_TestCase
{
    public function testIterator()
    {
        $dateTimePeriod = new DateTimePeriod(new \DateTime('2015-07-08'), new \DateTime('2015-07-13'));

        $this->assertInstanceOf('DatePeriod', $dateTimePeriod->getIterator());

        $expexted = [
            '2015-07-08',
            '2015-07-09',
            '2015-07-10',
            '2015-07-11',
            '2015-07-12',
            '2015-07-13',
        ];
        $actual = [];

        foreach ($dateTimePeriod as $dateTime) {
            $actual[] = $dateTime->format('Y-m-d');
        }

        $this->assertEquals($expexted, $actual);
    }

    /**
     * @expectedException \LogicException
     * @expectedExceptionMessage Start date must be earlier than end date.
     */
    public function testStartDateIsEarlier()
    {
        $dateTimePeriod = new DateTimePeriod(new \DateTime('2015-07-08'), new \DateTime('2015-07-07'));
    }
}


text:
package br.com.digidev.messenger4j.setup;

import static java.util.Collections.singletonList;

import br.com.digidev.messenger4j.internal.PreConditions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author Andriy Koretskyy
 */
final class SetupPayload {

    private final SettingType settingType;

    private final ThreadState threadState;

    private final List<CallToAction> callToActions;

    private final Greeting greeting;

    static SetupPayload.Builder newBuilder() {
        return new SetupPayload.Builder();
    }

    private SetupPayload(SetupPayload.Builder builder) {
        settingType = builder.settingType;
        threadState = builder.threadState;
        callToActions = builder.callToActions == null ? null : Collections.unmodifiableList(builder.callToActions);
        greeting = builder.greeting;
    }

    public SettingType getSettingType() {
        return settingType;
    }

    public ThreadState getThreadState() {
        return threadState;
    }

    public List<CallToAction> getCallToActions() {
        return callToActions;
    }

    public Greeting getGreeting() {
        return greeting;
    }

    static final class Builder {
        private SettingType settingType;
        private ThreadState threadState;
        private List<CallToAction> callToActions;
        private Greeting greeting;

        public SetupPayload.Builder settingType(SettingType settingType) {
            this.settingType = settingType;
            return this;
        }

        public SetupPayload.Builder threadState(ThreadState threadState) {
            this.threadState = threadState;
            return this;
        }

        public SetupPayload.Builder greeting(String greeting) {
            this.greeting = new Greeting(greeting);
            return this;
        }

        public SetupPayload.Builder payload(String payload) {
            this.callToActions = singletonList(new CallToAction(payload));
            return this;
        }

        public SetupPayload.Builder addMenuItems(Collection<CallToAction> menuItems) {
            if (callToActions == null) {
                callToActions = new ArrayList<>();
            }
            callToActions.addAll(menuItems);
            return this;
        }

        public SetupPayload build() {
            PreConditions.notNull(settingType, "settingType");
            return new SetupPayload(this);
        }
    }
}


text:
namespace engine {

class DropOnDeathSystem : public System
{
public:
    DEFINE_SYSTEM_BASE( DropOnDeathSystem )
    DropOnDeathSystem();
    virtual void Init();
    virtual void Update( double DeltaTime );
private:
    Scene& mScene;
    ActorFactory& mActorFactory;

    AutoId Roll( int32_t n );
    AutoId RollNormalItem( int32_t n );
    AutoId RollBuff( int32_t n );
};

} // namespace engine

#endif//INCLUDED_ENGINE_DROP_ON_DEATH_SYSTEM_H


text:
import os
import angr
import shutil
import tempfile
import subprocess
import shellphish_afl
from .fuzzer import Fuzzer

import logging
l = logging.getLogger("fuzzer.Showmap")

class Showmap(object):
    """Show map"""

    def __init__(self, binary_path, testcase, timeout=None):
        """
        :param binary_path: path to the binary which the testcase applies to
        :param testcase: string representing the contents of the testcase
        :param timeout: millisecond timeout
        """

        self.binary_path = binary_path
        self.testcase = testcase
        self.timeout = None

        if isinstance(binary_path, str):
            self.is_multicb = False
            self.binaries = [binary_path]
        elif isinstance(binary_path, (list,tuple)):
            self.is_multicb = True
            self.binaries = binary_path
        else:
            raise ValueError("Was expecting either a string or a list/tuple for binary_path! "
                "It's {} instead.".format(type(binary_path)))

        if timeout is not None:
            if isinstance(timeout, (int, long)):
                self.timeout = str(timeout)
            elif isinstance(timeout, (str)):
                self.timeout = timeout
            else:
                raise ValueError("timeout param must be of type int or str")

        # will be set by showmap's return code
        self.causes_crash = False

        Fuzzer._perform_env_checks()

        self.base = Fuzzer._get_base()
        l.debug("got base dir %s", self.base)

        # unfortunately here is some code reuse between Fuzzer and Minimizer (and Showmap!)
        p = angr.Project(self.binaries[0])
        tracer_id = 'cgc' if p.loader.main_object.os == 'cgc' else p.arch.qemu_name
        if self.is_multicb:
            tracer_id = 'multi-{}'.format(tracer_id)

        self.showmap_path = os.path.join(shellphish_afl.afl_dir(tracer_id), "afl-showmap")
        self.afl_path_var = shellphish_afl.afl_path_var(tracer_id)

        l.debug("showmap_path: %s", self.showmap_path)
        l.debug("afl_path_var: %s", self.afl_path_var)

        os.environ['AFL_PATH'] = self.afl_path_var

        # create temp
        self.work_dir = tempfile.mkdtemp(prefix='showmap-', dir='/tmp/')

        # flag for work directory removal
        self._removed = False

        self.input_testcase = os.path.join(self.work_dir, 'testcase')
        self.output = os.path.join(self.work_dir, 'out')

        l.debug("input_testcase: %s", self.input_testcase)
        l.debug("output: %s", self.output)

        # populate contents of input testcase
        with open(self.input_testcase, 'wb') as f:
            f.write(testcase)

    def __del__(self):
        if not self._removed:
            shutil.rmtree(self.work_dir)

    def showmap(self):
        """Create the map"""

        if self._start_showmap().wait() == 2:
            self.causes_crash = True

        with open(self.output) as f: result = f.read()

        shutil.rmtree(self.work_dir)
        self._removed = True

        shownmap = dict()
        for line in result.split("\n")[:-1]:
            key, h_count = map(int, line.split(":"))
            shownmap[key] = h_count

        return shownmap

    def _start_showmap(self, memory="8G"):

        args = [self.showmap_path]

        args += ["-o", self.output]
        if not self.is_multicb:
            args += ["-m", memory]
        args += ["-Q"]

        if self.timeout:
            args += ["-t", self.timeout]
        else:
            args += ["-t", "%d+" % (len(self.binaries) * 1000)]

        args += ["--"]
        args += self.binaries

        outfile = "minimizer.log"

        l.debug("execing: %s > %s", " ".join(args), outfile)

        outfile = os.path.join(self.work_dir, outfile)
        with open(outfile, "w") as fp, open(self.input_testcase, 'rb') as it, open("/dev/null", 'wb') as devnull:
            return subprocess.Popen(args, stdin=it, stdout=devnull, stderr=fp, close_fds=True)


text:
<!DOCTYPE html>

    <title>Selenium Test HTML File</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

@include('partial.nav')

Form Test

Form successfully submitted

Developed by:

Mohammed Mudassir (https://github.com/Modelizer)

John Hoopes (https://github.com/jhoopes)