diff --git a/README.md b/README.md index 64fbfd84f..1ea3bf2aa 100644 --- a/README.md +++ b/README.md @@ -741,7 +741,7 @@ In the above example for the collection `ethCollectionMean`, new custom job havi ### Logs -User can pass a separate flag --logFile followed with any name for log file along with command. The logs will be stored in ```.razor``` directory. +User can pass a separate flag --logFile followed with any name for log file along with command. The logs will be stored in ```.razor/logs``` directory. razor cli ``` diff --git a/cmd/vote_test.go b/cmd/vote_test.go index 6348e566a..f54a86992 100644 --- a/cmd/vote_test.go +++ b/cmd/vote_test.go @@ -319,6 +319,7 @@ func TestGetLastProposedEpoch(t *testing.T) { Data: []byte{4, 2}, }, }, + time: 10, contractAbi: abi.ABI{}, unpackErr: errors.New("unpack error"), }, diff --git a/path/path.go b/path/path.go index a651cdac9..faefbc405 100644 --- a/path/path.go +++ b/path/path.go @@ -25,13 +25,21 @@ func (PathUtils) GetLogFilePath(fileName string) (string, error) { if err != nil { return "", err } - filePath := home + "/" + fileName + ".log" - f, err := OSUtilsInterface.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0600) + fileDir := home + "/logs" + if _, err := OSUtilsInterface.Stat(fileDir); OSUtilsInterface.IsNotExist(err) { + mkdirErr := OSUtilsInterface.Mkdir(fileDir, 0700) + if mkdirErr != nil { + return "", mkdirErr + } + } + + logFilepath := fileDir + "/" + fileName + ".log" + f, err := OSUtilsInterface.OpenFile(logFilepath, os.O_CREATE|os.O_WRONLY, 0600) if err != nil { return "", err } defer f.Close() - return filePath, nil + return logFilepath, nil } //This function returns the config file path diff --git a/path/path_test.go b/path/path_test.go index 453ef4c46..ba9cf7976 100644 --- a/path/path_test.go +++ b/path/path_test.go @@ -95,12 +95,17 @@ func TestGetDefaultPath(t *testing.T) { } func TestGetLogFilePath(t *testing.T) { - var fileName string + var fileInfo fs.FileInfo + type args struct { - path string - pathErr error - file *os.File - fileErr error + fileName string + path string + pathErr error + file *os.File + fileErr error + statErr error + isNotExist bool + mkdirErr error } tests := []struct { name string @@ -111,9 +116,10 @@ func TestGetLogFilePath(t *testing.T) { { name: "Test 1: When GetLogFilePath() executes successfully", args: args{ - path: "./home/.razor", + fileName: "xyz", + path: "./home/.razor", }, - want: "./home/.razor/.log", + want: "./home/.razor/logs/xyz.log", wantErr: nil, }, { @@ -133,6 +139,28 @@ func TestGetLogFilePath(t *testing.T) { want: "", wantErr: errors.New("error in getting file"), }, + { + name: "Test 4: When there is stat error but not mkdir error", + args: args{ + fileName: "xyz", + path: "./home/.razor", + statErr: errors.New("file not exists"), + isNotExist: true, + }, + want: "./home/.razor/logs/xyz.log", + wantErr: nil, + }, + { + name: "Test 5: When there is stat error and mkdir error", + args: args{ + path: "./home/.razor", + statErr: errors.New("file not exists"), + isNotExist: true, + mkdirErr: errors.New("mkdir error"), + }, + want: "", + wantErr: errors.New("mkdir error"), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -144,8 +172,12 @@ func TestGetLogFilePath(t *testing.T) { pathMock.On("GetDefaultPath").Return(tt.args.path, tt.args.pathErr) osMock.On("OpenFile", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.file, tt.args.fileErr) + osMock.On("Stat", mock.AnythingOfType("string")).Return(fileInfo, tt.args.statErr) + osMock.On("IsNotExist", mock.Anything).Return(tt.args.isNotExist) + osMock.On("Mkdir", mock.Anything, mock.Anything).Return(tt.args.mkdirErr) + pa := PathUtils{} - got, err := pa.GetLogFilePath(fileName) + got, err := pa.GetLogFilePath(tt.args.fileName) if got != tt.want { t.Errorf("GetLogFilePath(), got = %v, want = %v", got, tt.want) }