-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from ycombinator/lib-golang-folder-validation
Golang library for validation: folder structure validation
- Loading branch information
Showing
26 changed files
with
604 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,4 @@ check-spec: | |
|
||
# Runs tests | ||
test: | ||
@go test -v ./... | ||
@go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
package validator | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"regexp" | ||
|
||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
const itemTypeFile = "file" | ||
const itemTypeFolder = "folder" | ||
|
||
type folderSpec struct { | ||
fs http.FileSystem | ||
specPath string | ||
commonSpec | ||
} | ||
|
||
type folderItemSpec struct { | ||
Description string `yaml:"description"` | ||
ItemType string `yaml:"type"` | ||
ContentMediaType string `yaml:"contentMediaType"` | ||
Name string `yaml:"name"` | ||
Pattern string `yaml:"pattern"` | ||
Required bool `yaml:"required"` | ||
Ref string `yaml:"$ref"` | ||
commonSpec `yaml:",inline"` | ||
} | ||
|
||
type commonSpec struct { | ||
AdditionalContents bool `yaml:"additionalContents"` | ||
Contents []folderItemSpec `yaml:"contents"` | ||
} | ||
|
||
func newFolderSpec(fs http.FileSystem, specPath string) (*folderSpec, error) { | ||
specFile, err := fs.Open(specPath) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not open folder specification file") | ||
} | ||
defer specFile.Close() | ||
|
||
data, err := ioutil.ReadAll(specFile) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not read folder specification file") | ||
} | ||
|
||
var wrapper struct { | ||
Spec commonSpec `yaml:"spec"` | ||
} | ||
if err := yaml.Unmarshal(data, &wrapper); err != nil { | ||
return nil, errors.Wrap(err, "could not parse folder specification file") | ||
} | ||
|
||
spec := folderSpec{ | ||
fs: fs, | ||
specPath: specPath, | ||
commonSpec: wrapper.Spec, | ||
} | ||
|
||
return &spec, nil | ||
} | ||
|
||
func (s *folderSpec) validate(folderPath string) ValidationErrors { | ||
var errs ValidationErrors | ||
files, err := ioutil.ReadDir(folderPath) | ||
if err != nil { | ||
errs = append(errs, errors.Wrapf(err, "could not read folder [%s]", folderPath)) | ||
return errs | ||
} | ||
|
||
for _, file := range files { | ||
fileName := file.Name() | ||
itemSpec, err := s.findItemSpec(fileName) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
if itemSpec == nil && s.AdditionalContents { | ||
// No spec found for current folder item but we do allow additional contents in folder. | ||
continue | ||
} | ||
|
||
if itemSpec == nil && !s.AdditionalContents { | ||
// No spec found for current folder item and we do not allow additional contents in folder. | ||
errs = append(errs, fmt.Errorf("item [%s] is not allowed in folder [%s]", fileName, folderPath)) | ||
continue | ||
} | ||
|
||
if file.IsDir() { | ||
if !itemSpec.isSameType(file) { | ||
errs = append(errs, fmt.Errorf("[%s] is a folder but is expected to be a file", fileName)) | ||
continue | ||
} | ||
|
||
if itemSpec.Ref == "" && itemSpec.Contents == nil { | ||
// No recursive validation needed | ||
continue | ||
} | ||
|
||
var subFolderSpec *folderSpec | ||
if itemSpec.Ref != "" { | ||
subFolderSpecPath := path.Join(filepath.Dir(s.specPath), itemSpec.Ref) | ||
subFolderSpec, err = newFolderSpec(s.fs, subFolderSpecPath) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
} else if itemSpec.Contents != nil { | ||
subFolderSpec = &folderSpec{ | ||
fs: s.fs, | ||
specPath: s.specPath, | ||
commonSpec: commonSpec{ | ||
AdditionalContents: itemSpec.AdditionalContents, | ||
Contents: itemSpec.Contents, | ||
}, | ||
} | ||
} | ||
|
||
subFolderPath := path.Join(folderPath, fileName) | ||
subErrs := subFolderSpec.validate(subFolderPath) | ||
if len(subErrs) > 0 { | ||
errs = append(errs, subErrs...) | ||
} | ||
|
||
} else { | ||
if !itemSpec.isSameType(file) { | ||
errs = append(errs, fmt.Errorf("[%s] is a file but is expected to be a folder", fileName)) | ||
continue | ||
} | ||
// TODO: more validation for file item | ||
} | ||
} | ||
|
||
// validate that required items in spec are all accounted for | ||
for _, itemSpec := range s.Contents { | ||
if !itemSpec.Required { | ||
continue | ||
} | ||
|
||
fileFound, err := itemSpec.matchingFileExists(files) | ||
if err != nil { | ||
errs = append(errs, err) | ||
continue | ||
} | ||
|
||
if !fileFound { | ||
var err error | ||
if itemSpec.Name != "" { | ||
err = fmt.Errorf("expecting to find [%s] %s in folder [%s]", itemSpec.Name, itemSpec.ItemType, folderPath) | ||
} else if itemSpec.Pattern != "" { | ||
err = fmt.Errorf("expecting to find %s matching pattern [%s] in folder [%s]", itemSpec.ItemType, itemSpec.Pattern, folderPath) | ||
} | ||
errs = append(errs, err) | ||
} | ||
} | ||
return errs | ||
} | ||
|
||
func (s *folderSpec) findItemSpec(folderItemName string) (*folderItemSpec, error) { | ||
for _, itemSpec := range s.Contents { | ||
if itemSpec.Name != "" && itemSpec.Name == folderItemName { | ||
return &itemSpec, nil | ||
} | ||
if itemSpec.Pattern != "" { | ||
isMatch, err := regexp.MatchString(itemSpec.Pattern, folderItemName) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "invalid folder item spec pattern") | ||
} | ||
if isMatch { | ||
return &itemSpec, nil | ||
} | ||
} | ||
} | ||
|
||
// No item spec found | ||
return nil, nil | ||
} | ||
|
||
func (s *folderItemSpec) matchingFileExists(files []os.FileInfo) (bool, error) { | ||
if s.Name != "" { | ||
for _, file := range files { | ||
if file.Name() == s.Name { | ||
return s.isSameType(file), nil | ||
} | ||
} | ||
} else if s.Pattern != "" { | ||
for _, file := range files { | ||
isMatch, err := regexp.MatchString(s.Pattern, file.Name()) | ||
if err != nil { | ||
return false, errors.Wrap(err, "invalid folder item spec pattern") | ||
} | ||
if isMatch { | ||
return s.isSameType(file), nil | ||
} | ||
} | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func (s *folderItemSpec) isSameType(file os.FileInfo) bool { | ||
switch s.ItemType { | ||
case itemTypeFile: | ||
return !file.IsDir() | ||
case itemTypeFolder: | ||
return file.IsDir() | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package validator | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Package represents an Elastic Package Registry package | ||
type Package struct { | ||
SpecVersion *semver.Version | ||
RootPath string | ||
} | ||
|
||
// NewPackage creates a new Package from a path to the package's root folder | ||
func NewPackage(pkgRootPath string) (*Package, error) { | ||
info, err := os.Stat(pkgRootPath) | ||
if os.IsNotExist(err) { | ||
return nil, errors.Wrapf(err, "no package found at path [%v]", pkgRootPath) | ||
} | ||
|
||
if !info.IsDir() { | ||
return nil, fmt.Errorf("no package folder found at path [%v]", pkgRootPath) | ||
} | ||
|
||
pkgManifestPath := path.Join(pkgRootPath, "manifest.yml") | ||
info, err = os.Stat(pkgManifestPath) | ||
if os.IsNotExist(err) { | ||
return nil, errors.Wrapf(err, "no package manifest file found at path [%v]", pkgManifestPath) | ||
} | ||
|
||
data, err := ioutil.ReadFile(pkgManifestPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not read package manifest file [%v]", pkgManifestPath) | ||
} | ||
|
||
var manifest struct { | ||
SpecVersion string `yaml:"format_version"` | ||
} | ||
if err := yaml.Unmarshal(data, &manifest); err != nil { | ||
return nil, errors.Wrapf(err, "could not parse package manifest file [%v]", pkgManifestPath) | ||
} | ||
|
||
specVersion, err := semver.NewVersion(manifest.SpecVersion) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "could not read specification version from package manifest file [%v]", pkgManifestPath) | ||
} | ||
|
||
// Instantiate Package object and return it | ||
p := Package{ | ||
specVersion, | ||
pkgRootPath, | ||
} | ||
|
||
return &p, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package validator | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewPackage(t *testing.T) { | ||
tests := map[string]struct { | ||
expectedErrContains string | ||
expectedSpecVersion *semver.Version | ||
}{ | ||
"good": { | ||
expectedSpecVersion: semver.MustParse("1.0.4"), | ||
}, | ||
"non_existent": { | ||
expectedErrContains: "no package found at", | ||
}, | ||
"no_manifest": { | ||
expectedErrContains: "no package manifest file found at", | ||
}, | ||
"no_spec_version": { | ||
expectedErrContains: "could not read specification version", | ||
}, | ||
} | ||
|
||
for pkgName, test := range tests { | ||
pkgRootPath := path.Join("test", "packages", pkgName) | ||
pkg, err := NewPackage(pkgRootPath) | ||
if test.expectedErrContains == "" { | ||
require.NoError(t, err) | ||
require.Equal(t, test.expectedSpecVersion, pkg.SpecVersion) | ||
require.Equal(t, pkgRootPath, pkg.RootPath) | ||
} else { | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), test.expectedErrContains) | ||
require.Nil(t, pkg) | ||
} | ||
} | ||
} |
Oops, something went wrong.