Skip to content

Commit

Permalink
azp: Fail on invalid elseif/else sequence (#269)
Browse files Browse the repository at this point in the history
* azp: Fail on invalid elseif/else sequence

* fix unclear error msg

* throw else after else
  • Loading branch information
ChristopherHX authored Nov 22, 2023
1 parent e3e3265 commit 9aed8e1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Sdk/DTObjectTemplating/ObjectTemplating/TemplateUnraveler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -894,14 +894,31 @@ private void StartIfInsertion()
var parentSequenceState = expressionState.Parent as SequenceState;
bool skip = false;
bool metastate = false;
if(expressionState.Value.Type == TokenType.IfExpression || (parentMappingState != null && parentMappingState.IfExpressionResults.TryGetValue(parentMappingState.Index - 1, out skip) || parentSequenceState != null && parentSequenceState.IfExpressionResults.TryGetValue(parentSequenceState.Index - 1, out skip)) && skip) {
Func<bool> shouldRun = () => {
if(parentMappingState != null) {
if(parentMappingState.IfExpressionResults.TryGetValue(parentMappingState.Index - 1, out skip)) {
return skip;
}
m_context.Error(expressionState.Value, $"This {expressionState.Value.ToString()} must be chained after an if token in the parent mapping");
} else if(parentSequenceState != null) {
if(parentSequenceState.IfExpressionResults.TryGetValue(parentSequenceState.Index - 1, out skip)) {
return skip;
}
m_context.Error(expressionState.Value, $"This {expressionState.Value.ToString()} must be chained after an if token in the parent sequence");
}
return false;
};
if(expressionState.Value.Type == TokenType.IfExpression || shouldRun()) {
metastate = skip = expressionState.Value.Type == TokenType.ElseExpression ? false : !PipelineTemplateConverter.ConvertToIfResult(m_context, new BasicExpressionToken(expressionState.Value.FileId, expressionState.Value.Line, expressionState.Value.Column, expressionState.Value.Condition).EvaluateTemplateToken(m_context, out _));
} else {
skip = true;
metastate = false;
}
if(parentSequenceState == null) {
parentMappingState.IfExpressionResults[parentMappingState.Index] = metastate;
if(expressionState.Value.Type != TokenType.ElseExpression) {
// Don't set this for ${{ else }}: tokens, because an else token is the last in the sequence
parentMappingState.IfExpressionResults[parentMappingState.Index] = metastate;
}
var nestedValue = parentMappingState.Value[parentMappingState.Index].Value;
var nestedMapping = nestedValue as MappingToken;
var removeBytes = 0;
Expand Down Expand Up @@ -948,7 +965,10 @@ private void StartIfInsertion()
expressionState.End();
}
} else {
parentSequenceState.IfExpressionResults[parentSequenceState.Index] = metastate;
if(expressionState.Value.Type != TokenType.ElseExpression) {
// Don't set this for ${{ else }}: tokens, because an else token is the last in the sequence
parentSequenceState.IfExpressionResults[parentSequenceState.Index] = metastate;
}
var nestedValue = (parentSequenceState.Value[parentSequenceState.Index] as MappingToken)[0].Value;
var nestedSequence = nestedValue as SequenceToken;
var removeBytes = 0;
Expand Down
9 changes: 9 additions & 0 deletions testworkflows/azpipelines/else-after-else-error/pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ExpectedException: TemplateValidationException
# ExpectedErrorMessage: else
steps:
- ${{ if true }}:
- ${{ if false }}:
- ${{ else }}:
- ${{ else }}:
- ${{ else }}:
- script: noop
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ExpectedException: TemplateValidationException
# ExpectedErrorMessage: else
steps:
- ${{ if true }}:
# This else token is an error if not skipped by if
- ${{ else }}:
- ${{ else }}:
- script: noop
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
steps:
- ${{ if false }}:
# This else token is not an error if skipped by if
- ${{ else }}:
- ${{ else }}:
- script: noop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ExpectedException: TemplateValidationException
# ExpectedErrorMessage: else
steps:
- ${{ if false }}:
- ${{ else }}
- ${{ else }}:
- script: noop

0 comments on commit 9aed8e1

Please sign in to comment.