From 59a814b51a393f087b0d000e965a05c1c27d07af Mon Sep 17 00:00:00 2001 From: Jiyong Huang Date: Tue, 24 Dec 2024 09:21:21 +0800 Subject: [PATCH] feat(lib): provide path to abs func Signed-off-by: Jiyong Huang --- internal/conf/path.go | 14 ++++++---- internal/conf/path_test.go | 35 ++++++++++++++++++++++- pkg/path/path.go | 28 +++++++++++++++++++ pkg/path/path_test.go | 57 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 pkg/path/path.go create mode 100644 pkg/path/path_test.go diff --git a/internal/conf/path.go b/internal/conf/path.go index bfeb2d6d72..226dfff2c1 100644 --- a/internal/conf/path.go +++ b/internal/conf/path.go @@ -164,12 +164,16 @@ func relativePath(subdir string) (dir string, err error) { } func ProcessPath(p string) (string, error) { - if abs, err := filepath.Abs(p); err != nil { - return "", nil - } else { - if _, err := os.Stat(abs); os.IsNotExist(err) { + abs := p + var err error + if !filepath.IsAbs(p) { + abs, err = GetLoc(p) + if err != nil { return "", err } - return abs, nil } + if _, err := os.Stat(abs); os.IsNotExist(err) { + return "", err + } + return abs, nil } diff --git a/internal/conf/path_test.go b/internal/conf/path_test.go index 647c1dd9fc..aa77360243 100644 --- a/internal/conf/path_test.go +++ b/internal/conf/path_test.go @@ -1,4 +1,4 @@ -// Copyright 2021 EMQ Technologies Co., Ltd. +// Copyright 2021-2024 EMQ Technologies Co., Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,9 +15,11 @@ package conf import ( + "path/filepath" "strings" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -99,3 +101,34 @@ func TestPathConfig(t *testing.T) { require.Equal(t, tc.expect, d) } } + +func TestProcessPath(t *testing.T) { + absPath, err := filepath.Abs("/data/uploads/test.txt") + require.NoError(t, err) + tests := []struct { + name string + path string + hasError bool + }{ + { + name: "absolute path", + path: absPath, + hasError: true, + }, + { + name: "relative path", + path: "etc/kuiper.yaml", + hasError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err = ProcessPath(tt.path) + if tt.hasError { + assert.NotNil(t, err) + } else { + assert.NoError(t, err) + } + }) + } +} diff --git a/pkg/path/path.go b/pkg/path/path.go new file mode 100644 index 0000000000..cf9995a10c --- /dev/null +++ b/pkg/path/path.go @@ -0,0 +1,28 @@ +// Copyright 2024 EMQ Technologies Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package path + +import ( + "path/filepath" + + "github.com/lf-edge/ekuiper/contract/v2/api" +) + +func AbsPath(ctx api.StreamContext, path string) string { + if filepath.IsAbs(path) { + return path + } + return filepath.Join(ctx.GetRootPath(), path) +} diff --git a/pkg/path/path_test.go b/pkg/path/path_test.go new file mode 100644 index 0000000000..815b404609 --- /dev/null +++ b/pkg/path/path_test.go @@ -0,0 +1,57 @@ +// Copyright 2024 EMQ Technologies Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package path + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + mockContext "github.com/lf-edge/ekuiper/v2/pkg/mock/context" +) + +func TestAbsPath(t *testing.T) { + err := os.Setenv("KuiperBaseKey", "/bigdata") + require.NoError(t, err) + defer os.Unsetenv("KuiperBaseKey") + absPath, err := filepath.Abs("/data/uploads/test.txt") + require.NoError(t, err) + tests := []struct { + name string + path string + want string + }{ + { + name: "absolute path", + path: absPath, + want: absPath, + }, + { + name: "relative path", + path: "data/uploads/test.txt", + want: filepath.Clean("/bigdata/data/uploads/test.txt"), + }, + } + ctx := mockContext.NewMockContext("test", "test") + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := AbsPath(ctx, tt.path) + assert.Equal(t, tt.want, got) + }) + } +}