org.sonar.api.utils.command.StreamConsumer Java Examples

The following examples show how to use org.sonar.api.utils.command.StreamConsumer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TsLintExecutorImplTest.java    From SonarTsPlugin with MIT License 6 votes vote down vote up
@Test
public void executesCommandWithCorrectArgumentsAndTimeouts() {
    final ArrayList<Command> capturedCommands = new ArrayList<Command>();
    final ArrayList<Long> capturedTimeouts = new ArrayList<Long>();

    Answer<Integer> captureCommand = new Answer<Integer>() {
        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            capturedCommands.add((Command) invocation.getArguments()[0]);
            capturedTimeouts.add((long) invocation.getArguments()[3]);
            return 0;
        }
    };

    when(this.commandExecutor.execute(any(Command.class), any(StreamConsumer.class), any(StreamConsumer.class), any(long.class))).then(captureCommand);
    this.executorImpl.execute(this.config, Arrays.asList(new String[] { "path/to/file", "path/to/another" }));

    assertEquals(1, capturedCommands.size());

    Command theCommand = capturedCommands.get(0);
    long theTimeout = capturedTimeouts.get(0);

    assertEquals("node path/to/tslint --format json --rules-dir path/to/rules --out path/to/temp --config path/to/config path/to/file path/to/another", theCommand.toCommandLine());
    // Expect one timeout period per file processed
    assertEquals(2 * 40000, theTimeout);
}
 
Example #2
Source File: TsLintExecutorImplTest.java    From SonarTsPlugin with MIT License 6 votes vote down vote up
@Test
public void usesTypeCheckParameter_ifConfigSaysToUseTypeCheck() {
    final ArrayList<Command> capturedCommands = new ArrayList<Command>();

    Answer<Integer> captureCommand = new Answer<Integer>() {
        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            capturedCommands.add((Command) invocation.getArguments()[0]);
            return 0;
        }
    };

    this.config.setPathToTsConfig("path/to/tsconfig.json");
    this.config.setShouldPerformTypeCheck(true);

    when(this.commandExecutor.execute(any(Command.class), any(StreamConsumer.class), any(StreamConsumer.class), any(long.class))).then(captureCommand);
    this.executorImpl.execute(this.config, Arrays.asList(new String[] { "path/to/file", "path/to/another" }));

    assertEquals(1, capturedCommands.size());

    Command theCommand = capturedCommands.get(0);

    assertEquals("node path/to/tslint --format json --rules-dir path/to/rules --out path/to/temp --config path/to/config --project path/to/tsconfig.json --type-check", theCommand.toCommandLine());
}
 
Example #3
Source File: TsLintExecutorImplTest.java    From SonarTsPlugin with MIT License 6 votes vote down vote up
@Test
public void DoesNotAddRulesDirParameter_IfNull() {
    final ArrayList<Command> capturedCommands = new ArrayList<Command>();
    final ArrayList<Long> capturedTimeouts = new ArrayList<Long>();

    Answer<Integer> captureCommand = new Answer<Integer>() {
        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            capturedCommands.add((Command) invocation.getArguments()[0]);
            capturedTimeouts.add((long) invocation.getArguments()[3]);
            return 0;
        }
    };

    when(this.commandExecutor.execute(any(Command.class), any(StreamConsumer.class), any(StreamConsumer.class), any(long.class))).then(captureCommand);

    this.config.setRulesDir(null);
    this.executorImpl.execute(this.config, Arrays.asList(new String[] { "path/to/file" }));

    Command theCommand = capturedCommands.get(0);
    assertFalse(theCommand.toCommandLine().contains("--rules-dir"));
}
 
Example #4
Source File: TsLintExecutorImplTest.java    From SonarTsPlugin with MIT License 6 votes vote down vote up
@Test
public void DoesNotAddRulesDirParameter_IfEmptyString() {
    final ArrayList<Command> capturedCommands = new ArrayList<Command>();
    final ArrayList<Long> capturedTimeouts = new ArrayList<Long>();

    Answer<Integer> captureCommand = new Answer<Integer>() {
        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            capturedCommands.add((Command) invocation.getArguments()[0]);
            capturedTimeouts.add((long) invocation.getArguments()[3]);
            return 0;
        }
    };

    when(this.commandExecutor.execute(any(Command.class), any(StreamConsumer.class), any(StreamConsumer.class), any(long.class))).then(captureCommand);

    this.config.setRulesDir("");
    this.executorImpl.execute(this.config, Arrays.asList(new String[] { "path/to/file" }));

    Command theCommand = capturedCommands.get(0);
    assertFalse(theCommand.toCommandLine().contains("--rules-dir"));
}
 
Example #5
Source File: TsLintExecutorImplTest.java    From SonarTsPlugin with MIT License 5 votes vote down vote up
@Test
public void doesNotSendFileListToTsLint_ifConfigSaysToUseProjectFile() {
    final ArrayList<Command> capturedCommands = new ArrayList<Command>();
    final ArrayList<Long> capturedTimeouts = new ArrayList<Long>();

    Answer<Integer> captureCommand = new Answer<Integer>() {
        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            capturedCommands.add((Command) invocation.getArguments()[0]);
            capturedTimeouts.add((long) invocation.getArguments()[3]);
            return 0;
        }
    };

    this.config.setPathToTsConfig("path/to/tsconfig.json");

    when(this.commandExecutor.execute(any(Command.class), any(StreamConsumer.class), any(StreamConsumer.class), any(long.class))).then(captureCommand);
    this.executorImpl.execute(this.config, Arrays.asList(new String[] { "path/to/file", "path/to/another" }));

    assertEquals(1, capturedCommands.size());

    Command theCommand = capturedCommands.get(0);
    long theTimeout = capturedTimeouts.get(0);

    assertEquals("node path/to/tslint --format json --rules-dir path/to/rules --out path/to/temp --config path/to/config --project path/to/tsconfig.json", theCommand.toCommandLine());
    // Timeout should be just what we specified since we're not batching

    assertEquals(40000, theTimeout);
}
 
Example #6
Source File: TsLintExecutorImplTest.java    From SonarTsPlugin with MIT License 5 votes vote down vote up
@Test
public void BatchesExecutions_IfTooManyFilesForCommandLine() {
    List<String> filenames = new ArrayList<String>();
    int currentLength = 0;
    int standardCmdLength = "node path/to/tslint --format json --rules-dir path/to/rules --out path/to/temp --config path/to/config".length();

    String firstBatch = "first batch";
    while (currentLength + 12 < TsLintExecutorImpl.MAX_COMMAND_LENGTH - standardCmdLength) {
        filenames.add(firstBatch);
        currentLength += firstBatch.length() + 1; // 1 for the space
    }
    filenames.add("second batch");

    final ArrayList<Command> capturedCommands = new ArrayList<Command>();
    final ArrayList<Long> capturedTimeouts = new ArrayList<Long>();

    Answer<Integer> captureCommand = new Answer<Integer>() {
        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            capturedCommands.add((Command) invocation.getArguments()[0]);
            capturedTimeouts.add((long) invocation.getArguments()[3]);
            return 0;
        }
    };

    when(this.commandExecutor.execute(any(Command.class), any(StreamConsumer.class), any(StreamConsumer.class), any(long.class))).then(captureCommand);
    this.executorImpl.execute(this.config, filenames);

    assertEquals(2, capturedCommands.size());

    Command theSecondCommand = capturedCommands.get(1);

    assertFalse(theSecondCommand.toCommandLine().contains("first batch"));
    assertTrue(theSecondCommand.toCommandLine().contains("second batch"));
}
 
Example #7
Source File: RubocopExecutor.java    From sonar-ruby-plugin with MIT License 4 votes vote down vote up
private String getCommandOutput(Command thisCommand, StreamConsumer stdOutConsumer, StreamConsumer stdErrConsumer, File rubocopOutputFile, Integer timeoutMs) {
    createExecutor().execute(thisCommand, stdOutConsumer, stdErrConsumer, timeoutMs);
    return getFileContent(rubocopOutputFile);
}
 
Example #8
Source File: TsLintExecutorImpl.java    From SonarTsPlugin with MIT License 4 votes vote down vote up
private String getCommandOutput(Command thisCommand, StreamConsumer stdOutConsumer, StreamConsumer stdErrConsumer, File tslintOutputFile, Integer timeoutMs) {
    this.createExecutor().execute(thisCommand, stdOutConsumer, stdErrConsumer, timeoutMs);

    return getFileContent(tslintOutputFile);
}