From 6387c46c2e859fcd853d3bbe697ba9978c35b1e1 Mon Sep 17 00:00:00 2001 From: Lukas Trippe Date: Mon, 23 Sep 2024 10:13:20 +0200 Subject: [PATCH] test: use makefile (#1315) * test: use makefile * more make commands --- .github/workflows/test.yaml | 2 +- .github/workflows/update-fixed-env.yaml | 2 +- Makefile | 59 +++++++++++++++++++++++++ envs/environment.fixed.yaml | 2 +- test.sh | 13 ------ 5 files changed, 62 insertions(+), 16 deletions(-) create mode 100755 Makefile delete mode 100755 test.sh diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 3435f6fe..5ac25db8 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -99,7 +99,7 @@ jobs: - name: Run snakemake test workflows run: | - ./test.sh + make test - name: Upload artifacts if: matrix.os == 'ubuntu' && matrix.inhouse == 'stable-inhouse-deps' diff --git a/.github/workflows/update-fixed-env.yaml b/.github/workflows/update-fixed-env.yaml index ec63a944..425b246f 100644 --- a/.github/workflows/update-fixed-env.yaml +++ b/.github/workflows/update-fixed-env.yaml @@ -23,7 +23,7 @@ jobs: - name: Update environment.fixed.yaml run: | - conda env export --name pypsa-eur --no-builds > envs/environment.fixed.yaml + conda env export --name pypsa-eur-fixed --no-builds > envs/environment.fixed.yaml - name: Add SPDX header run: | diff --git a/Makefile b/Makefile new file mode 100755 index 00000000..8f2d3901 --- /dev/null +++ b/Makefile @@ -0,0 +1,59 @@ +# SPDX-FileCopyrightText: : 2021-2024 The PyPSA-Eur Authors +# +# SPDX-License-Identifier: CC0-1.0 + +.PHONY: _conda_check install install-fixed test clean-tests reset + +# Helper: Check if conda or mamba is installed and set CONDA_OR_MAMBA variable +_conda_check: + @# Check if conda or mamba is installed and set CONDA_OR_MAMBA variable + @if command -v conda &> /dev/null; then \ + echo "Conda detected, using Conda..."; \ + $(eval CONDA_OR_MAMBA := conda) \ + elif command -v mamba &> /dev/null; then \ + echo "Conda not found, but Mamba detected. Using Mamba..."; \ + $(eval CONDA_OR_MAMBA := mamba) \ + else \ + echo "Neither Conda nor Mamba is installed. Please install one of them and retry."; \ + exit 1; \ + fi + +# Install the environment +install: _conda_check + @$(CONDA_OR_MAMBA) env create -f envs/environment.yaml + @$(CONDA_OR_MAMBA) run -n pypsa-eur pre-commit install + +# Install fixed environment +install-fixed: _conda_check + @$(CONDA_OR_MAMBA) env create -f envs/environment.fixed.yaml + @$(CONDA_OR_MAMBA) run -n pypsa-eur pre-commit install + +# Run default tests +test: + set -e + snakemake -call solve_elec_networks --configfile config/test/config.electricity.yaml --rerun-triggers=mtime + snakemake -call all --configfile config/test/config.overnight.yaml --rerun-triggers=mtime + snakemake -call all --configfile config/test/config.myopic.yaml --rerun-triggers=mtime + snakemake -call make_summary_perfect --configfile config/test/config.perfect.yaml --rerun-triggers=mtime + snakemake -call all --configfile config/test/config.scenarios.yaml --rerun-triggers=mtime -n + echo "All tests completed successfully." + +# Cleans all output files from tests +clean-tests: + snakemake -call solve_elec_networks --configfile config/test/config.electricity.yaml --rerun-triggers=mtime --delete-all-output + snakemake -call all --configfile config/test/config.overnight.yaml --rerun-triggers=mtime --delete-all-output + snakemake -call all --configfile config/test/config.myopic.yaml --rerun-triggers=mtime --delete-all-output + snakemake -call make_summary_perfect --configfile config/test/config.perfect.yaml --rerun-triggers=mtime --delete-all-output + snakemake -call all --configfile config/test/config.scenarios.yaml --rerun-triggers=mtime -n --delete-all-output + +# Removes all created files except for large cutout files (similar to fresh clone) +reset: + @echo "Do you really wanna continue? This will remove logs, resources, benchmarks, results, and .snakemake directories (y/n): " && \ + read ans && [ $${ans} = y ] && ( \ + rm -r ./logs || true; \ + rm -r ./resources || true; \ + rm -r ./benchmarks || true; \ + rm -r ./results || true; \ + rm -r ./.snakemake || true; \ + echo "Reset completed." \ + ) || echo "Reset cancelled." diff --git a/envs/environment.fixed.yaml b/envs/environment.fixed.yaml index aaa0039c..e45224c2 100644 --- a/envs/environment.fixed.yaml +++ b/envs/environment.fixed.yaml @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: : 2017-2024 The PyPSA-Eur Authors # SPDX-License-Identifier: CC0-1.0 -name: pypsa-eur +name: pypsa-eur-fixed channels: - conda-forge - bioconda diff --git a/test.sh b/test.sh deleted file mode 100755 index 64566aa2..00000000 --- a/test.sh +++ /dev/null @@ -1,13 +0,0 @@ -# SPDX-FileCopyrightText: : 2021-2024 The PyPSA-Eur Authors -# -# SPDX-License-Identifier: CC0-1.0 - -set -x && \ - -snakemake -call solve_elec_networks --configfile config/test/config.electricity.yaml --rerun-triggers=mtime && \ -snakemake -call all --configfile config/test/config.overnight.yaml --rerun-triggers=mtime && \ -snakemake -call all --configfile config/test/config.myopic.yaml --rerun-triggers=mtime && \ -snakemake -call make_summary_perfect --configfile config/test/config.perfect.yaml --rerun-triggers=mtime && \ -snakemake -call all --configfile config/test/config.scenarios.yaml --rerun-triggers=mtime -n && \ - -set +x