divnix / std

A DevOps framework for the SDLC with the power of Nix and Flakes. Good for keeping deadlines!
https://std.divnix.com
406 stars 43 forks source link

feat(namaka): allow overriding [dir] and allow snapshots to be generated/changed within std #346

Closed Pegasust closed 1 year ago

Pegasust commented 1 year ago

Overriding [dir]

This allows you to put in snap-dir

# nix/local/tests/default.nix
{inputs, cell}:
{
  snapshots = {
    meta.description="your desc"; 
    check = namaka.lib.load {...}; 
    snap-dir = "nix";
  };
}

Consider that your repo wants to have everything nix (apart from flake.nix) to be in nix/ or contrib/, maybe due to some kind of code ownership system.

You now have the option to put snapshots inside of the respective [dir]

Dealing with changing snapshots in std

Here's the reproducing bash that this change allows.

```bash rm -rf tests/_snapshots git add tests/ # (all fine) std //tests/checks/snapshots:check ``` ``` ------------------------------------------------------------------ Executing /Users//local_repos/std/.local/state/last-action With args [] ------------------------------------------------------------------ šŸž„ bt-blocktypes šŸž„ check-augmented-cell-inputs šŸž„ flake-module šŸž„ cells-lib-ops šŸž„ cells-lib-cfg šŸž„ cells-lib-dev 6 out of 6 tests failed ```
std //tests/checks/snapshots:review

# before>
------------------------------------------------------------------
Executing /Users/<user>/local_repos/std/.local/state/last-action
With args []
------------------------------------------------------------------
warning: Git tree '/Users/<user>/local_repos/std' is dirty
error: cached failure of attribute 'aarch64-darwin.tests.checks.snapshots.check'
Error: 
   0: unknown error

Location:
   src/cmd/review.rs:84

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.
####
# after>
(goes thru prompts correctly and there is no git diff)

Correctness

Basically check = namaka.lib.load {...}; actually just returns {} and relies heavily on the tracing and assertion system of Nix.

Since nix eval realizes .check anyways (and namaka.lib.load only traces its managed hierarchy), there's realistically no difference on the nix eval to include .check or not, we just need to make sure:

  1. To realize ONLY the set of snapshots we're interested in (I haven't tried the case of many snapshots yet, but this doesn't change the previous behavior)
  2. std is able to do its mapping magic

The std magic to pass this around into ./#${system}.${cell}.${cellBlock}.snapshots.check gets the error assertion in case of snapshot mismatch (or non-existence), which prevents the std magic to finish, resulting in obscure error message.

Removing .check lets this go through to nix eval, which still prints the correct trace: namaka={JSON as if we index inside .check} and to be compared with the correct snapshot path from namaka.lib.load.

blaggacao commented 1 year ago

Double nice! I've tangentially notices the cached failure problem but thought it would have been an operator error of mine. :smile:

Thanks also for the feature addition.

blaggacao commented 1 year ago

Merged in 3dca8edd82ff100878ff0797f67abda629ca8686 (with some sqashing)