Skip to content

Commit

Permalink
Calculate sourcedir from regular sources, not test ones
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jan 19, 2024
1 parent 5c27b82 commit 8bfa3c5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
goflag "flag"
"k8s.io/code-generator/pkg/codegen/helpers"
"k8s.io/code-generator/pkg/fs"
"k8s.io/code-generator/pkg/fs/current"
"k8s.io/klog/v2"
"k8s.io/klog/v2/ktesting"
klogtest "k8s.io/klog/v2/test"
Expand Down Expand Up @@ -69,10 +68,9 @@ func TestGenerate(t *testing.T) {

func prepareWorkdir(tb testing.TB) (string, string) {
var sourcedir string
if s, err := current.Dir(); err != nil {
if root, err := fs.RootDir(); err != nil {
tb.Fatal(err)
} else {
root := path.Dir(path.Dir(path.Dir(s)))
sourcedir = path.Join(root, "examples")
}
if _, err := os.Stat(sourcedir); err != nil {
Expand Down
16 changes: 15 additions & 1 deletion staging/src/k8s.io/code-generator/pkg/fs/current/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ limitations under the License.

package current

import "path/filepath"
import (
"errors"
"path/filepath"
"runtime"
)

// Dir is the __dirname equivalent.
func Dir() (string, error) {
Expand All @@ -26,3 +30,13 @@ func Dir() (string, error) {
}
return filepath.Dir(filename), nil
}

var ErrCantGetCurrentFilename = errors.New("unable to get the current filename")

func file(skip int) (string, error) {
_, filename, _, ok := runtime.Caller(skip)
if !ok {
return "", ErrCantGetCurrentFilename
}
return filename, nil
}
37 changes: 0 additions & 37 deletions staging/src/k8s.io/code-generator/pkg/fs/current/file.go

This file was deleted.

42 changes: 0 additions & 42 deletions staging/src/k8s.io/code-generator/pkg/fs/current/file_test.go

This file was deleted.

31 changes: 31 additions & 0 deletions staging/src/k8s.io/code-generator/pkg/fs/rootdir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2023 The Kubernetes 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
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 fs

import (
"k8s.io/code-generator/pkg/fs/current"
"path"
)

// RootDir returns the root directory of the code-generator repository.
func RootDir() (string, error) {
if c, err := current.Dir(); err != nil {
return "", err
} else {
return path.Dir(path.Dir(c)), nil
}
}

0 comments on commit 8bfa3c5

Please sign in to comment.