Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Nov 11, 2024
1 parent 57f5fb6 commit d07d8e3
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/packages/PkgUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,16 @@ has_notebook_environment(notebook_path::String)::Bool
Does the notebook file contain an embedded `Project.toml` and `Manifest.toml`?
"""
has_notebook_environment(path::String) = has_notebook_environment(load_notebook(path))
has_notebook_environment(notebook::Notebook) = notebook.nbpkg_ctx !== nothing
has_notebook_environment(path::String) = has_notebook_environment(load_notebook_nobackup(path))
function has_notebook_environment(notebook::Notebook)
ctx = notebook.nbpkg_ctx
ctx === nothing && return false
(project_file(ctx) |> isfile || manifest_file(ctx) |> isfile) && return true

# fallback, when nbpkg is defined buy there are no files: check if the notebook would use one (i.e. that Pkg.activate is not used).
topology = updated_topology(notebook.topology, notebook, cells)
return Pluto.use_plutopkg(topology)
end

"""
```julia
Expand Down Expand Up @@ -257,10 +265,22 @@ end

"""
```julia
activate_notebook_environment(f::Function, notebook_path::String)::Nothing
activate_notebook_environment(f::Function, notebook_path::String)
```
Activate the package environment embedded in a notebook file, for use inside scripts. Inside your function `f`, you can use Pkg commands to modify the environment, and any changes you make will be automatically saved in the notebook file after your function finishes.
Temporarily activate the package environment embedded in a notebook file, for use inside scripts. Inside your function `f`, you can use Pkg commands to modify the environment, and any changes you make will be automatically saved in the notebook file after your function finishes. Not thread-safe.
This method is best for scripts that update notebook files. For interactive use, the method `activate_notebook_environment(notebook_path::String)` is recommended.
# Example
```julia
Pluto.activate_notebook_environment("notebook.jl") do
Pkg.add("Example")
end
# Now the file "notebook.jl" was updated!
```
!!! warning
This function uses the private method `Pkg.activate(f::Function, path::String)`. This API might not be available in future Julia versions. 🤷
Expand All @@ -273,21 +293,21 @@ function activate_notebook_environment(f::Function, path::String)
mkpath(ourpath)
write_nb_to_dir(notebook, ourpath)

Pkg.activate(f, ourpath)
result = Pkg.activate(f, ourpath)

if !nb_and_dir_environments_equal(notebook, ourpath)
write_dir_to_nb(ourpath, notebook)
end

nothing
result
end

const activate_notebook = activate_notebook_environment

function testnb()
function testnb(name="simple_stdlib_import.jl")
t = tempname()

readwrite(Pluto.project_relative_path("test","packages","nb.jl"), t)
readwrite(Pluto.project_relative_path("test","packages","simple_stdlib_import.jl"), t)
t
end

Expand Down
160 changes: 160 additions & 0 deletions test/packages/PkgUtils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@




using Pluto


without_pluto_version(s) = replace(s, r"# v.*" => "")


@testset "PkgUtils" begin

@testset "activate_notebook_environment" begin
file = Pluto.testnb()
before = without_pluto_version(read(file, String))
@assert :activate_notebook_environment in names(Pluto)

@test !occursin("Artifacts", Pluto.PkgCompat.read_project_file(Pluto.load_notebook_nobackup(file)))

ap_before = Base.ACTIVE_PROJECT[]


###

Pluto.activate_notebook_environment(file; show_help=false)
@test Base.ACTIVE_PROJECT[] != ap_before

@test sort(collect(keys(Pkg.project().dependencies))) == ["Dates"]

Pkg.add("Artifacts")
@test sort(collect(keys(Pkg.project().dependencies))) == ["Artifacts", "Dates"]


### EXIT, activate another env and wait for our previous changes to get picked up
Base.ACTIVE_PROJECT[] = ap_before
sleep(5)


###
# get embedded project.toml from notebook:
@test occursin("Artifacts", Pluto.PkgCompat.read_project_file(Pluto.load_notebook_nobackup(file)))

after = without_pluto_version(read(file, String))
@test before != after
@test occursin("Artifacts", after)
end




@testset "activate_notebook_environment, functional 1" begin
file = Pluto.testnb()
before = read(file, String)
@assert :activate_notebook_environment in names(Pluto)

###
projs = Pluto.activate_notebook_environment(file) do
Pkg.project()
end

after = read(file, String)

@test projs !== nothing
@test sort(collect(keys(projs.dependencies))) == ["Dates"]

@test before == after
end




@testset "activate_notebook_environment, functional 2" begin
file = Pluto.testnb()
before = without_pluto_version(read(file, String))
@assert :activate_notebook_environment in names(Pluto)

@test !occursin("Artifacts", Pluto.PkgCompat.read_project_file(Pluto.load_notebook_nobackup(file)))
@test !occursin("Artifacts", before)


###
projs = Pluto.activate_notebook_environment(file) do
Pkg.add("Artifacts")
end

after = without_pluto_version(read(file, String))

@test occursin("Artifacts", Pluto.PkgCompat.read_project_file(Pluto.load_notebook_nobackup(file)))

@test before != after
@test occursin("Artifacts", after)
end




@testset "reset_notebook_environment" begin
file = Pluto.testnb()
before = without_pluto_version(read(file, String))
@assert :reset_notebook_environment in names(Pluto)

# project.toml fake cell id
@test occursin("00001", before)
@test occursin("[deps]", before)


###
Pluto.reset_notebook_environment(file; backup=false)


after = without_pluto_version(read(file, String))

@test before != after
# project.toml fake cell id
@test !occursin("00001", after)
@test !occursin("[deps]", after)

end




# TODO: too lazy to get a notebook with updatable package so just running the function and checking for errors
@testset "update_notebook_environment" begin
file = Pluto.testnb()
before = without_pluto_version(read(file, String))
@assert :update_notebook_environment in names(Pluto)

###
Pluto.update_notebook_environment(file)


# whatever
after = without_pluto_version(read(file, String))
@test occursin("[deps]", after)
end



@testset "has_notebook_environment" begin
file = Pluto.testnb()
before = read(file, String)
@assert :has_notebook_environment in names(Pluto)

###
@test Pluto.has_notebook_environment(file)


after = read(file, String)
@test before == after


file2 = Pluto.testnb("pkg_cell.jl")

@info "huh" Pluto.load_notebook_nobackup(file2).nbpkg_ctx
@test !Pluto.has_notebook_environment(file2)
end
end


1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ verify_no_running_processes()
# tests that don't start new processes:
@timeit_include("ReloadFromFile.jl")
@timeit_include("packages/PkgCompat.jl")
@timeit_include("packages/PkgUtils.jl")
@timeit_include("MethodSignatures.jl")
@timeit_include("MoreAnalysis.jl")
@timeit_include("is_just_text.jl")
Expand Down

0 comments on commit d07d8e3

Please sign in to comment.