Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS-186 ESTree AST should not be serialized when Armor is disabled #4737

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ static BuildResult runBuild(Orchestrator orchestrator) {
.setProjectName("Custom Rules")
.setProjectVersion("1.0")
.setDebugLogs(true)
// This flag is an intermediate solution to not run Armor for all projects during the development of the new analyzer.
.setProperty("sonar.armor.internal.enabled", "true")
.setSourceDirs("src");
orchestrator.getServer().provisionProject("custom-rules", "Custom Rules");
orchestrator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected void analyzeFile(InputFile file, @Nullable List<String> tsConfigs, @Nu
LOG.debug("Analyzing file: {}", file.uri());
progressReport.nextFile(file.toString());
var fileContent = contextUtils.shouldSendFileContent(file) ? file.contents() : null;
var skipAst = !consumers.hasConsumers();
var skipAst = !consumers.hasConsumers() || !contextUtils.isSonarArmorEnabled();
var request = getJsAnalysisRequest(file, fileContent, tsProgram, tsConfigs, skipAst);

var response = isJavaScript(file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

class ContextUtils {

/* Internal property to enable Armor (disabled by default) */
private static final String ARMOR_INTERNAL_ENABLED = "sonar.armor.internal.enabled";

private final SensorContext context;

ContextUtils(SensorContext context) {
Expand Down Expand Up @@ -59,4 +62,8 @@ boolean failFast() {
SensorContext context() {
return context;
}

boolean isSonarArmorEnabled() {
return context.config().getBoolean(ARMOR_INTERNAL_ENABLED).orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ void should_send_skipAst_flag_when_there_are_no_consumers() throws Exception {
@Test
void should_not_send_the_skipAst_flag_when_there_are_consumers() throws Exception {
var ctx = createSensorContext(baseDir);
ctx.setSettings(new MapSettings().setProperty("sonar.armor.internal.enabled", "true"));
var inputFile = createInputFile(ctx);
var tsProgram = new TsProgram("1", List.of(inputFile.absolutePath()), List.of());
var consumer = createConsumer();
Expand All @@ -400,6 +401,24 @@ void should_not_send_the_skipAst_flag_when_there_are_consumers() throws Exceptio
assertThat(captor.getValue().skipAst()).isFalse();
}

@Test
void should_send_the_skipAst_flag_when_there_are_consumers_but_armor_is_disabled() throws Exception {
var ctx = createSensorContext(baseDir);
ctx.setSettings(new MapSettings().setProperty("sonar.armor.internal.enabled", "false"));
var inputFile = createInputFile(ctx);
var tsProgram = new TsProgram("1", List.of(inputFile.absolutePath()), List.of());
var consumer = createConsumer();
var sensor = createSensorWithConsumer(consumer);
when(bridgeServerMock.createProgram(any())).thenReturn(tsProgram);
when(bridgeServerMock.analyzeTypeScript(any())).thenReturn(new AnalysisResponse());
sensor.execute(ctx);

createSensor().execute(context);
var captor = ArgumentCaptor.forClass(JsAnalysisRequest.class);
verify(bridgeServerMock).analyzeTypeScript(captor.capture());
assertThat(captor.getValue().skipAst()).isTrue();
}

@Test
void should_send_content_when_not_utf8() throws Exception {
var ctx = createSensorContext(baseDir);
Expand Down
Loading