mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-10 16:42:51 +00:00
Drain the top level internal.
This commit is contained in:
76
api/internal/loadertest/fakeloader.go
Normal file
76
api/internal/loadertest/fakeloader.go
Normal file
@@ -0,0 +1,76 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package loadertest holds a fake for the Loader interface.
|
||||
package loadertest
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"sigs.k8s.io/kustomize/v3/api/filesys"
|
||||
"sigs.k8s.io/kustomize/v3/api/ifc"
|
||||
"sigs.k8s.io/kustomize/v3/api/loader"
|
||||
)
|
||||
|
||||
// FakeLoader encapsulates the delegate Loader and the fake file system.
|
||||
type FakeLoader struct {
|
||||
fs filesys.FileSystem
|
||||
delegate ifc.Loader
|
||||
}
|
||||
|
||||
// NewFakeLoader returns a Loader that uses a fake filesystem.
|
||||
// The loader will be restricted to root only.
|
||||
// The initialDir argument should be an absolute file path.
|
||||
func NewFakeLoader(initialDir string) FakeLoader {
|
||||
return NewFakeLoaderWithRestrictor(
|
||||
loader.RestrictionRootOnly, initialDir)
|
||||
}
|
||||
|
||||
// NewFakeLoaderWithRestrictor returns a Loader that
|
||||
// uses a fake filesystem.
|
||||
// The initialDir argument should be an absolute file path.
|
||||
func NewFakeLoaderWithRestrictor(
|
||||
lr loader.LoadRestrictorFunc, initialDir string) FakeLoader {
|
||||
// Create fake filesystem and inject it into initial Loader.
|
||||
fSys := filesys.MakeFsInMemory()
|
||||
fSys.Mkdir(initialDir)
|
||||
ldr, err := loader.NewLoader(lr, initialDir, fSys)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to make loader: %v", err)
|
||||
}
|
||||
return FakeLoader{fs: fSys, delegate: ldr}
|
||||
}
|
||||
|
||||
// AddFile adds a fake file to the file system.
|
||||
func (f FakeLoader) AddFile(fullFilePath string, content []byte) error {
|
||||
return f.fs.WriteFile(fullFilePath, content)
|
||||
}
|
||||
|
||||
// AddDirectory adds a fake directory to the file system.
|
||||
func (f FakeLoader) AddDirectory(fullDirPath string) error {
|
||||
return f.fs.Mkdir(fullDirPath)
|
||||
}
|
||||
|
||||
// Root delegates.
|
||||
func (f FakeLoader) Root() string {
|
||||
return f.delegate.Root()
|
||||
}
|
||||
|
||||
// New creates a new loader from a new root.
|
||||
func (f FakeLoader) New(newRoot string) (ifc.Loader, error) {
|
||||
l, err := f.delegate.New(newRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return FakeLoader{fs: f.fs, delegate: l}, nil
|
||||
}
|
||||
|
||||
// Load delegates.
|
||||
func (f FakeLoader) Load(location string) ([]byte, error) {
|
||||
return f.delegate.Load(location)
|
||||
}
|
||||
|
||||
// Cleanup delegates.
|
||||
func (f FakeLoader) Cleanup() error {
|
||||
return f.delegate.Cleanup()
|
||||
}
|
||||
Reference in New Issue
Block a user