|
8 | 8 | import com.google.common.collect.Lists; |
9 | 9 | import com.hubspot.jinjava.Jinjava; |
10 | 10 | import com.hubspot.jinjava.JinjavaConfig; |
| 11 | +import com.hubspot.jinjava.features.FeatureConfig; |
| 12 | +import com.hubspot.jinjava.features.FeatureStrategies; |
11 | 13 | import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable; |
12 | 14 | import com.hubspot.jinjava.interpret.TemplateError.ErrorItem; |
13 | 15 | import com.hubspot.jinjava.interpret.TemplateError.ErrorReason; |
14 | 16 | import com.hubspot.jinjava.interpret.TemplateError.ErrorType; |
| 17 | +import com.hubspot.jinjava.mode.EagerExecutionMode; |
15 | 18 | import com.hubspot.jinjava.mode.PreserveRawExecutionMode; |
16 | 19 | import com.hubspot.jinjava.objects.date.FormattedDate; |
17 | 20 | import com.hubspot.jinjava.objects.date.StrftimeFormatter; |
18 | 21 | import com.hubspot.jinjava.tree.TextNode; |
19 | 22 | import com.hubspot.jinjava.tree.output.BlockInfo; |
| 23 | +import com.hubspot.jinjava.tree.output.OutputList; |
20 | 24 | import com.hubspot.jinjava.tree.parse.TextToken; |
21 | 25 | import com.hubspot.jinjava.tree.parse.TokenScannerSymbols; |
22 | 26 | import java.time.ZoneId; |
@@ -503,4 +507,53 @@ public void itFiltersDuplicateErrors() { |
503 | 507 |
|
504 | 508 | assertThat(interpreter.getErrors()).containsExactly(error1, error2); |
505 | 509 | } |
| 510 | + |
| 511 | + @Test |
| 512 | + public void itPreventsAccidentalExpressions() { |
| 513 | + String makeExpression = "if (true) {\n{%- print deferred -%}\n}"; |
| 514 | + String makeTag = "if (true) {\n{%- print '% print 123 %' -%}\n}"; |
| 515 | + String makeNote = "if (true) {\n{%- print '# note #' -%}\n}"; |
| 516 | + jinjava.getGlobalContext().put("deferred", DeferredValue.instance()); |
| 517 | + |
| 518 | + JinjavaInterpreter normalInterpreter = new JinjavaInterpreter( |
| 519 | + jinjava, |
| 520 | + jinjava.getGlobalContext(), |
| 521 | + JinjavaConfig.newBuilder().withExecutionMode(EagerExecutionMode.instance()).build() |
| 522 | + ); |
| 523 | + JinjavaInterpreter preventingInterpreter = new JinjavaInterpreter( |
| 524 | + jinjava, |
| 525 | + jinjava.getGlobalContext(), |
| 526 | + JinjavaConfig |
| 527 | + .newBuilder() |
| 528 | + .withFeatureConfig( |
| 529 | + FeatureConfig |
| 530 | + .newBuilder() |
| 531 | + .add(OutputList.PREVENT_ACCIDENTAL_EXPRESSIONS, FeatureStrategies.ACTIVE) |
| 532 | + .build() |
| 533 | + ) |
| 534 | + .withExecutionMode(EagerExecutionMode.instance()) |
| 535 | + .build() |
| 536 | + ); |
| 537 | + JinjavaInterpreter.pushCurrent(normalInterpreter); |
| 538 | + try { |
| 539 | + assertThat(normalInterpreter.render(makeExpression)) |
| 540 | + .isEqualTo("if (true) {{% print deferred %}}"); |
| 541 | + assertThat(normalInterpreter.render(makeTag)) |
| 542 | + .isEqualTo("if (true) {% print 123 %}"); |
| 543 | + assertThat(normalInterpreter.render(makeNote)).isEqualTo("if (true) {# note #}"); |
| 544 | + } finally { |
| 545 | + JinjavaInterpreter.popCurrent(); |
| 546 | + } |
| 547 | + JinjavaInterpreter.pushCurrent(preventingInterpreter); |
| 548 | + try { |
| 549 | + assertThat(preventingInterpreter.render(makeExpression)) |
| 550 | + .isEqualTo("if (true) {\n" + "{#- #}{% print deferred %}}"); |
| 551 | + assertThat(preventingInterpreter.render(makeTag)) |
| 552 | + .isEqualTo("if (true) {\n" + "{#- #}% print 123 %}"); |
| 553 | + assertThat(preventingInterpreter.render(makeNote)) |
| 554 | + .isEqualTo("if (true) {\n" + "{#- #}# note #}"); |
| 555 | + } finally { |
| 556 | + JinjavaInterpreter.popCurrent(); |
| 557 | + } |
| 558 | + } |
506 | 559 | } |
0 commit comments