From 1042ce6eaac52f721dc94be622c7f0cefe2ce524 Mon Sep 17 00:00:00 2001 From: mik-dass Date: Wed, 5 May 2021 16:46:06 +0530 Subject: [PATCH] Adds an error when the file/folder mentioned in a remote attribute is not found --- pkg/util/file_indexer.go | 10 ++++++++++ pkg/util/file_indexer_test.go | 32 +++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/pkg/util/file_indexer.go b/pkg/util/file_indexer.go index 6bcd79bb296..a50ca9f36c6 100644 --- a/pkg/util/file_indexer.go +++ b/pkg/util/file_indexer.go @@ -2,6 +2,7 @@ package util import ( "encoding/json" + "fmt" "io/ioutil" "os" "path/filepath" @@ -385,6 +386,9 @@ func runIndexerWithExistingFileIndex(directory string, ignoreRules []string, rem if err != nil { return IndexerRet{}, err } + if len(matches) == 0 { + return IndexerRet{}, fmt.Errorf("path %q doens't exist", remoteAttribute) + } for _, fileName := range matches { if checkFileExist(fileName) { // Fetch path of source file relative to that of source base path so that it can be passed to recursiveTar @@ -437,6 +441,8 @@ func runIndexerWithExistingFileIndex(directory string, ignoreRules []string, rem for _, remote := range innerRet.FilesDeleted { filesDeleted[remote] = true } + } else { + return IndexerRet{}, fmt.Errorf("path %q doens't exist", fileName) } } } @@ -509,6 +515,10 @@ func recursiveChecker(directory, srcBase, srcFile, destBase, destFile string, ig return IndexerRet{}, err } + if len(matchedPathsDir) == 0 { + return IndexerRet{}, fmt.Errorf("path %q doens't exist", joinedPath) + } + joinedRelPath, err := filepath.Rel(directory, joinedPath) if err != nil { return IndexerRet{}, err diff --git a/pkg/util/file_indexer_test.go b/pkg/util/file_indexer_test.go index e6addad3507..d6c27bc3d42 100644 --- a/pkg/util/file_indexer_test.go +++ b/pkg/util/file_indexer_test.go @@ -855,7 +855,7 @@ func Test_recursiveChecker(t *testing.T) { want: IndexerRet{ NewFileMap: map[string]FileData{}, }, - wantErr: false, + wantErr: true, }, { name: "case 19: folder doesn't exist", @@ -871,7 +871,7 @@ func Test_recursiveChecker(t *testing.T) { want: IndexerRet{ NewFileMap: map[string]FileData{}, }, - wantErr: false, + wantErr: true, }, { @@ -1230,7 +1230,7 @@ func Test_runIndexerWithExistingFileIndex(t *testing.T) { args: args{ directory: tempDirectoryName, ignoreRules: []string{}, - remoteDirectories: map[string]string{"blah": "new/Blah", htmlRelFilePath: "new/Folder0/view.html", viewsFolderStat.Name(): "new/Folder"}, + remoteDirectories: map[string]string{htmlRelFilePath: "new/Folder0/view.html", viewsFolderStat.Name(): "new/Folder"}, existingFileIndex: &FileIndex{ Files: map[string]FileData{ htmlRelFilePath: { @@ -1463,6 +1463,32 @@ func Test_runIndexerWithExistingFileIndex(t *testing.T) { }, wantErr: false, }, + { + name: "case 15: local file doesn't exist", + args: args{ + directory: tempDirectoryName, + ignoreRules: []string{}, + remoteDirectories: map[string]string{htmlRelFilePath + "blah": htmlRelFilePath}, + existingFileIndex: &FileIndex{ + Files: map[string]FileData{}, + }, + }, + wantRet: IndexerRet{}, + wantErr: true, + }, + { + name: "case 16: local folder doesn't exist", + args: args{ + directory: tempDirectoryName, + ignoreRules: []string{}, + remoteDirectories: map[string]string{viewsFolderPath + "blah": viewsFolderPath}, + existingFileIndex: &FileIndex{ + Files: map[string]FileData{}, + }, + }, + wantRet: IndexerRet{}, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {