From c8a4a4f4e9b515e839f36d3c872eba3f5d3c1c45 Mon Sep 17 00:00:00 2001 From: Aisuko Date: Fri, 2 Jun 2023 00:20:45 +1000 Subject: [PATCH] feat: Add new test cases for LoadConfigs (#447) Signed-off-by: Aisuko --- Makefile | 2 +- api/config.go | 13 ++++++++++--- api/config_test.go | 29 ++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 6fa334ae..8c79251d 100644 --- a/Makefile +++ b/Makefile @@ -235,7 +235,7 @@ test-models/testmodel: test: prepare test-models/testmodel cp tests/models_fixtures/* test-models - C_INCLUDE_PATH=${C_INCLUDE_PATH} LIBRARY_PATH=${LIBRARY_PATH} TEST_DIR=$(abspath ./)/test-dir/ FIXTURES=$(abspath ./)/tests/fixtures CONFIG_FILE=$(abspath ./)/test-models/config.yaml MODELS_PATH=$(abspath ./)/test-models $(GOCMD) run github.com/onsi/ginkgo/v2/ginkgo --flakeAttempts 5 -v -r ./api ./pkg + C_INCLUDE_PATH=${C_INCLUDE_PATH} LIBRARY_PATH=${LIBRARY_PATH} TEST_DIR=$(abspath ./)/test-dir/ FIXTURES=$(abspath ./)/tests/fixtures CONFIG_FILE=$(abspath ./)/test-models/config.yaml MODELS_PATH=$(abspath ./)/test-models $(GOCMD) run github.com/onsi/ginkgo/v2/ginkgo --flake-attempts 5 -v -r ./api ./pkg ## Help: help: ## Show this help. diff --git a/api/config.go b/api/config.go index 42aecbe8..5204cea7 100644 --- a/api/config.go +++ b/api/config.go @@ -3,7 +3,7 @@ package api import ( "encoding/json" "fmt" - "io/ioutil" + "io/fs" "os" "path/filepath" "strings" @@ -130,11 +130,18 @@ func (cm ConfigMerger) ListConfigs() []string { func (cm ConfigMerger) LoadConfigs(path string) error { cm.Lock() defer cm.Unlock() - files, err := ioutil.ReadDir(path) + entries, err := os.ReadDir(path) if err != nil { return err } - + files := make([]fs.FileInfo, 0, len(entries)) + for _, entry := range entries { + info, err := entry.Info() + if err != nil { + return err + } + files = append(files, info) + } for _, file := range files { // Skip templates, YAML and .keep files if !strings.Contains(file.Name(), ".yaml") { diff --git a/api/config_test.go b/api/config_test.go index 9950f803..626b90be 100644 --- a/api/config_test.go +++ b/api/config_test.go @@ -3,6 +3,7 @@ package api import ( "os" + "github.com/go-skynet/LocalAI/pkg/model" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" ) @@ -20,8 +21,34 @@ var _ = Describe("Test cases for config related functions", func() { Expect(err).To(BeNil()) Expect(config).ToNot(BeNil()) // two configs in config.yaml - Expect(len(config)).To(Equal(2)) + Expect(config[0].Name).To(Equal("list1")) + Expect(config[1].Name).To(Equal("list2")) }) + It("Test LoadConfigs", func() { + cm := NewConfigMerger() + options := newOptions() + modelLoader := model.NewModelLoader(os.Getenv("MODELS_PATH")) + WithModelLoader(modelLoader)(options) + + err := cm.LoadConfigs(options.loader.ModelPath) + Expect(err).To(BeNil()) + Expect(cm.configs).ToNot(BeNil()) + + // config should includes gpt4all models's api.config + Expect(cm.configs).To(HaveKey("gpt4all")) + + // config should includes gpt2 models's api.config + Expect(cm.configs).To(HaveKey("gpt4all-2")) + + // config should includes text-embedding-ada-002 models's api.config + Expect(cm.configs).To(HaveKey("text-embedding-ada-002")) + + // config should includes rwkv_test models's api.config + Expect(cm.configs).To(HaveKey("rwkv_test")) + + // config should includes whisper-1 models's api.config + Expect(cm.configs).To(HaveKey("whisper-1")) + }) }) })