diff --git a/flake.lock b/flake.lock index d0cb44ae..8755f8e2 100644 --- a/flake.lock +++ b/flake.lock @@ -70,6 +70,52 @@ "type": "github" } }, + "nix-github-actions": { + "inputs": { + "nixpkgs": [ + "nix-unit", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1688870561, + "narHash": "sha256-4UYkifnPEw1nAzqqPOTL2MvWtm3sNGw1UTYTalkTcGY=", + "owner": "nix-community", + "repo": "nix-github-actions", + "rev": "165b1650b753316aa7f1787f3005a8d2da0f5301", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nix-github-actions", + "type": "github" + } + }, + "nix-unit": { + "inputs": { + "flake-parts": [ + "flake-parts" + ], + "nix-github-actions": "nix-github-actions", + "nixpkgs": [ + "nixpkgs" + ], + "treefmt-nix": "treefmt-nix" + }, + "locked": { + "lastModified": 1690289081, + "narHash": "sha256-PCXQAQt8+i2pkUym9P1JY4JGoeZJLzzxWBhprHDdItM=", + "owner": "adisbladis", + "repo": "nix-unit", + "rev": "a9d6f33e50d4dcd9cfc0c92253340437bbae282b", + "type": "github" + }, + "original": { + "owner": "adisbladis", + "repo": "nix-unit", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1686501370, @@ -111,6 +157,7 @@ "devshell": "devshell", "flake-compat": "flake-compat", "flake-parts": "flake-parts", + "nix-unit": "nix-unit", "nixpkgs": "nixpkgs", "pre-commit-hooks": "pre-commit-hooks" } @@ -129,6 +176,27 @@ "repo": "default", "type": "github" } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nix-unit", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1689620039, + "narHash": "sha256-BtNwghr05z7k5YMdq+6nbue+nEalvDepuA7qdQMAKoQ=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "719c2977f958c41fa60a928e2fbc50af14844114", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index fdd2760d..e518b7fd 100644 --- a/flake.nix +++ b/flake.nix @@ -18,6 +18,10 @@ pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix"; pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs"; + nix-unit.url = "github:adisbladis/nix-unit"; + nix-unit.inputs.nixpkgs.follows = "nixpkgs"; + nix-unit.inputs.flake-parts.follows = "flake-parts"; + devshell = { url = "github:numtide/devshell"; flake = false; @@ -40,6 +44,7 @@ config, pkgs, system, + inputs', ... }: { apps = { @@ -78,6 +83,7 @@ packages = [ pkgs.alejandra pkgs.mdbook + inputs'.nix-unit.packages.nix-unit (pkgs.python3.withPackages (ps: [ pkgs.python3.pkgs.black ])) diff --git a/modules/flake-parts/checks.nix-unit.nix b/modules/flake-parts/checks.nix-unit.nix new file mode 100644 index 00000000..c7b43e41 --- /dev/null +++ b/modules/flake-parts/checks.nix-unit.nix @@ -0,0 +1,17 @@ +# evaluate packages from `/**/modules/drvs` and export them via `flake.packages` +{self, ...}: { + perSystem = { + pkgs, + inputs', + ... + }: { + # map all modules in /examples to a package output in the flake. + checks.nix-unit = pkgs.runCommand "nix-unit-tests" {} '' + export NIX_PATH=nixpkgs=${pkgs.path} + ${inputs'.nix-unit.packages.nix-unit}/bin/nix-unit \ + ${self}/tests/nix-unit/* \ + --eval-store $(realpath .) + touch $out + ''; + }; +} diff --git a/tests/nix-unit/test_pip_pyproject/default.nix b/tests/nix-unit/test_pip_pyproject/default.nix new file mode 100644 index 00000000..b91e145f --- /dev/null +++ b/tests/nix-unit/test_pip_pyproject/default.nix @@ -0,0 +1,75 @@ +{ + pkgs ? import {}, + lib ? import , + dream2nix ? (import (../../../modules + "/flake.nix")).outputs {}, +}: let + eval = module: + lib.evalModules { + modules = [module]; + specialArgs = { + inherit dream2nix; + packageSets.nixpkgs = pkgs; + }; + }; +in { + # test if dependencies are flattened + test_pip_flattened_dependencies = let + evaled = eval { + imports = [ + dream2nix.modules.drv-parts.pip + ]; + name = "test"; + lock.content = lib.mkForce { + fetchPipMetadata.targets.default.flask = []; + fetchPipMetadata.targets.default.requests = []; + }; + pip.flattenDependencies = true; + }; + config = evaled.config; + in { + expr = config.pip.rootDependencies; + expected = { + flask = true; + requests = true; + }; + }; + + # test if dependencies are ignored successfully in pip.rootDependencies + test_pip_ignore_dependencies = let + evaled = eval { + imports = [ + dream2nix.modules.drv-parts.pip + ]; + name = "test"; + pip.ignoredDependencies = ["requests"]; + lock.content = lib.mkForce { + fetchPipMetadata.targets.default.test = ["requests"]; + fetchPipMetadata.targets.default.requests = []; + }; + }; + config = evaled.config; + in { + expr = config.pip.targets.default ? requests; + expected = false; + }; + + # test if root dependency is picked correctly + test_pip_root_dependency = let + evaled = eval { + imports = [ + dream2nix.modules.drv-parts.pip + ]; + name = "test"; + lock.content = lib.mkForce { + fetchPipMetadata.targets.default.test = ["requests"]; + fetchPipMetadata.targets.default.requests = []; + }; + }; + config = evaled.config; + in { + expr = config.pip.rootDependencies; + expected = { + requests = true; + }; + }; +}