From 4dad94181f040451f0b26f6119f6c99b5315b864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Riedemann?= <38795484+longemen3000@users.noreply.github.com> Date: Sat, 24 Aug 2024 02:26:53 -0400 Subject: [PATCH 1/2] remove as much dependencies as possible --- Project.toml | 10 +++++++--- ext/FilePathsBaseMmapExt.jl | 7 +++++++ src/FilePathsBase.jl | 10 ++++++---- src/utils.jl | 9 +++++++-- 4 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 ext/FilePathsBaseMmapExt.jl diff --git a/Project.toml b/Project.toml index 0e1d4d8..231cac9 100644 --- a/Project.toml +++ b/Project.toml @@ -4,16 +4,20 @@ authors = ["Rory Finnegan"] version = "0.9.21" [deps] -Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Mmap = "a63ad114-7e13-5084-954f-fe012c677804" -Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + +[weakdeps] +Mmap = "a63ad114-7e13-5084-954f-fe012c677804" + +[extensions] +FilePathsBaseMmapExt = "Mmap" [compat] Compat = "3.33, 4" julia = "1" +Mmap = "<0.0.1, 1" [extras] JLSO = "9da8a3cd-07a3-59c0-a743-3fdc52c30d11" diff --git a/ext/FilePathsBaseMmapExt.jl b/ext/FilePathsBaseMmapExt.jl new file mode 100644 index 0000000..b3ba481 --- /dev/null +++ b/ext/FilePathsBaseMmapExt.jl @@ -0,0 +1,7 @@ +module FilePathsBaseMmapExt +using Mmap +using FilePathsBase + +Mmap.mmap(fp::FilePathsBase.SystemPath, args...; kwargs...) = Mmap.mmap(string(fp), args...; kwargs...) + +end #module \ No newline at end of file diff --git a/src/FilePathsBase.jl b/src/FilePathsBase.jl index 849268f..4b3dc67 100644 --- a/src/FilePathsBase.jl +++ b/src/FilePathsBase.jl @@ -1,12 +1,10 @@ module FilePathsBase -using Compat using Dates -using Mmap -using Printf -using UUIDs import Base: == +import Base: UUID + export # Types AbstractPath, @@ -134,4 +132,8 @@ include("windows.jl") include("test.jl") include("deprecates.jl") +if !isdefined(Base,:get_extension) + include("../ext/FilePathsBaseMmapExt.jl") +end + end # end of module diff --git a/src/utils.jl b/src/utils.jl index b70f903..ccffa30 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1,3 +1,9 @@ +function uuid4(rng::AbstractRNG=Random.RandomDevice()) + u = rand(rng, UInt128) + u &= 0xffffffffffff0fff3fffffffffffffff + u |= 0x00000000000040008000000000000000 + UUID(u) +end # Mostly copied from https://github.com/IainNZ/Humanize.jl/blob/master/src/Humanize.jl#L27 function _datasize(bytes::Number) base = 1024.0 @@ -13,6 +19,5 @@ function _datasize(bytes::Number) break end end - - return @sprintf("%.1f%s", (base * nbytes / unit), suffix) + return string(round(base * nbytes / unit,digits = 1)) * suffix end From 4880e452c0e0d7d6800c552b45006637ad7dae10 Mon Sep 17 00:00:00 2001 From: longemen3000 Date: Sun, 25 Aug 2024 18:39:24 -0400 Subject: [PATCH 2/2] move Test into an extension, fix errors in 1.11 --- Project.toml | 8 +- src/test.jl => ext/FilePathsBaseTestExt.jl | 360 ++++----------------- src/FilePathsBase.jl | 15 +- src/system.jl | 11 +- src/test_stub.jl | 312 ++++++++++++++++++ src/utils.jl | 4 +- test/mode.jl | 8 +- test/testpkg.jl | 2 +- 8 files changed, 402 insertions(+), 318 deletions(-) rename src/test.jl => ext/FilePathsBaseTestExt.jl (76%) create mode 100644 src/test_stub.jl diff --git a/Project.toml b/Project.toml index 231cac9..b87e574 100644 --- a/Project.toml +++ b/Project.toml @@ -4,24 +4,28 @@ authors = ["Rory Finnegan"] version = "0.9.21" [deps] +Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Mmap = "a63ad114-7e13-5084-954f-fe012c677804" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [weakdeps] Mmap = "a63ad114-7e13-5084-954f-fe012c677804" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [extensions] FilePathsBaseMmapExt = "Mmap" +FilePathsBaseTestExt = "Test" [compat] -Compat = "3.33, 4" julia = "1" Mmap = "<0.0.1, 1" +Compat = "3.33, 4" [extras] JLSO = "9da8a3cd-07a3-59c0-a743-3fdc52c30d11" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Mmap = "a63ad114-7e13-5084-954f-fe012c677804" [targets] -test = ["JLSO", "Test"] +test = ["JLSO", "Mmap", "Test"] diff --git a/src/test.jl b/ext/FilePathsBaseTestExt.jl similarity index 76% rename from src/test.jl rename to ext/FilePathsBaseTestExt.jl index 70d7b88..f52a771 100644 --- a/src/test.jl +++ b/ext/FilePathsBaseTestExt.jl @@ -1,192 +1,13 @@ -""" - TestPaths - -This module is intended to be used for testing new path types to -ensure that they are adhering to the AbstractPath API. - -# Example - -```julia -# Create a PathSet -ps = PathSet(; symlink=true) - -# Select the subset of tests to run -# Inspect TestPaths.TESTALL to see full list -testsets = [ - test_registration, - test_show, - test_cmd, - test_parse, - test_convert, - test_components, - test_parents, - test_join, - test_splitext, - test_basename, - test_splitdir, - test_filename, - test_extensions, - test_isempty, - test_normalize, - test_canonicalize, - test_relative, - test_absolute, - test_isdir, - test_isfile, - test_stat, - test_filesize, - test_modified, - test_created, - test_cd, - test_readpath, - test_walkpath, - test_read, - test_write, - test_mkdir, - test_cp, - test_mv, - test_sync, - test_symlink, - test_touch, - test_tmpname, - test_tmpdir, - test_mktmp, - test_mktmpdir, - test_download, - test_include, -] - -# Run all the tests -test(ps, testsets) -``` -""" -module TestPaths +module FilePathsBaseTestExt using Dates: Dates using FilePathsBase using FilePathsBase: /, join using Test - - export PathSet, - TESTALL, - test, - test_registration, - test_show, - test_cmd, - test_parse, - test_convert, - test_components, - test_indexing, - test_iteration, - test_parents, - test_descendants_and_ascendants, - test_join, - test_splitext, - test_basename, - test_splitdir, - test_filename, - test_extensions, - test_isempty, - test_normalize, - test_canonicalize, - test_relative, - test_absolute, - test_isdir, - test_isfile, - test_stat, - test_filesize, - test_modified, - test_created, - test_issocket, - test_isfifo, - test_ischardev, - test_isblockdev, - test_ismount, - test_isexecutable, - test_isreadable, - test_iswritable, - test_cd, - test_readpath, - test_walkpath, - test_read, - test_write, - test_mkdir, - test_cp, - test_mv, - test_sync, - test_symlink, - test_touch, - test_tmpname, - test_tmpdir, - test_mktmp, - test_mktmpdir, - test_chown, - test_chmod, - test_download, - test_include - - """ - PathSet(root::AbstractPath=tmpdir(); symlink=false) - - Constructs a common test path hierarchy to running shared API tests. - - Hierarchy: - - ``` - root - |-- foo - | |-- baz.txt - |-- bar - | |-- qux - | |-- quux.tar.gz - |-- fred - | |-- plugh - ``` - """ - struct PathSet{P<:AbstractPath} - root::P - foo::P - baz::P - bar::P - qux::P - quux::P - fred::P - plugh::P - link::Bool - end - - function PathSet(root=tmpdir() / "pathset_root"; symlink=false) - root = absolute(root) - - PathSet( - root, - root / "foo", - root / "foo" / "baz.txt", - root / "bar", - root / "bar" / "qux", - root / "bar" / "qux" / "quux.tar.gz", - root / "fred", - root / "fred" / "plugh", - symlink, - ) - end - - function initialize(ps::PathSet) - @info "Initializing $(typeof(ps))" - mkdir.([ps.foo, ps.qux, ps.fred]; recursive=true, exist_ok=true) - write(ps.baz, "Hello World!") - write(ps.quux, "Hello Again!") - - # If link is true then plugh is a symlink to foo - if ps.link - symlink(ps.foo, ps.plugh) - else - touch(ps.plugh) - end - end + using FilePathsBase.TestPaths # NOTE: Most paths should test their own constructors as necessary. - function test_registration(ps::PathSet{P}) where P <: AbstractPath + function TestPaths.test_registration(ps::PathSet{P}) where P <: AbstractPath @testset "Path constructor" begin str = string(ps.root) @test tryparse(P, str) !== nothing @@ -195,7 +16,7 @@ module TestPaths end end - function test_show(ps::PathSet) + function TestPaths.test_show(ps::PathSet) @testset "show" begin str = string(ps.root) # For windows paths @@ -206,14 +27,14 @@ module TestPaths end end - function test_cmd(ps::PathSet) + function TestPaths.test_cmd(ps::PathSet) @testset "cmd" begin str = string(ps.root) @test `echo $str` == `echo $(ps.root)` end end - function test_parse(ps::PathSet{P}) where P <: AbstractPath + function TestPaths.test_parse(ps::PathSet{P}) where P <: AbstractPath @testset "parsing" begin str = string(ps.root) @test parse(P, str) == ps.root @@ -221,7 +42,7 @@ module TestPaths end end - function test_convert(ps::PathSet{P}) where P <: AbstractPath + function TestPaths.test_convert(ps::PathSet{P}) where P <: AbstractPath @testset "convert" begin str = string(ps.root) @test convert(P, str) == ps.root @@ -229,7 +50,7 @@ module TestPaths end end - function test_components(ps::PathSet) + function TestPaths.test_components(ps::PathSet) @testset "components" begin str = string(ps.root) @test ps.root.anchor == ps.root.drive * ps.root.root @@ -241,7 +62,7 @@ module TestPaths end end - function test_indexing(ps::PathSet) + function TestPaths.test_indexing(ps::PathSet) @test firstindex(ps.root) == 1 @test lastindex(ps.root) == length(ps.root.segments) # `begin` indexing was only added in 1.4, so we'll leave this test commented @@ -252,13 +73,13 @@ module TestPaths @test ps.quux[:] == ps.quux.segments[:] end - function test_iteration(ps::PathSet) + function TestPaths.test_iteration(ps::PathSet) @test eltype(ps.root) == String @test tuple(ps.quux...) == ps.quux.segments @test all(in(ps.quux), ("bar", "qux", "quux.tar.gz")) end - function test_parents(ps::PathSet) + function TestPaths.test_parents(ps::PathSet) @testset "parents" begin @test parent(ps.foo) == ps.root @test parent(ps.qux) == ps.bar @@ -300,7 +121,7 @@ module TestPaths end end - function test_descendants_and_ascendants(ps::PathSet) + function TestPaths.test_descendants_and_ascendants(ps::PathSet) @testset "descendants and ascendants" begin # Test base cases @test isdescendant(p"/a/b", p"/") @@ -324,7 +145,7 @@ module TestPaths end end - function test_join(ps::PathSet) + function TestPaths.test_join(ps::PathSet) @testset "join" begin @test join(ps.root, "bar") == ps.bar @test ps.root / "foo" / "baz.txt" == ps.baz @@ -343,21 +164,21 @@ module TestPaths end end - function test_splitext(ps::PathSet) + function TestPaths.test_splitext(ps::PathSet) @testset "splitext" begin @test splitext(ps.foo) == (ps.foo, "") @test splitext(ps.baz) == (ps.foo / "baz", ".txt") @test splitext(ps.quux) == (ps.qux / "quux.tar", ".gz") end end - function test_basename(ps::PathSet) + function TestPaths.test_basename(ps::PathSet) @testset "basename" begin @test basename(ps.foo) == "foo" @test basename(ps.baz) == "baz.txt" @test basename(ps.quux) == "quux.tar.gz" end end - function test_splitdir(ps::PathSet) + function TestPaths.test_splitdir(ps::PathSet) @testset "splitdir" begin @test splitdir(ps.foo) == (ps.root, "foo") @test splitdir(ps.baz) == (ps.root / "foo", "baz.txt") @@ -365,7 +186,7 @@ module TestPaths end end - function test_filename(ps::PathSet) + function TestPaths.test_filename(ps::PathSet) @testset "filename" begin @test filename(ps.foo) == "foo" @test filename(ps.baz) == "baz" @@ -373,7 +194,7 @@ module TestPaths end end - function test_extensions(ps::PathSet) + function TestPaths.test_extensions(ps::PathSet) @testset "extensions" begin @test extension(ps.foo) == "" @test extension(ps.baz) == "txt" @@ -384,14 +205,14 @@ module TestPaths end end - function test_isempty(ps::PathSet{P}) where P <: AbstractPath + function TestPaths.test_isempty(ps::PathSet{P}) where P <: AbstractPath @testset "isempty" begin @test !isempty(ps.foo) @test isempty(P()) end end - function test_normalize(ps::PathSet) + function TestPaths.test_normalize(ps::PathSet) @testset "normalize" begin @test normalize(ps.bar / ".." / "foo") == ps.foo @test normalize(ps.bar / ".") == ps.bar @@ -400,7 +221,7 @@ module TestPaths end end - function test_canonicalize(ps::PathSet) + function TestPaths.test_canonicalize(ps::PathSet) @testset "canonicalize" begin # NOTE: We call `canonicalize` on ps.bar in the `normalize` case because on # macOS the temp directory may include a symlink. @@ -418,34 +239,34 @@ module TestPaths end end - function test_relative(ps::PathSet) + function TestPaths.test_relative(ps::PathSet) @testset "relative" begin @test relative(ps.foo, ps.qux).segments == ("..", "..", "foo") end end - function test_absolute(ps::PathSet) + function TestPaths.test_absolute(ps::PathSet) @testset "absolute" begin @test isabsolute(ps.root) || isabsolute(absolute(ps.root)) @test absolute(ps.root) == abspath(ps.root) end end - function test_isdir(ps::PathSet) + function TestPaths.test_isdir(ps::PathSet) @testset "isdir" begin @test isdir(ps.foo) @test !isdir(ps.baz) end end - function test_isfile(ps::PathSet) + function TestPaths.test_isfile(ps::PathSet) @testset "isfile" begin @test isfile(ps.baz) @test !isfile(ps.foo) end end - function test_stat(ps::PathSet) + function TestPaths.test_stat(ps::PathSet) @testset "stat" begin s = stat(ps.root) fields = fieldnames(typeof(s)) @@ -466,47 +287,47 @@ module TestPaths # Minimal testing of issocket, isfifo, ischardev, isblockdev and ismount which # people won't typically include. - function test_issocket(ps::PathSet) + function TestPaths.test_issocket(ps::PathSet) @test !issocket(ps.root) end - function test_isfifo(ps::PathSet) + function TestPaths.test_isfifo(ps::PathSet) @test !isfifo(ps.root) end - function test_ischardev(ps::PathSet) + function TestPaths.test_ischardev(ps::PathSet) @test !ischardev(ps.root) end - function test_isblockdev(ps::PathSet) + function TestPaths.test_isblockdev(ps::PathSet) @test !isblockdev(ps.root) end - function test_ismount(ps::PathSet) + function TestPaths.test_ismount(ps::PathSet) @test !ismount(ps.root) end - function test_filesize(ps::PathSet) + function TestPaths.test_filesize(ps::PathSet) @testset "filesize" begin @test filesize(ps.baz) > 0 end end - function test_modified(ps::PathSet) + function TestPaths.test_modified(ps::PathSet) @testset "modified" begin @test isa(modified(ps.baz), Dates.AbstractDateTime) @test modified(ps.baz) >= modified(ps.root) end end - function test_created(ps::PathSet) + function TestPaths.test_created(ps::PathSet) @testset "created" begin @test isa(created(ps.baz), Dates.AbstractDateTime) @test created(ps.baz) >= created(ps.root) end end - function test_isexecutable(ps::PathSet{P}) where P <: AbstractPath + function TestPaths.test_isexecutable(ps::PathSet{P}) where P <: AbstractPath @testset "isexecutable" begin # I'm not entirely sure how to test this generally @test !isexecutable(ps.baz) @@ -518,7 +339,7 @@ module TestPaths end end - function test_isreadable(ps::PathSet) + function TestPaths.test_isreadable(ps::PathSet) @testset "isreadable" begin # Our test files should be readable by default @test isreadable(ps.baz) @@ -530,7 +351,7 @@ module TestPaths end end - function test_iswritable(ps::PathSet) + function TestPaths.test_iswritable(ps::PathSet) @testset "iswritable" begin # Our test files should be writable by default @test iswritable(ps.baz) @@ -542,7 +363,7 @@ module TestPaths end end - function test_cd(ps::PathSet{P}) where P <: AbstractPath + function TestPaths.test_cd(ps::PathSet{P}) where P <: AbstractPath if P <: SystemPath @testset "cd" begin init_path = canonicalize(cwd()) @@ -565,7 +386,7 @@ module TestPaths end end - function test_readpath(ps::PathSet) + function TestPaths.test_readpath(ps::PathSet) @testset "readpath" begin @test readdir(ps.root) == ["bar", "foo", "fred"] @test readdir(ps.qux) == ["quux.tar.gz"] @@ -574,7 +395,7 @@ module TestPaths end end - function test_walkpath(ps::PathSet{P}) where P + function TestPaths.test_walkpath(ps::PathSet{P}) where P @testset "walkpath" begin topdown = [ps.bar, ps.qux, ps.quux, ps.foo, ps.baz, ps.fred, ps.plugh] bottomup = [ps.quux, ps.qux, ps.bar, ps.baz, ps.foo, ps.plugh, ps.fred] @@ -590,7 +411,7 @@ module TestPaths end end - function test_read(ps::PathSet) + function TestPaths.test_read(ps::PathSet) @testset "read" begin @test read(ps.baz, String) == "Hello World!" open(ps.quux, "r") do io @@ -599,7 +420,7 @@ module TestPaths end end - function test_write(ps::PathSet) + function TestPaths.test_write(ps::PathSet) @testset "write" begin write(ps.baz, "Goodbye World!") @test read(ps.baz, String) == "Goodbye World!" @@ -634,7 +455,7 @@ module TestPaths end end - function test_mkdir(ps::PathSet) + function TestPaths.test_mkdir(ps::PathSet) @testset "mkdir" begin garply = ps.root / "corge" / "grault" / "garply" @test_throws ErrorException mkdir(garply) @@ -647,7 +468,7 @@ module TestPaths end end - function test_cp(ps::PathSet) + function TestPaths.test_cp(ps::PathSet) @testset "cp" begin cp(ps.foo, ps.qux / "foo"; force=true) @test exists(ps.qux / "foo" / "baz.txt") @@ -680,7 +501,7 @@ module TestPaths end end - function test_mv(ps::PathSet) + function TestPaths.test_mv(ps::PathSet) @testset "mv" begin garply = ps.root / "corge" / "grault" / "garply" mkdir(garply; recursive=true, exist_ok=true) @@ -691,7 +512,7 @@ module TestPaths end end - function test_sync(ps::PathSet) + function TestPaths.test_sync(ps::PathSet) @testset "sync" begin @testset "empty destination" begin sync(ps.foo, ps.qux / "foo") @@ -792,7 +613,7 @@ module TestPaths end end - function test_symlink(ps::PathSet) + function TestPaths.test_symlink(ps::PathSet) if ps.link @testset "symlink" begin @test_throws ErrorException symlink(ps.foo, ps.plugh) @@ -803,7 +624,7 @@ module TestPaths end end - function test_touch(ps::PathSet) + function TestPaths.test_touch(ps::PathSet) @testset "touch" begin newfile = ps.root / "newfile" touch(newfile) @@ -813,7 +634,7 @@ module TestPaths end end - function test_tmpname(ps::PathSet) + function TestPaths.test_tmpname(ps::PathSet) @testset "tmpname" begin @test isa(tmpname(), AbstractPath) @test hasparent(tmpname()) @@ -821,7 +642,7 @@ module TestPaths end end - function test_tmpdir(ps::PathSet) + function TestPaths.test_tmpdir(ps::PathSet) @testset "tmpname" begin @test isa(tmpdir(), AbstractPath) @test exists(tmpdir()) @@ -829,7 +650,7 @@ module TestPaths end end - function test_mktmp(ps::PathSet) + function TestPaths.test_mktmp(ps::PathSet) @testset "mktmp" begin mktmp(ps.root) do path, io @test exists(path) @@ -841,7 +662,7 @@ module TestPaths end end - function test_mktmpdir(ps::PathSet) + function TestPaths.test_mktmpdir(ps::PathSet) @testset "mktmpdir" begin mktmpdir(ps.root) do path @test exists(path) @@ -851,7 +672,7 @@ module TestPaths end end - function test_chown(ps::PathSet) + function TestPaths.test_chown(ps::PathSet) @testset "chown" begin newfile = ps.root / "newfile" touch(newfile) @@ -870,7 +691,7 @@ module TestPaths end end - function test_chmod(ps::PathSet) + function TestPaths.test_chmod(ps::PathSet) @testset "chmod" begin newfile = ps.root / "newfile" newpath = ps.root / "thud" @@ -900,7 +721,7 @@ module TestPaths end end - function test_download(ps::PathSet) + function TestPaths.test_download(ps::PathSet) @testset "download" begin rm(ps.foo / "README.md"; force=true) download( @@ -920,78 +741,11 @@ module TestPaths end end - function test_include(ps::PathSet) + function TestPaths.test_include(ps::PathSet) @testset "include" begin write(ps.quux, "2 + 2\n") res = include(ps.quux) @test res == 4 end end - - TESTALL = [ - test_registration, - test_show, - test_cmd, - test_parse, - test_convert, - test_components, - test_indexing, - test_iteration, - test_parents, - test_descendants_and_ascendants, - test_join, - test_splitext, - test_basename, - test_filename, - test_extensions, - test_isempty, - test_normalize, - test_canonicalize, - test_relative, - test_absolute, - test_isdir, - test_isfile, - test_stat, - test_filesize, - test_modified, - test_created, - test_issocket, - test_isfifo, - test_ischardev, - test_isblockdev, - test_ismount, - test_isexecutable, - test_isreadable, - test_iswritable, - test_cd, - test_readpath, - test_walkpath, - test_read, - test_write, - test_mkdir, - test_cp, - test_mv, - test_symlink, - test_touch, - test_tmpname, - test_tmpdir, - test_mktmp, - test_mktmpdir, - test_chown, - test_chmod, - test_download, - test_include, - ] - - function test(ps::PathSet, test_sets=TESTALL) - try - initialize(ps) - - for ts in test_sets - ts(ps) - end - finally - rm(ps.root; recursive=true, force=true) - end - end -end +end \ No newline at end of file diff --git a/src/FilePathsBase.jl b/src/FilePathsBase.jl index 4b3dc67..38b8269 100644 --- a/src/FilePathsBase.jl +++ b/src/FilePathsBase.jl @@ -5,6 +5,18 @@ using Dates import Base: == import Base: UUID +#v3 of compat overloads Base.include(mapexpr::Function, path::AbstractString) +#on julia 1.6 afterwards this is not necessary. +@static if Base.VERSION < v"1.6" + using Compat +end + +#https://github.com/JuliaLang/julia/pull/53699 makes isexecutable public. +#we overload that method if available. +@static if isdefined(Base,:isexecutable) + import Base: isexecutable +end + export # Types AbstractPath, @@ -129,11 +141,12 @@ include("path.jl") include("system.jl") include("posix.jl") include("windows.jl") -include("test.jl") +include("test_stub.jl") include("deprecates.jl") if !isdefined(Base,:get_extension) include("../ext/FilePathsBaseMmapExt.jl") + include("../ext/FilePathsBaseTestExt.jl") end end # end of module diff --git a/src/system.jl b/src/system.jl index ffc38d9..139a8c7 100644 --- a/src/system.jl +++ b/src/system.jl @@ -385,6 +385,7 @@ end Base.readdir(fp::SystemPath; kwargs...) = readdir(string(fp); kwargs...) +#TODO: Base.download is deprecated; use Downloads.download instead (FilePathsBase.jl#173) function Base.download(url::AbstractString, dest::T) where T<:SystemPath return parse(T, download(url, string(dest))) end @@ -397,8 +398,8 @@ function canonicalize(fp::T) where T<:SystemPath return parse(T, realpath(string(fp))) end -Mmap.mmap(fp::SystemPath, args...; kwargs...) = Mmap.mmap(string(fp), args...; kwargs...) - -function Base.include(mapexpr::Function, m::Module, path::SystemPath) - return Base.include(mapexpr, m, string(path)) -end +@static if Base.VERSION >= v"1.5" + function Base.include(mapexpr::Function, m::Module, path::SystemPath) + return Base.include(mapexpr, m, string(path)) + end +end \ No newline at end of file diff --git a/src/test_stub.jl b/src/test_stub.jl new file mode 100644 index 0000000..612e428 --- /dev/null +++ b/src/test_stub.jl @@ -0,0 +1,312 @@ +""" + TestPaths + +This module is intended to be used for testing new path types to +ensure that they are adhering to the AbstractPath API. + +# Example + +```julia +# Create a PathSet +ps = PathSet(; symlink=true) + +# Select the subset of tests to run +# Inspect TestPaths.TESTALL to see full list +testsets = [ + test_registration, + test_show, + test_cmd, + test_parse, + test_convert, + test_components, + test_parents, + test_join, + test_splitext, + test_basename, + test_splitdir, + test_filename, + test_extensions, + test_isempty, + test_normalize, + test_canonicalize, + test_relative, + test_absolute, + test_isdir, + test_isfile, + test_stat, + test_filesize, + test_modified, + test_created, + test_cd, + test_readpath, + test_walkpath, + test_read, + test_write, + test_mkdir, + test_cp, + test_mv, + test_sync, + test_symlink, + test_touch, + test_tmpname, + test_tmpdir, + test_mktmp, + test_mktmpdir, + test_download, + test_include, +] + +# Run all the tests +test(ps, testsets) +``` +""" +module TestPaths + using Dates: Dates + using FilePathsBase + using FilePathsBase: /, join + + export PathSet, + TESTALL, + test, + test_registration, + test_show, + test_cmd, + test_parse, + test_convert, + test_components, + test_indexing, + test_iteration, + test_parents, + test_descendants_and_ascendants, + test_join, + test_splitext, + test_basename, + test_splitdir, + test_filename, + test_extensions, + test_isempty, + test_normalize, + test_canonicalize, + test_relative, + test_absolute, + test_isdir, + test_isfile, + test_stat, + test_filesize, + test_modified, + test_created, + test_issocket, + test_isfifo, + test_ischardev, + test_isblockdev, + test_ismount, + test_isexecutable, + test_isreadable, + test_iswritable, + test_cd, + test_readpath, + test_walkpath, + test_read, + test_write, + test_mkdir, + test_cp, + test_mv, + test_sync, + test_symlink, + test_touch, + test_tmpname, + test_tmpdir, + test_mktmp, + test_mktmpdir, + test_chown, + test_chmod, + test_download, + test_include + + """ + PathSet(root::AbstractPath=tmpdir(); symlink=false) + + Constructs a common test path hierarchy to running shared API tests. + + Hierarchy: + + ``` + root + |-- foo + | |-- baz.txt + |-- bar + | |-- qux + | |-- quux.tar.gz + |-- fred + | |-- plugh + ``` + """ + struct PathSet{P<:AbstractPath} + root::P + foo::P + baz::P + bar::P + qux::P + quux::P + fred::P + plugh::P + link::Bool + end + + function PathSet(root=tmpdir() / "pathset_root"; symlink=false) + root = absolute(root) + + PathSet( + root, + root / "foo", + root / "foo" / "baz.txt", + root / "bar", + root / "bar" / "qux", + root / "bar" / "qux" / "quux.tar.gz", + root / "fred", + root / "fred" / "plugh", + symlink, + ) + end + + function initialize(ps::PathSet) + @info "Initializing $(typeof(ps))" + mkdir.([ps.foo, ps.qux, ps.fred]; recursive=true, exist_ok=true) + write(ps.baz, "Hello World!") + write(ps.quux, "Hello Again!") + + # If link is true then plugh is a symlink to foo + if ps.link + symlink(ps.foo, ps.plugh) + else + touch(ps.plugh) + end + end + + # NOTE: Most paths should test their own constructors as necessary. + + function test_registration end + function test_show end + function test_cmd end + function test_parse end + function test_convert end + function test_components end + function test_indexing end + function test_iteration end + function test_parents end + function test_descendants_and_ascendants end + function test_join end + function test_splitext end + function test_basename end + function test_splitdir end + function test_filename end + function test_extensions end + function test_isempty end + function test_normalize end + function test_canonicalize end + function test_relative end + function test_absolute end + function test_isdir end + function test_isfile end + function test_stat end + + # Minimal testing of issocket, isfifo, ischardev, isblockdev and ismount which + # people won't typically include. + function test_issocket end + function test_isfifo end + function test_ischardev end + function test_isblockdev end + function test_ismount end + function test_filesize end + function test_modified end + function test_created end + function test_isexecutable end + function test_isreadable end + function test_iswritable end + function test_cd end + function test_readpath end + function test_walkpath end + function test_read end + function test_write end + function test_mkdir end + function test_cp end + function test_mv end + function test_sync end + function test_symlink end + function test_touch end + function test_tmpname end + function test_tmpdir end + function test_mktmp end + function test_mktmpdir end + function test_chown end + function test_chmod end + function test_download end + function test_include end + + TESTALL = [ + test_registration, + test_show, + test_cmd, + test_parse, + test_convert, + test_components, + test_indexing, + test_iteration, + test_parents, + test_descendants_and_ascendants, + test_join, + test_splitext, + test_basename, + test_filename, + test_extensions, + test_isempty, + test_normalize, + test_canonicalize, + test_relative, + test_absolute, + test_isdir, + test_isfile, + test_stat, + test_filesize, + test_modified, + test_created, + test_issocket, + test_isfifo, + test_ischardev, + test_isblockdev, + test_ismount, + test_isexecutable, + test_isreadable, + test_iswritable, + test_cd, + test_readpath, + test_walkpath, + test_read, + test_write, + test_mkdir, + test_cp, + test_mv, + test_symlink, + test_touch, + test_tmpname, + test_tmpdir, + test_mktmp, + test_mktmpdir, + test_chown, + test_chmod, + test_download, + test_include, + ] + + function test(ps::PathSet, test_sets=TESTALL) + try + initialize(ps) + + for ts in test_sets + ts(ps) + end + finally + rm(ps.root; recursive=true, force=true) + end + end +end #module diff --git a/src/utils.jl b/src/utils.jl index ccffa30..4f02ff7 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1,5 +1,5 @@ -function uuid4(rng::AbstractRNG=Random.RandomDevice()) - u = rand(rng, UInt128) +function uuid4() + u = rand(UInt128) u &= 0xffffffffffff0fff3fffffffffffffff u |= 0x00000000000040008000000000000000 UUID(u) diff --git a/test/mode.jl b/test/mode.jl index cc16134..daae564 100644 --- a/test/mode.jl +++ b/test/mode.jl @@ -22,10 +22,10 @@ @test Mode("--wx--x--x") == m - r @test Mode("-r-xr-x--x") == m - w - @test isexecutable(x, :USER) - @test isexecutable(x, :GROUP) - @test isexecutable(x, :OTHER) - @test isexecutable(x, :ALL) + @test FilePathsBase.isexecutable(x, :USER) + @test FilePathsBase.isexecutable(x, :GROUP) + @test FilePathsBase.isexecutable(x, :OTHER) + @test FilePathsBase.isexecutable(x, :ALL) @test iswritable(w, :USER) @test !iswritable(w, :GROUP) diff --git a/test/testpkg.jl b/test/testpkg.jl index ce80baa..72bd260 100644 --- a/test/testpkg.jl +++ b/test/testpkg.jl @@ -57,7 +57,7 @@ Base.isfifo(fp::TestPath) = issocket(mode(fp)) Base.ischardev(fp::TestPath) = ischardev(mode(fp)) Base.isblockdev(fp::TestPath) = isblockdev(mode(fp)) Base.ismount(fp::TestPath) = ismount(test2posix(fp)) -FilePathsBase.isexecutable(fp::TestPath) = isexecutable(test2posix(fp)) +FilePathsBase.isexecutable(fp::TestPath) = FilePathsBase.isexecutable(test2posix(fp)) Base.iswritable(fp::TestPath) = iswritable(test2posix(fp)) Base.isreadable(fp::TestPath) = isreadable(test2posix(fp)) Base.cd(fp::TestPath) = cd(test2posix(fp))