From 9d527cbdd6e59d8029cab719d33f5c0c37b55f36 Mon Sep 17 00:00:00 2001 From: Simeon Ehrig Date: Wed, 5 Jul 2023 13:15:46 +0200 Subject: [PATCH] add SetupEnv for unit tests - The SetupEnv script add the develop version of all QED packages to julia environment. - Run unit-test in the CI --- .gitlab-ci.yml | 14 +++-- SetupDevEnv/.gitignore | 1 + SetupDevEnv/Project.toml | 13 +++++ SetupDevEnv/README.md | 11 ++++ SetupDevEnv/src/SetupDevEnv.jl | 98 ++++++++++++++++++++++++++++++++++ SetupDevEnv/test/runtests.jl | 6 +++ 6 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 SetupDevEnv/.gitignore create mode 100644 SetupDevEnv/Project.toml create mode 100644 SetupDevEnv/README.md create mode 100644 SetupDevEnv/src/SetupDevEnv.jl create mode 100644 SetupDevEnv/test/runtests.jl diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0f779c6..e464aec 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 diff --git a/SetupDevEnv/.gitignore b/SetupDevEnv/.gitignore new file mode 100644 index 0000000..b067edd --- /dev/null +++ b/SetupDevEnv/.gitignore @@ -0,0 +1 @@ +/Manifest.toml diff --git a/SetupDevEnv/Project.toml b/SetupDevEnv/Project.toml new file mode 100644 index 0000000..107e12e --- /dev/null +++ b/SetupDevEnv/Project.toml @@ -0,0 +1,13 @@ +name = "SetupDevEnv" +uuid = "b422d612-8f72-4cc8-9700-415372519aaa" +authors = ["Simeon Ehrig and contributors"] +version = "1.0.0" + +[compat] +julia = "1" + +[extras] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[targets] +test = ["Test"] diff --git a/SetupDevEnv/README.md b/SetupDevEnv/README.md new file mode 100644 index 0000000..08a19c4 --- /dev/null +++ b/SetupDevEnv/README.md @@ -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_` 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. diff --git a/SetupDevEnv/src/SetupDevEnv.jl b/SetupDevEnv/src/SetupDevEnv.jl new file mode 100644 index 0000000..e61bb93 --- /dev/null +++ b/SetupDevEnv/src/SetupDevEnv.jl @@ -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_" 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 diff --git a/SetupDevEnv/test/runtests.jl b/SetupDevEnv/test/runtests.jl new file mode 100644 index 0000000..51b31df --- /dev/null +++ b/SetupDevEnv/test/runtests.jl @@ -0,0 +1,6 @@ +using SetupDevEnv +using Test + +@testset "SetupDevEnv.jl" begin + # Write your tests here. +end