Skip to content

Commit

Permalink
remove glob chars from spectral target filename (#1165)
Browse files Browse the repository at this point in the history
* remove glob chars from spectral target filename

* check for spectral cmd
  • Loading branch information
theganyo authored May 4, 2023
1 parent b04c507 commit ce5d8c3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/registry/cmd/compute/lint/lint-openapi-spectral.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"

"github.com/apigee/registry/pkg/application/style"
)
Expand All @@ -42,7 +43,17 @@ type SpectralLintLocation struct {
Character int32 `json:"character"`
}

var regexpSpecialCharacters = regexp.MustCompile(`[\!\.\+\*\?\^\$\(\)\[\]\{\}\|\\]`)

func lintFileForOpenAPIWithSpectral(path string, root string) (*style.LintFile, error) {
cleanpath := regexpSpecialCharacters.ReplaceAllString(path, "_")
if cleanpath != path {
err := os.Rename(filepath.Join(root, path), filepath.Join(root, cleanpath))
if err != nil {
return nil, err
}
path = cleanpath
}
cmd := exec.Command("spectral", "lint", path, "--f", "json", "--output", "spectral-lint.json")
cmd.Dir = root
// ignore errors from Spectral because Spectral returns an error result when APIs have errors.
Expand Down
68 changes: 68 additions & 0 deletions cmd/registry/cmd/compute/lint/lint-openapi-spectral_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2023 Google LLC
//
// 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 lint

import (
"os"
"os/exec"
"path/filepath"
"testing"
)

func TestSpectralFilenameWithGlobs(t *testing.T) {
path, err := exec.LookPath("spectral")
if path == "" || err != nil {
t.Skip("spectral not present, skipping check")
}

tests := []struct {
testName string
fileName string
}{
{"noglobs", "name.yaml"},
{"replacement", "name (1|2).yaml"},
{"sequence", "name [[:digit:]].yaml"},
{"expansion", "name {1..3}.yaml"},
{"dollar", "name$.yaml"},
{"question", "name?.yaml"},
{"asterik", "name*.yaml"},
{"negation", "!name.yaml"},
}

for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
root, err := os.MkdirTemp("", "registry-openapi-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(root)
if err := os.WriteFile(filepath.Join(root, ".spectral.yaml"), []byte(`extends: ["spectral:oas", "spectral:asyncapi"]`), 0644); err != nil {
t.Fatal(err)
}

if err := os.WriteFile(filepath.Join(root, test.fileName), []byte("not openapi"), 0644); err != nil {
t.Fatal(err)
}

lint, err := lintFileForOpenAPIWithSpectral(test.fileName, root)
if err != nil {
t.Fatal(err)
}
if len(lint.Problems) != 1 {
t.Error("should've gotten an error")
}
})
}
}

0 comments on commit ce5d8c3

Please sign in to comment.