Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to get version from package.json #10

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions package_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

// PackageJSON represents the contents of a package.json file.
type PackageJSON struct {
Engines struct {
Node string `json:"node"`
} `json:"engines"`
Scripts struct {
PostStart string `json:"poststart"`
PreStart string `json:"prestart"`
Expand Down Expand Up @@ -38,3 +41,7 @@ func ParsePackageJSON(path string) (PackageJSON, error) {
func (pj PackageJSON) HasStartScript() bool {
return pj.Scripts.Start != ""
}

func (pj PackageJSON) GetVersion() string {
return pj.Engines.Node
}
50 changes: 50 additions & 0 deletions package_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func testPackageJSON(t *testing.T, context spec.G, it spec.S) {
}`), 0600)).To(Succeed())
})

it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})

context("when parsing a valid package.json with start scripts", func() {
it("successfully extracts the scripts information", func() {
pkg, err := libnodejs.ParsePackageJSON(path)
Expand Down Expand Up @@ -88,4 +92,50 @@ func testPackageJSON(t *testing.T, context spec.G, it spec.S) {
})
})
})

context("ParseVersion", func() {
it.Before(func() {
Expect(os.WriteFile(filePath, []byte(`{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhdawson Each time the file is written, can it be deleted to keep things clean? (it.After)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure will look at doing that in next few days.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed commit do do cleanup

"engines": {
"node": "1.2.3"
}
}`), 0600)).To(Succeed())
})

it("parses the node engine version from a package.json file", func() {
pkg, err := libnodejs.ParsePackageJSON(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(pkg.GetVersion()).To(Equal("1.2.3"))
})

context("Engines, but no Node version", func() {
it.Before(func() {
Expect(os.WriteFile(filePath, []byte(`{
"engines": {
}
}`), 0600)).To(Succeed())
})

it("parses the node engine version from a package.json file when no version is specified", func() {

pkg, err := libnodejs.ParsePackageJSON(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(pkg.GetVersion()).To(Equal(""))
})
})

context("No Engines", func() {
it.Before(func() {
Expect(os.WriteFile(filePath, []byte(`{
}`), 0600)).To(Succeed())
})

it("parses the node engine version from a package.json file when no version is specified", func() {

pkg, err := libnodejs.ParsePackageJSON(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(pkg.GetVersion()).To(Equal(""))
})
})
})
}