feat: unit tests

This commit is contained in:
Ryan Yin
2024-03-13 14:47:36 +08:00
parent be963c0642
commit fff6ad6bff
17 changed files with 142 additions and 59 deletions

View File

@@ -1,24 +0,0 @@
name: Nix Flake Check
on: [push, pull_request, workflow_dispatch]
jobs:
checks:
name: Check expressions
runs-on: ubuntu-latest
steps:
# - name: Checkout repository
# uses: actions/checkout@v4
# - name: Install nix
# uses: cachix/install-nix-action@v24
# with:
# install_url: https://nixos.org/nix/install
# extra_nix_config: |
# access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
# experimental-features = nix-command flakes
- name: Run Nix Flake Check
run: |
echo 'TODO: nix flake check'
# nix flake check

24
.github/workflows/flake_unittests.yml vendored Normal file
View File

@@ -0,0 +1,24 @@
name: Nix Flake Unit Tests
on: [push, pull_request, workflow_dispatch]
jobs:
checks:
name: Check expressions
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install nix
uses: cachix/install-nix-action@v24
with:
install_url: https://nixos.org/nix/install
extra_nix_config: |
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
experimental-features = nix-command flakes
- name: Run Nix Flake Unit Tests
run: |
echo 'Flake Unit Tests'
nix eval .#unitTests --show-trace --print-build-logs --verbose

View File

@@ -7,6 +7,14 @@ There is no need to do this when you have a small number of machines.
But when you have a large number of machines, it is necessary to manage them in a fine-grained way,
otherwise, it will be difficult to manage and maintain them.
## Unit Tests
Unit Tests for all flake outputs(only NixOS systems currently).
```bash
nix eval .#unitTests --show-trace --print-build-logs --verbose
```
## Overview
All the outputs of this flake are defined here.
@@ -56,5 +64,3 @@ All the outputs of this flake are defined here.
12 directories, 28 files
```

View File

@@ -19,12 +19,3 @@
};
in
outputs
// {
inherit data; # for debugging purposes
# NixOS's unit tests.
# unit-tests = haumea.lib.loadEvalTests {
# src = ./tests;
# inputs = args;
# };
}

View File

@@ -31,8 +31,8 @@ in
inherit data; # for debugging purposes
# NixOS's unit tests.
# unit-tests = haumea.lib.loadEvalTests {
# src = ./tests;
# inputs = args;
# };
unitTests = haumea.lib.loadEvalTests {
src = ./tests;
inputs = args // {inherit outputs;};
};
}

View File

@@ -0,0 +1,8 @@
{
lib,
outputs,
}: let
hostsNames = builtins.attrNames outputs.nixosConfigurations;
expected = lib.genAttrs hostsNames (name: name);
in
expected

View File

@@ -0,0 +1,9 @@
{
lib,
outputs,
}:
lib.genAttrs
(builtins.attrNames outputs.nixosConfigurations)
(
name: outputs.nixosConfigurations.${name}.config.networking.hostName
)

View File

@@ -52,6 +52,9 @@ in {
# add attribute sets into outputs, for debugging
debugAttrs = {inherit nixosSystems darwinSystems allSystems allSystemNames;};
# Unit Tests for all NixOS systems.
unitTests = lib.lists.all (it: it.unitTests == {}) nixosSystemValues;
# NixOS Hosts
nixosConfigurations =
lib.attrsets.mergeAttrsList (map (it: it.nixosConfigurations or {}) nixosSystemValues);
@@ -89,9 +92,6 @@ in {
# Unit Tests, Intergraded Tests, and Pre-commit checks
checks = forAllSystems (
system: {
# Unit Tests for the system
# unit-tests = allSystems.${system}.unit-tests;
pre-commit-check = pre-commit-hooks.lib.${system}.run {
src = ./.;
hooks = {

View File

@@ -31,8 +31,8 @@ in
inherit data; # for debugging purposes
# NixOS's unit tests.
# unit-tests = haumea.lib.loadEvalTests {
# src = ./tests;
# inputs = args;
# };
unitTests = haumea.lib.loadEvalTests {
src = ./tests;
inputs = args // {inherit outputs;};
};
}

View File

@@ -0,0 +1,8 @@
{
lib,
outputs,
}: let
hostsNames = builtins.attrNames outputs.nixosConfigurations;
expected = lib.genAttrs hostsNames (name: name);
in
expected

View File

@@ -0,0 +1,9 @@
{
lib,
outputs,
}:
lib.genAttrs
(builtins.attrNames outputs.nixosConfigurations)
(
name: outputs.nixosConfigurations.${name}.config.networking.hostName
)

View File

@@ -19,12 +19,3 @@
};
in
outputs
// {
inherit data; # for debugging purposes
# NixOS's unit tests.
# unit-tests = haumea.lib.loadEvalTests {
# src = ./tests;
# inputs = args;
# };
}

View File

@@ -30,8 +30,8 @@ in
inherit data; # for debugging purposes
# NixOS's unit tests.
# unit-tests = haumea.lib.loadEvalTests {
# src = ./tests;
# inputs = args;
# };
unitTests = haumea.lib.loadEvalTests {
src = ./tests;
inputs = args // {inherit outputs;};
};
}

View File

@@ -0,0 +1,15 @@
{
myvars,
lib,
}: let
username = myvars.username;
hosts = [
"ai-hyprland"
"ai-i3"
"shoukei-hyprland"
"shoukei-i3"
"ruby"
"k3s-prod-1-master-1"
];
in
lib.genAttrs hosts (_: "/home/${username}")

View File

@@ -0,0 +1,20 @@
{
myvars,
lib,
outputs,
}: let
username = myvars.username;
hosts = [
"ai-hyprland"
"ai-i3"
"shoukei-hyprland"
"shoukei-i3"
"ruby"
"k3s-prod-1-master-1"
];
in
lib.genAttrs
hosts
(
name: outputs.nixosConfigurations.${name}.config.home-manager.users.${username}.home.homeDirectory
)

View File

@@ -0,0 +1,17 @@
{
lib,
outputs,
}: let
specialExpected = {
"ai-hyprland" = "ai";
"ai-i3" = "ai";
"shoukei-hyprland" = "shoukei";
"shoukei-i3" = "shoukei";
};
specialHostNames = builtins.attrNames specialExpected;
otherHosts = builtins.removeAttrs outputs.nixosConfigurations specialHostNames;
otherHostsNames = builtins.attrNames otherHosts;
# other hosts's hostName is the same as the nixosConfigurations name
otherExpected = lib.genAttrs otherHostsNames (name: name);
in (specialExpected // otherExpected)

View File

@@ -0,0 +1,9 @@
{
lib,
outputs,
}:
lib.genAttrs
(builtins.attrNames outputs.nixosConfigurations)
(
name: outputs.nixosConfigurations.${name}.config.networking.hostName
)