Skip to content

Commit

Permalink
Wrap project_toml_formatting.jl into module
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Jul 3, 2023
1 parent dcb2b79 commit d2c6607
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
40 changes: 35 additions & 5 deletions src/project_toml_formatting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
Aqua.test_project_toml_formatting(packages)
"""
function test_project_toml_formatting(packages)
@testset "$(result.label)" for result in analyze_project_toml_formatting(packages)
@testset "$(result.label)" for result in
ProjectTomlFormatting.analyze_project_toml_formatting(
packages,
)
@debug result.label result
@test result true
end
end

module ProjectTomlFormatting

using Base: PkgId
using Pkg: TOML

analyze_project_toml_formatting(packages) =
[_analyze_project_toml_formatting_1(path) for path in project_toml_files_in(packages)]
[analyze_project_toml_formatting_1(path) for path in project_toml_files_in(packages)]

project_toml_files_in(path::AbstractString) = [path]
project_toml_files_in(m::Module) = project_toml_files_in(PkgId(m))
Expand All @@ -31,18 +39,38 @@ end
project_toml_files_in(iterable) =
[path for x in iterable for path in project_toml_files_in(x)]

function _analyze_project_toml_formatting_1(path::AbstractString)
const _project_key_order = [
"name",
"uuid",
"keywords",
"license",
"desc",
"deps",
"weakdeps",
"extensions",
"compat",
"extras",
"targets",
]

print_project(io, dict) =
TOML.print(io, dict, sorted = true, by = key -> (project_key_order(key), key))

project_key_order(key::String) =
something(findfirst(x -> x == key, _project_key_order), length(_project_key_order) + 1)

function analyze_project_toml_formatting_1(path::AbstractString)
label = path

if !isfile(path)
return LazyTestResult(label, "Path `$path` is not an existing file.", false)
end

original = read(path, String)
return _analyze_project_toml_formatting_2(path, original)
return analyze_project_toml_formatting_2(path, original)
end

function _analyze_project_toml_formatting_2(path::AbstractString, original)
function analyze_project_toml_formatting_2(path::AbstractString, original)
@debug "Checking TOML style: `$path`" Text(original)
label = path

Expand All @@ -66,3 +94,5 @@ function _analyze_project_toml_formatting_2(path::AbstractString, original)
)
end
end

end # module
19 changes: 0 additions & 19 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,6 @@ catch
end
end

const _project_key_order = [
"name",
"uuid",
"keywords",
"license",
"desc",
"deps",
"weakdeps",
"extensions",
"compat",
"extras",
"targets",
]
project_key_order(key::String) =
something(findfirst(x -> x == key, _project_key_order), length(_project_key_order) + 1)

print_project(io, dict) =
TOML.print(io, dict, sorted = true, by = key -> (project_key_order(key), key))

ensure_exception(e::Exception) = e
ensure_exception(x) = ErrorException(string(x))

Expand Down
15 changes: 8 additions & 7 deletions test/test_project_toml_formatting.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
module TestProjectTomlFormatting

using Aqua: _analyze_project_toml_formatting_2,
using Aqua:
using Aqua.ProjectTomlFormatting: analyze_project_toml_formatting_2
using Test

@testset "_analyze_project_toml_formatting_2" begin
@testset "analyze_project_toml_formatting_2" begin
path = "DUMMY/PATH"
@testset "pass" begin
@test _analyze_project_toml_formatting_2(
@test analyze_project_toml_formatting_2(
path,
"""
[deps]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
""",
) true
@test _analyze_project_toml_formatting_2(
@test analyze_project_toml_formatting_2(
path,
"""
name = "Aqua"
Expand All @@ -38,7 +39,7 @@ using Test
) true
end
@testset "pass: ignore carriage returns" begin
@test _analyze_project_toml_formatting_2(
@test analyze_project_toml_formatting_2(
path,
join([
"""[deps]\r\n""",
Expand All @@ -48,7 +49,7 @@ using Test
) true
end
@testset "failure: reversed deps" begin
t = _analyze_project_toml_formatting_2(
t = analyze_project_toml_formatting_2(
path,
"""
[deps]
Expand All @@ -61,7 +62,7 @@ using Test
@test occursin("is not in canonical format", string(t))
end
@testset "failure: reversed table" begin
t = _analyze_project_toml_formatting_2(
t = analyze_project_toml_formatting_2(
path,
"""
[compat]
Expand Down

0 comments on commit d2c6607

Please sign in to comment.