From 8bfa3c529d212b3e7f60b8dd2039ff1832652801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Mon, 11 Dec 2023 20:54:27 +0100 Subject: [PATCH] Calculate sourcedir from regular sources, not test ones --- .../pkg/codegen/helpers/generator_test.go | 4 +- .../code-generator/pkg/fs/current/dir.go | 16 ++++++- .../code-generator/pkg/fs/current/file.go | 37 ---------------- .../pkg/fs/current/file_test.go | 42 ------------------- .../k8s.io/code-generator/pkg/fs/rootdir.go | 31 ++++++++++++++ 5 files changed, 47 insertions(+), 83 deletions(-) delete mode 100644 staging/src/k8s.io/code-generator/pkg/fs/current/file.go delete mode 100644 staging/src/k8s.io/code-generator/pkg/fs/current/file_test.go create mode 100644 staging/src/k8s.io/code-generator/pkg/fs/rootdir.go diff --git a/staging/src/k8s.io/code-generator/pkg/codegen/helpers/generator_test.go b/staging/src/k8s.io/code-generator/pkg/codegen/helpers/generator_test.go index a8fc011c0136f..41a9d5debcfe1 100644 --- a/staging/src/k8s.io/code-generator/pkg/codegen/helpers/generator_test.go +++ b/staging/src/k8s.io/code-generator/pkg/codegen/helpers/generator_test.go @@ -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" @@ -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 { diff --git a/staging/src/k8s.io/code-generator/pkg/fs/current/dir.go b/staging/src/k8s.io/code-generator/pkg/fs/current/dir.go index 9e7b9b6aa3448..0f62d0b0e72bd 100644 --- a/staging/src/k8s.io/code-generator/pkg/fs/current/dir.go +++ b/staging/src/k8s.io/code-generator/pkg/fs/current/dir.go @@ -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) { @@ -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 +} diff --git a/staging/src/k8s.io/code-generator/pkg/fs/current/file.go b/staging/src/k8s.io/code-generator/pkg/fs/current/file.go deleted file mode 100644 index 6cb3c6a630d7e..0000000000000 --- a/staging/src/k8s.io/code-generator/pkg/fs/current/file.go +++ /dev/null @@ -1,37 +0,0 @@ -/* -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 current - -import ( - "errors" - "runtime" -) - -var ErrCantGetCurrentFilename = errors.New("unable to get the current filename") - -// File is the __filename equivalent -func File() (string, error) { - return file(2) -} - -func file(skip int) (string, error) { - _, filename, _, ok := runtime.Caller(skip) - if !ok { - return "", ErrCantGetCurrentFilename - } - return filename, nil -} diff --git a/staging/src/k8s.io/code-generator/pkg/fs/current/file_test.go b/staging/src/k8s.io/code-generator/pkg/fs/current/file_test.go deleted file mode 100644 index 4b7d9a72d8fb6..0000000000000 --- a/staging/src/k8s.io/code-generator/pkg/fs/current/file_test.go +++ /dev/null @@ -1,42 +0,0 @@ -/* -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 current - -import ( - "os" - "strings" - "testing" -) - -func TestFile(t *testing.T) { - t.Parallel() - got, err := File() - if err != nil { - t.Errorf("no error expected, got %v", err) - } - want := "pkg/fs/current/file_test.go" - if !strings.Contains(got, want) { - t.Errorf("Wanted %#v in %#v, but not found", want, got) - } - st, ferr := os.Stat(got) - if ferr != nil { - t.Errorf("no error expected, got %v", ferr) - } - if st.IsDir() { - t.Errorf("Wanted %#v to be a file", got) - } -} diff --git a/staging/src/k8s.io/code-generator/pkg/fs/rootdir.go b/staging/src/k8s.io/code-generator/pkg/fs/rootdir.go new file mode 100644 index 0000000000000..1115793d03daa --- /dev/null +++ b/staging/src/k8s.io/code-generator/pkg/fs/rootdir.go @@ -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 + } +}