github.com/lgug2z/story@v0.4.1/node/packageJSON_test.go (about) 1 package node_test 2 3 import ( 4 "os" 5 6 "encoding/json" 7 "fmt" 8 9 "github.com/LGUG2Z/story/node" 10 "github.com/iancoleman/orderedmap" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 "github.com/spf13/afero" 14 ) 15 16 var p node.PackageJSON 17 var fs afero.Fs 18 19 var invalidFile = []byte(`{ 20 "name": "test", 21 "description": "test project", 22 "version": "0.0.1", 23 "devDependencies": { 24 "mocha": "*" 25 }`) 26 27 func packageJSONWithDependencies(dependencies ...string) []byte { 28 pkg := node.PackageJSON{ 29 Dependencies: make(map[string]string), 30 Raw: orderedmap.New(), 31 } 32 33 deps := make(map[string]string) 34 pkg.Raw.Set("dependencies", deps) 35 36 for _, dep := range dependencies { 37 deps[dep] = fmt.Sprintf("git+ssh://git@github.com:TestOrg/%s.git", dep) 38 pkg.Dependencies[dep] = fmt.Sprintf("git+ssh://git@github.com:TestOrg/%s.git", dep) 39 } 40 41 bytes, _ := json.MarshalIndent(pkg.Raw, "", " ") 42 return bytes 43 } 44 45 var _ = Describe("PackageJSON", func() { 46 BeforeEach(func() { 47 fs = afero.NewMemMapFs() 48 p = node.PackageJSON{} 49 }) 50 51 Describe("Loading a file", func() { 52 It("It should load a valid package.json file", func() { 53 // Given a project with a valid package.json file 54 validFile := packageJSONWithDependencies("one", "two", "three") 55 if err := fs.MkdirAll("valid", os.FileMode(0700)); err != nil { 56 Fail(err.Error()) 57 } 58 59 if err := afero.WriteFile(fs, "valid/package.json", validFile, os.FileMode(0600)); err != nil { 60 Fail(err.Error()) 61 } 62 63 // When I load the file 64 Expect(p.Load(fs, "valid")).To(Succeed()) 65 66 // Then I expect it to be unmarshalled into an object 67 Expect(p.Dependencies["one"]).To(Equal("git+ssh://git@github.com:TestOrg/one.git")) 68 }) 69 70 It("It should throw an error when trying to load an invalid package.json file", func() { 71 // Given a project with an invalid package.json file 72 if err := fs.MkdirAll("invalid", os.FileMode(0700)); err != nil { 73 Fail(err.Error()) 74 } 75 76 if err := afero.WriteFile(fs, "invalid/package.json", invalidFile, os.FileMode(0600)); err != nil { 77 Fail(err.Error()) 78 } 79 80 // When I load the file then an error is thrown 81 Expect(p.Load(fs, "invalid")).NotTo(Succeed()) 82 }) 83 }) 84 85 Describe("Writing a file", func() { 86 It("Should write out the object to a package.json file", func() { 87 // Given a project with a valid package.json file 88 validFile := packageJSONWithDependencies("one", "two", "three") 89 if err := fs.MkdirAll("valid", os.FileMode(0700)); err != nil { 90 Fail(err.Error()) 91 } 92 93 if err := afero.WriteFile(fs, "valid/package.json", validFile, os.FileMode(0600)); err != nil { 94 Fail(err.Error()) 95 } 96 97 // When I load the file 98 Expect(p.Load(fs, "valid")).To(Succeed()) 99 100 // And make a change to the file 101 p.Dependencies["new"] = "test" 102 103 // When I write the object for a project 104 Expect(p.Write(fs, "valid")).To(Succeed()) 105 106 // Then the file is written 107 content, err := afero.ReadFile(fs, "valid/package.json") 108 Expect(err).NotTo(HaveOccurred()) 109 110 Expect(string(content)).To(ContainSubstring(`"new": "test"`)) 111 }) 112 }) 113 114 Describe("Updating dependency branches", func() { 115 It("Should update only the dependencies of projects in allProjects", func() { 116 // Given a map of all projects and story projects 117 projects := []string{"one", "three"} 118 119 // And an unmarshalled package.json file 120 b := packageJSONWithDependencies("one", "two", "three") 121 Expect(json.Unmarshal(b, &p)).To(Succeed()) 122 p.Dependencies["one"] = "git+ssh://git@github.com:TestOrg/one.git#somegithash123" 123 124 // When I update the dependencies to the story branch 125 p.SetPrivateDependencyBranchesToStory("test-story", projects...) 126 127 // Then the project in allProjects should be updated 128 Expect(p.Dependencies["one"]).To(Equal("git+ssh://git@github.com:TestOrg/one.git#test-story")) 129 130 // If the project doesn't have a hash already, it should be split correctly 131 Expect(p.Dependencies["three"]).To(Equal("git+ssh://git@github.com:TestOrg/three.git#test-story")) 132 133 // But the project not in allProjects should not be updated 134 Expect(p.Dependencies["two"]).To(Equal("git+ssh://git@github.com:TestOrg/two.git")) 135 }) 136 137 It("Should reset references to removed dependencies to the master branch", func() { 138 // Given a package.json file with a dependency pinned to a story branch 139 b := packageJSONWithDependencies("one", "two") 140 Expect(json.Unmarshal(b, &p)).To(Succeed()) 141 p.Dependencies["one"] = "git+ssh://git@github.com:TestOrg/one.git#test-story" 142 143 // When I reset all the modified branches 144 p.ResetPrivateDependencyBranches("one", "test-story") 145 146 // Then that dependency should point to the master branch 147 Expect(p.Dependencies["one"]).To(Equal("git+ssh://git@github.com:TestOrg/one.git")) 148 }) 149 150 It("Should reset all modified dependencies to use the master branch", func() { 151 // Given a package.json file with a dependency pinned to a story branch 152 b := packageJSONWithDependencies("one", "two") 153 Expect(json.Unmarshal(b, &p)).To(Succeed()) 154 p.Dependencies["one"] = "git+ssh://git@github.com:TestOrg/one.git#test-story" 155 156 // When I reset all the modified branches 157 p.ResetPrivateDependencyBranchesToMaster("test-story") 158 159 // Then that dependency should point to the master branch 160 Expect(p.Dependencies["one"]).To(Equal("git+ssh://git@github.com:TestOrg/one.git")) 161 }) 162 }) 163 })