mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-16 11:09:33 +02:00
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
|
package npm
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/magiconair/properties"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"github.com/stretchr/testify/mock"
|
||
|
"github.com/stretchr/testify/require"
|
||
|
)
|
||
|
|
||
|
func TestNewNPMRC(t *testing.T) {
|
||
|
type args struct {
|
||
|
path string
|
||
|
}
|
||
|
tests := []struct {
|
||
|
name string
|
||
|
args args
|
||
|
want string
|
||
|
}{
|
||
|
{name: "current dir", args: args{""}, want: configFilename},
|
||
|
{name: "sub dir", args: args{mock.Anything}, want: mock.Anything + "/.npmrc"},
|
||
|
{name: "file path in current dir", args: args{".npmrc"}, want: ".npmrc"},
|
||
|
{name: "file path in sub dir", args: args{mock.Anything + "/.npmrc"}, want: mock.Anything + "/.npmrc"},
|
||
|
}
|
||
|
for _, tt := range tests {
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
if got := NewNPMRC(tt.args.path); !reflect.DeepEqual(got.filepath, tt.want) {
|
||
|
t.Errorf("NewNPMRC().filepath = %v, want %v", got.filepath, tt.want)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func mockLoadProperties(t *testing.T, result *properties.Properties, err error) func(filename string, enc properties.Encoding) (*properties.Properties, error) {
|
||
|
return func(filename string, enc properties.Encoding) (*properties.Properties, error) {
|
||
|
return result, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestLoad(t *testing.T) {
|
||
|
// init
|
||
|
config := NewNPMRC("")
|
||
|
|
||
|
new := properties.NewProperties()
|
||
|
new.Set("test", "anything")
|
||
|
propertiesLoadFile = mockLoadProperties(t, new, nil)
|
||
|
require.NotEmpty(t, new.Keys())
|
||
|
|
||
|
require.Empty(t, config.values.Keys())
|
||
|
// test
|
||
|
err := config.Load()
|
||
|
// assert
|
||
|
assert.NoError(t, err)
|
||
|
assert.NotEmpty(t, config.values.Keys())
|
||
|
|
||
|
}
|