Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
adds tests for projecttoml module
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei committed Jul 13, 2023
1 parent 146d726 commit 0f841bc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ var deployCmd = &cobra.Command{
}

var buildTimeEnvVars map[string]string
if ok := projecttoml.DoesProjectFileExist(); ok {
projectFile, err := projecttoml.GetProjectFile()
if ok := projecttoml.DoesProjectFileExist(projecttoml.ProjectTomlPath); ok {
projectFile, err := projecttoml.GetProjectFile(projecttoml.ProjectTomlPath)
if err != nil {
return err
}
Expand Down
19 changes: 19 additions & 0 deletions internal/projecttoml/fixtures/project1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[[io.buildpacks.build.env]]
name='HELLO'
value='FOO'

[[io.buildpacks.build.env]]
name='HELLO2'
value='WORLD2'

[[io.buildpacks.build.env]]
name='HELLO3'
value='WORLD3'

[[build.env]]
name='build'
value='value'

[[build.env]]
name="HELLO3"
value="WORLD3_OVERRIDE"
10 changes: 5 additions & 5 deletions internal/projecttoml/projecttoml.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ type Build struct {
}

const (
projectTomlPath = "./project.toml"
ProjectTomlPath = "./project.toml"
)

func DoesProjectFileExist() bool {
_, err := os.Stat(projectTomlPath)
func DoesProjectFileExist(filePath string) bool {
_, err := os.Stat(filePath)
return !errors.Is(err, os.ErrNotExist)
}

Expand Down Expand Up @@ -65,8 +65,8 @@ func GetBuildEnvVars(project *ProjectToml) (map[string]string, error) {
return buildEvs, nil
}

func GetProjectFile() (*ProjectToml, error) {
file, err := os.ReadFile(projectTomlPath)
func GetProjectFile(filePath string) (*ProjectToml, error) {
file, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
Expand Down
52 changes: 52 additions & 0 deletions internal/projecttoml/projecttoml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,58 @@ import (
"github.com/stretchr/testify/assert"
)

const (
project1FilePath = "./fixtures/project1.toml"
invalidFilePath = "./fixtures/idontexist.toml"
)

func TestGetProjectFile(t *testing.T) {
project, err := GetProjectFile(project1FilePath)
assert.Nil(t, err)
assert.Equal(t, project, &ProjectToml{
Io: Io{
Buildpacks: Buildpacks{
Build: Build{
Env: []map[string]string{
{"name": "HELLO", "value": "FOO"},
{"name": "HELLO2", "value": "WORLD2"},
{"name": "HELLO3", "value": "WORLD3"},
},
},
},
},
Build: Build{
Env: []map[string]string{
{"name": "build", "value": "value"},
{"name": "HELLO3", "value": "WORLD3_OVERRIDE"},
},
},
})
}

func TestGetBuildEnvsFromFile(t *testing.T) {
file, err := GetProjectFile(project1FilePath)
assert.Nil(t, err)

vals, err := GetBuildEnvVars(file)
assert.Nil(t, err)
assert.Equal(
t,
vals,
map[string]string{
"HELLO": "FOO",
"HELLO2": "WORLD2",
"build": "value",
"HELLO3": "WORLD3_OVERRIDE",
},
)
}

func TestDoesProjectFileExist(t *testing.T) {
assert.True(t, DoesProjectFileExist(project1FilePath))
assert.False(t, DoesProjectFileExist(invalidFilePath))
}

func TestGetBuildEnvVars(t *testing.T) {
vals, err := GetBuildEnvVars(buildFakeProjectToml(nil, nil))
assert.Nil(t, err)
Expand Down

0 comments on commit 0f841bc

Please sign in to comment.