aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/UnchangedFilesHandlerTest.java
blob: 51336d5e5e152c9b14454b6ff046199c656737c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.scanner.sensor;

import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.event.Level;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.config.Configuration;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.testfixtures.log.LogTester;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.scan.branch.DefaultBranchConfiguration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

public class UnchangedFilesHandlerTest {
  private final ExecutingSensorContext executingSensorContext = new ExecutingSensorContext();
  private final Configuration enabledConfig = new MapSettings().setProperty("sonar.unchangedFiles.optimize", true).asConfig();
  private final DefaultInputFile file = mock(DefaultInputFile.class);
  private final BranchConfiguration defaultBranchConfig = new DefaultBranchConfiguration();

  @Rule
  public LogTester logTester = new LogTester();

  @Test
  public void log_if_active() {
    new UnchangedFilesHandler(enabledConfig, defaultBranchConfig, executingSensorContext);
    assertThat(logTester.logs()).contains("Optimization for unchanged files enabled");
  }

  @Test
  public void not_active_if_its_pr() {
    logTester.setLevel(Level.DEBUG);
    BranchConfiguration prConfig = branchConfiguration(null, null, true);
    UnchangedFilesHandler handler = new UnchangedFilesHandler(enabledConfig, prConfig, executingSensorContext);
    assertThat(logTester.logs()).contains("Optimization for unchanged files not enabled because it's not an analysis of a branch with a previous analysis");

    handler.markAsUnchanged(file);
    verifyNoInteractions(file);
  }

  @Test
  public void not_active_if_using_different_reference() {
    logTester.setLevel(Level.DEBUG);
    BranchConfiguration differentRefConfig = branchConfiguration("a", "b", false);
    UnchangedFilesHandler handler = new UnchangedFilesHandler(enabledConfig, differentRefConfig, executingSensorContext);
    assertThat(logTester.logs()).contains("Optimization for unchanged files not enabled because it's not an analysis of a branch with a previous analysis");

    handler.markAsUnchanged(file);
    verifyNoInteractions(file);
  }

  @Test
  public void not_active_if_property_not_defined() {
    UnchangedFilesHandler handler = new UnchangedFilesHandler(new MapSettings().asConfig(), defaultBranchConfig, executingSensorContext);
    handler.markAsUnchanged(file);
    verifyNoInteractions(file);
    assertThat(logTester.logs()).isEmpty();
  }

  @Test
  public void mark_file_if_sensor_is_enabled() {
    when(file.status()).thenReturn(InputFile.Status.SAME);
    executingSensorContext.setSensorExecuting(new SensorId("cpp", "CFamily"));
    UnchangedFilesHandler handler = new UnchangedFilesHandler(enabledConfig, defaultBranchConfig, executingSensorContext);

    handler.markAsUnchanged(file);
    verify(file).setMarkedAsUnchanged(true);
  }

  @Test
  public void dont_mark_file_if_sensor_is_not_enabled() {
    executingSensorContext.setSensorExecuting(new SensorId("cpp", "other"));
    UnchangedFilesHandler handler = new UnchangedFilesHandler(enabledConfig, defaultBranchConfig, executingSensorContext);

    handler.markAsUnchanged(file);
    verifyNoInteractions(file);
  }

  @Test
  public void dont_mark_file_if_no_sensor_running() {
    executingSensorContext.clearExecutingSensor();
    UnchangedFilesHandler handler = new UnchangedFilesHandler(enabledConfig, defaultBranchConfig, executingSensorContext);

    handler.markAsUnchanged(file);
    verifyNoInteractions(file);
  }

  @Test
  public void dont_mark_file_is_status_is_not_same() {
    when(file.status()).thenReturn(InputFile.Status.CHANGED);
    executingSensorContext.setSensorExecuting(new SensorId("cpp", "CFamily"));
    UnchangedFilesHandler handler = new UnchangedFilesHandler(enabledConfig, defaultBranchConfig, executingSensorContext);

    handler.markAsUnchanged(file);
    verify(file, never()).setMarkedAsUnchanged(true);
  }

  private BranchConfiguration branchConfiguration(@Nullable String branchName, @Nullable String referenceName, boolean isPullRequest) {
    BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
    when(branchConfiguration.referenceBranchName()).thenReturn(referenceName);
    when(branchConfiguration.branchName()).thenReturn(branchName);
    when(branchConfiguration.isPullRequest()).thenReturn(isPullRequest);
    return branchConfiguration;
  }
}