Skip to content

Commit

Permalink
moved exports stuff into top level and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jkutner committed May 8, 2019
1 parent 6a08286 commit 5ab805c
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ target
*.iml
.DS_Store
bin/release
bin/exports
cnb-shim-*.tgz
12 changes: 1 addition & 11 deletions bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,7 @@ echo "launch = true" > ${layers_dir}/profile.toml
if [ -f "${target_dir}/export" ]; then
echo "build = true" >> ${layers_dir}/profile.toml
mkdir -p "${profile_dir}/profile.d/env.build/"
"${bp_dir}/bin/exports" "${target_dir}/export" "${platform_dir}"
ls -al "${profile_dir}/profile.d/env.build/"
cat "${profile_dir}/profile.d/env.build/PATH"
# else
# echo "bp_dir:"
# find ${bp_dir} -name export
# echo "target_dir:"
# find ${target_dir} -name export
# echo "pwd:"
# find $(pwd) -name export
# exit 1
"${bp_dir}/bin/exports" "${target_dir}/export" "${platform_dir}" "${profile_dir}/profile.d/env.build/"
fi

# run bin/release, read Procfile, and generate launch.toml
Expand Down
28 changes: 7 additions & 21 deletions cmd/exports/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/buildpack/libbuildpack/logger"
"github.com/heroku/cnb-shim"
)

func main() {
if len(os.Args) != 3 {
fmt.Println("Usage:", os.Args[0], "EXPORTS_PATH", "PLATFORM_DIR")
if len(os.Args) != 4 {
fmt.Println("Usage:", os.Args[0], "EXPORTS_PATH", "PLATFORM_DIR", "ENV_DIR")
return
}

exportsPath := os.Args[1]
platformDir := os.Args[2]
envDir := os.Args[3]

log, err := logger.DefaultLogger(platformDir)
if err != nil {
Expand All @@ -31,22 +31,8 @@ func main() {
os.Exit(2)
}

contents := string(data)
lines := strings.SplitN(contents, "\n", 2)

for _, line := range lines {
fmt.Println(line)
components := strings.Split(line, "=")
export := strings.Split(components[0], " ")
if strings.TrimSpace(export[0]) == "export" {
fmt.Println("Export 1:")
fmt.Println(export[1])
file := filepath.Join(platformDir, strings.TrimSpace(export[1]))
err := ioutil.WriteFile(file, []byte(components[1]), 0644)
if err != nil {
log.Info(err.Error())
os.Exit(3)
}
}
if err := cnbshim.DumpExportsFile(string(data), envDir); err != nil {
log.Info(err.Error())
os.Exit(3)
}
}
2 changes: 1 addition & 1 deletion cmd/release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
os.Exit(2)
}

err = releaser.WriteLaunchMetadata(appDir, layersDir, targetDir, log)
err = cnbshim.WriteLaunchMetadata(appDir, layersDir, targetDir, log)
if err != nil {
log.Info(err.Error())
os.Exit(3)
Expand Down
36 changes: 36 additions & 0 deletions exports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cnbshim

import (
"io/ioutil"
"path/filepath"
"strings"
)

func DumpExportsFile(exportsData, envDir string) error {
lines := strings.SplitN(exportsData, "\n", 2)

for _, line := range lines {
if found, key, value := ParseExportsFileLine(line); found {
err := WriteEnvFile(envDir, key, value)
if err != nil {
return err
}
}
}
return nil
}

func ParseExportsFileLine(line string) (bool, string, string) {
components := strings.Split(line, "=")
export := strings.Split(components[0], " ")
if strings.TrimSpace(export[0]) == "export" {
return true, strings.TrimSpace(export[1]), components[1]
} else {
return false, "", ""
}

}

func WriteEnvFile(envDir, filename, value string) error {
return ioutil.WriteFile(filepath.Join(envDir, filename), []byte(value), 0644)
}
57 changes: 57 additions & 0 deletions exports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cnbshim_test

import (
"io/ioutil"
"path/filepath"
"strings"
"testing"

"github.com/heroku/cnb-shim"
)

func TestWriteEnvFile(t *testing.T) {
envDir, err := ioutil.TempDir("", "env")
if err != nil {
t.Error(err.Error())
}

expectedFilename := "FOO"
expected := "bar"
err = cnbshim.WriteEnvFile(envDir, expectedFilename, expected)
if err != nil {
t.Error(err.Error())
}

expectedFile := filepath.Join(envDir, expectedFilename)
actual, err := ioutil.ReadFile(expectedFile)
if err != nil {
t.Error(err.Error())
}

if string(actual) != expected {
t.Errorf("Expected %s; got %s", expected, actual)
}
}

func TestDumpExportsFile(t *testing.T) {
envDir, err := ioutil.TempDir("", "env")

exports := `
export FOO=bar
`
err = cnbshim.DumpExportsFile(exports, envDir)
if err != nil {
t.Error(err.Error())
}

expectedFile := filepath.Join(envDir, "FOO")
expected := "bar"
actual, err := ioutil.ReadFile(expectedFile)
if err != nil {
t.Error(err.Error())
}

if strings.TrimSpace(string(actual)) != expected {
t.Errorf("Expected %s; got %s", expected, actual)
}
}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/buildpack/libbuildpack v1.11.0/go.mod h1:tKGgGnBYfOL23Ma4JWM8ucBcGX6I
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
github.com/heroku/cnb-shim v0.0.4 h1:BCPe2mIoqnN1k7L71sBMz0FfxvelG8TDqms1+qieuuE=
github.com/heroku/cnb-shim v0.0.4/go.mod h1:Mdp7qcTZ2xLuNaKFnxVeqbIZRp4yCtwEeBskvgYuQ0g=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down
2 changes: 1 addition & 1 deletion releaser.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package releaser
package cnbshim

import (
"io/ioutil"
Expand Down
9 changes: 5 additions & 4 deletions releaser_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package releaser_test
package cnbshim_test


import (
"github.com/BurntSushi/toml"
"github.com/buildpack/libbuildpack/layers"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/buildpack/libbuildpack/logger"
"github.com/heroku/cnb-shim"
"github.com/BurntSushi/toml"
"github.com/buildpack/libbuildpack/layers"
releaser "github.com/heroku/cnb-shim"
)

func TestReadProcfileWithWebAndWorker(t *testing.T) {
Expand Down

0 comments on commit 5ab805c

Please sign in to comment.