Skip to content
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

pkgversion #807

Merged
merged 8 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "4.10.0"
version = "4.11.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[compat]
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ changes in `julia`.

* `@something` and `@coalesce` as short-circuiting versions of `something` and `coalesce` ([#40729]) (since Compat 3.29)

* `pkgversion` Return the version of the package that imported module ([#45607]) (since Compat 4.11)
omus marked this conversation as resolved.
Show resolved Hide resolved

## Developer tips

One of the most important rules for `Compat.jl` is to avoid breaking user code
Expand Down Expand Up @@ -172,6 +174,7 @@ Note that you should specify the correct minimum version for `Compat` in the
[#43334]: https://github.com/JuliaLang/julia/issues/43334
[#43354]: https://github.com/JuliaLang/julia/issues/43354
[#43852]: https://github.com/JuliaLang/julia/issues/43852
[#45607]: https://github.com/JuliaLang/julia/issues/45607
[#46104]: https://github.com/JuliaLang/julia/issues/46104
[#48038]: https://github.com/JuliaLang/julia/issues/48038
[#50105]: https://github.com/JuliaLang/julia/issues/50105
50 changes: 50 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,56 @@
end
end

# this function is available as of Julia 1.9
# https://github.com/JuliaLang/julia/pull/45607
palday marked this conversation as resolved.
Show resolved Hide resolved
@static if !isdefined(Base, :pkgversion)
using TOML: parsefile
export pkgversion

const project_names = ("JuliaProject.toml", "Project.toml")
palday marked this conversation as resolved.
Show resolved Hide resolved

function locate_project_file(env::String)
for proj in project_names
project_file = joinpath(env, proj)
if Base.isfile_casesensitive(project_file)
return project_file
end
end
return nothing

Check warning on line 403 in src/Compat.jl

View check run for this annotation

Codecov / codecov/patch

src/Compat.jl#L403

Added line #L403 was not covered by tests
end

function get_pkgversion_from_path(path)
project_file = locate_project_file(path)
if project_file isa String
d = parsefile(project_file)
v = get(d, "version", nothing)
if v !== nothing
return VersionNumber(v::String)
end
end
return nothing

Check warning on line 415 in src/Compat.jl

View check run for this annotation

Codecov / codecov/patch

src/Compat.jl#L415

Added line #L415 was not covered by tests
end

"""
pkgversion(m::Module)

Return the version of the package that imported module `m`,
or `nothing` if `m` was not imported from a package, or imported
from a package without a version field set.

The version is read from the package's Project.toml during package
load.

To get the version of the package that imported the current module
the form `pkgversion(@__MODULE__)` can be used.
"""
function pkgversion(m::Module)
path = pkgdir(m)
isnothing(path) && return nothing
return get_pkgversion_from_path(path)
palday marked this conversation as resolved.
Show resolved Hide resolved
end
omus marked this conversation as resolved.
Show resolved Hide resolved
end

# https://github.com/JuliaLang/julia/pull/43334
if VERSION < v"1.9.0-DEV.1163"
import Base: IteratorSize, HasLength, HasShape, OneTo
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Compat
using Dates
using TOML
using Test

@test isempty(detect_ambiguities(Base, Core, Compat))
Expand Down Expand Up @@ -457,6 +458,12 @@ end
@test isempty(ea)
end

@testset "pkgversion" begin
toml = joinpath(pkgdir(Compat), "Project.toml")
@test pkgversion(Compat) == VersionNumber(TOML.parsefile(toml)["version"])
@test pkgversion(Base) === nothing
end

# https://github.com/JuliaLang/julia/pull/43334
@testset "stack" begin
# Basics
Expand Down