mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 00:52:55 +00:00
Isolated content of pkg/kustomize
This commit is contained in:
74
pkg/util/fs/fakefile.go
Normal file
74
pkg/util/fs/fakefile.go
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
Copyright 2018 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 (
|
||||
"bytes"
|
||||
"os"
|
||||
)
|
||||
|
||||
var _ File = &FakeFile{}
|
||||
|
||||
// FakeFile implements File in-memory for tests.
|
||||
type FakeFile struct {
|
||||
name string
|
||||
content []byte
|
||||
dir bool
|
||||
open bool
|
||||
}
|
||||
|
||||
// makeFile makes a fake file.
|
||||
func makeFile() *FakeFile {
|
||||
return &FakeFile{}
|
||||
}
|
||||
|
||||
// makeDir makes a fake directory.
|
||||
func makeDir(name string) *FakeFile {
|
||||
return &FakeFile{name: name, dir: true}
|
||||
}
|
||||
|
||||
// Close marks the fake file closed.
|
||||
func (f *FakeFile) Close() error {
|
||||
f.open = false
|
||||
return nil
|
||||
}
|
||||
|
||||
// Read never fails, and doesn't mutate p.
|
||||
func (f *FakeFile) Read(p []byte) (n int, err error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// Write saves the contents of the argument to memory.
|
||||
func (f *FakeFile) Write(p []byte) (n int, err error) {
|
||||
f.content = p
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// ContentMatches returns true if v matches fake file's content.
|
||||
func (f *FakeFile) ContentMatches(v []byte) bool {
|
||||
return bytes.Equal(v, f.content)
|
||||
}
|
||||
|
||||
// GetContent the content of a fake file.
|
||||
func (f *FakeFile) GetContent() []byte {
|
||||
return f.content
|
||||
}
|
||||
|
||||
// Stat returns nil.
|
||||
func (f *FakeFile) Stat() (os.FileInfo, error) {
|
||||
return nil, nil
|
||||
}
|
||||
47
pkg/util/fs/fakefileinfo.go
Normal file
47
pkg/util/fs/fakefileinfo.go
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
Copyright 2018 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 (
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var _ os.FileInfo = &Fakefileinfo{}
|
||||
|
||||
// Fakefileinfo implements Fakefileinfo using a fake in-memory filesystem.
|
||||
type Fakefileinfo struct {
|
||||
*FakeFile
|
||||
}
|
||||
|
||||
// Name returns the name of the file
|
||||
func (fi *Fakefileinfo) Name() string { return fi.name }
|
||||
|
||||
// Size returns the size of the file
|
||||
func (fi *Fakefileinfo) Size() int64 { return int64(len(fi.content)) }
|
||||
|
||||
// Mode returns the file mode
|
||||
func (fi *Fakefileinfo) Mode() os.FileMode { return 0777 }
|
||||
|
||||
// ModTime returns the modification time
|
||||
func (fi *Fakefileinfo) ModTime() time.Time { return time.Time{} }
|
||||
|
||||
// IsDir returns if it is a directory
|
||||
func (fi *Fakefileinfo) IsDir() bool { return fi.dir }
|
||||
|
||||
// Sys should return underlying data source, but it now returns nil
|
||||
func (fi *Fakefileinfo) Sys() interface{} { return nil }
|
||||
80
pkg/util/fs/fakefs.go
Normal file
80
pkg/util/fs/fakefs.go
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
Copyright 2018 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 (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var _ FileSystem = &FakeFS{}
|
||||
|
||||
// FakeFS implements FileSystem using a fake in-memory filesystem.
|
||||
type FakeFS struct {
|
||||
m map[string]*FakeFile
|
||||
}
|
||||
|
||||
// MakeFakeFS returns an instance of FakeFS with no files in it.
|
||||
func MakeFakeFS() *FakeFS {
|
||||
return &FakeFS{m: map[string]*FakeFile{}}
|
||||
}
|
||||
|
||||
// Create assures a fake file appears in the in-memory file system.
|
||||
func (fs *FakeFS) Create(name string) (File, error) {
|
||||
f := &FakeFile{}
|
||||
f.open = true
|
||||
fs.m[name] = f
|
||||
return fs.m[name], nil
|
||||
}
|
||||
|
||||
// Mkdir assures a fake directory appears in the in-memory file system.
|
||||
func (fs *FakeFS) Mkdir(name string, perm os.FileMode) error {
|
||||
fs.m[name] = makeDir(name)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Open returns a fake file in the open state.
|
||||
func (fs *FakeFS) Open(name string) (File, error) {
|
||||
if _, found := fs.m[name]; !found {
|
||||
return nil, fmt.Errorf("file %q cannot be opened", name)
|
||||
}
|
||||
return fs.m[name], nil
|
||||
}
|
||||
|
||||
// Stat always returns nil FileInfo, and returns an error if file does not exist.
|
||||
func (fs *FakeFS) Stat(name string) (os.FileInfo, error) {
|
||||
if f, found := fs.m[name]; found {
|
||||
return &Fakefileinfo{f}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("file %q does not exist", name)
|
||||
}
|
||||
|
||||
// ReadFile always returns an empty bytes and error depending on content of m.
|
||||
func (fs *FakeFS) ReadFile(name string) ([]byte, error) {
|
||||
if ff, found := fs.m[name]; found {
|
||||
return ff.content, nil
|
||||
}
|
||||
return nil, fmt.Errorf("cannot read file %q", name)
|
||||
}
|
||||
|
||||
// WriteFile always succeeds and does nothing.
|
||||
func (fs *FakeFS) WriteFile(name string, c []byte) error {
|
||||
ff := &FakeFile{}
|
||||
ff.Write(c)
|
||||
fs.m[name] = ff
|
||||
return nil
|
||||
}
|
||||
105
pkg/util/fs/fakefs_test.go
Normal file
105
pkg/util/fs/fakefs_test.go
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
Copyright 2018 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 (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStatNotExist(t *testing.T) {
|
||||
x := MakeFakeFS()
|
||||
info, err := x.Stat("foo")
|
||||
if info != nil {
|
||||
t.Fatalf("expected nil info")
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStat(t *testing.T) {
|
||||
x := MakeFakeFS()
|
||||
expectedName := "my-dir"
|
||||
err := x.Mkdir(expectedName, 0666)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
info, err := x.Stat(expectedName)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
name := info.Name()
|
||||
if name != expectedName {
|
||||
t.Fatalf("expected %v but got %v", expectedName, name)
|
||||
}
|
||||
if !info.IsDir() {
|
||||
t.Fatalf("expected IsDir() return true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
x := MakeFakeFS()
|
||||
f, err := x.Create("foo")
|
||||
if f == nil {
|
||||
t.Fatalf("expected file")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error")
|
||||
}
|
||||
info, err := x.Stat("foo")
|
||||
if info == nil {
|
||||
t.Fatalf("expected non-nil info")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFile(t *testing.T) {
|
||||
x := MakeFakeFS()
|
||||
f, err := x.Create("foo")
|
||||
if f == nil {
|
||||
t.Fatalf("expected file")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error")
|
||||
}
|
||||
content, err := x.ReadFile("foo")
|
||||
if len(content) != 0 {
|
||||
t.Fatalf("expected no content")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFile(t *testing.T) {
|
||||
x := MakeFakeFS()
|
||||
c := []byte("heybuddy")
|
||||
err := x.WriteFile("foo", c)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error")
|
||||
}
|
||||
content, err := x.ReadFile("foo")
|
||||
if err != nil {
|
||||
t.Fatalf("expected read to work: %v", err)
|
||||
}
|
||||
if bytes.Compare(c, content) != 0 {
|
||||
t.Fatalf("incorrect content: %v", content)
|
||||
}
|
||||
}
|
||||
38
pkg/util/fs/fs.go
Normal file
38
pkg/util/fs/fs.go
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
Copyright 2018 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 (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// FileSystem groups basic os filesystem methods.
|
||||
type FileSystem interface {
|
||||
Create(name string) (File, error)
|
||||
Mkdir(name string, perm os.FileMode) error
|
||||
Open(name string) (File, error)
|
||||
Stat(name string) (os.FileInfo, error)
|
||||
ReadFile(name string) ([]byte, error)
|
||||
WriteFile(name string, data []byte) error
|
||||
}
|
||||
|
||||
// File groups the basic os.File methods.
|
||||
type File interface {
|
||||
io.ReadWriteCloser
|
||||
Stat() (os.FileInfo, error)
|
||||
}
|
||||
49
pkg/util/fs/realfile.go
Normal file
49
pkg/util/fs/realfile.go
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
Copyright 2018 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 (
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
var _ File = &realFile{}
|
||||
|
||||
// realFile implements File using the local filesystem.
|
||||
type realFile struct {
|
||||
file *os.File
|
||||
}
|
||||
|
||||
// MakeRealFile makes an instance of realFile.
|
||||
func MakeRealFile(f *os.File) (File, error) {
|
||||
if f == nil {
|
||||
return nil, errors.New("file argument may not be nil")
|
||||
}
|
||||
return &realFile{file: f}, nil
|
||||
}
|
||||
|
||||
// Close closes a file.
|
||||
func (f *realFile) Close() error { return f.file.Close() }
|
||||
|
||||
// Read reads a file's content.
|
||||
func (f *realFile) Read(p []byte) (n int, err error) { return f.file.Read(p) }
|
||||
|
||||
// Write writes bytes to a file
|
||||
func (f *realFile) Write(p []byte) (n int, err error) { return f.file.Write(p) }
|
||||
|
||||
// Stat returns an interface which has all the information regarding the file.
|
||||
func (f *realFile) Stat() (os.FileInfo, error) { return f.file.Stat() }
|
||||
52
pkg/util/fs/realfs.go
Normal file
52
pkg/util/fs/realfs.go
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
Copyright 2018 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 (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
var _ FileSystem = realFS{}
|
||||
|
||||
// realFS implements FileSystem using the local filesystem.
|
||||
type realFS struct{}
|
||||
|
||||
// MakeRealFS makes an instance of realFS.
|
||||
func MakeRealFS() FileSystem {
|
||||
return realFS{}
|
||||
}
|
||||
|
||||
// Create delegates to os.Create.
|
||||
func (realFS) Create(name string) (File, error) { return os.Create(name) }
|
||||
|
||||
// Mkdir delegates to os.Mkdir.
|
||||
func (realFS) Mkdir(name string, perm os.FileMode) error { return os.Mkdir(name, perm) }
|
||||
|
||||
// Open delegates to os.Open.
|
||||
func (realFS) Open(name string) (File, error) { return os.Open(name) }
|
||||
|
||||
// Stat delegates to os.Stat.
|
||||
func (realFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }
|
||||
|
||||
// ReadFile delegates to ioutil.ReadFile.
|
||||
func (realFS) ReadFile(name string) ([]byte, error) { return ioutil.ReadFile(name) }
|
||||
|
||||
// WriteFile delegates to ioutil.WriteFile with read/write permissions.
|
||||
func (realFS) WriteFile(name string, c []byte) error {
|
||||
return ioutil.WriteFile(name, c, 0666)
|
||||
}
|
||||
Reference in New Issue
Block a user