Skip to content

Commit

Permalink
add SetupEnv for unit tests
Browse files Browse the repository at this point in the history
- The SetupEnv script add the develop version of all QED packages to julia environment.
- Run unit-test in the CI
  • Loading branch information
SimeonEhrig committed Jul 5, 2023
1 parent ad4c8e6 commit 9d527cb
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 3 deletions.
14 changes: 11 additions & 3 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
run_test:
image: alpine:latest
stages:
- unit-test

unit_tests_julia1.9:
image: julia:1.9
stage: unit-test
script:
- echo "Hello World"
- julia --project=. -e 'import Pkg; Pkg.Registry.add(Pkg.RegistrySpec(url="https://github.com/QEDjl-project/registry.git"));'
- julia --project=. -e 'import Pkg; Pkg.Registry.add(Pkg.RegistrySpec(url="https://github.com/JuliaRegistries/General"));'
- julia --project=. ${CI_PROJECT_DIR}/SetupDevEnv/src/SetupDevEnv.jl ${CI_PROJECT_DIR}/Project.toml
- julia --project=. -e 'import Pkg; Pkg.instantiate()'
- julia --project=. -e 'import Pkg; Pkg.test(; coverage = true)'
tags:
- cpuonly
1 change: 1 addition & 0 deletions SetupDevEnv/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Manifest.toml
13 changes: 13 additions & 0 deletions SetupDevEnv/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name = "SetupDevEnv"
uuid = "b422d612-8f72-4cc8-9700-415372519aaa"
authors = ["Simeon Ehrig <[email protected]> and contributors"]
version = "1.0.0"

[compat]
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
11 changes: 11 additions & 0 deletions SetupDevEnv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Usage

The application `SetupDevEnv` takes a `Project.toml` and add all dependencies which match a filter rule as develop version to the current julia environment. The path to the `Project.toml` is set via first application parameter.

```bash
julia --project=/path/to/the/julia/environment src/SetupDevEnv.jl /path/to/Project.toml
```

# Optional Environment variables

By default, all dependencies are added via `Pkg.develop("dep_name")`. Therefore the default develop branch is used. You can define the environment variables `CI_UNIT_PKG_URL_<dep_name>` to set a custom url. For example, you set environment variable `CI_UNIT_PKG_URL_QEDbase=https://github.com/User/QEDbase.jl#feature1`, the script will execute the command `Pkg.develop(url="https://github.com/User/QEDbase.jl#feature1")`, when the dependency `QEDbase` was found and matched in the `Project.toml`. Then the branch `feature1` from `https://github.com/User/QEDbase.jl` is used as dependency.
98 changes: 98 additions & 0 deletions SetupDevEnv/src/SetupDevEnv.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
module SetupDevEnv

using TOML
using Pkg

"""
extract_env_vars_from_git_message!()
Parse the commit message, if set via variable `CI_COMMIT_MESSAGE` and set custom urls.
"""
function extract_env_vars_from_git_message!()
if haskey(ENV, "CI_COMMIT_MESSAGE")
println("Found env variable CI_COMMIT_MESSAGE")
for line in split(ENV["CI_COMMIT_MESSAGE"], "\n")
line = strip(line)
if startswith(line, "CI_UNIT_PKG_URL_")
(var_name, url) = split(line, ":"; limit=2)
println("add " * var_name * "=" * strip(url))
ENV[var_name] = strip(url)
end
end
end
end

"""
get_dependencies(project_toml_path::AbstractString, package_prefix::Union{AbstractString,Regex}=r".*")::Set{AbstractString}
Parses a Project.toml located at `project_toml_path` and returns all dependencies, matching the regex `package_prefix`.
By default, the regex allows all dependencies.
"""
function get_dependencies(project_toml_path::AbstractString, package_prefix::Union{AbstractString,Regex}=r".*")::Set{AbstractString}
project_toml = TOML.parsefile(project_toml_path)
filtered_deps = filter((dep_name)-> startswith(dep_name, package_prefix), keys(project_toml["deps"]))
return filtered_deps
end

"""
add_develop_dep(dependencies::Set{AbstractString})
Add all dependencies listed in `dependencies` as develop version. By default, it takes the current development branch.
If the environment variable "CI_UNIT_PKG_URL_<dependency_name>" is set, take the url defined in the value to set
develop version, instead the the default develop branch (see `Pkg.develop(url=)`).
"""
function add_develop_dep(dependencies::Set{AbstractString})
# check if specific url was set for a dependency
env_prefix = "CI_UNIT_PKG_URL_"
modified_urls = Dict{String, String}()
for (env_key, env_var) in ENV
if startswith(env_key, env_prefix)
modified_urls[env_key[length(env_prefix)+1:end]] = env_var
end
end

if !isempty(modified_urls)
println("Found following env variables")
for (pkg_name, url) in modified_urls
println(" " * pkg_name * "=" * url)
end
println()
end

# add all dependencies as develop version to the current julia environment
for dep in dependencies
if haskey(modified_urls, dep)
split_url = split(modified_urls[dep], "#")
if length(split_url) > 2
@error "Ill formed url: $(url)"
exit(1)
end

if length(split_url) == 1
println("Pkg.develop(url=\"" * split_url[1] * "\")")
Pkg.add(url=split_url[1])
else
println("Pkg.develop(url=\"" * split_url[1] * ";\" rev=\"" * split_url[2] * "\")")
Pkg.add(url=split_url[1], rev=split_url[2])
end
else
println("Pkg.develop(\"" * dep * "\")")
Pkg.develop(dep)
end
end
end

if abspath(PROGRAM_FILE) == @__FILE__
if length(ARGS) < 1
println("Set path to Project.toml as first argument.")
exit(1)
end

project_toml_path = ARGS[1]
extract_env_vars_from_git_message!()
# get only dependencies, which starts with QED
deps = get_dependencies(project_toml_path, r"(QED)")
add_develop_dep(deps)
end

end
6 changes: 6 additions & 0 deletions SetupDevEnv/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using SetupDevEnv
using Test

@testset "SetupDevEnv.jl" begin
# Write your tests here.
end

0 comments on commit 9d527cb

Please sign in to comment.