diff --git a/CHANGELOG.md b/CHANGELOG.md index d022f4c9c..004b63efd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Bugfixes +* Fix issue with relative paths when loading data streams. [#742](https://github.com/elastic/package-registry/pull/742) + ### Added ### Deprecated diff --git a/packages/datastream.go b/packages/datastream.go index 01e5dd20a..7f0296bf3 100644 --- a/packages/datastream.go +++ b/packages/datastream.go @@ -122,7 +122,7 @@ func NewDataStream(basePath string, p *Package) (*DataStream, error) { // Check if manifest exists _, err = fs.Stat(manifestPath) if err != nil && os.IsNotExist(err) { - return nil, errors.Wrapf(err, "manifest does not exist for package: %s", p.BasePath) + return nil, errors.Wrapf(err, "manifest does not exist for data stream: %s", p.BasePath) } dataStreamPath := filepath.Base(basePath) diff --git a/packages/fs.go b/packages/fs.go index def8c9234..0b4e1f49e 100644 --- a/packages/fs.go +++ b/packages/fs.go @@ -49,7 +49,11 @@ func (fs *ExtractedPackageFileSystem) Glob(pattern string) (matches []string, er return } for i := range matches { - matches[i] = matches[i][len(fs.path+string(filepath.Separator)):] + match, err := filepath.Rel(fs.path, matches[i]) + if err != nil { + return nil, fmt.Errorf("failed to obtain path under package root path (%s): %w", fs.path, err) + } + matches[i] = match } return } diff --git a/packages/package.go b/packages/package.go index 66269742e..6eac65624 100644 --- a/packages/package.go +++ b/packages/package.go @@ -511,7 +511,11 @@ func (p *Package) GetDataStreamPaths() ([]string, error) { } for i, _ := range paths { - paths[i] = paths[i][len(dataStreamBasePath)+1:] + relPath, err := filepath.Rel(dataStreamBasePath, paths[i]) + if err != nil { + return nil, fmt.Errorf("failed to get data stream path inside package (%s): %w", dataStreamBasePath, err) + } + paths[i] = relPath } return paths, nil @@ -525,7 +529,6 @@ func (p *Package) LoadDataSets() error { dataStreamsBasePath := "data_stream" for _, dataStreamPath := range dataStreamPaths { - dataStreamBasePath := filepath.Join(dataStreamsBasePath, dataStreamPath) d, err := NewDataStream(dataStreamBasePath, p) diff --git a/packages/package_test.go b/packages/package_test.go index 6e8e8a869..c65c64c1e 100644 --- a/packages/package_test.go +++ b/packages/package_test.go @@ -6,10 +6,12 @@ package packages import ( "log" + "path/filepath" "testing" "github.com/Masterminds/semver/v3" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) var ( @@ -209,6 +211,45 @@ func TestHasKibanaVersion(t *testing.T) { } } +func TestNewPackageFromPath(t *testing.T) { + packagePath := "../testdata/package/reference/1.0.0" + absPath, err := filepath.Abs(packagePath) + require.NoError(t, err) + + cases := []struct { + title string + path string + }{ + { + title: "relative path", + path: packagePath, + }, + { + title: "relative path with slash", + path: packagePath + "/", + }, + { + title: "absolute path", + path: absPath, + }, + { + title: "absolute path with slash", + path: absPath + "/", + }, + } + + fsBuilder := func(p *Package) (PackageFileSystem, error) { + return NewExtractedPackageFileSystem(p) + } + + for _, c := range cases { + t.Run(c.title, func(t *testing.T) { + _, err := NewPackage(c.path, fsBuilder) + assert.NoError(t, err) + }) + } +} + func BenchmarkNewPackage(b *testing.B) { fsBuilder := func(p *Package) (PackageFileSystem, error) { return NewExtractedPackageFileSystem(p)