Skip to content

Commit

Permalink
feat(lib): provide path to abs func
Browse files Browse the repository at this point in the history
Signed-off-by: Jiyong Huang <huangjy@emqx.io>
  • Loading branch information
ngjaying committed Dec 24, 2024
1 parent 5f6f4aa commit 59a814b
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 6 deletions.
14 changes: 9 additions & 5 deletions internal/conf/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
35 changes: 34 additions & 1 deletion internal/conf/path_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -15,9 +15,11 @@
package conf

import (
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -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)
}
})
}
}
28 changes: 28 additions & 0 deletions pkg/path/path.go
Original file line number Diff line number Diff line change
@@ -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)
}
57 changes: 57 additions & 0 deletions pkg/path/path_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit 59a814b

Please sign in to comment.