github.com/konsorten/ktn-build-info@v1.0.11/ver/package_json_test.go (about) 1 package ver 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/Jeffail/gabs" 8 log "github.com/sirupsen/logrus" 9 ) 10 11 func TestTryReadPackageJSON(t *testing.T) { 12 log.SetLevel(log.DebugLevel) 13 14 // change to test data dir 15 currDir, _ := os.Getwd() 16 os.Chdir("examples/npm") 17 defer os.Chdir(currDir) 18 19 // read host information 20 host, err := TryReadFromBuildHost() 21 22 if err != nil { 23 t.Fatalf("Failed to read build host info: %v", err) 24 } 25 26 // read version information 27 found, err := TryReadFromPackageJSON(false, false, false, false, false) 28 29 if err != nil { 30 t.Fatalf("Failed to read version info: %v", err) 31 } 32 33 if found == nil { 34 t.Fatalf("%v not found", PackageJsonFilename) 35 } 36 37 found.CopyMissingFrom(host) 38 39 if ok, _ := found.IsValid(); !ok { 40 t.Fatal("Invalid version information") 41 } 42 } 43 44 func TestTryReadPackageJSON_oldAuthor(t *testing.T) { 45 log.SetLevel(log.DebugLevel) 46 47 // change to test data dir 48 currDir, _ := os.Getwd() 49 os.Chdir("examples/npm-old-author") 50 defer os.Chdir(currDir) 51 52 // read defaults 53 defaults, err := TryReadFromKonsortenDefaults() 54 55 if err != nil { 56 t.Fatalf("Failed to read defaults: %v", err) 57 } 58 59 // read host information 60 host, err := TryReadFromBuildHost() 61 62 if err != nil { 63 t.Fatalf("Failed to read build host info: %v", err) 64 } 65 66 // read version information 67 found, err := TryReadFromPackageJSON(false, false, false, false, false) 68 69 if err != nil { 70 t.Fatalf("Failed to read version info: %v", err) 71 } 72 73 if found == nil { 74 t.Fatalf("%v not found", PackageJsonFilename) 75 } 76 77 found.CopyMissingFrom(host) 78 found.CopyMissingFrom(defaults) 79 80 if ok, _ := found.IsValid(); !ok { 81 t.Fatal("Invalid version information") 82 } 83 } 84 85 func TestUpdatePackageJson(t *testing.T) { 86 log.SetLevel(log.DebugLevel) 87 88 // change to test data dir 89 currDir, _ := os.Getwd() 90 os.Chdir("examples/npm-update") 91 defer os.Chdir(currDir) 92 93 copyFile(t, "package.json.test", PackageJsonFilename) 94 95 found := createTestVersionInformationFromYAML(t) 96 97 // update package json 98 err := UpdatePackageJSON(found) 99 100 if err != nil { 101 t.Fatalf("Failed to update NPM package.json: %v", err) 102 } 103 104 // parse the output json file 105 _, err = gabs.ParseJSONFile(PackageJsonFilename) 106 107 if err != nil { 108 t.Fatalf("Failed to read updated NPM package.json: %v", err) 109 } 110 }