From 5be2a3b387dfb84c668abc7753c69c3663877380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Mon, 3 Jul 2023 09:55:52 +0200 Subject: [PATCH] Wrap `project_toml_formatting.jl` into module --- src/project_toml_formatting.jl | 46 ++++++++++++++++++++++++---- src/utils.jl | 21 ------------- test/test_project_toml_formatting.jl | 15 ++++----- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/src/project_toml_formatting.jl b/src/project_toml_formatting.jl index 7431f725..ff1eb61b 100644 --- a/src/project_toml_formatting.jl +++ b/src/project_toml_formatting.jl @@ -2,14 +2,21 @@ 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 -analyze_project_toml_formatting(packages) = - [_analyze_project_toml_formatting_1(path) for path in project_toml_files_in(packages)] +module ProjectTomlFormatting + +using Base: PkgId +using Pkg: TOML + +using Aqua: LazyTestResult, format_diff, project_toml_path project_toml_files_in(path::AbstractString) = [path] project_toml_files_in(m::Module) = project_toml_files_in(PkgId(m)) @@ -31,7 +38,32 @@ 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) + +splitlines(str; kwargs...) = readlines(IOBuffer(str); kwargs...) + +analyze_project_toml_formatting(packages) = + [analyze_project_toml_formatting_1(path) for path in project_toml_files_in(packages)] + +function analyze_project_toml_formatting_1(path::AbstractString) label = path if !isfile(path) @@ -39,10 +71,10 @@ function _analyze_project_toml_formatting_1(path::AbstractString) 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 @@ -66,3 +98,5 @@ function _analyze_project_toml_formatting_2(path::AbstractString, original) ) end end + +end # module diff --git a/src/utils.jl b/src/utils.jl index 81d891ad..92bda696 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -2,8 +2,6 @@ if !@isdefined(isnothing) isnothing(x) = x === nothing end -splitlines(str; kwargs...) = readlines(IOBuffer(str); kwargs...) - askwargs(kwargs) = (; kwargs...) function askwargs(flag::Bool) if !flag @@ -144,25 +142,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)) diff --git a/test/test_project_toml_formatting.jl b/test/test_project_toml_formatting.jl index 36ce6e81..56576b63 100644 --- a/test/test_project_toml_formatting.jl +++ b/test/test_project_toml_formatting.jl @@ -1,12 +1,13 @@ 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] @@ -14,7 +15,7 @@ using Test Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" """, ) ⊜ true - @test _analyze_project_toml_formatting_2( + @test analyze_project_toml_formatting_2( path, """ name = "Aqua" @@ -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""", @@ -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] @@ -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]