All checks were successful
continuous-integration/drone/push Build is passing
This is the first step to introduce a configuration file for abra. The config file must be named `abra.yaml` or àbra.yml`. abra look for the config file in the current directory and when not found traverses the directory tree up until it is found or the home/root directory is reached. For now there is only one setting that is made configurable: `abraDir`. The new logic for setting the abra dir is the following: 1. lookup `$ABRA_DIR` env 2. look for config file and take value from there 3. `$HOME/.abra` as fallback See coop-cloud/organising#303. Reviewed-on: coop-cloud/abra#419 Reviewed-by: decentral1se <decentral1se@noreply.git.coopcloud.tech> Co-authored-by: p4u1 <p4u1_f4u1@riseup.net> Co-committed-by: p4u1 <p4u1_f4u1@riseup.net>
104 lines
2.3 KiB
Go
104 lines
2.3 KiB
Go
package config
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestFindAbraConfig(t *testing.T) {
|
|
wd, err := os.Getwd()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
tests := []struct {
|
|
Dir string
|
|
Config string
|
|
}{
|
|
{
|
|
Dir: "testdata/abraconfig1",
|
|
Config: filepath.Join(wd, "testdata/abraconfig1/abra.yaml"),
|
|
},
|
|
{
|
|
Dir: "testdata/abraconfig1/subdir",
|
|
Config: filepath.Join(wd, "testdata/abraconfig1/abra.yaml"),
|
|
},
|
|
{
|
|
Dir: "testdata/abraconfig2",
|
|
Config: filepath.Join(wd, "testdata/abraconfig2/abra.yml"),
|
|
},
|
|
{
|
|
Dir: "testdata/abraconfig2/subdir",
|
|
Config: filepath.Join(wd, "testdata/abraconfig2/abra.yml"),
|
|
},
|
|
{
|
|
Dir: "testdata",
|
|
Config: "",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.Dir, func(t *testing.T) {
|
|
config := findAbraConfig(tc.Dir)
|
|
if config != tc.Config {
|
|
t.Errorf("\nwant: %s\ngot: %s", tc.Config, config)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadAbraConfigGetAbraDir(t *testing.T) {
|
|
wd, err := os.Getwd()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
t.Setenv("ABRA_DIR", "")
|
|
|
|
t.Run("default", func(t *testing.T) {
|
|
cfg := LoadAbraConfig()
|
|
wantAbraDir := os.ExpandEnv("$HOME/.abra")
|
|
if cfg.GetAbraDir() != wantAbraDir {
|
|
t.Errorf("\nwant: %s\ngot: %s", wantAbraDir, cfg.GetAbraDir())
|
|
}
|
|
})
|
|
|
|
t.Run("from config file", func(t *testing.T) {
|
|
t.Cleanup(func() { os.Chdir(wd) })
|
|
err = os.Chdir(filepath.Join(wd, "testdata/abraconfig1"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
cfg := LoadAbraConfig()
|
|
log.Println(cfg.GetAbraDir())
|
|
wantAbraDir := filepath.Join(wd, "testdata/abraconfig1/abra.yaml/foobar")
|
|
if cfg.GetAbraDir() != wantAbraDir {
|
|
t.Errorf("\nwant: %s\ngot: %s", wantAbraDir, cfg.GetAbraDir())
|
|
}
|
|
})
|
|
|
|
t.Run("default when config file is empty", func(t *testing.T) {
|
|
t.Cleanup(func() { os.Chdir(wd) })
|
|
err := os.Chdir(filepath.Join(wd, "testdata/abraconfig2"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
cfg := LoadAbraConfig()
|
|
wantAbraDir := os.ExpandEnv("$HOME/.abra")
|
|
if cfg.GetAbraDir() != wantAbraDir {
|
|
t.Errorf("\nwant: %s\ngot: %s", wantAbraDir, cfg.GetAbraDir())
|
|
}
|
|
})
|
|
|
|
t.Run("from env variable", func(t *testing.T) {
|
|
t.Setenv("ABRA_DIR", "foo")
|
|
cfg := LoadAbraConfig()
|
|
wantAbraDir := "foo"
|
|
if cfg.GetAbraDir() != wantAbraDir {
|
|
t.Errorf("\nwant: %s\ngot: %s", wantAbraDir, cfg.GetAbraDir())
|
|
}
|
|
})
|
|
}
|