mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-28 05:47:08 +02:00
Introduce stat command into Files utils and correspoding mock (#1721)
Introduce stat command into Files utils and correspoding mock
This commit is contained in:
parent
7f69f4eb16
commit
49d4f6269a
@ -6,13 +6,29 @@ import (
|
||||
"fmt"
|
||||
"github.com/bmatcuk/doublestar"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var dirContent []byte
|
||||
|
||||
type fileInfoMock struct {
|
||||
name string
|
||||
mode os.FileMode
|
||||
size int64
|
||||
isDir bool
|
||||
}
|
||||
|
||||
func (fInfo fileInfoMock) Name() string { return fInfo.name }
|
||||
func (fInfo fileInfoMock) Size() int64 { return fInfo.size }
|
||||
func (fInfo fileInfoMock) Mode() os.FileMode { return fInfo.mode }
|
||||
func (fInfo fileInfoMock) ModTime() time.Time { return time.Time{} }
|
||||
func (fInfo fileInfoMock) IsDir() bool { return fInfo.isDir }
|
||||
func (fInfo fileInfoMock) Sys() interface{} { return nil }
|
||||
|
||||
type fileProperties struct {
|
||||
content *[]byte
|
||||
mode *os.FileMode
|
||||
@ -250,3 +266,38 @@ func (f *FilesMock) Chdir(path string) error {
|
||||
f.currentDir = strings.TrimLeft(path, f.Separator)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stat ...
|
||||
func (f *FilesMock) Stat(name string) (os.FileInfo, error) {
|
||||
|
||||
props, exists := f.files[f.toAbsPath(name)]
|
||||
|
||||
if !exists {
|
||||
|
||||
isDir, err := f.DirExists(f.toAbsPath(name))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Internal error inside mock: %w", err)
|
||||
}
|
||||
if !isDir {
|
||||
return nil, &os.PathError{
|
||||
Op: "stat",
|
||||
Path: name,
|
||||
Err: fmt.Errorf("no such file or directory"),
|
||||
}
|
||||
}
|
||||
|
||||
// we assume some default // in the free wild wia umask
|
||||
var mode os.FileMode = 0755
|
||||
|
||||
props = &fileProperties{}
|
||||
props.mode = &mode
|
||||
props.content = &dirContent
|
||||
}
|
||||
|
||||
return fileInfoMock{
|
||||
name: path.Base(name),
|
||||
mode: *props.mode,
|
||||
size: int64(len(*props.content)),
|
||||
isDir: props.content == &dirContent,
|
||||
}, nil
|
||||
}
|
||||
|
@ -242,3 +242,49 @@ func TestFilesMockGlob(t *testing.T) {
|
||||
assert.Len(t, matches, 0)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStat(t *testing.T) {
|
||||
|
||||
var (
|
||||
onlyMe os.FileMode = 0700
|
||||
othersCanRead os.FileMode = 0644
|
||||
othersCanReadAndExecute os.FileMode = 0755
|
||||
)
|
||||
|
||||
files := FilesMock{}
|
||||
files.AddFile("tmp/dummy.txt", []byte("Hello SAP"))
|
||||
files.AddDirWithMode("bin", 0700)
|
||||
|
||||
t.Run("non existing file", func(t *testing.T) {
|
||||
_, err := files.Stat("doesNotExist.txt")
|
||||
assert.EqualError(t, err, "stat doesNotExist.txt: no such file or directory")
|
||||
})
|
||||
|
||||
t.Run("check file info", func(t *testing.T) {
|
||||
info, err := files.Stat("tmp/dummy.txt")
|
||||
|
||||
if assert.NoError(t, err) {
|
||||
// only the base name is returned.
|
||||
assert.Equal(t, "dummy.txt", info.Name())
|
||||
assert.False(t, info.IsDir())
|
||||
// if not specified otherwise the 644 file mode is used.
|
||||
assert.Equal(t, othersCanRead, info.Mode())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("check implicit dir", func(t *testing.T) {
|
||||
info, err := files.Stat("tmp")
|
||||
if assert.NoError(t, err) {
|
||||
assert.True(t, info.IsDir())
|
||||
assert.Equal(t, othersCanReadAndExecute, info.Mode())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("check explicit dir", func(t *testing.T) {
|
||||
info, err := files.Stat("bin")
|
||||
if assert.NoError(t, err) {
|
||||
assert.True(t, info.IsDir())
|
||||
assert.Equal(t, onlyMe, info.Mode())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -205,3 +205,8 @@ func (f Files) Getwd() (string, error) {
|
||||
func (f Files) Chdir(path string) error {
|
||||
return os.Chdir(path)
|
||||
}
|
||||
|
||||
// Stat is a wrapper for os.Stat()
|
||||
func (f Files) Stat(path string) (os.FileInfo, error) {
|
||||
return os.Stat(path)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user