|
| 1 | +"Tags starting with this identifier are broken. Remove upon fix." |
| 2 | +const brokenidentifiers = ( |
| 3 | + "deprecated_returnsquarepower", |
| 4 | + "deprecated_squarepower", |
| 5 | + "DocTestSetup", |
| 6 | + "printgreeting", |
| 7 | + "returnsquarepower", |
| 8 | +) |
| 9 | +"Kinds that have not been added yet (and are therefore broken). Remove upon fix." |
| 10 | +const brokenkinds = ('m', 's') |
| 11 | + |
| 12 | +"Path to Ctags configuration file (the parser)." |
| 13 | +const ctagsconfigpath = joinpath(@__DIR__, "..", "ctags") |
| 14 | +"Test file to generate the tags from." |
| 15 | +const testfilepath = joinpath(@__DIR__, "testfile.jl") |
| 16 | +"File containing test tags to compare against." |
| 17 | +const testtagspath = joinpath(@__DIR__, "testtags") |
| 18 | + |
| 19 | +"Ctags binaries to sequentially try to call if one is not found." |
| 20 | +const ctagsbins = ( |
| 21 | + "ctags", |
| 22 | + "uctags", |
| 23 | + "ectags", |
| 24 | + "universal-ctags", |
| 25 | + "exuberant-ctags", |
| 26 | + "u-ctags", |
| 27 | + "e-ctags", |
| 28 | +) |
| 29 | +"Arguments to pass to a Ctags binary." |
| 30 | +const ctagsargs = ( |
| 31 | + "--options=$ctagsconfigpath", # Include our tags file. |
| 32 | + "-f -" # Write to stdout. |
| 33 | +) |
| 34 | + |
| 35 | + |
| 36 | +"Utility function to convert an `AbstractVector` of tags to a common format." |
| 37 | +function converttags(tags::AbstractVector) |
| 38 | + tags = filter(s -> !isempty(s) && s[1] != '!', tags) |
| 39 | + Set(tags) |
| 40 | +end |
| 41 | + |
| 42 | +"Return tags generatad from the given file and convert them using [`converttags`](@ref)." |
| 43 | +function generatetags(testfile::AbstractString, ctagsbin::AbstractString="ctags") |
| 44 | + ctagscmd = `$ctagsbin $ctagsargs $testfile` |
| 45 | + tags = open(ctagscmd, "r", stdout) do io |
| 46 | + readlines(io) |
| 47 | + end |
| 48 | + return converttags(tags) |
| 49 | +end |
| 50 | + |
| 51 | +function trygeneratetags(testfile::AbstractString) |
| 52 | + for ctagsbinary in ctagsbins |
| 53 | + try |
| 54 | + return generatetags(testfile, ctagsbinary) |
| 55 | + catch e |
| 56 | + # For compatibility accept either exception type. |
| 57 | + if !(err isa LoadError || err isa ErrorException |
| 58 | + || err isa ProcessFailedException) |
| 59 | + rethrow() |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | +end |
| 64 | + |
| 65 | +""" |
| 66 | +Return tags to compare against. |
| 67 | +The desired output of the Ctags program applied to [`testfilepath`](@ref). |
| 68 | +""" |
| 69 | +function gettargettags(testtags::AbstractString) |
| 70 | + tags = readlines(testtags) |
| 71 | + converttags(tags) |
| 72 | +end |
| 73 | + |
| 74 | + |
| 75 | +"Return the path listed in the output tags." |
| 76 | +function getpath(outputs) |
| 77 | + # We are operating on a set, so we cannot use `lastindex`. |
| 78 | + refoutput = pop!(outputs) |
| 79 | + push!(outputs, refoutput) |
| 80 | + path = split(refoutput, '\t')[2] |
| 81 | +end |
| 82 | + |
| 83 | +"Return whether the given tag should be evaluated using [`test_broken`](@ref)." |
| 84 | +function isbroken(target) |
| 85 | + columns = split(target, '\t') |
| 86 | + return columns[1] in brokenidentifiers || columns[4][1] in brokenkinds |
| 87 | +end |
| 88 | + |
| 89 | +"Replace the path listed in the given tag with the given path." |
| 90 | +function replacepath(tag, path) |
| 91 | + arraytag = split(tag, '\t') |
| 92 | + arraytag[2] = path |
| 93 | + return join(arraytag, '\t') |
| 94 | +end |
| 95 | + |
| 96 | + |
| 97 | +""" |
| 98 | +Test whether the given target is in the given outputs. |
| 99 | +The target's listed path is replaced with `outputpath` to ensure static tests. |
| 100 | +""" |
| 101 | +function test_target_in_outputs(target, outputs, outputpath) |
| 102 | + target = replacepath(target, outputpath) |
| 103 | + target_in_outputs = target in outputs |
| 104 | + |
| 105 | + if isbroken(target) |
| 106 | + println("testing broken tag: ", target) |
| 107 | + result = @test_broken target_in_outputs |
| 108 | + else |
| 109 | + println("testing tag: ", target) |
| 110 | + result = @test target_in_outputs |
| 111 | + end |
| 112 | + target_in_outputs && delete!(outputs, target) |
| 113 | +end |
| 114 | + |
| 115 | +"Test the two given tags against each other." |
| 116 | +function testtags(targets, outputs) |
| 117 | + outputpath = getpath(outputs) |
| 118 | + @testset "Tags" begin |
| 119 | + for target in targets |
| 120 | + test_target_in_outputs(target, outputs, outputpath) |
| 121 | + end |
| 122 | + @test_broken isempty(outputs) |
| 123 | + end |
| 124 | +end |
| 125 | + |
0 commit comments