github.com/jfrog/jfrog-cli-core@v1.12.1/artifactory/commands/npm/installorci_test.go (about) 1 package npm 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestPrepareConfigData(t *testing.T) { 13 currentDir, err := os.Getwd() 14 assert.NoError(t, err) 15 testdataPath := filepath.Join(currentDir, "artifactory", "commands", "testdata") 16 testdataPath, err = filepath.Abs(testdataPath) 17 assert.NoError(t, err) 18 configBefore := []byte( 19 "json=true\n" + 20 "user-agent=npm/5.5.1 node/v8.9.1 darwin x64\n" + 21 "metrics-registry=http://somebadregistry\nscope=\n" + 22 "//reg=ddddd\n" + 23 "@jfrog:registry=http://somebadregistry\n" + 24 "registry=http://somebadregistry\n" + 25 "email=ddd@dd.dd\n" + 26 "allow-same-version=false\n" + 27 "cache-lock-retries=10") 28 29 expectedConfig := 30 []string{ 31 "json = true", 32 "allow-same-version=false", 33 "user-agent=npm/5.5.1 node/v8.9.1 darwin x64", 34 "@jfrog:registry = http://goodRegistry", 35 "email=ddd@dd.dd", 36 "cache-lock-retries=10", 37 "registry = http://goodRegistry", 38 "_auth = YWRtaW46QVBCN1ZkZFMzN3NCakJiaHRGZThVb0JlZzFl"} 39 40 npmi := NpmCommandArgs{registry: "http://goodRegistry", jsonOutput: true, npmAuth: "_auth = YWRtaW46QVBCN1ZkZFMzN3NCakJiaHRGZThVb0JlZzFl"} 41 configAfter, err := npmi.prepareConfigData(configBefore) 42 if err != nil { 43 t.Error(err) 44 } 45 actualConfigArray := strings.Split(string(configAfter), "\n") 46 for _, eConfig := range expectedConfig { 47 found := false 48 for _, aConfig := range actualConfigArray { 49 if aConfig == eConfig { 50 found = true 51 break 52 } 53 } 54 if !found { 55 t.Errorf("The expected config: %s is missing from the actual configuration list:\n %s", eConfig, actualConfigArray) 56 } 57 } 58 } 59 60 func TestPrepareConfigDataTypeRestriction(t *testing.T) { 61 var typeRestrictions = map[string]typeRestriction{ 62 "production=\"true\"": prodOnly, 63 "production=true": prodOnly, 64 "only = prod": prodOnly, 65 "only=production": prodOnly, 66 "only = development": devOnly, 67 "only=dev": devOnly, 68 "only=": defaultRestriction, 69 "omit = [\"dev\"]\ndev = true": prodOnly, 70 "omit = [\"abc\"]\ndev = true": all, 71 "only=dev\nomit = [\"abc\"]": all, 72 "dev=true\nomit = [\"dev\"]": prodOnly, 73 "kuku=true": defaultRestriction} 74 75 for json, typeRestriction := range typeRestrictions { 76 npmi := NpmCommandArgs{} 77 npmi.prepareConfigData([]byte(json)) 78 if npmi.typeRestriction != typeRestriction { 79 t.Errorf("Type restriction was supposed to be %d but set to: %d when using the json:\n%s", typeRestriction, npmi.typeRestriction, json) 80 } 81 } 82 } 83 84 func TestParseDependencies(t *testing.T) { 85 dependenciesJsonList, err := os.ReadFile("../testdata/dependenciesList.json") 86 if err != nil { 87 t.Error(err) 88 } 89 90 expectedDependenciesList := []struct { 91 Key string 92 pathToRoot [][]string 93 }{ 94 {"underscore:1.4.4", [][]string{{"binary-search-tree:0.2.4", "nedb:1.0.2", "root"}}}, 95 {"@jfrog/npm_scoped:1.0.0", [][]string{{"root"}}}, 96 {"xml:1.0.1", [][]string{{"root"}}}, 97 {"xpm:0.1.1", [][]string{{"@jfrog/npm_scoped:1.0.0", "root"}}}, 98 {"binary-search-tree:0.2.4", [][]string{{"nedb:1.0.2", "root"}}}, 99 {"nedb:1.0.2", [][]string{{"root"}}}, 100 {"@ilg/es6-promisifier:0.1.9", [][]string{{"@ilg/cli-start-options:0.1.19", "xpm:0.1.1", "@jfrog/npm_scoped:1.0.0", "root"}}}, 101 {"wscript-avoider:3.0.2", [][]string{{"@ilg/cli-start-options:0.1.19", "xpm:0.1.1", "@jfrog/npm_scoped:1.0.0", "root"}}}, 102 {"yaml:0.2.3", [][]string{{"root"}}}, 103 {"@ilg/cli-start-options:0.1.19", [][]string{{"xpm:0.1.1", "@jfrog/npm_scoped:1.0.0", "root"}}}, 104 {"async:0.2.10", [][]string{{"nedb:1.0.2", "root"}}}, 105 {"find:0.2.7", [][]string{{"root"}}}, 106 {"jquery:3.2.0", [][]string{{"root"}}}, 107 {"nub:1.0.0", [][]string{{"find:0.2.7", "root"}, {"root"}}}, 108 {"shopify-liquid:1.d7.9", [][]string{{"xpm:0.1.1", "@jfrog/npm_scoped:1.0.0", "root"}}}, 109 } 110 npmi := NpmCommandArgs{} 111 npmi.dependencies = make(map[string]*dependency) 112 err = npmi.parseDependencies([]byte(dependenciesJsonList), "myScope", []string{"root"}) 113 if err != nil { 114 t.Error(err) 115 } 116 if len(expectedDependenciesList) != len(npmi.dependencies) { 117 t.Error("The expected dependencies list length is", len(expectedDependenciesList), "and should be:\n", expectedDependenciesList, 118 "\nthe actual dependencies list length is", len(npmi.dependencies), "and the list is:\n", npmi.dependencies) 119 t.Error("The expected dependencies list length is", len(expectedDependenciesList), "and should be:\n", expectedDependenciesList, 120 "\nthe actual dependencies list length is", len(npmi.dependencies), "and the list is:\n", npmi.dependencies) 121 } 122 for _, eDependency := range expectedDependenciesList { 123 found := false 124 for aDependency, v := range npmi.dependencies { 125 if aDependency == eDependency.Key && assert.ElementsMatch(t, v.pathToRoot, eDependency.pathToRoot) { 126 found = true 127 break 128 } 129 } 130 if !found { 131 t.Error("The expected dependency:", eDependency, "is missing from the actual dependencies list:\n", npmi.dependencies) 132 } 133 } 134 }