github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/golang/archive_test.go (about) 1 package golang 2 3 import ( 4 "bytes" 5 "github.com/stretchr/testify/assert" 6 "os" 7 "path/filepath" 8 "reflect" 9 "testing" 10 11 "github.com/jfrog/build-info-go/utils" 12 13 "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" 14 "github.com/jfrog/jfrog-cli-core/v2/utils/log" 15 "github.com/jfrog/jfrog-cli-core/v2/utils/tests" 16 ) 17 18 func TestArchiveProject(t *testing.T) { 19 log.SetDefaultLogger() 20 if coreutils.IsWindows() { 21 t.Skip("Skipping archive test...") 22 } 23 pwd, err := os.Getwd() 24 if err != nil { 25 t.Error(err) 26 } 27 buff := &bytes.Buffer{} 28 originalFolder := "test_.git_suffix" 29 baseDir, dotGitPath := tests.PrepareDotGitDir(t, originalFolder, "testdata") 30 var archiveWithExclusion = []struct { 31 buff *bytes.Buffer 32 filePath string 33 mod string 34 version string 35 excludedPatterns []string 36 expected map[utils.Algorithm]string 37 }{ 38 {buff, filepath.Join(pwd, "testdata"), "myproject.com/module/name", "v1.0.0", nil, map[utils.Algorithm]string{utils.MD5: "5b3603a7bf637622516673b845249205", utils.SHA1: "7386685c432c39428c9cb8584a2b970139c5e626", utils.SHA256: "eefd8aa3f9ac89876c8442d5feebbc837666bf40114d201219e3e6d51c208949"}}, 39 {buff, filepath.Join(pwd, "testdata"), "myproject.com/module/name", "v1.0.0", []string{"./testdata/dir1/*"}, map[utils.Algorithm]string{utils.MD5: "c2eeb4ef958edee91570690bf4111fc7", utils.SHA1: "d77e10eaa9bd863a9ff3775d3e452041e6f5aa40", utils.SHA256: "ecf66c1256263b2b4386efc299fa0c389263608efda9d1d91af8a746e6c5709a"}}, 40 {buff, filepath.Join(pwd, "testdata"), "myproject.com/module/name", "v1.0.0", []string{"./testdata/dir2/*"}, map[utils.Algorithm]string{utils.MD5: "bbe78a98ba10c1428f3a364570015e11", utils.SHA1: "99fd22ea2fe9c2c48124e741881fc3a555458a7e", utils.SHA256: "e2299f3c4e1f22d36befba191a347783dc2047e8e38cf6b9b96c273090f6e25b"}}, 41 {buff, filepath.Join(pwd, "testdata"), "myproject.com/module/name", "v1.0.0", []string{"./testdata/dir2/*", "testdata/dir3/*"}, map[utils.Algorithm]string{utils.MD5: "28617d6e74fce3dd2bab21b1bd65009b", utils.SHA1: "410814fbf21afdfb9c5b550151a51c2e986447fa", utils.SHA256: "e877c07315d6d3ad69139035defc08c04b400b36cd069b35ea3c2960424f2dc6"}}, 42 {buff, filepath.Join(pwd, "testdata"), "myproject.com/module/name", "v1.0.0", []string{"./testdata/dir2/*", "./testdata/dir3/dir4/*"}, map[utils.Algorithm]string{utils.MD5: "46a3ded48ed7998b1b35c80fbe0ffab5", utils.SHA1: "a26e73e7d29e49dd5d9c87da8f7c93cf929750df", utils.SHA256: "cf224b12eca12de4a052ef0f444519d64b6cecaf7b06050a02998be190e88847"}}, 43 {buff, filepath.Join(pwd, "testdata"), "myproject.com/module/name", "v1.0.0", []string{"./testdata/dir3/*"}, map[utils.Algorithm]string{utils.MD5: "c2a2dd6a7af84c2d88a48caf0c3aec34", utils.SHA1: "193d761317a602d18566561678b7bddc4773385c", utils.SHA256: "3efcd8b0d88081ec64333ff98b43616d283c4d52ed26cd7c8df646d9ea452c31"}}, 44 {buff, filepath.Join(pwd, "testdata"), "myproject.com/module/name", "v1.0.0", []string{"*.txt"}, map[utils.Algorithm]string{utils.MD5: "e93953b4be84d7753e0f33589b7dc4ba", utils.SHA1: "280c7492f57262b6e0af56b06c9db6a128e32ab9", utils.SHA256: "e7357986c59bf670af1e2f4868edb1406a87d328b7681b15cf038491cdc7e88c"}}, 45 {buff, filepath.Join(pwd, "testdata"), "myproject.com/module/name", "v1.0.0", []string{"./*/dir4/*.txt"}, map[utils.Algorithm]string{utils.MD5: "785f0c0c7b20dfd716178856edb79834", utils.SHA1: "d07204277ece1d7bef6a9f289a56afb91d66125f", utils.SHA256: "6afa0dd70bfa7c6d3aca1a3dfcd6465c542d64136c6391fa611795e6fa5800ce"}}, 46 } 47 for _, testData := range archiveWithExclusion { 48 err = archiveProject(testData.buff, testData.filePath, testData.mod, testData.version, testData.excludedPatterns) 49 assert.NoError(t, err) 50 actual, err := utils.CalcChecksums(buff) 51 assert.NoError(t, err) 52 53 if !reflect.DeepEqual(testData.expected, actual) { 54 t.Errorf("Expecting: %v, Got: %v", testData.expected, actual) 55 } 56 } 57 tests.RenamePath(dotGitPath, filepath.Join(baseDir, originalFolder), t) 58 } 59 60 func TestGetAbsolutePaths(t *testing.T) { 61 testData := []string{filepath.Join(".", "dir1", "*"), "*.txt", filepath.Join("*", "dir2", "*")} 62 result, err := getAbsolutePaths(testData) 63 assert.NoError(t, err) 64 wd, err := os.Getwd() 65 assert.NoError(t, err) 66 expectedResults := []string{filepath.Join(wd, "dir1", "*"), filepath.Join(wd, "*.txt"), filepath.Join(wd, "*", "dir2", "*")} 67 assert.ElementsMatch(t, result, expectedResults) 68 }