2023-04-26 18:16:42 +03:00
|
|
|
package yamlstore_test
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
import (
|
2023-04-22 16:37:07 +03:00
|
|
|
"context"
|
2022-12-18 02:11:33 +03:00
|
|
|
"os"
|
2020-08-06 20:58:47 +03:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
"github.com/neilotoole/sq/cli"
|
|
|
|
"github.com/neilotoole/sq/cli/config/yamlstore"
|
|
|
|
"github.com/neilotoole/sq/libsq/core/options"
|
2022-12-18 10:18:35 +03:00
|
|
|
"github.com/neilotoole/sq/testh/tutil"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/config"
|
|
|
|
"github.com/neilotoole/sq/testh/proj"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFileStore_Nil_Save(t *testing.T) {
|
2023-04-26 18:16:42 +03:00
|
|
|
var f *yamlstore.Store
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-22 16:37:07 +03:00
|
|
|
err := f.Save(context.Background(), config.New())
|
2020-08-06 20:58:47 +03:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileStore_LoadSaveLoad(t *testing.T) {
|
2023-05-01 06:59:34 +03:00
|
|
|
ctx := context.Background()
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-04-26 18:16:42 +03:00
|
|
|
const wantVers = `v0.34.0`
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// good.01.sq.yml has a bunch of fixtures in it
|
2023-05-01 06:59:34 +03:00
|
|
|
fs := &yamlstore.Store{
|
|
|
|
Path: "testdata/good.01.sq.yml",
|
|
|
|
HookLoad: hookExpand,
|
|
|
|
OptionsRegistry: &options.Registry{},
|
|
|
|
}
|
|
|
|
cli.RegisterDefaultOpts(fs.OptionsRegistry)
|
2020-08-06 20:58:47 +03:00
|
|
|
const expectGood01SrcCount = 34
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
cfg, err := fs.Load(ctx)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, cfg)
|
2023-04-19 08:28:09 +03:00
|
|
|
require.NotNil(t, cfg.Collection)
|
2023-04-26 18:16:42 +03:00
|
|
|
require.Equal(t, wantVers, cfg.Version)
|
2023-04-19 08:28:09 +03:00
|
|
|
require.Equal(t, expectGood01SrcCount, len(cfg.Collection.Sources()))
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2022-12-18 02:11:33 +03:00
|
|
|
f, err := os.CreateTemp("", "*.sq.yml")
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() { assert.NoError(t, f.Close()) })
|
|
|
|
|
|
|
|
fs.Path = f.Name()
|
|
|
|
t.Logf("writing to tmp file: %s", fs.Path)
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
err = fs.Save(ctx, cfg)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
cfg2, err := fs.Load(ctx)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, cfg2)
|
2023-04-19 08:28:09 +03:00
|
|
|
require.Equal(t, expectGood01SrcCount, len(cfg2.Collection.Sources()))
|
2020-08-06 20:58:47 +03:00
|
|
|
require.EqualValues(t, cfg, cfg2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// hookExpand expands variables in data, e.g. ${SQ_ROOT}.
|
|
|
|
var hookExpand = func(data []byte) ([]byte, error) {
|
|
|
|
return []byte(proj.Expand(string(data))), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileStore_Load(t *testing.T) {
|
2023-04-26 18:16:42 +03:00
|
|
|
optsReg := &options.Registry{}
|
|
|
|
cli.RegisterDefaultOpts(optsReg)
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
good, err := filepath.Glob("testdata/good.*")
|
|
|
|
require.NoError(t, err)
|
|
|
|
bad, err := filepath.Glob("testdata/bad.*")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Logf("%d good fixtures, %d bad fixtures", len(good), len(bad))
|
|
|
|
|
2023-05-01 06:59:34 +03:00
|
|
|
fs := &yamlstore.Store{
|
|
|
|
HookLoad: hookExpand,
|
|
|
|
OptionsRegistry: optsReg,
|
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
for _, match := range good {
|
2021-02-22 10:37:00 +03:00
|
|
|
match := match
|
2022-12-17 05:09:49 +03:00
|
|
|
t.Run(tutil.Name(match), func(t *testing.T) {
|
2021-02-22 10:37:00 +03:00
|
|
|
fs.Path = match
|
2023-05-01 06:59:34 +03:00
|
|
|
_, err = fs.Load(context.Background())
|
2021-02-22 10:37:00 +03:00
|
|
|
require.NoError(t, err, match)
|
|
|
|
})
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, match := range bad {
|
2021-02-22 10:37:00 +03:00
|
|
|
match := match
|
2022-12-17 05:09:49 +03:00
|
|
|
t.Run(tutil.Name(match), func(t *testing.T) {
|
2021-02-22 10:37:00 +03:00
|
|
|
fs.Path = match
|
2023-05-01 06:59:34 +03:00
|
|
|
_, err := fs.Load(context.Background())
|
2021-02-22 10:37:00 +03:00
|
|
|
require.Error(t, err, match)
|
|
|
|
})
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
}
|