Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions foo/caller_not_allowed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2020 The Knative Authors

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

https://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 foo_test

import (
"errors"
"testing"

"knative.dev/hack/shell"
)

func TestCallerNotAllowed(t *testing.T) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it just needs the deprecated comment, this test will be removed in the vendoring process.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course test will be removed.

But, there's a validation added to shell/project.go that will return error in used from not allowed location

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW: Using deprecated comments will upset the linter.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW: Using deprecated comments will upset the linter.

That is the point. You have to opt-in to using the deprecated code

_, err := shell.NewProjectLocation("..")
if !errors.Is(err, shell.ErrCallerNotAllowed) {
t.Error("usage should be blocked")
}
}
32 changes: 30 additions & 2 deletions shell/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ package shell

import (
"errors"
"fmt"
"path"
"regexp"
"runtime"
)

// ErrCantGetCaller is raised when we can't calculate a caller of NewProjectLocation.
var ErrCantGetCaller = errors.New("can't get caller")
var (
// ErrCantGetCaller is raised when we can't calculate a caller of NewProjectLocation.
ErrCantGetCaller = errors.New("can't get caller")

// ErrCallerNotAllowed is raised when user tries to use this shell-out package
// outside of allowed places. This package is deprecated from start and was
// introduced to allow rewriting of shell code to Golang in small chunks.
ErrCallerNotAllowed = errors.New("don't try use knative.dev/hack/shell package outside of allowed places")
)

// NewProjectLocation creates a ProjectLocation that is used to calculate
// relative paths within the project.
Expand All @@ -32,6 +41,10 @@ func NewProjectLocation(pathToRoot string) (ProjectLocation, error) {
if !ok {
return nil, ErrCantGetCaller
}
err := ensureIsValid(filename)
if err != nil {
return nil, err
}
return &callerLocation{
caller: filename,
pathToRoot: pathToRoot,
Expand All @@ -50,3 +63,18 @@ type callerLocation struct {
caller string
pathToRoot string
}

func ensureIsValid(filename string) error {
validPaths := []string{
"knative.+/test/upgrade/",
"knative(:?\\.dev/|-)hack/shell/",
}
for _, validPath := range validPaths {
r := regexp.MustCompile(validPath)
if loc := r.FindStringIndex(filename); loc != nil {
return nil
}
}
return fmt.Errorf("%w, tried using from: %s",
ErrCallerNotAllowed, filename)
}