Package org.sonar.api.utils.command

Examples of org.sonar.api.utils.command.Command


  public void testAllParams() {
    CommandExecutor commandExecutor = mock(CommandExecutor.class);
    Settings settings = new Settings(new PropertyDefinitions(SvnConfiguration.getProperties()));
    SvnBlameCommand svnBlameCommand = new SvnBlameCommand(commandExecutor, new SvnConfiguration(settings));

    Command commandLine = svnBlameCommand.createCommandLine(baseDir, "src/main/java/Foo.java");
    assertThat(commandLine.toCommandLine()).isEqualTo("svn blame --xml --non-interactive -x -w src/main/java/Foo.java");
    assertThat(commandLine.toString()).isEqualTo("svn blame --xml --non-interactive -x -w src/main/java/Foo.java");

    settings.setProperty(SvnConfiguration.USER_PROP_KEY, "myUser");
    settings.setProperty(SvnConfiguration.PASSWORD_PROP_KEY, "myPass");
    commandLine = svnBlameCommand.createCommandLine(baseDir, "src/main/java/Foo.java");
    assertThat(commandLine.toCommandLine()).isEqualTo("svn blame --xml --non-interactive -x -w --username myUser --password myPass src/main/java/Foo.java");
    assertThat(commandLine.toString()).isEqualTo("svn blame --xml --non-interactive -x -w --username ******** --password ******** src/main/java/Foo.java");

    settings.setProperty(SvnConfiguration.CONFIG_DIR_PROP_KEY, "/home/julien/.svn");
    settings.setProperty(SvnConfiguration.TRUST_SERVER_PROP_KEY, "true");
    settings.setProperty(SvnConfiguration.USE_MERGE_HISTORY_KEY, "true");
    commandLine = svnBlameCommand.createCommandLine(baseDir, "src/main/java/Foo.java");
    assertThat(commandLine.toCommandLine())
      .isEqualTo(
        "svn blame --xml --use-merge-history --non-interactive -x -w --config-dir /home/julien/.svn --username myUser --password myPass --trust-server-cert src/main/java/Foo.java");
    assertThat(commandLine.toString())
      .isEqualTo(
        "svn blame --xml --use-merge-history --non-interactive -x -w --config-dir /home/julien/.svn --username ******** --password ******** --trust-server-cert src/main/java/Foo.java");
  }
View Full Code Here


    });
  }

  private void blame(final FileSystem fs, final InputFile inputFile, final BlameOutput output) {
    String filename = inputFile.relativePath();
    Command cl = createCommandLine(fs.baseDir(), filename);
    SvnBlameConsumer consumer = new SvnBlameConsumer(filename);
    StringStreamConsumer stderr = new StringStreamConsumer();
    int exitCode;
    try {
      exitCode = execute(cl, consumer, stderr);
    } catch (CommandException e) {
      // Unwrap CommandException
      throw e.getCause() instanceof RuntimeException ? (RuntimeException) e.getCause() : new IllegalStateException(e.getCause());
    }
    if (exitCode != 0) {
      throw new IllegalStateException("The svn blame command [" + cl.toString() + "] failed: " + stderr.getOutput());
    }
    List<BlameLine> lines = consumer.getLines();
    if (lines.size() == inputFile.lines() - 1) {
      // SONARPLUGINS-3097 SVN do not report blame on last empty line
      lines.add(lines.get(lines.size() - 1));
View Full Code Here

    return commandExecutor.execute(cl, consumer, stderr, -1);
  }

  @VisibleForTesting
  Command createCommandLine(File baseDir, String filename) {
    Command cl = Command.create("svn");
    for (Entry<String, String> env : System.getenv().entrySet()) {
      cl.setEnvironmentVariable(env.getKey(), env.getValue());
    }
    cl.setEnvironmentVariable("LC_MESSAGES", "en");

    cl.setDirectory(baseDir);
    cl.addArgument("blame");
    cl.addArgument("--xml");
    if (configuration.useMergeHistory()) {
      cl.addArgument("--use-merge-history");
    }
    cl.addArgument("--non-interactive");
    cl.addArgument("-x");
    cl.addArgument("-w");
    String configDir = configuration.configDir();
    if (configDir != null) {
      cl.addArgument("--config-dir");
      cl.addArgument(configDir);
    }
    String username = configuration.username();
    if (username != null) {
      cl.addArgument("--username");
      cl.addMaskedArgument(username);
      String password = configuration.password();
      if (password != null) {
        cl.addArgument("--password");
        cl.addMaskedArgument(password);
      }
    }
    if (configuration.trustServerCert()) {
      cl.addArgument("--trust-server-cert");
    }
    cl.addArgument(filename);
    return cl;
  }
View Full Code Here

    });
  }

  private void blame(File baseDir, BlameOutput output, InputFile inputFile) {
    String filename = inputFile.relativePath();
    Command cl = createCommandLine(baseDir, filename);
    GitBlameConsumer consumer = new GitBlameConsumer(filename);
    StringStreamConsumer stderr = new StringStreamConsumer();

    int exitCode = execute(cl, consumer, stderr);
    if (exitCode != 0) {
      throw new IllegalStateException("The git blame command [" + cl.toString() + "] failed: " + stderr.getOutput());
    }
    List<BlameLine> lines = consumer.getLines();
    if (lines.size() == inputFile.lines() - 1) {
      // SONARPLUGINS-3097 Git do not report blame on last empty line
      lines.add(lines.get(lines.size() - 1));
View Full Code Here

    LOG.debug("Executing: " + cl);
    return commandExecutor.execute(cl, consumer, stderr, -1);
  }

  private Command createCommandLine(File workingDirectory, String filename) {
    Command cl = Command.create("git");
    cl.addArgument("blame");
    if (workingDirectory != null) {
      cl.setDirectory(workingDirectory);
    }
    cl.addArgument("--porcelain");
    cl.addArgument(filename);
    cl.addArgument("-w");
    return cl;
  }
View Full Code Here

TOP

Related Classes of org.sonar.api.utils.command.Command

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.