github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/services/utils/deleteutils_test.go (about) 1 package utils 2 3 import ( 4 "path/filepath" 5 "strconv" 6 "testing" 7 8 "github.com/cobalt77/jfrog-client-go/utils" 9 "github.com/cobalt77/jfrog-client-go/utils/io/content" 10 "github.com/cobalt77/jfrog-client-go/utils/io/fileutils" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestMatchingDelete(t *testing.T) { 15 var actual string 16 actual, _ = WildcardToDirsPath("s/*/path/", "s/a/path/b.zip") 17 assertDeletePattern("s/a/path/", actual, t) 18 actual, _ = WildcardToDirsPath("s/*/path/", "s/a/b/c/path/b.zip") 19 assertDeletePattern("s/a/b/c/path/", actual, t) 20 actual, _ = WildcardToDirsPath("s/a/*/", "s/a/b/path/b.zip") 21 assertDeletePattern("s/a/b/", actual, t) 22 actual, _ = WildcardToDirsPath("s/*/path/*/", "s/a/path/a/b.zip") 23 assertDeletePattern("s/a/path/a/", actual, t) 24 actual, _ = WildcardToDirsPath("s/*/path/*/", "s/a/a/path/a/b/c/d/b.zip") 25 assertDeletePattern("s/a/a/path/a/", actual, t) 26 actual, _ = WildcardToDirsPath("s/*/", "s/a/a/path/a/b/c/d/b.zip") 27 assertDeletePattern("s/a/", actual, t) 28 actual, _ = WildcardToDirsPath("s/*/a/*/", "s/a/a/path/k/b/c/d/b.zip") 29 assertDeletePattern("s/a/a/path/", actual, t) 30 actual, _ = WildcardToDirsPath("s/*/a/*/*/", "s/a/a/path/k/b/c/d/b.zip") 31 assertDeletePattern("s/a/a/path/k/", actual, t) 32 actual, _ = WildcardToDirsPath("s/*/*l*/*/*/", "s/a/l/path/k/b/c/d/b.zip") 33 assertDeletePattern("s/a/l/path/k/", actual, t) 34 actual, _ = WildcardToDirsPath("s/*/a*/", "s/a/a/path/k/b/c/d/b.zip") 35 assertDeletePattern("s/a/a/", actual, t) 36 actual, _ = WildcardToDirsPath("s/a*/", "s/a/a/path/k/b/c/d/b.zip") 37 assertDeletePattern("s/a/", actual, t) 38 actual, _ = WildcardToDirsPath("s/*/", "s/a/a/path/k/b/c/d/b.zip") 39 assertDeletePattern("s/a/", actual, t) 40 actual, _ = WildcardToDirsPath("s/*/*path*/", "s/a/h/path/k/b/c/d/b.zip") 41 assertDeletePattern("s/a/h/path/", actual, t) 42 actual, _ = WildcardToDirsPath("a/b/*********/*******/", "a/b/c/d/e.zip") 43 assertDeletePattern("a/b/c/d/", actual, t) 44 actual, err := WildcardToDirsPath("s/*/a/*/*", "s/a/a/path/k/b/c/d/b.zip") 45 assertDeletePatternErr(err.Error(), "Delete pattern must end with \"/\"", t) 46 } 47 48 func assertDeletePattern(expected, actual string, t *testing.T) { 49 if expected != actual { 50 t.Error("Wrong matching expected: `" + expected + "` Got `" + actual + "`") 51 } 52 } 53 54 func assertDeletePatternErr(expected, actual string, t *testing.T) { 55 if expected != actual { 56 t.Error("Wrong err message expected: `" + expected + "` Got `" + actual + "`") 57 } 58 } 59 60 func TestWriteCandidateDirsToBeDeleted(t *testing.T) { 61 testPath, err := getBaseTestDir() 62 { 63 assert.NoError(t, err) 64 var bufferFiles []*content.ContentReader 65 for i := 1; i <= 3; i++ { 66 bufferFiles = append(bufferFiles, content.NewContentReader(filepath.Join(testPath, "buffer_file_ascending_order_"+strconv.Itoa(i)+".json"), content.DefaultKey)) 67 } 68 resultWriter, err := content.NewContentWriter(content.DefaultKey, true, false) 69 assert.NoError(t, err) 70 artifactNotToBeDeleteReader := content.NewContentReader(filepath.Join(testPath, "artifact_file_1.json"), content.DefaultKey) 71 assert.NoError(t, WriteCandidateDirsToBeDeleted(bufferFiles, artifactNotToBeDeleteReader, resultWriter)) 72 assert.NoError(t, resultWriter.Close()) 73 result, err := fileutils.FilesIdentical(filepath.Join(testPath, "candidate_dirs_to_be_deleted_results.json"), resultWriter.GetFilePath()) 74 assert.NoError(t, err) 75 assert.True(t, result) 76 assert.NoError(t, resultWriter.RemoveOutputFilePath()) 77 } 78 // Fixes issue https://github.com/jfrog/jfrog-cli/issues/808 79 { 80 resultWriter, err := content.NewContentWriter(content.DefaultKey, true, false) 81 assert.NoError(t, err) 82 var bufferFiles []*content.ContentReader 83 bufferFiles = append(bufferFiles, content.NewContentReader(filepath.Join(testPath, "buffer_file_ascending_order_4.json"), content.DefaultKey)) 84 artifactNotToBeDeleteReader := content.NewContentReader(filepath.Join(testPath, "artifact_file_2.json"), content.DefaultKey) 85 assert.NoError(t, WriteCandidateDirsToBeDeleted(bufferFiles, artifactNotToBeDeleteReader, resultWriter)) 86 assert.NoError(t, resultWriter.Close()) 87 assert.True(t, resultWriter.IsEmpty()) 88 } 89 } 90 91 func TestFilterCandidateToBeDeleted(t *testing.T) { 92 testPath, err := getBaseTestDir() 93 assert.NoError(t, err) 94 resultWriter, err := content.NewContentWriter(content.DefaultKey, true, false) 95 assert.NoError(t, err) 96 deleteCandidates := content.NewContentReader(filepath.Join(testPath, "prebuffer_file.json"), content.DefaultKey) 97 assert.NoError(t, err) 98 oldMaxSize := utils.MaxBufferSize 99 defer func() { utils.MaxBufferSize = oldMaxSize }() 100 utils.MaxBufferSize = 3 101 sortedFiles, err := FilterCandidateToBeDeleted(deleteCandidates, resultWriter) 102 assert.Len(t, sortedFiles, 3) 103 assert.NoError(t, err) 104 for i, val := range sortedFiles { 105 result, err := fileutils.FilesIdentical(val.GetFilePath(), filepath.Join(testPath, "buffer_file_ascending_order_"+strconv.Itoa(i+1)+".json")) 106 assert.NoError(t, err) 107 assert.True(t, result) 108 assert.NoError(t, val.Close()) 109 } 110 assert.NoError(t, resultWriter.Close()) 111 result, err := fileutils.FilesIdentical(resultWriter.GetFilePath(), filepath.Join(testPath, "candidate_artifact_to_be_deleted_results.json")) 112 assert.NoError(t, err) 113 assert.True(t, result) 114 assert.NoError(t, resultWriter.RemoveOutputFilePath()) 115 }