JanDeDobbeleer / aliae

Cross shell and platform alias management
https://aliae.dev
MIT License
73 stars 3 forks source link

fix: func map unordered inconsistency #106

Closed kiliantyler closed 2 months ago

kiliantyler commented 2 months ago

Prerequisites

Issue:

There is a transient failure in the func map since it parses for "!include_dir" and "!include" -- Maps are unordered by nature in Go which means that when "!include" gets parsed first on a "!include_dir" directive it fails

Solution:

Create a struct for the FuncMap which will order the range inherently

Testing:

Script

#!/usr/bin/env bash

good_count=0
bad_count=0

for i in {1..100}; do
  if go test > /dev/null 2>&1; then
    good_count=$((good_count+1))
  else
    bad_count=$((bad_count+1))
  fi
done

echo "good_count: $good_count"
echo "bad_count: $bad_count"

Before change:

❯ ./src/config/test.sh
good_count: 91
bad_count: 9
❯ ./test.sh
good_count: 83
bad_count: 17

After Change:

❯ ./src/config/test.sh
good_count: 100
bad_count: 0
❯ ./test.sh
good_count: 100
bad_count: 0

Original transient failure in the test:

❯ go test
PASS
ok      github.com/jandedobbeleer/aliae/src/config  0.168s
❯ go test
--- FAIL: TestLoadConfig (0.00s)
    config_test.go:68:
            Error Trace:    /Users/kilian/Github/aliae/src/config/config_test.go:68
            Error:          Received unexpected error:
                            Failed to parse config file: read test/aliases: is a directory
            Test:           TestLoadConfig
            Messages:       Valid
    config_test.go:71:
            Error Trace:    /Users/kilian/Github/aliae/src/config/config_test.go:71
            Error:          Not equal:
                            expected: &config.Aliae{Aliae:shell.Aliae{(*shell.Alias)(0x140000fc000), (*shell.Alias)(0x140000fc090)}, Envs:shell.Envs{(*shell.Env)(0x14000096300)}, Paths:shell.Paths(nil), Scripts:shell.Scripts(nil)}
                            actual  : (*config.Aliae)(nil)

                            Diff:
                            --- Expected
                            +++ Actual
                            @@ -1,40 +1,2 @@
                            -(*config.Aliae)({
                            - Aliae: (shell.Aliae) (len=2) {
                            -  (*shell.Alias)({
                            -   Name: (string) (len=4) "test",
                            -   Value: (shell.Template) (len=4) "test",
                            -   Type: (shell.Type) "",
                            -   If: (shell.If) "",
                            -   Description: (string) "",
                            -   Force: (bool) false,
                            -   Option: (shell.Option) "",
                            -   Scope: (shell.Option) "",
                            -   template: (string) ""
                            -  }),
                            -  (*shell.Alias)({
                            -   Name: (string) (len=5) "test2",
                            -   Value: (shell.Template) (len=5) "test2",
                            -   Type: (shell.Type) "",
                            -   If: (shell.If) "",
                            -   Description: (string) "",
                            -   Force: (bool) false,
                            -   Option: (shell.Option) "",
                            -   Scope: (shell.Option) "",
                            -   template: (string) ""
                            -  })
                            - },
                            - Envs: (shell.Envs) (len=1) {
                            -  (*shell.Env)({
                            -   Name: (string) (len=8) "TEST_ENV",
                            -   Value: (string) (len=4) "test",
                            -   Delimiter: (shell.Template) "",
                            -   If: (shell.If) "",
                            -   Persist: (bool) false,
                            -   template: (string) "",
                            -   parsed: (bool) false
                            -  })
                            - },
                            - Paths: (shell.Paths) <nil>,
                            - Scripts: (shell.Scripts) <nil>
                            -})
                            +(*config.Aliae)(<nil>)

            Test:           TestLoadConfig
            Messages:       Valid
FAIL
exit status 1
FAIL    github.com/jandedobbeleer/aliae/src/config  0.164s
JanDeDobbeleer commented 2 months ago

@kiliantyler good catch. I did see that error pop up but I incorrectly assumed it was due to the cached config location. Learned something today.