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

azp: Fail on invalid elseif/else sequence #269

Merged
merged 3 commits into from
Nov 22, 2023
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
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
Loading