numtide / flake-utils

Pure Nix flake utility functions [maintainer=@zimbatm]
MIT License
1.19k stars 78 forks source link

Add checks-utils #36

Closed gytis-ivaskevicius closed 3 years ago

gytis-ivaskevicius commented 3 years ago

Adding a little overlay with few convenient functions to evaluate Nix using checks.

Here is nix flake show with all the edge cases I was able to think of:

git+file:///home/gytis/Projects/flake-utils?dir=examples%2fchecks-utils
└───checks
    ├───aarch64-linux
    │   ├───contains_key2: derivation 'SUCCESS__key2__EXISTS_IN_ATTRSET'
    │   ├───failing_contains_key2: derivation 'FAILURE__failing-data__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_3'
    │   ├───failing_valid_key1: derivation 'FAILURE__value1__NOT_EQUAL__failing-data'
    │   ├───null_formatting_hasKey: derivation 'FAILURE__null__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_3'
    │   ├───null_formatting_key1: derivation 'FAILURE__value1__NOT_EQUAL__null'
    │   ├───number_formatting_hasKey: derivation 'FAILURE__123__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_3'
    │   ├───number_formatting_isEqual: derivation 'FAILURE__value1__NOT_EQUAL__123'
    │   └───valid_key1: derivation 'SUCCESS__value1__IS_EQUAL__value1'
    ├───i686-linux
    │   ├───contains_key2: derivation 'SUCCESS__key2__EXISTS_IN_ATTRSET'
    │   ├───failing_contains_key2: derivation 'FAILURE__failing-data__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_3'
    │   ├───failing_valid_key1: derivation 'FAILURE__value1__NOT_EQUAL__failing-data'
    │   ├───null_formatting_hasKey: derivation 'FAILURE__null__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_3'
    │   ├───null_formatting_key1: derivation 'FAILURE__value1__NOT_EQUAL__null'
    │   ├───number_formatting_hasKey: derivation 'FAILURE__123__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_3'
    │   ├───number_formatting_isEqual: derivation 'FAILURE__value1__NOT_EQUAL__123'
    │   └───valid_key1: derivation 'SUCCESS__value1__IS_EQUAL__value1'
    ├───x86_64-darwin
    │   ├───contains_key2: derivation 'SUCCESS__key2__EXISTS_IN_ATTRSET'
    │   ├───failing_contains_key2: derivation 'FAILURE__failing-data__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_3'
    │   ├───failing_valid_key1: derivation 'FAILURE__value1__NOT_EQUAL__failing-data'
    │   ├───null_formatting_hasKey: derivation 'FAILURE__null__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_3'
    │   ├───null_formatting_key1: derivation 'FAILURE__value1__NOT_EQUAL__null'
    │   ├───number_formatting_hasKey: derivation 'FAILURE__123__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_3'
    │   ├───number_formatting_isEqual: derivation 'FAILURE__value1__NOT_EQUAL__123'
    │   └───valid_key1: derivation 'SUCCESS__value1__IS_EQUAL__value1'
    └───x86_64-linux
        ├───contains_key2: derivation 'SUCCESS__key2__EXISTS_IN_ATTRSET'
        ├───failing_contains_key2: derivation 'FAILURE__failing-data__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_3'
        ├───failing_valid_key1: derivation 'FAILURE__value1__NOT_EQUAL__failing-data'
        ├───null_formatting_hasKey: derivation 'FAILURE__null__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_3'
        ├───null_formatting_key1: derivation 'FAILURE__value1__NOT_EQUAL__null'
        ├───number_formatting_hasKey: derivation 'FAILURE__123__DOES_NOT_EXISTS_IN_ATTRSET_SIZE_3'
        ├───number_formatting_isEqual: derivation 'FAILURE__value1__NOT_EQUAL__123'
        └───valid_key1: derivation 'SUCCESS__value1__IS_EQUAL__value1'

Checks definition used in this example:

        checks = {
          valid_key1 = pkgs.isEqual testDataset.key1 "value1";
          contains_key2 = pkgs.hasKey testDataset "key2";

          failing_valid_key1 = pkgs.isEqual testDataset.key1 "failing-data";
          failing_contains_key2 = pkgs.hasKey testDataset "failing-data";

          number_formatting_isEqual = pkgs.isEqual testDataset.key1 123;
          number_formatting_hasKey = pkgs.hasKey testDataset 123;

          null_formatting_key1 = pkgs.isEqual testDataset.key1 null;
          null_formatting_hasKey = pkgs.hasKey testDataset null;
        };

I am not sure about what kind checks attrset key formatting/naming scheme should we encourage, any suggestions? Note: At first I tried camelcase - did not seem particularly readable. Do checks even need any fancy names? maybe test1, test2, test3... is good enough?

blaggacao commented 3 years ago

I'm ready for using this. :crown: