|
| 1 | +/* eslint-disable @typescript-eslint/no-non-null-assertion -- |
| 2 | + * Test code intentionally dereferences values after explicit existence/null checks |
| 3 | + * to keep expectations concise and readable. |
| 4 | + */ |
| 5 | +import {describe, expect, it} from "@jest/globals"; |
| 6 | +import { |
| 7 | + findGithubActionsExpressionInText, |
| 8 | + findGithubActionsInlineExpression, |
| 9 | + readFixture, |
| 10 | + readJson |
| 11 | +} from "./syntax-test-utils"; |
| 12 | + |
| 13 | +type ExpressionsGrammar = { |
| 14 | + repository: { |
| 15 | + "block-inline-expression": { |
| 16 | + begin: string; |
| 17 | + end: string; |
| 18 | + patterns: Array<{include: string}>; |
| 19 | + }; |
| 20 | + expression: { |
| 21 | + patterns: Array<{include: string}>; |
| 22 | + }; |
| 23 | + "if-expression": { |
| 24 | + match: string; |
| 25 | + }; |
| 26 | + "block-if-expression": { |
| 27 | + begin: string; |
| 28 | + beginCaptures: { |
| 29 | + "6": { |
| 30 | + patterns: Array<{include: string}>; |
| 31 | + }; |
| 32 | + }; |
| 33 | + }; |
| 34 | + "op-comparison": { |
| 35 | + match: string; |
| 36 | + }; |
| 37 | + "op-logical": { |
| 38 | + match: string; |
| 39 | + }; |
| 40 | + }; |
| 41 | +}; |
| 42 | + |
| 43 | +function collectInlineExpressions(line: string): string[] { |
| 44 | + const result: string[] = []; |
| 45 | + let offset = 0; |
| 46 | + |
| 47 | + while (offset < line.length) { |
| 48 | + const chunk = line.slice(offset); |
| 49 | + const extracted = findGithubActionsInlineExpression(chunk); |
| 50 | + if (!extracted) { |
| 51 | + break; |
| 52 | + } |
| 53 | + result.push(extracted); |
| 54 | + const localIndex = chunk.indexOf(extracted); |
| 55 | + offset += localIndex + extracted.length; |
| 56 | + } |
| 57 | + |
| 58 | + return result; |
| 59 | +} |
| 60 | + |
| 61 | +describe("workflow expression syntax highlighting", () => { |
| 62 | + // Regression test for https://github.com/github/vscode-github-actions/issues/531 |
| 63 | + it("does not swallow trailing YAML comments on if: lines after quoted strings (#531)", () => { |
| 64 | + const grammar = readJson<ExpressionsGrammar>("language/syntaxes/expressions.tmGrammar.json"); |
| 65 | + const fixture = readFixture("src/workflow/syntax/fixtures/if-comment-after-string.yml"); |
| 66 | + const ifLine = fixture.split(/\r?\n/).find(line => line.includes("if: matrix.os != 'macos-latest' #")); |
| 67 | + |
| 68 | + expect(ifLine).toBeDefined(); |
| 69 | + |
| 70 | + const ifExpression = new RegExp(grammar.repository["if-expression"].match); |
| 71 | + const match = ifExpression.exec(ifLine!); |
| 72 | + |
| 73 | + expect(match).not.toBeNull(); |
| 74 | + expect(match![1]).toBe("if:"); |
| 75 | + |
| 76 | + // Desired behavior: the expression capture should stop before the YAML comment. |
| 77 | + // Current bug (#531): capture 2 includes the trailing "# ...", preventing comment tokenization. |
| 78 | + expect(match![2]).toBe("matrix.os != 'macos-latest'"); |
| 79 | + expect(match![3]).toBe(" # Cache causes errors on macOS"); |
| 80 | + }); |
| 81 | + |
| 82 | + // Regression test for https://github.com/github/vscode-github-actions/issues/223 |
| 83 | + it("does not terminate ${{ }} expression early when }} appears inside quoted strings", () => { |
| 84 | + const grammar = readJson<ExpressionsGrammar>("language/syntaxes/expressions.tmGrammar.json"); |
| 85 | + const fixture = readFixture("src/workflow/syntax/fixtures/expression-nested-braces.yml"); |
| 86 | + const matrixLine = fixture.split(/\r?\n/).find(line => line.includes("matrix: ${{")); |
| 87 | + |
| 88 | + expect(matrixLine).toBeDefined(); |
| 89 | + expect(grammar.repository["block-inline-expression"]).toBeDefined(); |
| 90 | + expect(grammar.repository.expression.patterns).toContainEqual({include: "#string"}); |
| 91 | + |
| 92 | + const extracted = findGithubActionsInlineExpression(matrixLine!); |
| 93 | + expect(extracted).toBe("${{ fromJSON(format('{{\"linting\":[\"{0}\"]}}', 'ubuntu-latest')).linting }}"); |
| 94 | + }); |
| 95 | + |
| 96 | + it("supports block if-expression syntax (if: | / if: >)", () => { |
| 97 | + const grammar = readJson<ExpressionsGrammar>("language/syntaxes/expressions.tmGrammar.json"); |
| 98 | + const fixture = readFixture("src/workflow/syntax/fixtures/if-block-expression.yml"); |
| 99 | + const ifLiteralLine = fixture.split(/\r?\n/).find(line => /^\s*if:\s*\|(?:\s+#.*)?$/.test(line)); |
| 100 | + const ifFoldedLine = fixture.split(/\r?\n/).find(line => /^\s*if:\s*>(?:\s+#.*)?$/.test(line)); |
| 101 | + |
| 102 | + expect(ifLiteralLine).toBeDefined(); |
| 103 | + expect(ifFoldedLine).toBeDefined(); |
| 104 | + expect(grammar.repository["block-if-expression"]).toBeDefined(); |
| 105 | + |
| 106 | + const blockIfBegin = new RegExp(grammar.repository["block-if-expression"].begin); |
| 107 | + expect(blockIfBegin.test(ifLiteralLine!)).toBe(true); |
| 108 | + expect(blockIfBegin.test(ifFoldedLine!)).toBe(true); |
| 109 | + }); |
| 110 | + |
| 111 | + it("supports multi-line inline expressions", () => { |
| 112 | + const grammar = readJson<ExpressionsGrammar>("language/syntaxes/expressions.tmGrammar.json"); |
| 113 | + const fixture = readFixture("src/workflow/syntax/fixtures/expression-multiline.yml"); |
| 114 | + |
| 115 | + expect(grammar.repository["block-inline-expression"]).toBeDefined(); |
| 116 | + expect(grammar.repository["block-inline-expression"].begin).toContain("\\$\\{\\{"); |
| 117 | + expect(grammar.repository["block-inline-expression"].end).toContain("\\}\\}"); |
| 118 | + |
| 119 | + const extracted = findGithubActionsExpressionInText(fixture); |
| 120 | + expect(extracted).not.toBeNull(); |
| 121 | + expect(extracted).toContain("${{ format("); |
| 122 | + expect(extracted).toContain("github.ref"); |
| 123 | + expect(extracted).toContain("github.sha"); |
| 124 | + expect(extracted).toContain(") }}"); |
| 125 | + }); |
| 126 | + |
| 127 | + it("supports logical and comparison operators in expression patterns", () => { |
| 128 | + const grammar = readJson<ExpressionsGrammar>("language/syntaxes/expressions.tmGrammar.json"); |
| 129 | + |
| 130 | + expect(grammar.repository["op-comparison"]).toBeDefined(); |
| 131 | + expect(grammar.repository["op-logical"]).toBeDefined(); |
| 132 | + |
| 133 | + const comparison = new RegExp(grammar.repository["op-comparison"].match, "g"); |
| 134 | + const logical = new RegExp(grammar.repository["op-logical"].match, "g"); |
| 135 | + const sample = "github.ref == 'refs/heads/main' && github.event_name != 'pull_request' || false"; |
| 136 | + |
| 137 | + expect(sample.match(comparison)).toEqual(["==", "!="]); |
| 138 | + expect(sample.match(logical)).toEqual(["&&", "||"]); |
| 139 | + }); |
| 140 | + |
| 141 | + it("keeps # inside quoted strings and still separates trailing comments on if: lines", () => { |
| 142 | + const grammar = readJson<ExpressionsGrammar>("language/syntaxes/expressions.tmGrammar.json"); |
| 143 | + const fixture = readFixture("src/workflow/syntax/fixtures/if-inline-edge-cases.yml"); |
| 144 | + const lines = fixture.split(/\r?\n/); |
| 145 | + const hashLine = lines.find(line => line.includes("if: contains(github.ref, '#main') #")); |
| 146 | + const escapedLine = lines.find(line => line.includes("if: github.ref == 'it''s-main' #")); |
| 147 | + |
| 148 | + expect(hashLine).toBeDefined(); |
| 149 | + expect(escapedLine).toBeDefined(); |
| 150 | + |
| 151 | + const ifExpression = new RegExp(grammar.repository["if-expression"].match); |
| 152 | + const hashMatch = ifExpression.exec(hashLine!); |
| 153 | + const escapedMatch = ifExpression.exec(escapedLine!); |
| 154 | + |
| 155 | + expect(hashMatch).not.toBeNull(); |
| 156 | + expect(escapedMatch).not.toBeNull(); |
| 157 | + expect(hashMatch![2]).toBe("contains(github.ref, '#main')"); |
| 158 | + expect(escapedMatch![2]).toBe("github.ref == 'it''s-main'"); |
| 159 | + expect(hashMatch![3]).toBe(" # trailing comment"); |
| 160 | + expect(escapedMatch![3]).toBe(" # escaped quote case"); |
| 161 | + }); |
| 162 | + |
| 163 | + it("handles multiple inline expressions on one line", () => { |
| 164 | + const fixture = readFixture("src/workflow/syntax/fixtures/inline-multiple-expressions.yml"); |
| 165 | + const combinedLine = fixture.split(/\r?\n/).find(line => line.includes("COMBINED: ")); |
| 166 | + |
| 167 | + expect(combinedLine).toBeDefined(); |
| 168 | + const extracted = collectInlineExpressions(combinedLine!); |
| 169 | + |
| 170 | + expect(extracted).toEqual(["${{ github.ref }}", "${{ github.sha }}"]); |
| 171 | + }); |
| 172 | +}); |
0 commit comments