mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
ffc931aad1
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files. * Added unit build tag to all unit test files. * added to new unit test unit build tag * Update verify-go.yml * small fix --------- Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com> Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
168 lines
3.9 KiB
Go
168 lines
3.9 KiB
Go
//go:build unit
|
|
// +build unit
|
|
|
|
package npm
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
filesmock "github.com/SAP/jenkins-library/pkg/mock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
)
|
|
|
|
func TestNewNPMRC(t *testing.T) {
|
|
type args struct {
|
|
path string
|
|
}
|
|
type want struct {
|
|
path string
|
|
errLoad string
|
|
errWrite string
|
|
files map[string]string
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
files map[string]string
|
|
want want
|
|
}{
|
|
{
|
|
name: "success - path pointing to no dir - no content",
|
|
args: args{""},
|
|
files: map[string]string{
|
|
defaultConfigFilename: "",
|
|
},
|
|
want: want{
|
|
path: defaultConfigFilename,
|
|
files: map[string]string{
|
|
defaultConfigFilename: "",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "success - path pointing to cwd - no content",
|
|
args: args{"."},
|
|
files: map[string]string{
|
|
defaultConfigFilename: "",
|
|
},
|
|
want: want{
|
|
path: defaultConfigFilename,
|
|
files: map[string]string{
|
|
defaultConfigFilename: "",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "success - path pointing to sub dir - no content",
|
|
args: args{mock.Anything},
|
|
files: map[string]string{
|
|
filepath.Join(mock.Anything, ".piperNpmrc"): "",
|
|
},
|
|
want: want{
|
|
path: filepath.Join(mock.Anything, ".piperNpmrc"),
|
|
files: map[string]string{
|
|
filepath.Join(mock.Anything, ".piperNpmrc"): "",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "success - path pointing to file in current folder - no content",
|
|
args: args{".piperNpmrc"},
|
|
files: map[string]string{
|
|
".piperNpmrc": "",
|
|
},
|
|
want: want{
|
|
path: ".piperNpmrc",
|
|
files: map[string]string{
|
|
".piperNpmrc": "",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "success - path pointing to file in sub folder - no content",
|
|
args: args{filepath.Join(mock.Anything, ".piperNpmrc")},
|
|
files: map[string]string{
|
|
filepath.Join(mock.Anything, ".piperNpmrc"): "",
|
|
},
|
|
want: want{
|
|
path: filepath.Join(mock.Anything, ".piperNpmrc"),
|
|
files: map[string]string{
|
|
filepath.Join(mock.Anything, ".piperNpmrc"): "",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "success - doesn't modify existing content",
|
|
args: args{filepath.Join(mock.Anything, ".piperNpmrc")},
|
|
files: map[string]string{
|
|
filepath.Join(mock.Anything, ".piperNpmrc"): `
|
|
_auth=dGVzdDp0ZXN0
|
|
registry=https://my.private.registry/
|
|
@piper:registry=https://my.scoped.private.registry/
|
|
//my.private.registry/:_auth=dGVzdDp0ZXN0
|
|
//my.scoped.private.registry/:_auth=dGVzdDp0ZXN0
|
|
`,
|
|
},
|
|
want: want{
|
|
path: filepath.Join(mock.Anything, ".piperNpmrc"),
|
|
files: map[string]string{
|
|
filepath.Join(mock.Anything, ".piperNpmrc"): `
|
|
_auth=dGVzdDp0ZXN0
|
|
registry=https://my.private.registry/
|
|
@piper:registry=https://my.scoped.private.registry/
|
|
//my.private.registry/:_auth=dGVzdDp0ZXN0
|
|
//my.scoped.private.registry/:_auth=dGVzdDp0ZXN0
|
|
`,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "failure - path pointing to sth which doesnt exist",
|
|
args: args{"./this/path/doesnt/exist/.piperNpmrc"},
|
|
files: map[string]string{},
|
|
want: want{
|
|
path: "./this/path/doesnt/exist/.piperNpmrc",
|
|
errLoad: "could not read './this/path/doesnt/exist/.piperNpmrc'",
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
files := filesmock.FilesMock{}
|
|
|
|
for path, content := range tt.files {
|
|
files.AddFile(path, []byte(content))
|
|
}
|
|
|
|
propertiesLoadFile = files.FileRead
|
|
propertiesWriteFile = files.FileWrite
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
uut := NewNPMRC(tt.args.path)
|
|
|
|
assert.Equal(t, tt.want.path, uut.filepath)
|
|
|
|
if err := uut.Load(); len(tt.want.errLoad) == 0 {
|
|
assert.NoError(t, err)
|
|
} else {
|
|
assert.EqualError(t, err, tt.want.errLoad)
|
|
}
|
|
|
|
if err := uut.Write(); len(tt.want.errWrite) == 0 {
|
|
assert.NoError(t, err)
|
|
} else {
|
|
assert.EqualError(t, err, tt.want.errWrite)
|
|
}
|
|
|
|
for wantFile, wantContent := range tt.want.files {
|
|
if actualContent, err := files.FileRead(wantFile); assert.NoError(t, err) {
|
|
assert.Equal(t, wantContent, string(actualContent))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|