1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-04-11 11:41:53 +02:00

Introduce chmod in file utils (#1808)

This commit is contained in:
Marcus Holl 2020-07-16 14:25:01 +02:00 committed by GitHub
parent fef38cec0a
commit 8af0540de2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 71 additions and 6 deletions

View File

@ -70,6 +70,10 @@ func (f *kanikoFileMock) MkdirAll(path string, perm os.FileMode) error {
return nil
}
func (f *kanikoFileMock) Chmod(path string, mode os.FileMode) error {
return fmt.Errorf("not implemented. func is only present in order to fullfil the interface contract. Needs to be ajusted in case it gets used.")
}
func TestRunKanikoExecute(t *testing.T) {
t.Run("success case", func(t *testing.T) {

View File

@ -5,6 +5,7 @@ import (
"os"
"testing"
"fmt"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/maven"
"github.com/SAP/jenkins-library/pkg/mock"
@ -366,6 +367,10 @@ func (f *MtaTestFileUtilsMock) MkdirAll(path string, perm os.FileMode) error {
return nil
}
func (f *MtaTestFileUtilsMock) Chmod(path string, mode os.FileMode) error {
return fmt.Errorf("not implemented. func is only present in order to fullfil the interface contract. Needs to be ajusted in case it gets used.")
}
func newNpmExecutor(execRunner *mock.ExecMockRunner) *npm.Execute {
utils := newNpmMockUtilsBundle()
utils.execRunner = execRunner

View File

@ -38,6 +38,10 @@ func (f *FileUtilsMock) MkdirAll(path string, perm os.FileMode) error {
return nil
}
func (f *FileUtilsMock) Chmod(path string, mode os.FileMode) error {
return fmt.Errorf("not implemented. func is only present in order to fullfil the interface contract. Needs to be ajusted in case it gets used.")
}
func TestDeploy(t *testing.T) {
myXsDeployOptions := xsDeployOptions{
APIURL: "https://example.org:12345",

View File

@ -200,3 +200,7 @@ func (f *fileUtilsMock) FileWrite(path string, content []byte, perm os.FileMode)
func (f *fileUtilsMock) MkdirAll(path string, perm os.FileMode) error {
return nil
}
func (f *fileUtilsMock) Chmod(path string, mode os.FileMode) error {
return fmt.Errorf("not implemented. func is only present in order to fullfil the interface contract. Needs to be ajusted in case it gets used.")
}

View File

@ -301,3 +301,13 @@ func (f *FilesMock) Stat(name string) (os.FileInfo, error) {
isDir: props.content == &dirContent,
}, nil
}
//Chmod ...
func (f *FilesMock) Chmod(path string, mode os.FileMode) error {
props, exists := f.files[f.toAbsPath(path)]
if !exists {
return fmt.Errorf("chmod: %s: No such file or directory", path)
}
props.mode = &mode
return nil
}

View File

@ -243,13 +243,14 @@ func TestFilesMockGlob(t *testing.T) {
})
}
func TestStat(t *testing.T) {
var (
onlyMe os.FileMode = 0700
othersCanRead os.FileMode = 0644
othersCanReadAndExecute os.FileMode = 0755
everybodyCanReadAndExecute os.FileMode = 0777
)
var (
onlyMe os.FileMode = 0700
othersCanRead os.FileMode = 0644
othersCanReadAndExecute os.FileMode = 0755
)
func TestStat(t *testing.T) {
files := FilesMock{}
files.AddFile("tmp/dummy.txt", []byte("Hello SAP"))
@ -288,3 +289,34 @@ func TestStat(t *testing.T) {
}
})
}
func TestGetChod(t *testing.T) {
files := FilesMock{}
files.AddDirWithMode("tmp", 0777)
files.AddFileWithMode("tmp/log.txt", []byte("build failed"), 0777)
t.Run("non existing file", func(t *testing.T) {
err := files.Chmod("does/not/exist", 0400)
assert.EqualError(t, err, "chmod: does/not/exist: No such file or directory")
})
t.Run("chmod for file", func(t *testing.T) {
err := files.Chmod("tmp/log.txt", 0644)
if assert.NoError(t, err) {
info, e := files.Stat("tmp/log.txt")
if assert.NoError(t, e) {
assert.Equal(t, othersCanRead, info.Mode())
}
}
})
t.Run("chmod for directory", func(t *testing.T) {
err := files.Chmod("tmp", 0755)
if assert.NoError(t, err) {
info, e := files.Stat("tmp")
if assert.NoError(t, e) {
assert.Equal(t, othersCanReadAndExecute, info.Mode())
}
}
})
}

View File

@ -19,6 +19,7 @@ type FileUtils interface {
FileRead(path string) ([]byte, error)
FileWrite(path string, content []byte, perm os.FileMode) error
MkdirAll(path string, perm os.FileMode) error
Chmod(path string, mode os.FileMode) error
}
// Files ...
@ -86,6 +87,11 @@ func (f Files) Copy(src, dst string) (int64, error) {
return nBytes, err
}
//Chmod ...
func (f Files) Chmod(path string, mode os.FileMode) error {
return os.Chmod(path, mode)
}
// Unzip will decompress a zip archive, moving all files and folders
// within the zip file (parameter 1) to an output directory (parameter 2).
// from https://golangcode.com/unzip-files-in-go/ with the following license: