generated from quinnj/Example.jl
-
Couldn't load subscription status.
- Fork 9
[WIP] Try to filter test items without constructing the AST #226
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
Draft
Drvi
wants to merge
6
commits into
td-include-tasks
Choose a base branch
from
td-filter-on-tokens
base: td-include-tasks
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.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
97a43ca
Try to filter test items without constructing the AST
Drvi 60fea81
.
Drvi d124aa8
.
Drvi 33b7073
Try a line number workaround
Drvi 456f1d1
Also try to early filter-out test_rels
Drvi c33eccd
Try removing explicit JuliaSyntax dep
Drvi 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
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
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
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
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,95 @@ | ||
| using Base.JuliaSyntax: JuliaSyntax, ParseError, ParseStream, @K_str | ||
| using Base.JuliaSyntax: any_error, build_tree, first_byte, kind, parse!, peek_full_token, peek_token | ||
| using StringViews | ||
|
|
||
| function include_test_file(ti_filter::TestItemFilter, path::String) | ||
| bytes = read(path) | ||
| stream = ParseStream(bytes) | ||
| line_starts = Int32[Int32(i) for (i, x) in enumerate(bytes) if x == 0x0a] # :( | ||
| tls = task_local_storage() | ||
| tls[:SOURCE_PATH] = path # This is also done by Base.include | ||
| try | ||
| @inbounds while true | ||
| JuliaSyntax.bump_trivia(stream, skip_newlines=true) | ||
| t = peek_token(stream, 1) | ||
| k = kind(t) | ||
| k == K"EndMarker" && break | ||
| line = _source_line_index(line_starts, first_byte(stream)) | ||
| if k == K"@" | ||
| tf = peek_full_token(stream, 2) | ||
| v = @view(bytes[tf.first_byte:tf.last_byte]) | ||
| if v == b"testitem" ; _eval_test_item_stream(stream, path, line, ti_filter, bytes) | ||
| elseif v == b"testsetup"; _eval_test_setup_stream(stream, path, line) | ||
| elseif v == b"test_rel" ; _eval_test_rel_stream(stream, path, line, ti_filter, bytes) | ||
| else | ||
| error("Test files must only include `@testitem` and `@testsetup` calls, got an `@$(StringView(v))` at $(path):$(line)") # TODO | ||
| end | ||
| else | ||
| error("Test files must only include `@testitem` and `@testsetup` calls, got a $t at $(path):$(line)") # TODO | ||
| end | ||
| empty!(stream) # TODO: SourceFile created on a reset stream always start line number at 1 | ||
| end | ||
| finally | ||
| delete!(tls, :SOURCE_PATH) | ||
| end | ||
| end | ||
|
|
||
| @inline function _source_line_index(line_starts, bytes_pos) | ||
| lineidx = searchsortedfirst(line_starts, bytes_pos) | ||
| return (lineidx < lastindex(line_starts)) ? lineidx : lineidx-1 | ||
| end | ||
|
|
||
| # unconditionally eval | ||
| function _eval_test_setup_stream(stream, path, line) | ||
| parse!(stream; rule=:statement) | ||
| ast = build_tree(Expr, stream; filename=path, first_line=line) | ||
| Core.eval(Main, ast) | ||
| return nothing | ||
| end | ||
|
|
||
| # test_rel -> apply ti_filter on the parsed ast | ||
| function _eval_test_rel_stream(stream, path, line, ti_filter::TestItemFilter, bytes) | ||
| parse!(stream; rule=:statement) | ||
| if !(ti_filter.name isa Nothing) | ||
| @inbounds for (i, token) in enumerate(stream.tokens) | ||
| if kind(token) == K"Identifier" | ||
| fbyte = JuliaSyntax.token_first_byte(stream, i) | ||
| lbyte = JuliaSyntax.token_last_byte(stream, i) | ||
| if @view(bytes[fbyte:lbyte]) == b"name" | ||
| fbyte = JuliaSyntax.token_first_byte(stream, i + 3) | ||
| lbyte = JuliaSyntax.token_last_byte(stream, i + 3) | ||
| name = StringView(@view(bytes[fbyte:lbyte])) | ||
| _contains(name, ti_filter.name) && break | ||
| return nothing | ||
| end | ||
| end | ||
| end | ||
| end | ||
| ast = build_tree(Expr, stream; filename=path, first_line=line) | ||
| any_error(stream) && throw(ParseError(stream, filename=path)) | ||
| filtered = __filter_rai(ti_filter, ast)::Union{Nothing, Expr} | ||
| filtered === nothing || Core.eval(Main, filtered::Expr) | ||
| return nothing | ||
| end | ||
|
|
||
| # like above, but tries to avoid parsing the ast if it sees from the name identifier token | ||
| # it won't pass the filter | ||
| function _eval_test_item_stream(stream, path, line, ti_filter::TestItemFilter, bytes) | ||
| if !(ti_filter.name isa Nothing) | ||
| name_t = peek_full_token(stream, 4) # 3 was '\"' | ||
| name = @inbounds StringView(@view(bytes[name_t.first_byte:name_t.last_byte])) | ||
| parse!(stream; rule=:statement) | ||
| _contains(name, ti_filter.name) || return nothing | ||
| else | ||
| parse!(stream; rule=:statement) | ||
| end | ||
|
|
||
| ast = build_tree(Expr, stream; filename=path, first_line=line) | ||
| any_error(stream) && throw(ParseError(stream, filename=path)) | ||
| filtered = __filter_ti(ti_filter, ast)::Union{Nothing, Expr} | ||
| filtered === nothing || Core.eval(Main, filtered::Expr) | ||
| return nothing | ||
| end | ||
|
|
||
| @inline _contains(s::AbstractString, pattern::Regex) = occursin(pattern, s) | ||
| @inline _contains(s::AbstractString, pattern::AbstractString) = s == pattern |
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
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
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't (don't want) any non-stdlib deps... how much benefit does StringViews give us? Our options are (i think): drop it, reimplement a minimal version, vendor it
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.
The only thing this gives is the ability to run regexes against a view of a Vector{UInt8} (and comparing them to Strings, but that is easy to do manually)