github.com/jfrog/jfrog-cli-core@v1.12.1/artifactory/utils/dotnet/dependencies/assetsjson_test.go (about) 1 package dependencies 2 3 import ( 4 "encoding/json" 5 "path/filepath" 6 "reflect" 7 "testing" 8 ) 9 10 func TestJson(t *testing.T) { 11 content := []byte(`{ 12 "version": 3, 13 "targets": { 14 "t1": { 15 "dep1/1.0.1": {}, 16 "dep2/1.0.2": { 17 "dependencies": { 18 "dep1": "1.0.1" 19 } 20 } 21 } 22 }, 23 "libraries": { 24 "dep1/1.0.1": { 25 "type": "project", 26 "path": "dep1/path/1.0.1", 27 "files": [ 28 "file1", 29 "file2" 30 ] 31 }, 32 "dep2/1.0.2": { 33 "path": "dep2/path/1.0.2", 34 "files": [ 35 "file1", 36 "file2" 37 ] 38 } 39 }, 40 "project": { 41 "version": "1.0.0", 42 "restore": { 43 "packagesPath": "path/to/packages" 44 }, 45 "frameworks": { 46 "net461": { 47 "dependencies": { 48 "dep1": { 49 "target": "Package", 50 "version": "[1.0.1, )" 51 } 52 } 53 } 54 } 55 } 56 }`) 57 58 var assetsObj assets 59 err := json.Unmarshal(content, &assetsObj) 60 if err != nil { 61 t.Error(err) 62 } 63 64 expected := assets{ 65 Version: 3, 66 Targets: map[string]map[string]targetDependency{"t1": { 67 "dep1/1.0.1": targetDependency{}, 68 "dep2/1.0.2": targetDependency{Dependencies: map[string]string{"dep1": "1.0.1"}}, 69 }}, 70 Libraries: map[string]library{ 71 "dep1/1.0.1": {Type: "project", Path: "dep1/path/1.0.1", Files: []string{"file1", "file2"}}, 72 "dep2/1.0.2": {Path: "dep2/path/1.0.2", Files: []string{"file1", "file2"}}, 73 }, 74 Project: project{Version: "1.0.0", Restore: restore{PackagesPath: "path/to/packages"}, 75 Frameworks: map[string]framework{"net461": { 76 Dependencies: map[string]dependency{"dep1": {Target: "Package", Version: "[1.0.1, )"}}}}}, 77 } 78 79 if !reflect.DeepEqual(expected, assetsObj) { 80 t.Errorf("Expected: \n%v, \nGot: \n%v", expected, assetsObj) 81 } 82 } 83 84 func TestNewAssetsExtractor(t *testing.T) { 85 assets := assetsExtractor{} 86 extractor, err := assets.new(filepath.Join("testdata", "assetsproject", "obj", "project.assets.json")) 87 if err != nil { 88 t.Error(err) 89 } 90 91 directDependencies, err := extractor.DirectDependencies() 92 if err != nil { 93 t.Error(err) 94 } 95 96 expectedDirectDependencies := []string{"dep1"} 97 if !reflect.DeepEqual(expectedDirectDependencies, directDependencies) { 98 t.Errorf("Expected: \n%s, \nGot: \n%s", expectedDirectDependencies, directDependencies) 99 } 100 101 allDependencies, err := extractor.AllDependencies() 102 expectedAllDependencies := []string{"dep1", "dep2"} 103 for _, v := range expectedAllDependencies { 104 if _, ok := allDependencies[v]; !ok { 105 t.Error("Expecting", v, "dependency") 106 } 107 } 108 109 childrenMap, err := extractor.ChildrenMap() 110 if err != nil { 111 t.Error(err) 112 } 113 114 if len(childrenMap["dep1"]) != 0 { 115 t.Error("Expected: []string{} got :", childrenMap["dep1"]) 116 } 117 118 if len(childrenMap["dep2"]) != 1 { 119 t.Error("Expected: []string{\"dep1\"} got :", childrenMap["dep2"]) 120 } 121 } 122 123 func TestGetDependencyIdForBuildInfo(t *testing.T) { 124 args := []string{ 125 "dep1/1.0.1", 126 "dep2.another.hierarchy/1.0.2", 127 "dep3:with;special?chars@test/1.0.3", 128 } 129 130 expected := []string{ 131 "dep1:1.0.1", 132 "dep2.another.hierarchy:1.0.2", 133 "dep3:with;special?chars@test:1.0.3", 134 } 135 136 for index, test := range args { 137 actualId := getDependencyIdForBuildInfo(test) 138 if actualId != expected[index] { 139 t.Errorf("Expected dependency name to be: %s, got: %s.", expected[index], actualId) 140 } 141 } 142 }