Skip to content

Commit

Permalink
feat: centralize knowledge of more spec elements
Browse files Browse the repository at this point in the history
Centralize knowledge of

- name of package.json
- BP_NODE_PROJECT_PATH environment variable

into libnodejs

Callers of find_project_path should not have to know
about BP_NODE_PROJECT_PATH themselves.

Callsers of ParsePackageJSON should not have to
add standard `package.json` name to path.

Signed-off-by: Michael Dawson <[email protected]>
  • Loading branch information
mhdawson authored and thitch97 committed Apr 27, 2023
1 parent fdd2ac4 commit 1055d49
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
3 changes: 2 additions & 1 deletion find_project_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

// FindProjectPath will validate that project path exists and is valid relative to the
// working directory.
func FindProjectPath(workingDir, projectPath string) (string, error) {
func FindProjectPath(workingDir string) (string, error) {
projectPath := os.Getenv(ProjectPathEnvName)
if projectPath == "" {
return workingDir, nil
}
Expand Down
11 changes: 8 additions & 3 deletions find_project_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ func testFindProjectPath(t *testing.T, context spec.G, it spec.S) {

it.Before(func() {
workingDir = t.TempDir()
t.Setenv("BP_NODE_PROJECT_PATH", "custom/path")

err := os.MkdirAll(filepath.Join(workingDir, "custom", "path"), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
})

it("returns the project path", func() {
result, err := libnodejs.FindProjectPath(workingDir, "custom/path")
result, err := libnodejs.FindProjectPath(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(filepath.Join(workingDir, "custom", "path")))
})
Expand All @@ -42,14 +43,18 @@ func testFindProjectPath(t *testing.T, context spec.G, it spec.S) {
})

it("returns an error", func() {
_, err := libnodejs.FindProjectPath(workingDir, "custom/path")
_, err := libnodejs.FindProjectPath(workingDir)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})

context("when the project path subdirectory does not exist", func() {
it.Before(func() {
t.Setenv("BP_NODE_PROJECT_PATH", "some-garbage")
})

it("returns an error", func() {
_, err := libnodejs.FindProjectPath(workingDir, "some-garbage")
_, err := libnodejs.FindProjectPath(workingDir)
Expect(err).To(MatchError(ContainSubstring("could not find project path")))
Expect(err).To(MatchError(ContainSubstring("no such file or directory")))
})
Expand Down
3 changes: 2 additions & 1 deletion package_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)

// PackageJSON represents the contents of a package.json file.
Expand All @@ -17,7 +18,7 @@ type PackageJSON struct {

// ParsePackageJSON parses the contents of a package.json file.
func ParsePackageJSON(path string) (PackageJSON, error) {
file, err := os.Open(path)
file, err := os.Open(filepath.Join(path, "package.json"))
if err != nil {
return PackageJSON{}, err
}
Expand Down
10 changes: 6 additions & 4 deletions package_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ func testPackageJSON(t *testing.T, context spec.G, it spec.S) {

var (
path string
filePath string
workingDir string
)

it.Before(func() {
workingDir = t.TempDir()

path = filepath.Join(workingDir, "package.json")
Expect(os.WriteFile(path, []byte(`{
path = workingDir
filePath = filepath.Join(workingDir, "package.json")
Expect(os.WriteFile(filePath, []byte(`{
"scripts": {
"poststart": "echo \"poststart\"",
"prestart": "echo \"prestart\"",
Expand All @@ -46,7 +48,7 @@ func testPackageJSON(t *testing.T, context spec.G, it spec.S) {
context("failure cases", func() {
context("when the package.json is not a valid json file", func() {
it.Before(func() {
Expect(os.WriteFile(path, []byte(`%%%`), 0600)).To(Succeed())
Expect(os.WriteFile(filePath, []byte(`%%%`), 0600)).To(Succeed())
})

it("fails parsing", func() {
Expand Down Expand Up @@ -75,7 +77,7 @@ func testPackageJSON(t *testing.T, context spec.G, it spec.S) {

context("when a start script is NOT present", func() {
it.Before(func() {
Expect(os.WriteFile(path, []byte(`{}`), 0600)).To(Succeed())
Expect(os.WriteFile(filePath, []byte(`{}`), 0600)).To(Succeed())
})

it("indicates that the package.json file does not have a start script", func() {
Expand Down
3 changes: 3 additions & 0 deletions spec_constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package libnodejs

const ProjectPathEnvName = "BP_NODE_PROJECT_PATH"

0 comments on commit 1055d49

Please sign in to comment.