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

Add test_pr for testing PRs #137

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/QuartoNotebookRunner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ include("WorkerSetup.jl")
include("server.jl")
include("socket.jl")
include("worker.jl")
include("utilities.jl")
include("precompile.jl")

end # module QuartoNotebookRunner
109 changes: 109 additions & 0 deletions src/utilities.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""
test_pr(cmd::Cmd; url::String, rev::String)

Test a PR by running the given command with the PR's changes. `url` defaults to
the `QuartoNotebookRunner.jl` repo. `rev` is required, and should be the branch
name of the PR. `cmd` should be the `quarto render` command to run.
"""
function test_pr(

Check warning on line 8 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L8

Added line #L8 was not covered by tests
cmd::Cmd;
url::String = "https://github.com/PumasAI/QuartoNotebookRunner.jl",
rev::String,
)
# Require the user to already have a `quarto` install on their path.
quarto = Sys.which("quarto")
if isnothing(quarto)
error("Quarto not found. Please install Quarto.")

Check warning on line 16 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L14-L16

Added lines #L14 - L16 were not covered by tests
end

# We require at least v1.5.29 to run this backend.
version = VersionNumber(readchomp(`quarto --version`))
if version < v"1.5.29"
error(

Check warning on line 22 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L20-L22

Added lines #L20 - L22 were not covered by tests
"Quarto version $version is not supported. Please upgrade to at least v1.5.29.",
)
end

# Ensure that any running server is stopped before we start.
_stop_running_server() || error("Failed to stop the running server.")

Check warning on line 28 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L28

Added line #L28 was not covered by tests

mktempdir() do dir
file = joinpath(dir, "file.jl")
write(

Check warning on line 32 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L30-L32

Added lines #L30 - L32 were not covered by tests
file,
"""
import Pkg
Pkg.add(; url = $(repr(url)), rev = $(repr(rev)))
""",
)
run(`$(Base.julia_cmd()) --startup-file=no --project=$dir $file`)
run(addenv(`$cmd --execute-debug`, "QUARTO_JULIA_PROJECT" => dir))

Check warning on line 40 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L39-L40

Added lines #L39 - L40 were not covered by tests
end
end

function _stop_running_server()
cache_dir = _quarto_julia_cache_dir()
transport_file = joinpath(cache_dir, "julia_transport.txt")
if isfile(transport_file)
@info "Removing transport file." transport_file

Check warning on line 48 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L44-L48

Added lines #L44 - L48 were not covered by tests

json = open(JSON3.read, transport_file)
pid = get(json, "pid", nothing)
try
_kill_proc(pid)

Check warning on line 53 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L50-L53

Added lines #L50 - L53 were not covered by tests
catch error
@error "Failed to stop the running server." error transport_file pid
return false

Check warning on line 56 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L55-L56

Added lines #L55 - L56 were not covered by tests
end
try
rm(transport_file)

Check warning on line 59 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L58-L59

Added lines #L58 - L59 were not covered by tests
catch error
@error "Failed to remove the transport file." error transport_file
return false

Check warning on line 62 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L61-L62

Added lines #L61 - L62 were not covered by tests
end
else
@info "No transport file found."

Check warning on line 65 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L65

Added line #L65 was not covered by tests
end
return true

Check warning on line 67 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L67

Added line #L67 was not covered by tests
end

function _kill_proc(id::Integer)
if Sys.iswindows()
run(`taskkill /F /PID $id`)

Check warning on line 72 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L70-L72

Added lines #L70 - L72 were not covered by tests
else
run(`kill -9 $id`)

Check warning on line 74 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L74

Added line #L74 was not covered by tests
end
end

function _quarto_julia_cache_dir()
home = homedir()
if Sys.isapple()
path = joinpath(home, "Library", "Caches", "quarto", "julia")
isdir(path) && return path
elseif Sys.iswindows()
localappdata = get(ENV, "LOCALAPPDATA", nothing)
if !isnothing(localappdata)
path = joinpath(localappdata, "quarto", "julia")
isdir(path) && return path

Check warning on line 87 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L78-L87

Added lines #L78 - L87 were not covered by tests
end

appdata = get(ENV, "APPDATA", nothing)
if !isnothing(appdata)
path = joinpath(appdata, "quarto", "julia")
isdir(path) && return path

Check warning on line 93 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L90-L93

Added lines #L90 - L93 were not covered by tests
end
elseif Sys.islinux()
xdg_cache_home = get(ENV, "XDG_CACHE_HOME", nothing)
if !isnothing(xdg_cache_home)
path = joinpath(xdg_cache_home, ".cache", "quarto", "julia")
isdir(path) && return path

Check warning on line 99 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L95-L99

Added lines #L95 - L99 were not covered by tests
end

path = joinpath(home, ".cache", "quarto", "julia")
isdir(path) && return path

Check warning on line 103 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L102-L103

Added lines #L102 - L103 were not covered by tests
else
error("Unsupported OS.")

Check warning on line 105 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L105

Added line #L105 was not covered by tests
end

error("Could not find a suitable cache directory.")

Check warning on line 108 in src/utilities.jl

View check run for this annotation

Codecov / codecov/patch

src/utilities.jl#L108

Added line #L108 was not covered by tests
end
Loading