-
Notifications
You must be signed in to change notification settings - Fork 0
Metadata params checks and undefined object checks changes #54
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
Open
wgrzeszczak
wants to merge
4
commits into
master
Choose a base branch
from
metadata-undefined-object-checks-reworked
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
...es/platformos-check-common/src/checks/metadata-params/extract-undefined-variables.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { extractUndefinedVariables } from './extract-undefined-variables'; | ||
|
|
||
| describe('extractUndefinedVariables', () => { | ||
| it('should return variables used but not defined', () => { | ||
| const source = `{% liquid | ||
| assign b = a | ||
| %} | ||
| {{ b }}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal(['a']); | ||
| }); | ||
|
|
||
| it('should not include assigned variables', () => { | ||
| const source = `{% assign x = 1 %}{{ x }}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('should not include captured variables', () => { | ||
| const source = `{% capture x %}hello{% endcapture %}{{ x }}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('should not include for loop variables', () => { | ||
| const source = `{% for item in items %}{{ item }}{% endfor %}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal(['items']); | ||
| }); | ||
|
|
||
| it('should not include forloop variable', () => { | ||
| const source = `{% for item in items %}{{ forloop.index }}{% endfor %}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal(['items']); | ||
| }); | ||
|
|
||
| it('should handle function result variables', () => { | ||
| const source = `{% function res = 'my_partial' %}{{ res }}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('should handle graphql result variables', () => { | ||
| const source = `{% graphql res = 'my_query' %}{{ res }}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('should handle inline graphql result variables', () => { | ||
| const source = `{% graphql res %}{ users { id } }{% endgraphql %}{{ res }}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('should handle parse_json result variables', () => { | ||
| const source = `{% parse_json data %}{"a":1}{% endparse_json %}{{ data }}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('should not include global objects', () => { | ||
| const source = `{{ context.session }}`; | ||
| const result = extractUndefinedVariables(source, [ | ||
| 'context', | ||
| 'null', | ||
| 'true', | ||
| 'false', | ||
| 'blank', | ||
| 'empty', | ||
| ]); | ||
| expect(result).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('should deduplicate results', () => { | ||
| const source = `{{ a }}{{ a }}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal(['a']); | ||
| }); | ||
|
|
||
| it('should return empty array if source fails to parse', () => { | ||
| const source = `{% invalid unclosed`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('should handle increment/decrement as definitions', () => { | ||
| const source = `{% increment counter %}{{ counter }}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('should handle background file-based result variables', () => { | ||
| const source = `{% background my_job = 'some_partial' %}{{ my_job }}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('should handle inline background tag without job_id', () => { | ||
| const source = `{% background source_name: 'my_task' %}echo "hello"{% endbackground %}{{ my_job }}`; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal(['my_job']); | ||
| }); | ||
|
|
||
| it('should not include doc param names', () => { | ||
| const source = ` | ||
| {% doc %} | ||
| @param {String} name - a name | ||
| {% enddoc %} | ||
| {{ name }} | ||
| `; | ||
| const result = extractUndefinedVariables(source); | ||
| expect(result).to.deep.equal(['name']); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can also add a test for inline graphql aka
{% graphql res %}...{% endgraphql %}, for background tag - both as a function and inlineThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done